Skip to content

Commit

Permalink
handle server-side deletions (#5)
Browse files Browse the repository at this point in the history
There is no obvious way to find the needle in the haystack.
The adopted strategy is to fetch entries metadata in big bulks. While it means
fetching the whole referential it also allow to get the most out of the least
round-trip.
  • Loading branch information
casimir authored Jul 25, 2023
1 parent a2db3f4 commit 0481389
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ final _log = Logger('frigoligo');
// - [x] better server configuration flow (more interactive)
// - [x] save scroll position in article view
// - [x] padding and spacing everywhere
// - [ ] handle server side deletion of articles
// - [x] handle server side deletion of articles
// - [ ] unit tests (at least for WallabagClient) -> https://github.com/wallabag/docker#sqlite
// - [ ] handle filtering by tag
// 0.3
// - [ ] add article (in app + share sheet)
// - [ ] translations -> https://docs.flutter.dev/accessibility-and-localization/internationalization
// - [ ] handle filtering by tag
// 1.0
// - [ ] background sync
// - [ ] notification badge with https://pub.dev/packages/flutter_app_badger ?
Expand Down
25 changes: 25 additions & 0 deletions lib/services/wallabag.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,30 @@ class ArticlesProvider with ChangeNotifier {
int count(StateFilter state, StarredFilter starred) =>
_buildQuery(state: state, starred: starred).countSync();

Future<int> syncRemoteDeletes() async {
final remoteCount = await wallabag.fetchTotalEntriesCount();
var localIds = (await db.articles.where().idProperty().findAll()).toSet();
final delta = localIds.length - remoteCount;
if (delta <= 0) return 0;
_log.info('server-side deletion detected: delta=$delta');

// seems overkill but the only way to enumerate all entry ids efficiently
final entriesStream = wallabag.fetchAllEntries(
perPage: 100,
detail: DetailValue.metadata,
);
await for (final (entries, _) in entriesStream) {
localIds = localIds.difference(entries.map((e) => e.id).toSet());
}

await db.writeTxn(() async {
await db.articles.deleteAll(localIds.toList());
await db.articleScrollPositions.deleteAll(localIds.toList());
});

return delta;
}

Future<int> fullRefresh({int? since}) async {
assert(!refreshInProgress);

Expand Down Expand Up @@ -151,6 +175,7 @@ class ArticlesProvider with ChangeNotifier {
}
_log.info(
'completed refresh of $count entries in ${stopwatch.elapsed.inSeconds} s');
syncRemoteDeletes();

final now = DateTime.now().millisecondsSinceEpoch / 1000;
await SharedPreferences.getInstance()
Expand Down

0 comments on commit 0481389

Please sign in to comment.