Library pages, public link writes and Nextcloud downloads
One read of the shares per request
Every request reads the library shares its user received once. OneReadOfLibrarySharesMiddleware
(apps/backend/api/sharing/middleware.py, in MIDDLEWARE after authentication) opens
one_read_of_received_shares, and received_library_shares (api/models/library_share.py) keeps the shares it read
for the rest of the request. Everything that decides which library photos a user reaches or what they see of them
reads that list: shared_library_q (and so visible_to, in_shared_libraries and shared_with_me),
received_grants, library_grant, without_switch, timeline_shares and library_share. A share that ends, starts
or changes its switches while the request runs is seen by the next request. Outside a request (a job, a shell) every
call reads the database again, as before. The photo detail, the search, the media routes and the sync feed then
never pair photos read through one set of shares with the switches of another.
A page of a library shared with the viewer (GET /api/days/<id>/photos/?library=<owner> or ?libraries=true, and the
legacy /api/albums/date/<id>/) reads the viewer's shares first: library_share or timeline_shares in
apps/backend/api/sharing/library_photos.py. library_view and own_view (api/timeline/photos.py) then filter the
photos by the owner ids of those shares, not by a subquery on the live share rows, and keep the grants of the same
shares in TimelineView.grants, a LibraryViewLookup (api/sharing/grants.py). The view hands it to
PhotoSummarySerializer under the LIBRARY_GRANTS context key, and lookup_for uses it instead of reading the
shares again. A photo of another owner that is missing from those shares is redacted as with every switch off
(LibraryGrant()).
So a share that ends, starts or changes its switches while the page runs never mixes two moments: the page shows the photos and the switches of one read. Up to 1.7.0 the photos were read through the live share rows and the grants when the serializer rendered the first row; a recipient who left in between got that page's rows with no redaction (places, map position, rating).
The search (SearchListViewSet, api/views/search.py) passes the grants of the shares its LibrarySearch read
(LibrarySearch.lookup) to the ranked, size-sorted and grouped answers the same way, and filters the shared photos by
those owner ids. The embedded motion video (_serve_embedded_media, api/views/media.py) reads the library grant
before it checks access, as the derived and original routes do.
The "Shared with you" place, thing and person photo lists (api/views/sharing.py) read the shares once too
(GrantLookup.read_now) and give that one read to the serializer and to redact_indirect_rows
(api/sharing/indirect.py). Up to 1.7.0 the second read could find a library share made in between and skip the
album's "What members see" rule for a row the serializer had not redacted.
Public link writes
SetPhotoShare (api/views/public_photos.py) and SetUserAlbumPublic (api/views/public_albums.py) load the link
row and save it in one transaction.atomic() that first locks the photo or album row
(select_for_update(no_key=True)) through owned_by(requester), so a photo or album that changed owner after the
first check is refused under the lock. On PostgreSQL a revoke and another change of the same link (a new expiry, new
switches, a retried enable) then run one after the other, and the second loads what the first committed. On SQLite
the transaction begins IMMEDIATE, which serializes them the same way. Up to 1.7.0 the load and the save were two
statements, and a save that had loaded the link before a revoke wrote the revoked slug back, so the link worked again.
Nextcloud downloads stay out of every library
The Nextcloud scan downloads into a folder of the account's own below DATA_ROOT/nextcloud_media
(nextcloud_media_root in api/photos/paths.py) and imports those files itself. _discover_scan_paths (api/directory_watcher/scan_jobs.py)
leaves that folder out of every library walk whose root holds it, such as a scan directory set to the data root. A
root stored inside it before 1.8.0 still walks its own user's folder there, and leaves the other users' folders out
(_other_downloads). The walk compares the folders it leaves out with the target of every symbolic link it follows
(_DirectoryWalk.left_out_by_target), so a link does not lead into them either; that holds for the folders a user
left out of their library too. reject_overlap_with_another_user (api/accounts/directories.py) refuses a scan
directory, an upload folder or a library folder inside the downloads folder; a directory already stored there stays
editable. Up to 1.7.0 a scan of the data folder walked into the Nextcloud downloads folder.
The folder of an account
NextcloudFolder records which folder is whose. folder_of (nextcloud/folders.py) chooses it at the account's first
Nextcloud scan, under hold_library_folders: the username, else <username>~<id>, else that with -2, -3 and so on.
It takes the first name that has no record in any spelling of upper and lower case, no File or FileCopy row below it,
and that os.mkdir could create, so the file system decides about names it treats as one. After that the folder never
changes: not on a rename, and not when somebody removes it from the disk. When the account is deleted its record stays,
without a user, and the name is not given to anyone else.
It is a table of its own and not a column of the user: the record outlives the account, and no save of a whole user row can write an older value back.
Migration 0271 records the folders in use. A file row says which account indexed it first (the id at the end of
File.hash, which a rename, a delete or a later scan never changes; a FileCopy points at such a row). Up to 1.7.0 a
library scan of the data folder also imported the downloads, so an admin can have photos in everyone's folder, and can
be the first with a file that was downloaded and not imported yet. An id alone does not prove it on SQLite, which can
give the id of a deleted account to a new one after a migration that rebuilt the table of users. So the dates count too:
a photo of a file there that is older than the account (added_on before date_joined, which only the creation of an
account sets) was somebody else's. Where the rows leave a doubt the account gets a new folder: one more download is better
than a folder that may hold somebody else's files.
| The account at the upgrade | Its folder |
|---|---|
| It was the first with every file in the folder of its name, and no photo of them is older than the account. Who else has photos of them does not matter, and missing files, exact copies and files whose photo is gone count too. | stays |
| The same, but an admin's library scan was first with some of them. The admin's library still holds the folder, the admin has no Nextcloud settings, and has a photo of each of these files that is newer than the account's first one there. | stays |
| A photo of a file there is older than the account. | new |
| Somebody else was first with a file there in any other way: an account that had the name before, also one that is deleted or scans the data folder; an admin with Nextcloud settings; an admin who was deleted, has another scan directory since, or whose photo of the file is gone; a library scan before the account's first import, also in the middle of its first download. | new |
| It was renamed since its last Nextcloud scan, so it was the first with no file in the folder of its name now. One of the first two rows holds for another folder, and everybody else with photos there is an account whose library holds it. | the one before the rename stays |
| The same, and its own library holds the downloads, but nobody else was ever first with a file in any of the folders or has a photo of one, no other account has Nextcloud settings (its first download may not be imported yet), and no other account has the name of the folder (an installation with one user, say). | the one before the rename stays |
| The same, but its own library holds the downloads and somebody else used them too or may have, its Nextcloud settings are removed, or an admin who scanned the folder was deleted or has another scan directory since. | new |
| It was renamed and has scanned again since. | the one of the new name stays |
| Its name and another account's are one folder on this disk, and both used it. | new for both |
| It never scanned its Nextcloud. | chosen at the first scan |
When the clock of the server was wrong while an account was created or photos were imported, an account can get a new folder too, and downloads once more.
A new folder is chosen at the next Nextcloud scan, as for a new account. That scan downloads everything once more. A
file the account was the first with becomes a copy (FileCopy), not a second photo. One that somebody else was first
with, or that changed in Nextcloud or here since, becomes a second photo next to the one that is there. Files on the disk without a
row in the database are downloaded again too, as they can be anyone's. Nothing is moved or deleted: what the account had
stays in the old folder, whose photos keep working, until an admin has checked and removed it.
The migration reads the paths below the downloads folder once, and asks once per account for a photo that is older than it. It looks at more photos only for a folder with files of more than one account and for a renamed account.
A worker of the old version that still runs during the update uses the folder of the username until it stops.
Tests and models
apps/backend/api/tests/sharing_and_public/test_library_share_snapshot.py: a recipient who leaves, or an owner who turns a switch on, after the page read its photos;LibraryViewLookupfor an owner missing from the read; a library share made while a "Shared with you" list renders; a leave during the ranked and the grouped search and during the photo detail; one read of the shares inside a request.apps/backend/api/tests/media_serving/test_embedded_media.py: no motion video without original files, also when the recipient leaves during the request.apps/backend/api/tests/sharing_and_public/test_public_link_revoke_order.py: every photo and album link write loads and saves inside the transaction, after the row lock, and a photo or album whose owner changed before the lock is refused.apps/backend/api/tests/sharing_and_public/test_public_link_revoke_postgres.py: on PostgreSQL, a revoke waits for a save of the same link (pg_blocking_pids) and the link stays revoked.apps/backend/api/tests/scanning/test_nextcloud_folder_owner.pyandapi/tests/migrations/test_migration_0271_nextcloud_folder.py: the folder of an account, and the folders in use at the upgrade; the model isverification/sharing/NextcloudFolder.tla.apps/backend/api/tests/scanning/test_nextcloud_downloads_out_of_libraries.py: a data-root scan, refused scan directories and library folders, a stored root inside the downloads or above its users' folders, and links into the downloads or into a folder left out of the library (a junction on Windows).verification/sharing/LibraryPageGrants.tlaandverification/sharing/PublicLinkRevoke.tla.