feat: store subscription posts with a read mark and split the library controller
This commit is contained in:
parent
b311b9f7dd
commit
953ae798eb
15 changed files with 1787 additions and 386 deletions
|
|
@ -1,4 +1,3 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:drift/drift.dart';
|
||||
|
|
@ -6,9 +5,14 @@ import 'package:drift/native.dart';
|
|||
import 'package:path/path.dart' as p;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
import '../mews/document.dart' show MewsEntry;
|
||||
|
||||
part 'database.g.dart';
|
||||
|
||||
/// Visited pages, newest first; the most recent row is the app's start page.
|
||||
/// The same page can appear many times, so the index carries the lookups that
|
||||
/// group or remove by address.
|
||||
@TableIndex(name: 'history_items_url', columns: {#url})
|
||||
class HistoryItems extends Table {
|
||||
IntColumn get id => integer().autoIncrement()();
|
||||
TextColumn get url => text()();
|
||||
|
|
@ -16,22 +20,59 @@ class HistoryItems extends Table {
|
|||
DateTimeColumn get visitedAt => dateTime().withDefault(currentDateAndTime)();
|
||||
}
|
||||
|
||||
/// A bookmarked page, keyed by its address so it can only be held once.
|
||||
class Bookmarks extends Table {
|
||||
IntColumn get id => integer().autoIncrement()();
|
||||
TextColumn get url => text()();
|
||||
TextColumn get title => text().nullable()();
|
||||
DateTimeColumn get createdAt => dateTime().withDefault(currentDateAndTime)();
|
||||
|
||||
@override
|
||||
Set<Column> get primaryKey => {url};
|
||||
}
|
||||
|
||||
/// A subscription to one page (spec 6). [entryUrls] is a JSON list of the
|
||||
/// entry URLs already seen, so a refresh can report only new posts.
|
||||
/// A subscribed page (spec 6). Its posts live in [FeedEntries].
|
||||
class Subscriptions extends Table {
|
||||
IntColumn get id => integer().autoIncrement()();
|
||||
TextColumn get url => text()();
|
||||
TextColumn get title => text().nullable()();
|
||||
TextColumn get entryUrls => text().withDefault(const Constant('[]'))();
|
||||
|
||||
/// The page's Atom feed, resolved absolute, when it declares one (spec 6.2).
|
||||
TextColumn get atomHref => text().nullable()();
|
||||
DateTimeColumn get createdAt => dateTime().withDefault(currentDateAndTime)();
|
||||
DateTimeColumn get lastCheckedAt => dateTime().nullable()();
|
||||
|
||||
@override
|
||||
Set<Column> get primaryKey => {url};
|
||||
}
|
||||
|
||||
/// One post found on a subscribed page, through its dated links (spec 6.1) or
|
||||
/// its Atom feed (spec 6.2). An entry's URL identifies it for its lifetime, so
|
||||
/// (siteUrl, url) is its identity and finding it again never clears [readAt].
|
||||
class FeedEntries extends Table {
|
||||
TextColumn get siteUrl => text()();
|
||||
TextColumn get url => text()();
|
||||
TextColumn get title => text()();
|
||||
DateTimeColumn get date => dateTime()();
|
||||
DateTimeColumn get foundAt => dateTime().withDefault(currentDateAndTime)();
|
||||
DateTimeColumn get readAt => dateTime().nullable()();
|
||||
|
||||
@override
|
||||
Set<Column> get primaryKey => {siteUrl, url};
|
||||
}
|
||||
|
||||
/// What a tag is attached to.
|
||||
enum TagKind { bookmark, subscription }
|
||||
|
||||
/// Tags the reader puts on a bookmark or a subscribed site. A tag exists only
|
||||
/// while something carries it, so there is no separate table of tags to keep
|
||||
/// in step. Comparison is case-sensitive.
|
||||
class ItemTags extends Table {
|
||||
TextColumn get kind => textEnum<TagKind>()();
|
||||
TextColumn get url => text()();
|
||||
TextColumn get tag => text()();
|
||||
DateTimeColumn get createdAt => dateTime().withDefault(currentDateAndTime)();
|
||||
|
||||
@override
|
||||
Set<Column> get primaryKey => {kind, url, tag};
|
||||
}
|
||||
|
||||
/// Single-row table (id 0) holding the reader's presentation choices.
|
||||
|
|
@ -59,7 +100,15 @@ class CachedPages extends Table {
|
|||
Set<Column> get primaryKey => {url};
|
||||
}
|
||||
|
||||
@DriftDatabase(tables: [HistoryItems, Bookmarks, Subscriptions, ReaderSettings, CachedPages])
|
||||
@DriftDatabase(tables: [
|
||||
HistoryItems,
|
||||
Bookmarks,
|
||||
Subscriptions,
|
||||
FeedEntries,
|
||||
ItemTags,
|
||||
ReaderSettings,
|
||||
CachedPages,
|
||||
])
|
||||
class MewsDatabase extends _$MewsDatabase {
|
||||
MewsDatabase() : super(_open());
|
||||
|
||||
|
|
@ -67,7 +116,13 @@ class MewsDatabase extends _$MewsDatabase {
|
|||
MewsDatabase.test(super.executor);
|
||||
|
||||
@override
|
||||
int get schemaVersion => 1;
|
||||
int get schemaVersion => 2;
|
||||
|
||||
/// Pre-release: the entry store replaces the old list of seen entry URLs and
|
||||
/// the library tables gain real primary keys, so an existing database is
|
||||
/// recreated rather than migrated.
|
||||
@override
|
||||
MigrationStrategy get migration => destructiveFallback;
|
||||
|
||||
static QueryExecutor _open() => LazyDatabase(() async {
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
|
|
@ -92,14 +147,17 @@ class MewsDatabase extends _$MewsDatabase {
|
|||
}
|
||||
|
||||
/// Row counts for the hub, so it can say what each page holds without
|
||||
/// reading every row.
|
||||
Future<int> countHistory() => _countOf(historyItems, historyItems.id);
|
||||
Future<int> countBookmarks() => _countOf(bookmarks, bookmarks.id);
|
||||
Future<int> countSubscriptions() => _countOf(subscriptions, subscriptions.id);
|
||||
/// reading every row. History counts addresses, not visits.
|
||||
Future<int> countHistory() =>
|
||||
_countOf(historyItems, historyItems.url, distinct: true);
|
||||
Future<int> countBookmarks() => _countOf(bookmarks, bookmarks.url);
|
||||
Future<int> countSubscriptions() => _countOf(subscriptions, subscriptions.url);
|
||||
|
||||
Future<int> _countOf(ResultSetImplementation<HasResultSet, dynamic> table,
|
||||
GeneratedColumn<int> column) async {
|
||||
final total = column.count();
|
||||
Future<int> _countOf<T extends Object>(
|
||||
ResultSetImplementation<HasResultSet, dynamic> table,
|
||||
GeneratedColumn<T> column,
|
||||
{bool distinct = false}) async {
|
||||
final total = column.count(distinct: distinct);
|
||||
final query = selectOnly(table)..addColumns([total]);
|
||||
return (await query.getSingle()).read(total) ?? 0;
|
||||
}
|
||||
|
|
@ -120,7 +178,8 @@ class MewsDatabase extends _$MewsDatabase {
|
|||
final query = (select(bookmarks)
|
||||
..orderBy([
|
||||
(r) => OrderingTerm.desc(r.createdAt),
|
||||
(r) => OrderingTerm.desc(r.id),
|
||||
// Tiebreak bookmarks added within the same clock tick.
|
||||
(r) => OrderingTerm.asc(r.url),
|
||||
]));
|
||||
return query.get();
|
||||
}
|
||||
|
|
@ -133,30 +192,153 @@ class MewsDatabase extends _$MewsDatabase {
|
|||
BookmarksCompanion.insert(url: url, title: Value(title)),
|
||||
mode: InsertMode.insertOrIgnore);
|
||||
|
||||
Future<void> removeBookmark(String url) =>
|
||||
(delete(bookmarks)..where((r) => r.url.equals(url))).go();
|
||||
/// Removes a bookmark and whatever the reader tagged it with.
|
||||
Future<void> removeBookmark(String url) => transaction(() async {
|
||||
await (delete(bookmarks)..where((r) => r.url.equals(url))).go();
|
||||
await _removeTagsOf(TagKind.bookmark, url);
|
||||
});
|
||||
|
||||
// Subscriptions ---------------------------------------------------------
|
||||
|
||||
Future<void> addSubscription(String url, String? title, List<String> entryUrls) => into(
|
||||
subscriptions)
|
||||
.insert(
|
||||
Future<void> addSubscription(String url, String? title, String? atomHref) =>
|
||||
into(subscriptions).insert(
|
||||
SubscriptionsCompanion.insert(
|
||||
url: url, title: Value(title), entryUrls: Value(jsonEncode(entryUrls))),
|
||||
url: url, title: Value(title), atomHref: Value(atomHref)),
|
||||
mode: InsertMode.insertOrIgnore);
|
||||
|
||||
Future<void> removeSubscription(String url) =>
|
||||
(delete(subscriptions)..where((r) => r.url.equals(url))).go();
|
||||
/// Removes a subscription along with its posts and tags, so nothing is left
|
||||
/// pointing at a site the reader has left.
|
||||
Future<void> removeSubscription(String url) => transaction(() async {
|
||||
await (delete(subscriptions)..where((r) => r.url.equals(url))).go();
|
||||
await (delete(feedEntries)..where((r) => r.siteUrl.equals(url))).go();
|
||||
await _removeTagsOf(TagKind.subscription, url);
|
||||
});
|
||||
|
||||
Future<List<Subscription>> allSubscriptions() => select(subscriptions).get();
|
||||
Future<List<Subscription>> allSubscriptions() async {
|
||||
final query = (select(subscriptions)
|
||||
..orderBy([
|
||||
(r) => OrderingTerm.desc(r.createdAt),
|
||||
(r) => OrderingTerm.asc(r.url),
|
||||
]));
|
||||
return query.get();
|
||||
}
|
||||
|
||||
Future<void> updateSubscription(String url,
|
||||
{String? title, List<String>? entryUrls, DateTime? lastCheckedAt}) =>
|
||||
(update(subscriptions)..where((r) => r.url.equals(url))).write(SubscriptionsCompanion(
|
||||
title: Value(title),
|
||||
entryUrls: entryUrls == null ? const Value.absent() : Value(jsonEncode(entryUrls)),
|
||||
lastCheckedAt: Value(lastCheckedAt),
|
||||
));
|
||||
/// Records that a site was checked, whether or not anything was found.
|
||||
Future<void> touchSubscription(String url, DateTime checkedAt) =>
|
||||
(update(subscriptions)..where((r) => r.url.equals(url)))
|
||||
.write(SubscriptionsCompanion(lastCheckedAt: Value(checkedAt)));
|
||||
|
||||
/// Updates what is known about a site. Each field is left alone when it is
|
||||
/// not given, so a refresh cannot blank a stored title.
|
||||
Future<void> describeSubscription(String url,
|
||||
{String? title, String? atomHref}) =>
|
||||
(update(subscriptions)..where((r) => r.url.equals(url))).write(
|
||||
SubscriptionsCompanion(
|
||||
title: title == null ? const Value.absent() : Value(title),
|
||||
atomHref: atomHref == null ? const Value.absent() : Value(atomHref),
|
||||
),
|
||||
);
|
||||
|
||||
// Posts -------------------------------------------------------------------
|
||||
|
||||
/// Stores the posts of [siteUrl] that are not held yet and returns them, so
|
||||
/// the caller can say what is new. An entry already held keeps its read
|
||||
/// mark. Pass [read] to store them already read, which is how a new
|
||||
/// subscription takes on a site's back catalogue without flagging it.
|
||||
Future<List<MewsEntry>> addEntries(String siteUrl, List<MewsEntry> entries,
|
||||
{bool read = false}) async {
|
||||
if (entries.isEmpty) return const [];
|
||||
final urls = entries.map((e) => e.url).toList();
|
||||
final held = await (select(feedEntries)
|
||||
..where((r) => r.siteUrl.equals(siteUrl) & r.url.isIn(urls)))
|
||||
.get();
|
||||
final known = held.map((r) => r.url).toSet();
|
||||
final fresh = entries.where((e) => !known.contains(e.url)).toList();
|
||||
if (fresh.isEmpty) return const [];
|
||||
await batch((b) => b.insertAll(
|
||||
feedEntries,
|
||||
[
|
||||
for (final entry in fresh)
|
||||
FeedEntriesCompanion.insert(
|
||||
siteUrl: siteUrl,
|
||||
url: entry.url,
|
||||
title: entry.title,
|
||||
date: entry.date,
|
||||
readAt: Value(read ? DateTime.now() : null),
|
||||
),
|
||||
],
|
||||
mode: InsertMode.insertOrIgnore,
|
||||
));
|
||||
return fresh;
|
||||
}
|
||||
|
||||
/// Marks a post read. The address identifies a post for its lifetime, so
|
||||
/// the site is only needed when two sites happen to list the same post.
|
||||
Future<void> markEntryRead(String url, {String? siteUrl}) {
|
||||
final statement = update(feedEntries)..where((r) => r.url.equals(url));
|
||||
if (siteUrl != null) statement.where((r) => r.siteUrl.equals(siteUrl));
|
||||
return statement.write(FeedEntriesCompanion(readAt: Value(DateTime.now())));
|
||||
}
|
||||
|
||||
Future<void> markEntryUnread(String url, {String? siteUrl}) {
|
||||
final statement = update(feedEntries)..where((r) => r.url.equals(url));
|
||||
if (siteUrl != null) statement.where((r) => r.siteUrl.equals(siteUrl));
|
||||
return statement.write(const FeedEntriesCompanion(readAt: Value(null)));
|
||||
}
|
||||
|
||||
/// How many posts are unread, for one site or across all of them.
|
||||
Future<int> unreadCount({String? siteUrl}) async {
|
||||
final total = feedEntries.url.count();
|
||||
final query = selectOnly(feedEntries)
|
||||
..addColumns([total])
|
||||
..where(feedEntries.readAt.isNull());
|
||||
if (siteUrl != null) query.where(feedEntries.siteUrl.equals(siteUrl));
|
||||
return (await query.getSingle()).read(total) ?? 0;
|
||||
}
|
||||
|
||||
/// The unread count per site, so a list of sites costs one query.
|
||||
Future<Map<String, int>> unreadCounts() async {
|
||||
final total = feedEntries.url.count();
|
||||
final query = selectOnly(feedEntries)
|
||||
..addColumns([feedEntries.siteUrl, total])
|
||||
..where(feedEntries.readAt.isNull())
|
||||
..groupBy([feedEntries.siteUrl]);
|
||||
return {
|
||||
for (final row in await query.get())
|
||||
row.read(feedEntries.siteUrl)!: row.read(total) ?? 0,
|
||||
};
|
||||
}
|
||||
|
||||
// Tags ---------------------------------------------------------------------
|
||||
|
||||
/// The tags on one item, in the order they were added.
|
||||
Future<List<String>> tagsOf(TagKind kind, String url) async {
|
||||
final query = (select(itemTags)
|
||||
..where((r) => r.kind.equalsValue(kind) & r.url.equals(url))
|
||||
..orderBy([
|
||||
(r) => OrderingTerm.asc(r.createdAt),
|
||||
(r) => OrderingTerm.asc(r.tag),
|
||||
]));
|
||||
return (await query.get()).map((row) => row.tag).toList();
|
||||
}
|
||||
|
||||
Future<void> addTag(TagKind kind, String url, String tag) =>
|
||||
into(itemTags).insert(
|
||||
ItemTagsCompanion.insert(kind: kind, url: url, tag: tag),
|
||||
mode: InsertMode.insertOrIgnore);
|
||||
|
||||
Future<void> removeTag(TagKind kind, String url, String tag) =>
|
||||
(delete(itemTags)
|
||||
..where((r) =>
|
||||
r.kind.equalsValue(kind) &
|
||||
r.url.equals(url) &
|
||||
r.tag.equals(tag)))
|
||||
.go();
|
||||
|
||||
Future<void> _removeTagsOf(TagKind kind, String url) =>
|
||||
(delete(itemTags)
|
||||
..where((r) => r.kind.equalsValue(kind) & r.url.equals(url)))
|
||||
.go();
|
||||
|
||||
// Settings ---------------------------------------------------------------
|
||||
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue