fix(playback): harden offline source reporting

This commit is contained in:
edde746
2026-05-29 19:55:01 +02:00
parent 7501f461b9
commit d93ea9813f
34 changed files with 739 additions and 273 deletions
+104 -44
View File
@@ -60,7 +60,7 @@ class AppDatabase extends _$AppDatabase {
AppDatabase.forTesting(super.e);
@override
int get schemaVersion => 14;
int get schemaVersion => 15;
@override
MigrationStrategy get migration {
@@ -206,6 +206,13 @@ class AppDatabase extends _$AppDatabase {
() => m.create(idxOfflineWatchProgressProfile),
);
}
if (from < 15) {
appLogger.i('Adding mediaSourceId column to DownloadedMedia (v15 migration)');
await _ignoreAlreadyExists(
'DownloadedMedia.mediaSourceId column',
() => m.addColumn(downloadedMedia, downloadedMedia.mediaSourceId),
);
}
},
);
}
@@ -282,6 +289,52 @@ class AppDatabase extends _$AppDatabase {
.getSingleOrNull();
}
Future<List<OfflineWatchProgressItem>> getWatchActionsForKey(
String globalKey, {
String? profileId,
bool filterProfile = false,
String? clientScopeId,
bool filterClientScope = false,
}) {
return (select(offlineWatchProgress)
..where(
(t) =>
t.globalKey.equals(globalKey) &
(filterProfile ? _nullableTextPredicate(t.profileId, profileId) : const Constant(true)) &
(filterClientScope ? _clientScopePredicate(t.clientScopeId, clientScopeId) : const Constant(true)),
)
..orderBy([(t) => OrderingTerm.desc(t.updatedAt)]))
.get();
}
Future<Map<String, List<OfflineWatchProgressItem>>> getWatchActionsForKeys(
Set<String> globalKeys, {
String? profileId,
bool filterProfile = false,
Map<String, String?>? clientScopeIdsByGlobalKey,
}) async {
if (globalKeys.isEmpty) return const {};
final rows =
await (select(offlineWatchProgress)
..where(
(t) =>
t.globalKey.isIn(globalKeys) &
(filterProfile ? _nullableTextPredicate(t.profileId, profileId) : const Constant(true)),
)
..orderBy([(t) => OrderingTerm.desc(t.updatedAt)]))
.get();
final result = <String, List<OfflineWatchProgressItem>>{};
for (final action in rows) {
if (clientScopeIdsByGlobalKey != null && clientScopeIdsByGlobalKey.containsKey(action.globalKey)) {
final expectedScope = clientScopeIdsByGlobalKey[action.globalKey];
if (!_clientScopeValuesMatch(action.clientScopeId, expectedScope)) continue;
}
result.putIfAbsent(action.globalKey, () => <OfflineWatchProgressItem>[]).add(action);
}
return result;
}
/// Get the latest actions for multiple items in a single query
///
/// Returns a map of globalKey -> latest action for each key.
@@ -338,49 +391,53 @@ class AppDatabase extends _$AppDatabase {
final globalKey = buildGlobalKey(serverId, ratingKey);
final now = DateTime.now().millisecondsSinceEpoch;
// Check for existing progress entry
final existing =
await (select(offlineWatchProgress)
..where(
(t) =>
t.globalKey.equals(globalKey) &
_nullableTextPredicate(t.profileId, profileId) &
_clientScopePredicate(t.clientScopeId, clientScopeId) &
t.actionType.equals(OfflineActionType.progress.id),
)
..limit(1))
.getSingleOrNull();
await transaction(() async {
final existing =
await (select(offlineWatchProgress)
..where(
(t) =>
t.globalKey.equals(globalKey) &
_nullableTextPredicate(t.profileId, profileId) &
_clientScopePredicate(t.clientScopeId, clientScopeId) &
t.actionType.equals(OfflineActionType.progress.id),
)
..orderBy([(t) => OrderingTerm.asc(t.id)]))
.get();
if (existing != null) {
// Update existing progress entry
await (update(offlineWatchProgress)..where((t) => t.id.equals(existing.id))).write(
OfflineWatchProgressCompanion(
viewOffset: Value(viewOffset),
duration: Value(duration),
shouldMarkWatched: Value(shouldMarkWatched),
profileId: Value(profileId),
clientScopeId: Value(clientScopeId),
updatedAt: Value(now),
),
);
} else {
// Insert new progress entry
await into(offlineWatchProgress).insert(
OfflineWatchProgressCompanion.insert(
serverId: serverId,
profileId: Value(profileId),
clientScopeId: Value(clientScopeId),
ratingKey: ratingKey,
globalKey: globalKey,
actionType: OfflineActionType.progress.id,
viewOffset: Value(viewOffset),
duration: Value(duration),
shouldMarkWatched: Value(shouldMarkWatched),
createdAt: now,
updatedAt: now,
),
);
}
final keep = existing.isEmpty ? null : existing.first;
if (keep != null) {
await (update(offlineWatchProgress)..where((t) => t.id.equals(keep.id))).write(
OfflineWatchProgressCompanion(
viewOffset: Value(viewOffset),
duration: Value(duration),
shouldMarkWatched: Value(shouldMarkWatched),
profileId: Value(profileId),
clientScopeId: Value(clientScopeId),
updatedAt: Value(now),
),
);
final duplicateIds = existing.skip(1).map((row) => row.id).toList(growable: false);
if (duplicateIds.isNotEmpty) {
await (delete(offlineWatchProgress)..where((t) => t.id.isIn(duplicateIds))).go();
}
} else {
await into(offlineWatchProgress).insert(
OfflineWatchProgressCompanion.insert(
serverId: serverId,
profileId: Value(profileId),
clientScopeId: Value(clientScopeId),
ratingKey: ratingKey,
globalKey: globalKey,
actionType: OfflineActionType.progress.id,
viewOffset: Value(viewOffset),
duration: Value(duration),
shouldMarkWatched: Value(shouldMarkWatched),
createdAt: now,
updatedAt: now,
),
);
}
});
}
/// Insert a manual watch action (watched or unwatched).
@@ -436,11 +493,14 @@ class AppDatabase extends _$AppDatabase {
}
/// Get count of pending sync items
Future<int> getPendingSyncCount({String? profileId}) async {
Future<int> getPendingSyncCount({String? profileId, int? maxSyncAttempts}) async {
final query = selectOnly(offlineWatchProgress)..addColumns([offlineWatchProgress.id.count()]);
if (profileId != null) {
query.where(offlineWatchProgress.profileId.equals(profileId));
}
if (maxSyncAttempts != null) {
query.where(offlineWatchProgress.syncAttempts.isSmallerThanValue(maxSyncAttempts));
}
final count = await query.map((row) => row.read(offlineWatchProgress.id.count())).getSingle();
return count ?? 0;
}
+80 -3
View File
@@ -221,6 +221,17 @@ class $DownloadedMediaTable extends DownloadedMedia
requiredDuringInsert: false,
defaultValue: const Constant(0),
);
static const VerificationMeta _mediaSourceIdMeta = const VerificationMeta(
'mediaSourceId',
);
@override
late final GeneratedColumn<String> mediaSourceId = GeneratedColumn<String>(
'media_source_id',
aliasedName,
true,
type: DriftSqlType.string,
requiredDuringInsert: false,
);
@override
List<GeneratedColumn> get $columns => [
id,
@@ -242,6 +253,7 @@ class $DownloadedMediaTable extends DownloadedMedia
retryCount,
bgTaskId,
mediaIndex,
mediaSourceId,
];
@override
String get aliasedName => _alias ?? actualTableName;
@@ -397,6 +409,15 @@ class $DownloadedMediaTable extends DownloadedMedia
mediaIndex.isAcceptableOrUnknown(data['media_index']!, _mediaIndexMeta),
);
}
if (data.containsKey('media_source_id')) {
context.handle(
_mediaSourceIdMeta,
mediaSourceId.isAcceptableOrUnknown(
data['media_source_id']!,
_mediaSourceIdMeta,
),
);
}
return context;
}
@@ -482,6 +503,10 @@ class $DownloadedMediaTable extends DownloadedMedia
DriftSqlType.int,
data['${effectivePrefix}media_index'],
)!,
mediaSourceId: attachedDatabase.typeMapping.read(
DriftSqlType.string,
data['${effectivePrefix}media_source_id'],
),
);
}
@@ -512,6 +537,7 @@ class DownloadedMediaItem extends DataClass
final int retryCount;
final String? bgTaskId;
final int mediaIndex;
final String? mediaSourceId;
const DownloadedMediaItem({
required this.id,
required this.serverId,
@@ -532,6 +558,7 @@ class DownloadedMediaItem extends DataClass
required this.retryCount,
this.bgTaskId,
required this.mediaIndex,
this.mediaSourceId,
});
@override
Map<String, Expression> toColumns(bool nullToAbsent) {
@@ -573,6 +600,9 @@ class DownloadedMediaItem extends DataClass
map['bg_task_id'] = Variable<String>(bgTaskId);
}
map['media_index'] = Variable<int>(mediaIndex);
if (!nullToAbsent || mediaSourceId != null) {
map['media_source_id'] = Variable<String>(mediaSourceId);
}
return map;
}
@@ -615,6 +645,9 @@ class DownloadedMediaItem extends DataClass
? const Value.absent()
: Value(bgTaskId),
mediaIndex: Value(mediaIndex),
mediaSourceId: mediaSourceId == null && nullToAbsent
? const Value.absent()
: Value(mediaSourceId),
);
}
@@ -645,6 +678,7 @@ class DownloadedMediaItem extends DataClass
retryCount: serializer.fromJson<int>(json['retryCount']),
bgTaskId: serializer.fromJson<String?>(json['bgTaskId']),
mediaIndex: serializer.fromJson<int>(json['mediaIndex']),
mediaSourceId: serializer.fromJson<String?>(json['mediaSourceId']),
);
}
@override
@@ -670,6 +704,7 @@ class DownloadedMediaItem extends DataClass
'retryCount': serializer.toJson<int>(retryCount),
'bgTaskId': serializer.toJson<String?>(bgTaskId),
'mediaIndex': serializer.toJson<int>(mediaIndex),
'mediaSourceId': serializer.toJson<String?>(mediaSourceId),
};
}
@@ -693,6 +728,7 @@ class DownloadedMediaItem extends DataClass
int? retryCount,
Value<String?> bgTaskId = const Value.absent(),
int? mediaIndex,
Value<String?> mediaSourceId = const Value.absent(),
}) => DownloadedMediaItem(
id: id ?? this.id,
serverId: serverId ?? this.serverId,
@@ -721,6 +757,9 @@ class DownloadedMediaItem extends DataClass
retryCount: retryCount ?? this.retryCount,
bgTaskId: bgTaskId.present ? bgTaskId.value : this.bgTaskId,
mediaIndex: mediaIndex ?? this.mediaIndex,
mediaSourceId: mediaSourceId.present
? mediaSourceId.value
: this.mediaSourceId,
);
DownloadedMediaItem copyWithCompanion(DownloadedMediaCompanion data) {
return DownloadedMediaItem(
@@ -763,6 +802,9 @@ class DownloadedMediaItem extends DataClass
mediaIndex: data.mediaIndex.present
? data.mediaIndex.value
: this.mediaIndex,
mediaSourceId: data.mediaSourceId.present
? data.mediaSourceId.value
: this.mediaSourceId,
);
}
@@ -787,7 +829,8 @@ class DownloadedMediaItem extends DataClass
..write('errorMessage: $errorMessage, ')
..write('retryCount: $retryCount, ')
..write('bgTaskId: $bgTaskId, ')
..write('mediaIndex: $mediaIndex')
..write('mediaIndex: $mediaIndex, ')
..write('mediaSourceId: $mediaSourceId')
..write(')'))
.toString();
}
@@ -813,6 +856,7 @@ class DownloadedMediaItem extends DataClass
retryCount,
bgTaskId,
mediaIndex,
mediaSourceId,
);
@override
bool operator ==(Object other) =>
@@ -836,7 +880,8 @@ class DownloadedMediaItem extends DataClass
other.errorMessage == this.errorMessage &&
other.retryCount == this.retryCount &&
other.bgTaskId == this.bgTaskId &&
other.mediaIndex == this.mediaIndex);
other.mediaIndex == this.mediaIndex &&
other.mediaSourceId == this.mediaSourceId);
}
class DownloadedMediaCompanion extends UpdateCompanion<DownloadedMediaItem> {
@@ -859,6 +904,7 @@ class DownloadedMediaCompanion extends UpdateCompanion<DownloadedMediaItem> {
final Value<int> retryCount;
final Value<String?> bgTaskId;
final Value<int> mediaIndex;
final Value<String?> mediaSourceId;
const DownloadedMediaCompanion({
this.id = const Value.absent(),
this.serverId = const Value.absent(),
@@ -879,6 +925,7 @@ class DownloadedMediaCompanion extends UpdateCompanion<DownloadedMediaItem> {
this.retryCount = const Value.absent(),
this.bgTaskId = const Value.absent(),
this.mediaIndex = const Value.absent(),
this.mediaSourceId = const Value.absent(),
});
DownloadedMediaCompanion.insert({
this.id = const Value.absent(),
@@ -900,6 +947,7 @@ class DownloadedMediaCompanion extends UpdateCompanion<DownloadedMediaItem> {
this.retryCount = const Value.absent(),
this.bgTaskId = const Value.absent(),
this.mediaIndex = const Value.absent(),
this.mediaSourceId = const Value.absent(),
}) : serverId = Value(serverId),
ratingKey = Value(ratingKey),
globalKey = Value(globalKey),
@@ -925,6 +973,7 @@ class DownloadedMediaCompanion extends UpdateCompanion<DownloadedMediaItem> {
Expression<int>? retryCount,
Expression<String>? bgTaskId,
Expression<int>? mediaIndex,
Expression<String>? mediaSourceId,
}) {
return RawValuesInsertable({
if (id != null) 'id': id,
@@ -947,6 +996,7 @@ class DownloadedMediaCompanion extends UpdateCompanion<DownloadedMediaItem> {
if (retryCount != null) 'retry_count': retryCount,
if (bgTaskId != null) 'bg_task_id': bgTaskId,
if (mediaIndex != null) 'media_index': mediaIndex,
if (mediaSourceId != null) 'media_source_id': mediaSourceId,
});
}
@@ -970,6 +1020,7 @@ class DownloadedMediaCompanion extends UpdateCompanion<DownloadedMediaItem> {
Value<int>? retryCount,
Value<String?>? bgTaskId,
Value<int>? mediaIndex,
Value<String?>? mediaSourceId,
}) {
return DownloadedMediaCompanion(
id: id ?? this.id,
@@ -991,6 +1042,7 @@ class DownloadedMediaCompanion extends UpdateCompanion<DownloadedMediaItem> {
retryCount: retryCount ?? this.retryCount,
bgTaskId: bgTaskId ?? this.bgTaskId,
mediaIndex: mediaIndex ?? this.mediaIndex,
mediaSourceId: mediaSourceId ?? this.mediaSourceId,
);
}
@@ -1056,6 +1108,9 @@ class DownloadedMediaCompanion extends UpdateCompanion<DownloadedMediaItem> {
if (mediaIndex.present) {
map['media_index'] = Variable<int>(mediaIndex.value);
}
if (mediaSourceId.present) {
map['media_source_id'] = Variable<String>(mediaSourceId.value);
}
return map;
}
@@ -1080,7 +1135,8 @@ class DownloadedMediaCompanion extends UpdateCompanion<DownloadedMediaItem> {
..write('errorMessage: $errorMessage, ')
..write('retryCount: $retryCount, ')
..write('bgTaskId: $bgTaskId, ')
..write('mediaIndex: $mediaIndex')
..write('mediaIndex: $mediaIndex, ')
..write('mediaSourceId: $mediaSourceId')
..write(')'))
.toString();
}
@@ -5319,6 +5375,7 @@ typedef $$DownloadedMediaTableCreateCompanionBuilder =
Value<int> retryCount,
Value<String?> bgTaskId,
Value<int> mediaIndex,
Value<String?> mediaSourceId,
});
typedef $$DownloadedMediaTableUpdateCompanionBuilder =
DownloadedMediaCompanion Function({
@@ -5341,6 +5398,7 @@ typedef $$DownloadedMediaTableUpdateCompanionBuilder =
Value<int> retryCount,
Value<String?> bgTaskId,
Value<int> mediaIndex,
Value<String?> mediaSourceId,
});
class $$DownloadedMediaTableFilterComposer
@@ -5446,6 +5504,11 @@ class $$DownloadedMediaTableFilterComposer
column: $table.mediaIndex,
builder: (column) => ColumnFilters(column),
);
ColumnFilters<String> get mediaSourceId => $composableBuilder(
column: $table.mediaSourceId,
builder: (column) => ColumnFilters(column),
);
}
class $$DownloadedMediaTableOrderingComposer
@@ -5551,6 +5614,11 @@ class $$DownloadedMediaTableOrderingComposer
column: $table.mediaIndex,
builder: (column) => ColumnOrderings(column),
);
ColumnOrderings<String> get mediaSourceId => $composableBuilder(
column: $table.mediaSourceId,
builder: (column) => ColumnOrderings(column),
);
}
class $$DownloadedMediaTableAnnotationComposer
@@ -5638,6 +5706,11 @@ class $$DownloadedMediaTableAnnotationComposer
column: $table.mediaIndex,
builder: (column) => column,
);
GeneratedColumn<String> get mediaSourceId => $composableBuilder(
column: $table.mediaSourceId,
builder: (column) => column,
);
}
class $$DownloadedMediaTableTableManager
@@ -5696,6 +5769,7 @@ class $$DownloadedMediaTableTableManager
Value<int> retryCount = const Value.absent(),
Value<String?> bgTaskId = const Value.absent(),
Value<int> mediaIndex = const Value.absent(),
Value<String?> mediaSourceId = const Value.absent(),
}) => DownloadedMediaCompanion(
id: id,
serverId: serverId,
@@ -5716,6 +5790,7 @@ class $$DownloadedMediaTableTableManager
retryCount: retryCount,
bgTaskId: bgTaskId,
mediaIndex: mediaIndex,
mediaSourceId: mediaSourceId,
),
createCompanionCallback:
({
@@ -5738,6 +5813,7 @@ class $$DownloadedMediaTableTableManager
Value<int> retryCount = const Value.absent(),
Value<String?> bgTaskId = const Value.absent(),
Value<int> mediaIndex = const Value.absent(),
Value<String?> mediaSourceId = const Value.absent(),
}) => DownloadedMediaCompanion.insert(
id: id,
serverId: serverId,
@@ -5758,6 +5834,7 @@ class $$DownloadedMediaTableTableManager
retryCount: retryCount,
bgTaskId: bgTaskId,
mediaIndex: mediaIndex,
mediaSourceId: mediaSourceId,
),
withReferenceMapper: (p0) => p0
.map((e) => (e.readTable(table), BaseReferences(db, table, e)))
+8
View File
@@ -85,6 +85,7 @@ extension DownloadDatabaseOperations on AppDatabase {
String? grandparentRatingKey,
required int status,
int mediaIndex = 0,
String? mediaSourceId,
}) async {
await into(downloadedMedia).insert(
DownloadedMediaCompanion.insert(
@@ -97,6 +98,7 @@ extension DownloadDatabaseOperations on AppDatabase {
grandparentRatingKey: Value(grandparentRatingKey),
status: status,
mediaIndex: Value(mediaIndex),
mediaSourceId: Value(mediaSourceId),
),
mode: InsertMode.insertOrReplace,
);
@@ -145,6 +147,12 @@ extension DownloadDatabaseOperations on AppDatabase {
)..where((t) => t.globalKey.equals(globalKey))).write(DownloadedMediaCompanion(status: Value(status)));
}
Future<void> updateDownloadMediaSource(String globalKey, String? mediaSourceId) async {
await (update(downloadedMedia)..where((t) => t.globalKey.equals(globalKey))).write(
DownloadedMediaCompanion(mediaSourceId: Value(mediaSourceId)),
);
}
Future<void> updateDownloadProgress(String globalKey, int progress, int downloadedBytes, int totalBytes) async {
await (update(downloadedMedia)..where((t) => t.globalKey.equals(globalKey))).write(
DownloadedMediaCompanion(
+1
View File
@@ -59,6 +59,7 @@ class DownloadedMedia extends Table {
IntColumn get retryCount => integer().withDefault(const Constant(0))();
TextColumn get bgTaskId => text().nullable()();
IntColumn get mediaIndex => integer().withDefault(const Constant(0))();
TextColumn get mediaSourceId => text().nullable()();
}
/// Profile ownership for shared physical downloads.