User data and sync
Status: works on phone and TV; no open bugs known. As of 2026-11-23 (main at 26e62f27a).
Start here
Section titled “Start here”- The rules (played, favourite, resume position, last played, who wins in a sync) are
PlayState,PlayStopandSyncMergeindomain/.../domain/userdata/and nowhere else (rule R14). Times are parameters; nothing there reads a clock. - The writes are in
MediaRepositoryImpl(markAsPlayed,markAsUnplayed,markAsFavoriteof the roleUserDataUpdates;postPlaybackProgress,postPlaybackStopof the rolePlaybackReports, #967), throughreportUserDataandwriteAndReport(UserDataReport.ktinapplication, over the store roleDownloadUserDatasince #966): write the row, mark ittoBeSynced, ask the server, unmark when it answered. - What is owed to the server is
PendingUserDataStore(port inapplication, Room indata).UserDataSyncandSyncWorkerincoresend it, throughUserDataSyncPort(JellyfinUserDataSync). The current user’s marks go throughUserDataPort(JellyfinUserData). - Only downloads have a row; for any other item a write is the server call alone. State is per user:
AccountKey(serverId, userId). - Test fast:
./gradlew :domain:test :data:testDebugUnitTest, and./gradlew :core:testLibreDebugUnitTest --tests '*Sync*'.
What it does
Section titled “What it does”Played, favourite, resume position and last played date belong to a user and an item. The server keeps them. For a download the app keeps a copy in Room (UserDataEntity, table userdata), so the item can be watched and marked without the server.
A change goes to the row first and is marked toBeSynced in the same transaction. Then the request goes out. If it reaches the server, the mark is taken off. If not, the mark stays and UserDataSync.request asks WorkManager for a SyncWorker run. The worker visits each saved server and user with marked rows, reads what the server has, lets PlayState.mergeWithServer decide and sends. If another device played the item later, the server’s progress replaces the local one (SyncMerge.TakeServer); otherwise the local state is sent, with its date only when newer (SyncMerge.SendLocal). The worker then refreshes the rows of downloads from the server, except the item open in the player.
Playback reports are the other flow: playback-reporting. Its progress and stop write user data through the same reportUserData; a stop becomes a PlayState through PlayStop.of, with the server’s resume thresholds (#637).
Where the code is
Section titled “Where the code is”| Path | What |
|---|---|
domain/.../domain/userdata/PlayState.kt |
markedPlayed, markedUnplayed, afterUnplayedReport, withFavorite, progressed, stopped |
domain/.../domain/userdata/PlayStop.kt, SyncMerge.kt |
What a stop writes; mergeWithServer, refreshedFromServer |
domain/.../domain/userdata/WatchRows.kt |
Which downloads Home’s Continue Watching and Next Up hold while the server is away, from the play states (PlayState.hasResumePosition, WatchRows.continueWatching, WatchRows.nextUp; #1054, see home and library); WatchRowsTest |
application/.../repository/MediaRepositoryImpl.kt |
The writes, reportUserData, currentUserId |
application/.../repository/UserDataReport.kt |
writeAndReport, over the role DownloadUserData (#966) |
application/.../repository/DownloadUserData.kt |
writeAndMarkToBeSynced (one transaction, gives MarkedUserData), markToBeSynced, clearToBeSynced, updatePlayState; Room in RoomDownloadStore |
data/.../database/ServerDatabaseDao.kt |
updatePlayState, writeAndMarkToBeSynced, clearUserDataToBeSynced |
application/.../repository/PendingUserDataStore.kt |
The sync’s store port; Room in data/.../database/RoomPendingUserDataStore.kt |
core/.../work/UserDataSync.kt |
The UserDataSyncRequester: unique work, restart when the server is back |
core/.../work/SyncWorker.kt |
doWork, syncUserData, refreshFromServer, retry rules |
domain/.../domain/ports/UserDataPort.kt, UserDataSyncPort.kt |
The server ports |
server/jellyfin/.../JellyfinUserData.kt, JellyfinUserDataSync.kt |
The Jellyfin adapters |
domain/.../models/AccountKey.kt |
One user on one server |
- R14: the play state rules are declared in
domainalone;dataand the sync compare no dates. A new rule goes intoPlayState. - R12: tick conversions are
Ticks. R10: ids are value classes. - R4, R9: only
dataimportsServerDatabaseDao; only the apps depend ondata. The sync reaches Room throughPendingUserDataStore, the repository throughDownloadUserData(#966:MediaRepositoryImplis inapplicationand names no DAO). - R1, R2: no SDK and no
JellyfinApioutsideserver/jellyfin. - A Room migration gets an independent review when no bot reviewed the PR (
CLAUDE.md).
How to test it
Section titled “How to test it”./gradlew :domain:test :application:test :architecture:test # PlayStateTest, PlayStopTest, SyncMergeTest, R14./gradlew :application:test --tests '*MediaRepositoryImplTest' # the writes over fakes./gradlew :data:testDebugUnitTest --tests '*PlayStateWritesTest' --tests '*PendingUserDataSyncTest' --tests '*UserDataReportTest'./gradlew :core:testLibreDebugUnitTest --tests '*Sync*' # SyncWorkerRunTest, SyncWorkerDecisionsTest, LastPlayedDateSyncTestOn a device (docs/dev-environment.md, test-server skill): users tester and second on one server, a downloaded Short Movie for both (so both have a row), Resume Show for positions. Marking items on the server is free during tests.
- Server away:
adb root,adb shell iptables -I OUTPUT -d 10.0.2.2 -p tcp --dport 8096 -j REJECT --reject-with tcp-reset(-Dundoes it). Mark the item played, unplayed, favourite; watch part of it. The row keepstoBeSynced; with the server back the sync sends it. - A request held open (#907):
tools/test-server/forward.py --hold <METHOD> <path-prefix>holds one chosen request until Enter or--release-file(its rate limit slows only long answers). Or hold the call in a unit test, with a fake whose call suspends or switches the user (PlayStateWritesTest), and read the rows. On the phone the race did not show (emulator, a proxy holdingDELETE /UserPlayedItems15 s, switch from Home): the mark runs in the item page’sviewModelScope, and the switch is on Home only, so the page and its mark are most likely gone before the server answers. - Look at the rows with
run-as nl.midasvo.findroid.ce.debugon the database:userdatahasplayed,favorite,playbackPositionTicks,lastPlayedDate,toBeSynced.
Before a PR
Section titled “Before a PR”./gradlew ktfmtCheck./gradlew :domain:test :application:test :architecture:test./gradlew :data:testDebugUnitTest :core:testLibreDebugUnitTestA new rule in PlayState comes with a test in PlayStateTest or SyncMergeTest. Then run the server-away steps on an emulator. A change to how SyncWorker treats the item open in the player (playingHere) also needs player checked.
Decisions and history
Section titled “Decisions and history”- #281, #552: no offline mode; rows are written and marked first; the sync runs when the server is back.
- #584: the sync ends a stale play session of this device.
- #633, #635: last played date in the sync; the server’s newer progress wins; the refresh of downloads.
- #637: the local stop follows the server’s resume thresholds.
- #762, #770: the sync uses
UserDataSyncPort, by server type. - #898 / #906:
PlayStateowns the rules. #899:PendingUserDataStore,AccountKey.
Pitfalls
Section titled “Pitfalls”- Read the user once per write (#907).
currentUserIdis a getter onserver.session.userId: every read can answer another user after a profile switch.markAsUnplayedused to read it again after the request, so a switch while the request waited cleared the new user’s date; it now reads it once and hands it toreportUserData. A new write that touches the row after a server call does the same. - A request is pinned to its server, the user id is not.
JellyfinUserDatakeeps theActiveServerit was made for; the id used for the local row is a separate read. - An unplayed mark keeps the last-played date until the server has it (
markedUnplayed,afterUnplayedReport): the sync needs it to see the mark is not older than the play. - The server’s date is a local date-time;
toPlayStateandepochMillisOfinSyncWorker.ktconvert it. - The refresh leaves marked rows and the open item alone; overwriting them would lose offline progress (#635).
- A write for a streamed item is the server call only. If it fails, it is lost.
- A sync asked during a run does nothing (unique work,
KEEP);needsAnotherRuncovers a row marked meanwhile. A refused login (401) leaves the row marked for the next sync.
Source: docs/areas/user-data.md · raw Markdown