fix(continue-watching): clear the resume position when an item is marked watched
Marking a movie or episode watched left it sitting in Continue Watching with a checkmark, and the only way to shift it was to play it and skip to the end. Continue Watching membership on a MediaBrowser server is derived from UserData.PlaybackPositionTicks alone; Played is never consulted. Marking played normally zeroes that position as a side effect, so the row usually disappears and nothing ever checked that it had. When something writes a position back afterwards the item is left played *and* resumable, which the resume route happily keeps returning forever. markWatched now reads the UserItemDataDto the mark already returns and clears the bookmark itself when the server left one behind, so the postcondition holds however the item got into that state. The follow-up write costs a request only when the invariant is actually broken. The writer putting items there is our own offline queue. insertWatchAction already drops queued progress for an item when the mark is itself queued, but the online mark writes straight to the server and queues nothing, so a progress row recorded earlier survived and replayed afterwards — pending actions go out oldest first — restoring the very position the mark had cleared. The sync service now listens for watch-state events and discards queued progress for that item as the mark lands. Progress recorded after a mark is a rewatch and is queued later, so it is untouched. Plex never showed this because it forwards the recorded-at timestamp and lets the server discard a stale replay; the MediaBrowser stop report has nowhere to put one. Continue Watching also drops the row locally now instead of waiting a round trip for the refetch to confirm it, matching what removal events already did, and marking a season or show takes its on-deck episode with it. Watched items are deliberately still not filtered out of the shelf: Jellyfin keeps Played set when new progress arrives, so a rewatch in progress is indistinguishable from a stuck row, and filtering would hide it. close #1812
This commit is contained in:
@@ -1,10 +1,41 @@
|
||||
part of '../../jellyfin_client.dart';
|
||||
|
||||
mixin _JellyfinWatchStateMethods on _JellyfinClientInternals {
|
||||
/// Marking played normally zeroes `UserData.PlaybackPositionTicks` server-side,
|
||||
/// which is what drops the row from Continue Watching — membership on this API
|
||||
/// is derived from the position alone, never from `Played` (verified on
|
||||
/// Jellyfin 10.11.10). Relying on that side effect is not enough: a stale
|
||||
/// playback report replayed from the offline queue, another client, or a
|
||||
/// server-side plugin can leave `Played` set *and* a position behind, and the
|
||||
/// row is then pinned to Continue Watching forever (#1812).
|
||||
///
|
||||
/// So assert the postcondition instead of assuming it, using the
|
||||
/// `UserItemDataDto` the mark already returns. The follow-up write costs a
|
||||
/// request only when the invariant is actually broken.
|
||||
///
|
||||
/// Folders (series/season) report their own position as 0 while the server
|
||||
/// resets their children recursively, so nothing extra is owed here.
|
||||
@override
|
||||
Future<void> markWatched(MediaItem item) async {
|
||||
final response = await _http.post(paths.playedItem(item.id), queryParameters: {'userId': connection.userId});
|
||||
throwIfHttpError(response);
|
||||
|
||||
final data = response.data;
|
||||
final positionMs = data is Map<String, dynamic> ? jellyfinTicksToMs(data['PlaybackPositionTicks']) : null;
|
||||
if (positionMs == null || positionMs <= 0) return;
|
||||
|
||||
appLogger.d('JellyfinClient: ${item.id} stayed resumable after mark-played; clearing its resume position');
|
||||
await _clearResumePosition(item.id);
|
||||
}
|
||||
|
||||
/// Drop [itemId]'s resume bookmark without touching its played flag.
|
||||
Future<void> _clearResumePosition(String itemId) async {
|
||||
final response = await _http.post(
|
||||
paths.userItemData(itemId),
|
||||
queryParameters: {'userId': connection.userId},
|
||||
body: {'PlaybackPositionTicks': 0},
|
||||
);
|
||||
throwIfHttpError(response);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -34,6 +34,18 @@ class MediaBrowserPaths {
|
||||
String playedItem(String itemId) =>
|
||||
dialect.requiresUserScopedItemRoutes ? '$_user/PlayedItems/${_id(itemId)}' : '/UserPlayedItems/${_id(itemId)}';
|
||||
|
||||
/// Per-user playback-state write. `POST` with `{"PlaybackPositionTicks": 0}`
|
||||
/// clears the resume bookmark while leaving `Played` untouched (verified on
|
||||
/// Jellyfin 10.11.10 for both spellings).
|
||||
///
|
||||
/// Continue Watching membership on this API is derived purely from
|
||||
/// `UserData.PlaybackPositionTicks > 0` — `Played` is not consulted — so this
|
||||
/// is the only route that can guarantee a finished item stops being
|
||||
/// resumable. See [MediaServerClient.markWatched].
|
||||
String userItemData(String itemId) => dialect.requiresUserScopedItemRoutes
|
||||
? '$_user/Items/${_id(itemId)}/UserData'
|
||||
: '/UserItems/${_id(itemId)}/UserData';
|
||||
|
||||
/// Favourite flag write route (`POST` to add, `DELETE` to remove).
|
||||
String favoriteItem(String itemId) => dialect.requiresUserScopedItemRoutes
|
||||
? '$_user/FavoriteItems/${_id(itemId)}'
|
||||
|
||||
@@ -86,7 +86,48 @@ class OfflineWatchSyncService extends ChangeNotifier {
|
||||
/// silently drops local watch progress.
|
||||
static const int maxSyncAttempts = 5;
|
||||
|
||||
OfflineWatchSyncService({required this._database, required this._serverManager});
|
||||
StreamSubscription<WatchStateEvent>? _watchStateSubscription;
|
||||
|
||||
OfflineWatchSyncService({required this._database, required this._serverManager}) {
|
||||
_watchStateSubscription = WatchStateNotifier().stream.listen(_onWatchStateChanged);
|
||||
}
|
||||
|
||||
/// A terminal watch state just landed, so any progress still queued for that
|
||||
/// item is stale and must not replay.
|
||||
///
|
||||
/// [AppDatabase.insertWatchAction] already purges the queue when the mark is
|
||||
/// itself queued (offline). The online path writes straight to the server and
|
||||
/// queues nothing, so without this the older progress row survives and
|
||||
/// [syncPendingItems] later rewrites the resume position the mark cleared —
|
||||
/// on MediaBrowser that pins the item to Continue Watching for good (#1812).
|
||||
/// Plex is unaffected in practice (PMS discards a replay whose `updated`
|
||||
/// timestamp is stale) but the queue entry is meaningless there too.
|
||||
///
|
||||
/// Progress recorded *after* the mark is a genuine rewatch: it is queued
|
||||
/// later, so it is never touched here.
|
||||
void _onWatchStateChanged(WatchStateEvent event) {
|
||||
if (_isShutDown) return;
|
||||
if (event.changeType != WatchStateChangeType.watched && event.changeType != WatchStateChangeType.unwatched) {
|
||||
return;
|
||||
}
|
||||
unawaited(_discardQueuedProgress(ServerId(event.serverId), event.itemId));
|
||||
}
|
||||
|
||||
Future<void> _discardQueuedProgress(ServerId serverId, String itemId) async {
|
||||
try {
|
||||
final removed = await _database.deleteQueuedProgressForItem(
|
||||
profileId: _activeProfileId,
|
||||
serverId: serverId,
|
||||
clientScopeId: await _clientScopeIdForItem(serverId, itemId),
|
||||
ratingKey: itemId,
|
||||
);
|
||||
if (removed == 0) return;
|
||||
appLogger.d('Dropped $removed superseded queued progress action(s) for $serverId:$itemId');
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
appLogger.w('Failed to drop superseded queued progress for $serverId:$itemId', error: e);
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a sync is currently in progress
|
||||
bool get isSyncing => _isSyncing;
|
||||
@@ -834,6 +875,8 @@ class OfflineWatchSyncService extends ChangeNotifier {
|
||||
@override
|
||||
void dispose() {
|
||||
_isShutDown = true;
|
||||
_watchStateSubscription?.cancel();
|
||||
_watchStateSubscription = null;
|
||||
if (_offlineModeSource != null && _offlineModeListener != null) {
|
||||
_offlineModeSource!.removeListener(_offlineModeListener!);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user