Files
TREK/server/src/nest/collections/collections.service.ts
T
jubnl 305b113d5f Feat/storage driver (#1949)
* feat(storage): storage key validation, driver contract suite, LocalDriver

* feat(storage): MirrorDriver composite backend

* feat(server): add TREK_UPLOADS_DIR env var for the uploads root

* feat(storage): settings-backed registry, StorageService facade, module wiring

* test(storage): pin src/nest/storage coverage thresholds

* feat(storage): multer upload options factory

* refactor(files): spool trip-file uploads through storage

* refactor(collab): spool note attachments through storage; normalize files/ prefix

* refactor(journey): spool journey media uploads through storage

* refactor(trips): spool trip cover upload through storage

* refactor(places): spool place-image and collection uploads through storage

* refactor(auth): spool avatar upload through storage

* fix(server): consolidate allowed_file_types onto AllowedFileTypesService

* fix(storage): default defParamCharset to utf8 in the upload factory

* test(db): re-point the trailing-migration guard at the trip_files prefix strip

* fix(storage): age-gate the boot spool sweep

* test(files): key FILE-P02 residue checks on upload size

* test(storage): raise src/nest/storage coverage pin

* refactor(platform): serve /uploads routes through storage

* refactor(files): serve trip-file downloads through storage

* refactor(memories): serve photo bytes through storage

* refactor(maps): stream place-photo bytes through storage

* refactor(share): stream shared place-photo bytes through storage

* refactor(journey): serve public journey assets through storage

* fix(share): answer place-photo cache miss with 204 like the maps proxy

* test(server): close serving-swap gaps surfaced by the full tier

* test(storage): raise src/nest/storage branches pin

* refactor(place-photos): route place-photo cache through storage

* refactor(places): reclaim place images and collection covers through storage

* refactor(trips): trip covers through storage

* refactor(auth): avatar reclaim through storage

* refactor(collab): note-file deletion through storage

* refactor(files): trash deletion through storage

* refactor(memories): trek photo cache internals through storage

* refactor(memories): thumbnail service category-addressed

* refactor(memories): immich upload reads through storage; drop uploads-root

* refactor(journey): photo deletion through storage

* fix(memories): reclaim orphaned trek photo cache objects in the ttl sweep

* feat(journey): journey-thumbs orphan sweep and delete-site derived-artifact reclaim

* test(server): close slice-4 gaps surfaced by the full tier

* test(coverage): raise slice-4 domain pins

* refactor(backup): list, existence and delete through storage

* refactor(backup): download through storage sendToResponse

* refactor(backup): create backup through per-category storage enumeration

* refactor(backup): restore and upload-restore through storage

* refactor(backup): auto-backup retention through storage

* refactor(server): boot dir-creation to driver init

* test(server): close slice-5 gaps surfaced by the coverage tier

* test(coverage): raise slice-5 backup pin

* test(server): resolve async backupFileExists mocks for the tests typecheck

* test(server): pin ranged-read stat and zero-byte roundtrip in the storage driver contract

* feat(server): TREK_S3_* env wiring — schema, s3 namespace, all-or-nothing boot rule

* feat(server): S3 driver core — aws-lite client seam, stat/delete, driver-owned deadlines

* refactor(server): type the aws-lite S3 client via s3-types reference

* feat(server): S3 driver getStream and list — ranged full-object stat, iterator pagination

* feat(server): S3 driver put — bounded-peek routing, Upload inactivity deadline

* fix(server): S3 driver Upload monitoring — Transform interposition, stream cleanup

* feat(server): env-declared s3-main storage backend; settings-declared s3 rejected

* test(server): env-gated S3 driver contract suite against MinIO

* fix(server): S3 driver zero-byte put — work around aws4 duplicate Content-Length signing defect

* fix(server): scope the zero-byte put workaround to buffered bodies; bind test MinIO to localhost

* ci(server): MinIO-backed S3 driver contract job

* ci(server): align s3-contract shim comment and MinIO bind with references

* docs: S3 storage backend configuration and off-box backup mirroring

* docs: restore README formatting, keep S3 additions only

* ci(server): make live-MinIO contract runs manual-only (AIStor licensing)

MinIO's community line is discontinued entirely; the maintained successor, AIStor, requires a license key that can't safely live in a shared CI runner. The s3-contract CI job is removed; live-provider contract runs against MinIO/AIStor are now a manual, license-gated procedure documented in docker-compose.minio-test.yml's header.

* chore(server): final-review polish — stale CI refs, dockerignore key, boundary pins, zero-byte tempfile contract case

* expose admin ui

* feat(shared): storage backend type registry, wire schemas, category relocation

* feat(server): accept settings-declared s3 backends — shared-schema options, decrypt-at-build, registry preview()

* feat(server): remove TREK_S3_*/TREK_UPLOADS_DIR — tombstone boot rule, built-in override as the relocation path

* feat(server): deriveSecurity.encryptionKeySet and storage secret plumbing (gate, encrypt, unmask, mask, redact)

* chore(server): drop storage-secrets import that only Task 8's probe helper will use

* feat(server): seed-once storage-config.json boot import — fail closed on any invalid seed

* feat(server): registry snapshot() — effective storage config with provenance

* feat(server): api/admin/storage GET/PUT — masked secrets, preview-gated saves, wired reload, audits

* feat(server): api/admin/storage/test — per-target ephemeral probes, mirror targets probed individually

* test(server): e2e coverage for api/admin/storage — guards, managed refusal, envelope, probes

* test(server): give e2e harness DBs the app_settings table the storage registry reads at boot

* test(coverage): raise pins after the storage admin-config slice

* fix(server): final-review polish — encryption-gate wording, all-field sentinel guard, README/e2e notes

* test(server): storage slice-1 review follow-ups — mirror s3-replica probe decrypt, all-encrypted seed without key, test-route 401

* feat(i18n): storage admin domain + tab label in every locale

* feat(client): adminApi storage endpoints — getStorage, updateStorage, testStorageBackend

* feat(client): storage admin draft-document helpers + relative-time util

* feat(client): schema-driven storage BackendForm — fields by kind, mask echo, encryption banner

* feat(client): admin storage panel — backends list, category map, health strip, one-PUT save

* feat(client): storage tab on the admin page

* feat(client): storage admin panel and section on the phone shell

* fix(client): block renaming a storage backend onto an existing name

* feat(server): allow mirrors on any storage category — lift the backups-only rule

* feat(i18n): replicas-on-primary mirror strings in every locale

* feat(client): mirror fold/synthesize model helpers for replicas-on-primary

* feat(client): BackendForm mirror-targets composer; raw mirror type hidden

* feat(client): desktop storage panel folds mirrors into their primaries

* feat(client): phone storage panel folds mirrors into their primaries

* fix(client): strip removed backends from remaining mirror replicas — no phantom refs

* feat(storage): retire photos as a configurable category — served-only via the ServedCategory seam

* feat(i18n): descriptive storage category names and descriptions in every locale

* feat(client): descriptive category names, descriptions and id badges in the storage panel

* docs(storage): fix stale nine-category comments after the eight-category shrink

* docs: storage configuration via the admin UI and seed file — README + compose

* docs(wiki): Admin Storage page and storage cross-links

* docs(server): storage domain blueprint entry; TREK_PLACE_PHOTO_DIR tombstone clarifier

* refactor(server): drop the storage env-var tombstones — the variables never shipped

* docs: storage config is simply new in v4 — drop the removed-variables story

* feat(shared): backfill-status and usage wire schemas on the storage admin state

* feat(server): MirrorDriver.backfill — prefix-scoped, size-compared, cancellable replica catch-up

* feat(server): one-at-a-time storage backfill jobs + the storage events bridge

* feat(server): storage usage scan — nightly cron, on-demand service, persisted with computedAt

* feat(server): replica_failure admin notifications — events bridge, hourly debounce, channel registration

* feat(i18n): replica-failure notification text in every locale

* feat(server): storage backfill and stats-refresh admin endpoints

* feat(client): adminApi backfill and stats-refresh endpoints

* feat(i18n): storage sync and usage strings in every locale

* feat(client): desktop storage panel — sync now, usage display, backfill polling

* feat(client): phone storage panel — sync now, usage display, backfill polling

* fix(client): per-category usage line on the phone storage panel

* docs: sync now, usage stats and replica-failure notifications

* test(server): admin scope now includes replica_failure, not just version_available

Commit 6db135da added replica_failure to ADMIN_SCOPED_EVENTS but left
NPREF-005b asserting the admin scope contained only version_available.
Update the expectation to match.

* test(server): cover storage backfill/stats/jobs branches to hold the coverage pin

* fix(client): guard storage admin polling against stale responses racing a save

A poll GET issued before a save's PUT resolves could land after the save
applied its fresher response, overwriting it with the pre-save world and
re-deriving the draft from that stale snapshot. useStorageAdmin now tracks
a state-application sequence number and drops any refreshState() response
whose sequence has moved on, or that resolves while a save is in flight.
Also guard the fire-and-forget refreshState() calls in startBackfill/
cancelBackfill against unhandled rejections, matching the poll's style.

* fix(server): report 404 before 409 when a backfill start names an unknown mirror

startBackfill checked the global busy flag before validating that :name is
a mirror routed by some category, so POST .../backends/garbage/backfill
answered 409 instead of 404 whenever a sync was already running. Resolve
and validate the target first (BackfillTargetError -> 404), then check
busy (BackfillBusyError -> 409).

* feat(client): two-column category grid on the storage panel

* feat(server): plugin files.getContent reads through the storage layer

* feat(server): plugin files.create writes through the storage layer

* refactor(server): drop FilesService.resolveFilePath, last direct-fs seam in the plugin surface

* fix(server): map StorageInvalidKeyError to the plugin file accessibility envelope

* feat(storage): allow credentialed backends without an explicit ENCRYPTION_KEY

Secrets are still always encrypted at rest; without an explicit key the
implicit data-directory key is used. Removes the plaintext-secret refusal
from the admin PUT and the seed import, the encryptionReady GET field, the
save-blocking banner in both admin shells, and the now-consumerless
security.encryptionKeySet env derivation. Docs now recommend (not require)
an explicit key, since the implicit key rides inside backup archives.

* test(client): pin the stale-poll release to the pre-save GET in FE-ADMIN-STOR-024

The stub deferred EVERY poll GET and rebound the release handle to the
newest one, so a tick landing after the save was released with the
pre-save payload — a response no real backend can produce, which the seq
guard rightly applied. Only the first (genuinely stale) GET is held now;
later GETs answer with the world the mock server currently holds. Also
drops the encryptionReady fixture field removed from the schema in the
previous commit.

* fix(storage): backfill puts carry content-type, stat failures report op stat, progress clamps

* feat(storage): category migration job — copy, flip, delta sweep, reclaimable tally

* feat(storage): migration start/cancel endpoints and client API

* test(client): storage admin fixtures carry the new migrations field

* fix(storage): migration POST answers 200 and validates its body via the shared schema

* fix(storage): cancel running storage jobs when a config save removes their backend

* fix(notifications): in-app replica-failure row carries the suppressed count

* feat(client): migration prompt, queue, and progress on the storage panel

* fix(client): migration queue holds a synchronous single-flight lock

* feat(client): migration prompt and progress on the mobile storage panel

* fix(server): register category-migration routes in the managed-route allow list

StorageAdminController.migrationStart/migrationCancel were added under the
controller-level @ManagedForbidden() decorator but never added to
MANAGED_ROUTE_ALLOW_LIST, so validateManagedRoutes() threw "Managed surface
changed" on every buildApp() call, failing 46 test files (any test booting
the real app).

* test(server): cover category-migration copy/sweep branches to hold the storage coverage pin

Adds MIG-005 (a target object already matching size/key is skipped in the
copy phase itself, not just the delta sweep) and MIG-006 (a copy failure
during the delta sweep is counted without undoing the flip that already
happened). src/nest/storage/**'s branch coverage was 83.91%, just under the
84% pin.

* docs(storage): category migration — move existing objects on reassign

* fix(storage): close the pre-flip cancel window and cancel re-routed migrations

A cancel landing between the last copy iteration and the category flip was
silently ignored, and a config save that re-routed the migrating category to
a third, still-defined backend never stopped the in-flight migration. Also
guards assignCategory against writing an unknown backend name, and fixes a
missing `suppressed` notification param being treated as truthy suppression.

* fix(client): migrations keep mirrors, queue waits on backfills, sweep failures surface

Reassigning a category onto a mirrored primary now posts the migration to the
mirror's wire name instead of the bare primary, so replication survives the
move. The queued-migration effect also waits out a running backfill (the
server's one-job rule spans both), and a done migration with sweep failures
now surfaces a doneFailures line in both shells, translated into every locale.

* ci(server): run the S3 contract suite against MinIO

Resurrects the s3-contract job removed in 6fbff835, adapted for AIStor:
writes the MINIO_LICENSE_KEY secret to a license file, boots the licensed
container, and runs the contract suite hermetically (no --coverage) so the
storage coverage pin keeps holding from unit tests alone. Gated to same-repo
PRs since fork PRs have no access to the license secret.

* fix(server): backup archives uploads from a remote primary

createBackup() enumerated uploads through withLocalFile, which downloads a
remote object to a temp file, hands it to the callback, then deletes it
before archiver ever reads it — archiver reads entries lazily during
finalize(), so a remote (S3) primary silently backed up zero upload bytes.

Add StorageService.getLocalPathOrNull(category, name): a narrow locality
probe that returns a real path only when one exists on disk right now (the
fs-existence check is the fail-safe half of the contract — a driver-reported
path that vanished between list() and here falls back too, not just a
path-less remote driver). The enumeration loop uses it directly when
available (zero-copy, unchanged for a local primary) and otherwise streams
the object into a per-backup staging dir under the backups spool, cleaned up
in the existing finally alongside zipSpool/dbSnap.

Also wire archive.on('warning', reject): archiver treats a missing/unreadable
entry as a warning, not an error, and skips it by default — which would
silently drop the entry from an otherwise "successful" backup.

* fix(server): client aborts stop 500ing and leaking streams

storage.service.ts sendToResponse: the local res.sendFile branch now treats
ECONNABORTED/ECONNRESET/ERR_STREAM_PREMATURE_CLOSE/syscall=write as success,
mirroring res.sendFile's own default callback (the contract
storageStaticHandler already implements caller-side). The remote branch
replaces the hand-rolled pipe+Promise with pipeline(stream, res), which
destroys the source stream on any failure — fixing the leaked S3 keep-alive
socket on a client abort — while swallowing the same client-abort error set;
a real source error still rejects so callers keep their miss contracts.

This centralizes the fix for every caller that streams through
sendToResponse: files-download, journey-public, photo-resolver,
trek-photo-cache, backup downloads, and /uploads/photos.

trek-exception.filter.ts now no-ops (destroying the socket) when headers are
already sent, so no filter path ever writes onto a response in flight.

maps.controller.ts and share.controller.ts's place-photo bytes proxies
switch their direct getStream+pipe to pipeline() with the same abort-tolerant
catch, keeping the existing 204-empty fallback for pre-header failures.

* fix(storage): mirror read fallback consults replicas on primary miss

MirrorDriver.getStream/stat now fall back to replicas on a primary miss
(StorageNotFoundError / null stat), not just an infra error — binding
decision: a read succeeds if ANY member holds the object. Only a
deterministic StorageInvalidKeyError skips fallback.

stat() gets a dedicated fallback loop instead of reusing fromReplicas:
a replica returning null means "keep looking," which the generic
catch-and-continue helper would have short-circuited on.

StorageService.sendToResponse/withLocalFile stop treating composite
driver.stat() as proof the object is at localPath; both now fs-check
the path itself and fall through to the stream branch when it's
missing, which is what actually reaches a MirrorDriver's replicas.

* feat(storage): remote serving parity — content-type, validators, range

Local backends get content type, ETag/Last-Modified, conditional GET, Range
and HEAD for free from res.sendFile; the proxying branch every remote
backend (and a local primary whose file vanished) falls through to had none
of it — every request re-streamed the whole object with no cache validator
and no way to seek.

sendStreamed() reproduces the contract from ONE pre-flight driver.stat():
a miss throws before any header is set (the caller's 404/204/next()
contract), and that same stat then answers 304s, 416s, HEADs and suffix
ranges without ever opening a body. The decisions live in a new pure module,
http-serving.ts:

- validatorsFor: the driver's ETag, else express/send's weak
  W/"<size hex>-<mtime hex>". mtimeMs 0 (S3 can report it) means no
  Last-Modified and no derived tag — serving uncached beats a 1970 header
  that reads as fresh forever.
- isNotModified: If-None-Match (weak compare, *, list) wins over
  If-Modified-Since, per RFC 9110 §13.2.2.
- decideRange: single range only, inclusive clamp, suffix computed from the
  pre-stat size, unsatisfiable → 416, If-Range strong-exact or full entity.
  A multi-range or malformed spec is answered with the full object rather
  than multipart/byteranges. If-Match/If-Unmodified-Since stay out of scope.

Range math uses the pre-stat size as the single source of truth, the ranged
getStream stat is never re-consulted, and the pipeline abort semantics are
unchanged.

S3Driver now surfaces the bucket's ETag on stat()/getStream() (on a ranged
GET it is still the full object's tag). aws-lite strips the tag's
surrounding quotes — verified live against AIStor RELEASE.2026-08-07 — and
an unquoted entity-tag is not a valid HTTP ETag, so the driver restores
exactly the quotes the SDK removed and nothing else.

* fix(server): key rotation covers storage.backends

The S3 storage backend's secretAccessKey is encrypted at rest inside the
app_settings row keyed storage.backends, but scripts/migrate-encryption.ts
didn't know about it — after a key rotation that secret stayed encrypted
under the old key and silently failed to decrypt on the next read.

The script now re-encrypts secretAccessKey for every s3 backend inside the
storage.backends JSON array, using a pinned copy of the secret-field list
(it deliberately can't import @trek/shared). The parity test pins that copy
against storageSecretFields('s3') so a new secret field can't be missed
silently.

* fix(server): restore reloads the storage registry

restoreFromZip closed and reopened the DB but never reloaded the
storage registry, so uploads rehydration ran against the STALE
pre-restore driver/category map instead of the config the restored
archive's own travel.db carries (audit #4, major).

StorageService gains reloadConfig(), a narrow passthrough to the
(unexported) registry's reload() — same shape as health(). backup.impl's
restoreFromZip now calls it right after reinitialize() and right before
the uploads wipe/rehydrateUploads/plugin staging, so rehydrated bytes
land where the restored config says. When reopen fails (reinitFailed),
reload and rehydration are skipped entirely — already reported as
"restart required", and there is no live DB handle for the registry to
read anyway.

* fix(server): category migration rewrites keys across prefix changes

Category migration copied objects to the destination backend using the
SOURCE key verbatim. A mode-A ('' prefix) source migrating onto a
prefixed backend (or vice versa) landed objects at keys the registry
could never resolve post-flip (audit #8).

Extracts StorageRegistryService.keyPrefixFor(category, backendName) from
build()'s inline conditional so startMigration can compute the
destination prefix before the flip. runMigration now maps every
destination key (destPrefix + key.slice(sourcePrefix.length)) in the
copy skip-check, the put, and the delta sweep; the reclaimable tally
stays source-keyed. Equal-prefix migrations are unaffected (identity
mapping).

* fix(plugins): await the now-async place/collection services in RPCs

PlacesService.update/remove and CollectionsService.deletePlace became
async when they started deleting storage objects, but their plugin RPC
handlers still called them synchronously: the null/false refusal check
compared against an always-truthy Promise, and places.update broadcast
a Promise instead of the resolved place. Both handlers now await the
service call; mapCollectionError is async-aware (accepts a sync-or-
async thunk) so the sync callers (create/update/savePlace/copyToTrip)
keep working through the same path. Verified the RPC host already
awaits handler results, so no change was needed there.

* fix(storage): backfill Sync now gains a deletion sweep

The Sync now backfill copied primary objects to replicas but never removed
replica objects the primary no longer has, leaving mirror read-fallback able
to serve stale data indefinitely after a failed replica delete. backfill()
now sweeps each replica post-copy: any replica key absent from the primary
key-set is re-checked against primary.stat (a race gate against the primary
regaining the key) before being deleted. Adds a deleted counter end-to-end
(driver, jobs service, shared schema, both admin shells, all 23 locales) and
a new ReplicaFailure 'list' op for a replica whose listing itself fails
during the sweep.

* feat(storage): optimistic concurrency for admin config saves

A stale admin PUT of storage settings could silently undo a category
migration's just-flipped assignment. Adds a version counter
(app_settings row storage.config_version) bumped inside the same
transaction by both writers — applyConfig and the migration flip
(assignCategory). applyConfig compares the submitted version first
and throws StorageConflictError, mapped to 409 ahead of the blanket
400. The client carries the draft's loaded version through every
local edit and maps a 409 to a distinct "settings changed underneath
you" message, refreshing state while preserving the dirty draft for
review.

* fix(storage): surface a failed config load + redact secretAccessKey

Registry load() now records the exact parse/validate failure as
lastLoadError() instead of silently falling back to last-good config or
built-in defaults; StorageAdminService exposes it as configError on the
admin state (storageAdminStateSchema extended additively), and
AdminStoragePanel renders it as a warning banner naming the error. Save
stays enabled — it's the recovery path — but an admin can no longer save
over a broken stored config without knowing.

Also adds 'secretaccesskey' to globalMiddleware's SENSITIVE_KEYS request-log
redaction list, so storage admin PUT bodies never leak the S3 secret into
debug logs.

* fix(client): admin storage panel state races

Migration prompt now recomputes computeMigrationCandidates(draft, state)
on render and again at confirm time, so a stale open-time snapshot never
drives stripCategories or the migration queue; adds a Cancel button to
the dialog. A failed startMigration POST explicitly clears the remaining
queue with a toast naming the dropped categories and refreshes state,
instead of leaving it invisibly stranded; a "Queued: X, Y" line renders
while the queue is non-empty. Testing a mirrored row now expands the
draft primary + replicas client-side and probes each individually,
instead of sending the mirror stub to the server (which resolved
targets from its own live snapshot, silently testing saved options over
unsaved draft edits). A completed/cancelled/errored backfill now renders
its terminal status alongside the Sync now button instead of hiding it,
so it can be re-run without a page reload.

* fix(storage): refuse shared replica backends so the sync sweep cannot cross categories

A mirror's "Sync now" sweep makes each replica MATCH the primary: it lists the
replica under every prefix the mirror's categories occupy and deletes the keys
the primary doesn't hold. The backups category's prefix is '' — the replica's
entire root — so making a category-serving backend (say uploads-local) a
replica of a backups mirror meant the first sync deleted every user upload.
Nothing forbade the config: validateConfig only checked that mirror targets
existed and weren't nested, and the admin picker offered every backend.

The sweep is correct, so the fix is at the config level:

- validateConfig refuses a backend that is both a mirror replica and a
  category target (directly, or as the primary of a mirror a category routes
  to), and refuses a backend replicating two mirrors whose swept prefixes
  overlap — '' overlaps everything. Disjoint prefixes stay legal.
- assignCategory (the migration flip, the other writer of the settings rows)
  refuses routing a category onto a backend that is currently a replica.
- The admin mirror-target picker (both shells) only offers backends that serve
  no category; already-selected targets stay listed so they can be unchecked.
  A category assigned to a replica still surfaces the server's 400 verbatim.

Existing shared-replica fixtures were rewritten onto dedicated replicas.

* fix(client): a 409 conflict save is recoverable without a page reload

After a 409 the admin storage panel was a dead end. refreshState leaves a
dirty draft's version alone on purpose (audit #7: the background poll must
never float a mid-edit draft past a migration flip it never carried), setDraft
re-attaches that same version to every later edit, and save() resubmits it —
so every retry 409s again and only a full page reload got the operator out,
while the banner told them to "review and save again".

The version block stays. Instead the conflict banner now offers the one action
that can clear it: discard the local edits and reload the saved settings —
a fresh GET applied through applyState, re-deriving the draft (and its
version) and clearing dirty/saveError. Bumping the version in refreshState,
or only in the 409 branch, was rejected: either resurrects audit #7, since a
full-document save would then revert a flip the draft never carried.

storage.saveConflict now points at that action instead of implying a plain
re-save works (all 23 locales). The escape lives in the shared hook, so the
phone panel renders it too.

* fix(client): deflake the queued-migration-waits-out-backfill panel test

FE-ADMIN-STOR-037 seeds a running backfill, so the panel polls GET from
first render — but the stub served the post-save world (files: off-box)
before the PUT had landed, which no real server would do. A poll
resolving while the confirm dialog was open made the confirm-time
candidate recompute see from === to, so nothing was queued and the
migration never POSTed.

The GET stub now stays faithful (pre-save world until the PUT lands,
usage included so candidates keep their object counts), and the test
deterministically forces the once-flaky interleaving: it waits for
polls to land while the dialog is open and asserts the candidate
survives them.

* fix(storage): surface the underlying cause in probe target errors

The admin storage Test probe reported only the driver wrapper's outer
message ("put failed for '…' on 'probe-s3'"), dropping the cause that
says WHY — connection refused vs missing bucket. Flatten the error's
cause chain into the probe result, appending an S3-style code when it
isn't already in the message.

* fix(storage): surface the underlying cause in replica-failure reports

Same defect as the probe fix, one layer over: MirrorDriver's
reportReplicaFailure captured only the wrapper's message, so
replica-failure notifications said "put failed" without the why.
Move the probe's cause-chain flattener into storage.types.ts as
describeError and use it in both call sites.

* fix(storage): strip ContentType from multipart Upload calls (MalformedXML)

Every multipart S3 put carrying a contentType — every backup zip and
photo over the 10MB threshold — failed with 400 MalformedXML. Upstream
@aws-lite/s3's Upload leaks the ContentType param into its final
CompleteMultipartUpload headers ({ ...xml, ...paramHeaders }), clobbering
application/xml, which flips @aws-lite/client's payload serializer to
JSON — so the server receives a JSON parts list where XML belongs.
Confirmed live against MinIO AIStor; any S3 refuses the same.

uploadMonitored now drops ContentType before s3.Upload. Multipart
objects are stored without a content-type, which TREK never reads back
(serving derives Content-Type from the key/caller, never object
metadata); single-request PutObject paths keep sending it.

Also closes the coverage gap that let this ship: the live contract
suite never crossed MULTIPART_THRESHOLD, so s3.Upload had never run
against a real server — it now round-trips a >10MB put carrying a
contentType.

---------

Co-authored-by: Maurice <mauriceboe@icloud.com>
2026-08-22 11:33:55 +02:00

1249 lines
62 KiB
TypeScript

import path from 'path';
import { Injectable } from '@nestjs/common';
import { DatabaseService } from '../database/database.service';
import { PermissionsService } from '../permissions/permissions.service';
import { RealtimeService } from '../realtime/realtime.service';
import { reclaimPlaceImage } from '../places/place-image';
import { StorageService } from '../storage/storage.service';
import {
COORD_DEDUP_TOLERANCE,
externalIdsOf,
isPlaceDuplicate,
trackInsertedInDedupSet,
type DedupSet,
} from '../places/places.helpers';
import type {
Collection,
CollectionDetailResponse,
CollectionListResponse,
CollectionMember,
CollectionMembership,
CollectionPlace,
CollectionLink,
CollectionCreateRequest,
CollectionUpdateRequest,
CollectionSavePlaceRequest,
CollectionSaveResult,
CollectionCopyToTripRequest,
CollectionPlaceUpdateRequest,
CollectionStatus,
CollectionLabel,
CollectionImportablesResponse,
} from '@trek/shared';
import { NotificationsService } from '../notifications/notifications.service';
/** Links are stored as a JSON TEXT column; parse on read, stringify on write. */
function parseLinks(raw: unknown): CollectionLink[] | undefined {
if (typeof raw !== 'string' || !raw) return undefined;
try {
const v = JSON.parse(raw);
return Array.isArray(v) ? (v as CollectionLink[]) : undefined;
} catch {
return undefined;
}
}
function serializeLinks(links: CollectionLink[] | undefined): string | null {
return links && links.length ? JSON.stringify(links) : null;
}
// ---------------------------------------------------------------------------
// Errors — thrown as plain Errors carrying a status; TrekExceptionFilter maps
// `err.status` → that HTTP code with an `{ error: message }` body.
// ---------------------------------------------------------------------------
function httpError(status: number, message: string): never {
const err = new Error(message) as Error & { status: number };
err.status = status;
throw err;
}
export type EffectiveRole = 'owner' | 'admin' | 'editor' | 'viewer' | null;
interface PlaceRow extends CollectionPlace {
category_name?: string | null;
category_color?: string | null;
category_icon?: string | null;
}
const MAX_LABELS_PER_COLLECTION = 50;
/**
* Collections domain service — owns the collections SQL (moved 1:1 from the
* legacy services/collectionsService.ts: identical statements, the `??`
* defaults, the post-write re-selects, the exact error strings and the
* per-user broadcast fan-outs). The `place_edit` trip permission (copyToTrip)
* goes through PermissionsService; per-user WebSocket delivery goes through
* RealtimeService; the notification send in sendInvite goes through an injected
* NotificationsService (collab precedent).
*/
@Injectable()
export class CollectionsService {
constructor(
private readonly db: DatabaseService,
private readonly permissions: PermissionsService,
private readonly realtime: RealtimeService,
private readonly notifications: NotificationsService,
private readonly storage: StorageService,
) {}
/**
* Reclaim a replaced cover object (mirrors tripService.deleteOldCover — kept
* local so this service doesn't pull the trips import graph). Collection +
* trip covers share the 'covers' category; basename() tolerates the stored
* /uploads/covers/<name> URL form and any external URL the client saved
* (central key validation rejects hostile values; the catch swallows them
* like the old containment guard did).
*/
private async deleteOldCollectionCover(coverImage: string | null | undefined): Promise<void> {
if (!coverImage) return;
await this.storage.delete('covers', path.basename(coverImage)).catch(() => {
/* external URL or already gone */
});
}
// -------------------------------------------------------------------------
// Visibility — a user may see/edit a collection if they own it OR are an
// accepted member. Every read/write goes through assertAccess.
// -------------------------------------------------------------------------
accessibleCollectionIds(userId: number): number[] {
const rows = this.db.all<{ id: number }>(`
SELECT id FROM collections WHERE owner_id = ?
UNION
SELECT collection_id FROM collection_members WHERE user_id = ? AND status = 'accepted'
`, userId, userId);
return rows.map(r => r.id);
}
private isVisible(userId: number, collectionId: number): boolean {
const row = this.db.get(`
SELECT 1 FROM collections WHERE id = ? AND owner_id = ?
UNION
SELECT 1 FROM collection_members WHERE collection_id = ? AND user_id = ? AND status = 'accepted'
LIMIT 1
`, collectionId, userId, collectionId, userId);
return !!row;
}
assertAccess(userId: number, collectionId: number): void {
if (!this.isVisible(userId, collectionId)) httpError(404, 'Collection not found');
}
isOwner(userId: number, collectionId: number): boolean {
const row = this.db.get('SELECT 1 FROM collections WHERE id = ? AND owner_id = ?', collectionId, userId);
return !!row;
}
/** The viewer's effective permission on a list: owner (full), or their accepted
* member role, or null when they have no access. */
roleOf(userId: number, collectionId: number): EffectiveRole {
if (this.isOwner(userId, collectionId)) return 'owner';
const row = this.db.get<{ role: string }>(
"SELECT role FROM collection_members WHERE collection_id = ? AND user_id = ? AND status = 'accepted'",
collectionId, userId,
);
if (!row) return null;
return row.role === 'admin' || row.role === 'viewer' ? row.role : 'editor';
}
/** Add/edit a place — owner, admin or editor. 404 hides lists you can't see,
* 403 for a read-only (viewer) member. */
assertCanEdit(userId: number, collectionId: number): void {
const r = this.roleOf(userId, collectionId);
if (r === null) httpError(404, 'Collection not found');
if (r === 'viewer') httpError(403, 'You have read-only access to this list');
}
/** Delete a place — owner or admin only. */
assertCanDelete(userId: number, collectionId: number): void {
const r = this.roleOf(userId, collectionId);
if (r === null) httpError(404, 'Collection not found');
if (r !== 'owner' && r !== 'admin') httpError(403, 'Only an admin can delete places from this list');
}
private ownerOf(collectionId: number): number {
const row = this.db.get<{ owner_id: number }>('SELECT owner_id FROM collections WHERE id = ?', collectionId);
if (!row) httpError(404, 'Collection not found');
return row.owner_id;
}
// -------------------------------------------------------------------------
// Hydration helpers
// -------------------------------------------------------------------------
private loadTagsByCollectionPlaceIds(placeIds: number[]): Record<number, { id: number; name: string; color: string }[]> {
const out: Record<number, { id: number; name: string; color: string }[]> = {};
if (placeIds.length === 0) return out;
const placeholders = placeIds.map(() => '?').join(',');
const rows = this.db.all<{ pid: number; id: number; name: string; color: string }>(`
SELECT cpt.collection_place_id AS pid, t.id, t.name, t.color
FROM collection_place_tags cpt
JOIN tags t ON t.id = cpt.tag_id
WHERE cpt.collection_place_id IN (${placeholders})
`, ...placeIds);
for (const r of rows) {
if (!out[r.pid]) out[r.pid] = [];
out[r.pid].push({ id: r.id, name: r.name, color: r.color });
}
return out;
}
/** A list's own label definitions, in display order. */
private loadLabelsByCollection(collectionId: number): CollectionLabel[] {
return this.db.all<CollectionLabel>(
'SELECT id, collection_id, name, color, sort_order FROM collection_labels WHERE collection_id = ? ORDER BY sort_order, id',
collectionId,
);
}
/** Assigned label ids per place, batched (mirrors loadTagsByCollectionPlaceIds). */
private loadLabelIdsByPlaceIds(placeIds: number[]): Record<number, number[]> {
const out: Record<number, number[]> = {};
if (placeIds.length === 0) return out;
const placeholders = placeIds.map(() => '?').join(',');
const rows = this.db.all<{ pid: number; label_id: number }>(
`SELECT collection_place_id AS pid, label_id FROM collection_place_labels WHERE collection_place_id IN (${placeholders})`,
...placeIds,
);
for (const r of rows) {
if (!out[r.pid]) out[r.pid] = [];
out[r.pid].push(r.label_id);
}
return out;
}
/** Per-voter rating rows (#1435), batched (mirrors loadTagsByCollectionPlaceIds). */
private loadRatingsByCollectionPlaceIds(placeIds: number[]): Record<number, { user_id: number; username: string; avatar: string | null; rating: number }[]> {
const out: Record<number, { user_id: number; username: string; avatar: string | null; rating: number }[]> = {};
if (placeIds.length === 0) return out;
const rows = this.db.all<{ pid: number; user_id: number; username: string; avatar: string | null; rating: number }>(`
SELECT cpr.collection_place_id AS pid, cpr.user_id, u.username, u.avatar, cpr.rating
FROM collection_place_ratings cpr
JOIN users u ON u.id = cpr.user_id
WHERE cpr.collection_place_id IN (${placeIds.map(() => '?').join(',')})
ORDER BY cpr.created_at
`, ...placeIds);
for (const { pid, ...rest } of rows) {
if (!out[pid]) out[pid] = [];
out[pid].push(rest);
}
return out;
}
private hydratePlaces(rows: PlaceRow[]): CollectionPlace[] {
const ids = rows.map(r => r.id);
const tagsByPlace = this.loadTagsByCollectionPlaceIds(ids);
const labelsByPlace = this.loadLabelIdsByPlaceIds(ids);
const ratingsByPlace = this.loadRatingsByCollectionPlaceIds(ids);
return rows.map(r => {
const { category_name, category_color, category_icon, ...rest } = r;
const ratings = ratingsByPlace[r.id] || [];
return {
...rest,
links: parseLinks((r as { links?: unknown }).links),
category: r.category_id
? { id: r.category_id, name: category_name ?? '', color: category_color ?? null, icon: category_icon ?? null }
: undefined,
tags: tagsByPlace[r.id] || [],
label_ids: labelsByPlace[r.id] || [],
ratings,
rating_avg: ratings.length > 0 ? ratings.reduce((s, x) => s + x.rating, 0) / ratings.length : null,
rating_count: ratings.length,
} as CollectionPlace;
});
}
private getPlaceById(placeId: number): CollectionPlace {
const row = this.db.get<PlaceRow>(`
SELECT cp.*, c.name AS category_name, c.color AS category_color, c.icon AS category_icon
FROM collection_places cp
LEFT JOIN categories c ON cp.category_id = c.id
WHERE cp.id = ?
`, placeId);
if (!row) httpError(404, 'Place not found');
return this.hydratePlaces([row])[0];
}
private collectionIdOfPlace(placeId: number): number {
const row = this.db.get<{ collection_id: number }>('SELECT collection_id FROM collection_places WHERE id = ?', placeId);
if (!row) httpError(404, 'Place not found');
return row.collection_id;
}
private buildMembers(collectionId: number): CollectionMember[] {
const owner = this.db.get<Omit<CollectionMember, 'status' | 'is_owner'>>(`
SELECT u.id AS user_id, u.username, u.email, u.avatar
FROM collections col JOIN users u ON u.id = col.owner_id
WHERE col.id = ?
`, collectionId);
const members = this.db.all<Omit<CollectionMember, 'is_owner'>>(`
SELECT u.id AS user_id, u.username, u.email, u.avatar, cm.status, cm.role
FROM collection_members cm JOIN users u ON u.id = cm.user_id
WHERE cm.collection_id = ?
ORDER BY u.username
`, collectionId);
const result: CollectionMember[] = [];
if (owner) result.push({ ...owner, status: 'accepted', role: 'admin', is_owner: true });
for (const m of members) result.push({ ...m, is_owner: false });
return result;
}
private getCollectionRow(id: number): Collection {
const col = this.db.get<Collection & { links?: unknown }>('SELECT * FROM collections WHERE id = ?', id);
if (!col) httpError(404, 'Collection not found');
const placeCount = this.db.get<{ n: number }>('SELECT COUNT(*) AS n FROM collection_places WHERE collection_id = ?', id)!.n;
return { ...col, links: parseLinks(col.links), place_count: placeCount, members: this.buildMembers(id) };
}
// -------------------------------------------------------------------------
// Lists CRUD
// -------------------------------------------------------------------------
listCollections(userId: number): CollectionListResponse {
const ids = this.accessibleCollectionIds(userId);
const collections: Collection[] = ids
.map(id => {
const col = this.getCollectionRow(id);
return { ...col, is_owner: col.owner_id === userId };
})
.sort((a, b) => (a.sort_order ?? 0) - (b.sort_order ?? 0) || a.id - b.id);
const incomingInvites = this.db.all<{ collection_id: number; name: string; from_id: number; from_username: string }>(`
SELECT cm.collection_id, c.name, u.id AS from_id, u.username AS from_username
FROM collection_members cm
JOIN collections c ON c.id = cm.collection_id
JOIN users u ON u.id = c.owner_id
WHERE cm.user_id = ? AND cm.status = 'pending'
`, userId)
.map(r => ({ collection_id: r.collection_id, name: r.name, from: { id: r.from_id, username: r.from_username } }));
return { collections, incomingInvites };
}
getCollection(userId: number, id: number): CollectionDetailResponse {
this.assertAccess(userId, id);
const collection = this.getCollectionRow(id);
const rows = this.db.all<PlaceRow>(`
SELECT cp.*, c.name AS category_name, c.color AS category_color, c.icon AS category_icon
FROM collection_places cp
LEFT JOIN categories c ON cp.category_id = c.id
WHERE cp.collection_id = ?
ORDER BY cp.sort_order, cp.created_at
`, id);
return {
collection: { ...collection, is_owner: collection.owner_id === userId, labels: this.loadLabelsByCollection(id) },
places: this.hydratePlaces(rows),
};
}
createCollection(userId: number, body: CollectionCreateRequest): Collection {
const max = this.db.get<{ m: number }>('SELECT COALESCE(MAX(sort_order), -1) AS m FROM collections WHERE owner_id = ?', userId)!.m;
const result = this.db.run(`
INSERT INTO collections (owner_id, name, description, color, icon, cover_image, links, sort_order)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`,
userId,
body.name,
body.description ?? null,
body.color ?? '#6366f1',
body.icon ?? 'Bookmark',
body.cover_image ?? null,
serializeLinks(body.links),
max + 1,
);
const col = this.getCollectionRow(Number(result.lastInsertRowid));
return { ...col, is_owner: true };
}
updateCollection(userId: number, id: number, body: CollectionUpdateRequest, socketId?: string): Collection {
this.assertCanEdit(userId, id);
const updates: string[] = [];
const params: (string | number | null)[] = [];
if (body.name !== undefined) { updates.push('name = ?'); params.push(body.name); }
if (body.description !== undefined) { updates.push('description = ?'); params.push(body.description ?? null); }
if (body.color !== undefined) { updates.push('color = ?'); params.push(body.color ?? null); }
if (body.icon !== undefined) { updates.push('icon = ?'); params.push(body.icon ?? null); }
if (body.cover_image !== undefined) { updates.push('cover_image = ?'); params.push(body.cover_image ?? null); }
if (body.links !== undefined) { updates.push('links = ?'); params.push(serializeLinks(body.links)); }
if (body.sort_order !== undefined) { updates.push('sort_order = ?'); params.push(body.sort_order); }
if (updates.length > 0) {
updates.push("updated_at = CURRENT_TIMESTAMP");
params.push(id);
this.db.run(`UPDATE collections SET ${updates.join(', ')} WHERE id = ?`, ...params);
}
this.notifyCollectionUsers(id, socketId, 'collections:updated');
const col = this.getCollectionRow(id);
return { ...col, is_owner: col.owner_id === userId };
}
/** Set (or clear) a list's cover image, reclaiming the previous file. */
async setCollectionCover(userId: number, id: number, coverUrl: string | null, socketId?: string): Promise<Collection> {
this.assertCanEdit(userId, id);
const prev = this.db.get<{ cover_image: string | null }>('SELECT cover_image FROM collections WHERE id = ?', id)?.cover_image ?? null;
this.db.run('UPDATE collections SET cover_image = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?', coverUrl, id);
if (prev && prev !== coverUrl) await this.deleteOldCollectionCover(prev);
this.notifyCollectionUsers(id, socketId, 'collections:updated');
const col = this.getCollectionRow(id);
return { ...col, is_owner: col.owner_id === userId };
}
deleteCollection(userId: number, id: number): void {
this.assertAccess(userId, id);
if (!this.isOwner(userId, id)) httpError(403, 'Only the owner can delete this list');
// Snapshot recipients BEFORE the cascade wipes collection_members.
const accepted = this.db.all<{ user_id: number }>("SELECT user_id FROM collection_members WHERE collection_id = ? AND status = 'accepted'", id).map(r => r.user_id);
const pending = this.db.all<{ user_id: number }>("SELECT user_id FROM collection_members WHERE collection_id = ? AND status = 'pending'", id).map(r => r.user_id);
this.db.run('DELETE FROM collections WHERE id = ?', id); // CASCADE drops members + places + tags
[...new Set([...accepted, ...pending])]
.filter(uid => uid !== userId)
.forEach(uid => this.realtime.broadcastToUser(uid, { type: 'collections:deleted', collectionId: id }));
}
reorderCollections(userId: number, orderedIds: number[]): void {
const visible = new Set(this.accessibleCollectionIds(userId));
const stmt = this.db.prepare('UPDATE collections SET sort_order = ? WHERE id = ?');
this.db.transaction(() => {
orderedIds.forEach((cid, index) => {
if (visible.has(cid)) stmt.run(index, cid);
});
});
}
// -------------------------------------------------------------------------
// Dedup (collection-scoped ports of placeService helpers)
// -------------------------------------------------------------------------
private findDuplicateCollectionPlace(
collectionId: number,
candidate: { name: string | null | undefined; lat: number | null | undefined; lng: number | null | undefined },
): { id: number; name: string } | null {
const normalizedName = candidate.name?.trim().toLowerCase();
if (normalizedName) {
const dup = this.db.get<{ id: number; name: string }>(`
SELECT id, name FROM collection_places
WHERE collection_id = ? AND lower(trim(name)) = ?
ORDER BY id ASC LIMIT 1
`, collectionId, normalizedName);
if (dup) return dup;
}
if (candidate.lat != null && candidate.lng != null) {
return this.db.get<{ id: number; name: string }>(`
SELECT id, name FROM collection_places
WHERE collection_id = ? AND lat IS NOT NULL AND lng IS NOT NULL
AND abs(lat - ?) <= ? AND abs(lng - ?) <= ?
ORDER BY id ASC LIMIT 1
`, collectionId, candidate.lat, COORD_DEDUP_TOLERANCE, candidate.lng, COORD_DEDUP_TOLERANCE) || null;
}
return null;
}
// -------------------------------------------------------------------------
// Saved places CRUD
// -------------------------------------------------------------------------
/**
* A tag has no list of its own, only a `user_id`, so "belongs here" resolves
* through the people on the list — the same shape places.service uses against
* a trip roster. Off-list tag ids drop silently rather than being stored and
* read straight back out: the tag read-back ships `tags.user_id`, so an
* unfiltered id answers who owns a tag the caller cannot otherwise see.
*/
private attachTags(collectionPlaceId: number, tagIds: number[] | undefined): void {
if (!tagIds || tagIds.length === 0) return;
const unique = [...new Set(tagIds)];
const eligible = this.collectionMemberIds(this.collectionIdOfPlace(collectionPlaceId));
const owned = this.db.all<{ id: number; user_id: number }>(
`SELECT id, user_id FROM tags WHERE id IN (${unique.map(() => '?').join(',')})`,
...unique,
);
const stmt = this.db.prepare('INSERT OR IGNORE INTO collection_place_tags (collection_place_id, tag_id) VALUES (?, ?)');
for (const t of owned) if (eligible.has(t.user_id)) stmt.run(collectionPlaceId, t.id);
}
/** Owner + accepted members — the users whose votes may live in this list. */
private collectionMemberIds(collectionId: number): Set<number> {
const ids = new Set<number>([this.ownerOf(collectionId)]);
const rows = this.db.all<{ user_id: number }>("SELECT user_id FROM collection_members WHERE collection_id = ? AND status = 'accepted'", collectionId);
rows.forEach(r => ids.add(r.user_id));
return ids;
}
/**
* Carry star votes (#1435) from a trip place into a freshly saved collection
* place. Only votes by members of the target collection come along (the saver
* is always a member) — other trip members' opinions stay in the trip.
*/
private copyTripRatings(sourcePlaceId: number, collectionPlaceId: number, collectionId: number): void {
const eligible = this.collectionMemberIds(collectionId);
const rows = this.db.all<{ user_id: number; rating: number }>('SELECT user_id, rating FROM place_ratings WHERE place_id = ?', sourcePlaceId);
const ins = this.db.prepare('INSERT OR IGNORE INTO collection_place_ratings (collection_place_id, user_id, rating) VALUES (?, ?, ?)');
for (const r of rows) {
if (eligible.has(r.user_id)) ins.run(collectionPlaceId, r.user_id, r.rating);
}
}
savePlace(userId: number, body: CollectionSavePlaceRequest, socketId?: string): CollectionSaveResult {
this.assertCanEdit(userId, body.collection_id);
if (!body.force) {
const dup = this.findDuplicateCollectionPlace(body.collection_id, { name: body.name, lat: body.lat, lng: body.lng });
if (dup) return { duplicate: true, duplicateOf: dup };
}
const ownerId = this.ownerOf(body.collection_id);
// Insert + tags + ratings-copy are one logical write — atomic since the
// post-fold quirk pass (the relocation carried them un-transacted).
const placeId = this.db.transaction(() => {
const result = this.db.run(`
INSERT INTO collection_places (
collection_id, owner_id, saved_by, name, description, lat, lng, address,
category_id, price, currency, notes, image_url, google_place_id, google_ftid,
osm_id, website, phone, status, source_trip_id, source_place_id, links
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`,
body.collection_id, ownerId, userId,
body.name, body.description ?? null, body.lat ?? null, body.lng ?? null, body.address ?? null,
body.category_id ?? null, body.price ?? null, body.currency ?? null, body.notes ?? null,
body.image_url ?? null, body.google_place_id ?? null, body.google_ftid ?? null,
body.osm_id ?? null, body.website ?? null, body.phone ?? null,
body.status ?? 'idea', body.source_trip_id ?? null, body.source_place_id ?? null,
serializeLinks(body.links),
);
const id = Number(result.lastInsertRowid);
this.attachTags(id, body.tag_ids);
// Carry trip ratings ONLY when the caller can actually see the source place.
// source_place_id/source_trip_id are raw client input, so verify trip access +
// that the place lives in that trip before reading place_ratings — otherwise a
// member could harvest co-members' votes on places in trips they cannot access
// (mirrors the canAccessTrip gate in saveFromTripPlace).
if (
body.source_place_id && body.source_trip_id &&
this.db.canAccessTrip(body.source_trip_id, userId) &&
this.db.get('SELECT 1 FROM places WHERE id = ? AND trip_id = ?', body.source_place_id, body.source_trip_id)
) {
this.copyTripRatings(body.source_place_id, id, body.collection_id);
}
return id;
});
this.notifyCollectionUsers(body.collection_id, socketId, 'collections:updated');
return { place: this.getPlaceById(placeId) };
}
saveFromTripPlace(
userId: number, collectionId: number, tripId: number, placeId: number, force?: boolean, socketId?: string,
): CollectionSaveResult {
this.assertCanEdit(userId, collectionId);
if (!this.db.canAccessTrip(tripId, userId)) httpError(404, 'Trip not found');
const place = this.db.get<Record<string, unknown>>('SELECT * FROM places WHERE id = ? AND trip_id = ?', placeId, tripId);
if (!place) httpError(404, 'Place not found');
return this.savePlace(userId, {
collection_id: collectionId,
name: place.name as string,
description: (place.description as string | null) ?? null,
lat: (place.lat as number | null) ?? null,
lng: (place.lng as number | null) ?? null,
address: (place.address as string | null) ?? null,
category_id: (place.category_id as number | null) ?? null,
price: (place.price as number | null) ?? null,
currency: (place.currency as string | null) ?? null,
notes: (place.notes as string | null) ?? null,
image_url: (place.image_url as string | null) ?? null,
google_place_id: (place.google_place_id as string | null) ?? null,
google_ftid: (place.google_ftid as string | null) ?? null,
osm_id: (place.osm_id as string | null) ?? null,
website: (place.website as string | null) ?? null,
phone: (place.phone as string | null) ?? null,
source_trip_id: tripId,
source_place_id: placeId,
force,
}, socketId);
}
/** The trip's places as offered to the bulk import, each already carrying the verdict
* saveFromTripPlaces would reach for it. Reusing findDuplicateCollectionPlace is the
* whole point: a row the dialog shows as new can never come back as `skipped`, and a
* greyed-out one is exactly a row the import would refuse. Re-deriving that rule in the
* client would be a second copy of it, free to drift.
*
* `scheduled` is false for places no day holds. Those are what a trip leaves behind and
* what this import exists for, so the dialog pre-selects them. */
importablePlaces(userId: number, collectionId: number, tripId: number): CollectionImportablesResponse {
this.assertCanEdit(userId, collectionId);
if (!this.db.canAccessTrip(tripId, userId)) httpError(404, 'Trip not found');
// One row per place: a place can sit on several days, so the day columns resolve to the
// earliest one rather than multiplying the place out across its assignments.
const rows = this.db.all<{
place_id: number; name: string; address: string | null; lat: number | null; lng: number | null;
category_id: number | null; image_url: string | null; day_number: number | null; date: string | null;
}>(`
SELECT p.id AS place_id, p.name, p.address, p.lat, p.lng, p.category_id, p.image_url,
(SELECT MIN(d.day_number) FROM day_assignments da
JOIN days d ON d.id = da.day_id
WHERE da.place_id = p.id AND d.trip_id = p.trip_id) AS day_number,
(SELECT d.date FROM day_assignments da
JOIN days d ON d.id = da.day_id
WHERE da.place_id = p.id AND d.trip_id = p.trip_id
ORDER BY d.day_number ASC LIMIT 1) AS date
FROM places p
WHERE p.trip_id = ?
ORDER BY p.name COLLATE NOCASE
`, tripId);
return {
places: rows.map(r => ({
...r,
already_in_list: this.findDuplicateCollectionPlace(collectionId, { name: r.name, lat: r.lat, lng: r.lng }) != null,
scheduled: r.day_number != null,
})),
};
}
/** Bulk copy of several trip places into a list in one shot — one access check,
* one WS notify (vs saving each place individually). Mirrors saveFromTripPlace's
* field mapping + dedup; skips duplicates unless force. Status starts at 'idea'. */
saveFromTripPlaces(
userId: number, collectionId: number, tripId: number, placeIds: number[], force?: boolean, socketId?: string,
): { copied: number; skipped: { id: number; name: string }[] } {
this.assertCanEdit(userId, collectionId);
if (!this.db.canAccessTrip(tripId, userId)) httpError(404, 'Trip not found');
const ownerId = this.ownerOf(collectionId);
const insert = this.db.prepare(`
INSERT INTO collection_places (
collection_id, owner_id, saved_by, name, description, lat, lng, address,
category_id, price, currency, notes, image_url, google_place_id, google_ftid,
osm_id, website, phone, status, source_trip_id, source_place_id, links
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'idea', ?, ?, NULL)
`);
let copied = 0;
const skipped: { id: number; name: string }[] = [];
// The whole batch is one logical write — atomic since the post-fold quirk pass.
this.db.transaction(() => {
for (const placeId of placeIds) {
const p = this.db.get<Record<string, unknown>>('SELECT * FROM places WHERE id = ? AND trip_id = ?', placeId, tripId);
if (!p) continue;
const name = p.name as string;
const lat = (p.lat as number | null) ?? null;
const lng = (p.lng as number | null) ?? null;
if (!force && this.findDuplicateCollectionPlace(collectionId, { name, lat, lng })) {
skipped.push({ id: placeId, name });
continue;
}
const res = insert.run(
collectionId, ownerId, userId,
name, (p.description as string | null) ?? null, lat, lng, (p.address as string | null) ?? null,
(p.category_id as number | null) ?? null, (p.price as number | null) ?? null, (p.currency as string | null) ?? null, (p.notes as string | null) ?? null,
(p.image_url as string | null) ?? null, (p.google_place_id as string | null) ?? null, (p.google_ftid as string | null) ?? null,
(p.osm_id as string | null) ?? null, (p.website as string | null) ?? null, (p.phone as string | null) ?? null,
tripId, placeId,
);
this.copyTripRatings(placeId, Number(res.lastInsertRowid), collectionId);
copied++;
}
});
if (copied > 0) this.notifyCollectionUsers(collectionId, socketId, 'collections:updated');
return { copied, skipped };
}
async updatePlace(userId: number, placeId: number, body: CollectionPlaceUpdateRequest, socketId?: string): Promise<CollectionPlace> {
const currentCollection = this.collectionIdOfPlace(placeId);
this.assertCanEdit(userId, currentCollection);
// Capture the previous thumbnail so a replaced/cleared custom upload (#1136)
// can be reclaimed once nothing references it any more.
const prevImage = body.image_url !== undefined
? this.db.get<{ image_url: string | null }>('SELECT image_url FROM collection_places WHERE id = ?', placeId)?.image_url ?? null
: null;
const updates: string[] = [];
const params: (string | number | null)[] = [];
if (body.name !== undefined) { updates.push('name = ?'); params.push(body.name); }
if (body.description !== undefined) { updates.push('description = ?'); params.push(body.description ?? null); }
if (body.notes !== undefined) { updates.push('notes = ?'); params.push(body.notes ?? null); }
if (body.lat !== undefined) { updates.push('lat = ?'); params.push(body.lat ?? null); }
if (body.lng !== undefined) { updates.push('lng = ?'); params.push(body.lng ?? null); }
if (body.address !== undefined) { updates.push('address = ?'); params.push(body.address ?? null); }
if (body.status !== undefined) { updates.push('status = ?'); params.push(body.status); }
if (body.category_id !== undefined) { updates.push('category_id = ?'); params.push(body.category_id ?? null); }
if (body.image_url !== undefined) { updates.push('image_url = ?'); params.push(body.image_url ?? null); }
if (body.links !== undefined) { updates.push('links = ?'); params.push(serializeLinks(body.links)); }
let movedTo: number | null = null;
if (body.collection_id !== undefined && body.collection_id !== currentCollection) {
this.assertCanEdit(userId, body.collection_id);
updates.push('collection_id = ?'); params.push(body.collection_id);
updates.push('owner_id = ?'); params.push(this.ownerOf(body.collection_id));
movedTo = body.collection_id;
}
// Field update + tag rewrite + label rewrite are one logical write — atomic
// since the post-fold quirk pass.
this.db.transaction(() => {
if (updates.length > 0) {
updates.push("updated_at = CURRENT_TIMESTAMP");
params.push(placeId);
this.db.run(`UPDATE collection_places SET ${updates.join(', ')} WHERE id = ?`, ...params);
}
if (body.tag_ids !== undefined) {
this.db.run('DELETE FROM collection_place_tags WHERE collection_place_id = ?', placeId);
this.attachTags(placeId, body.tag_ids);
}
// Labels are collection-scoped: a move invalidates the source list's labels;
// a provided label_ids set replaces them against the (target) collection.
if (movedTo) this.db.run('DELETE FROM collection_place_labels WHERE collection_place_id = ?', placeId);
if (body.label_ids !== undefined) this.setPlaceLabels(placeId, movedTo ?? currentCollection, body.label_ids);
});
if (body.image_url !== undefined && prevImage !== (body.image_url ?? null)) {
await reclaimPlaceImage(this.storage, prevImage);
}
this.notifyCollectionUsers(currentCollection, socketId, 'collections:updated');
if (movedTo) this.notifyCollectionUsers(movedTo, socketId, 'collections:updated');
return this.getPlaceById(placeId);
}
setStatus(userId: number, placeId: number, status: CollectionStatus, socketId?: string): CollectionPlace {
const collectionId = this.collectionIdOfPlace(placeId);
this.assertCanEdit(userId, collectionId);
this.db.run("UPDATE collection_places SET status = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?", status, placeId);
this.notifyCollectionUsers(collectionId, socketId, 'collections:updated');
return this.getPlaceById(placeId);
}
/**
* Set (1-5) or clear (null) the user's own star vote on a saved place (#1435).
* Gated on assertAccess, not assertCanEdit — a vote is the member's personal
* opinion, so read-only viewers get to cast one too.
*/
setRating(userId: number, placeId: number, rating: number | null, socketId?: string): CollectionPlace {
const collectionId = this.collectionIdOfPlace(placeId);
this.assertAccess(userId, collectionId);
if (rating === null) {
this.db.run('DELETE FROM collection_place_ratings WHERE collection_place_id = ? AND user_id = ?', placeId, userId);
} else {
this.db.run(`
INSERT INTO collection_place_ratings (collection_place_id, user_id, rating) VALUES (?, ?, ?)
ON CONFLICT(collection_place_id, user_id) DO UPDATE SET rating = excluded.rating
`, placeId, userId, rating);
}
this.notifyCollectionUsers(collectionId, socketId, 'collections:updated');
return this.getPlaceById(placeId);
}
async deletePlace(userId: number, placeId: number, socketId?: string): Promise<void> {
const collectionId = this.collectionIdOfPlace(placeId);
this.assertCanDelete(userId, collectionId);
const image = this.db.get<{ image_url: string | null }>('SELECT image_url FROM collection_places WHERE id = ?', placeId)?.image_url ?? null;
this.db.run('DELETE FROM collection_places WHERE id = ?', placeId); // CASCADE drops tags. NO photo-cache reclaim.
await reclaimPlaceImage(this.storage, image);
this.notifyCollectionUsers(collectionId, socketId, 'collections:updated');
}
async deletePlacesMany(userId: number, ids: number[], socketId?: string): Promise<number[]> {
// All-or-nothing since the post-fold quirk pass: every id is resolved and
// permission-checked BEFORE any delete (the relocated legacy interleaved
// checks with deletes, so a mid-list 403/404 left earlier deletes committed),
// and the deletes then run in one transaction.
const deleted: number[] = [];
const touched = new Set<number>();
const images: (string | null)[] = [];
for (const id of ids) {
const collectionId = this.collectionIdOfPlace(id);
this.assertCanDelete(userId, collectionId);
images.push(this.db.get<{ image_url: string | null }>('SELECT image_url FROM collection_places WHERE id = ?', id)?.image_url ?? null);
touched.add(collectionId);
}
this.db.transaction(() => {
for (const id of ids) {
this.db.run('DELETE FROM collection_places WHERE id = ?', id);
deleted.push(id);
}
});
for (const image of images) await reclaimPlaceImage(this.storage, image);
touched.forEach(cid => this.notifyCollectionUsers(cid, socketId, 'collections:updated'));
return deleted;
}
/**
* Set the same status on several saved places at once (#1469).
*
* Same all-or-nothing shape as deletePlacesMany: every id is resolved and
* permission-checked before the first write, so a list the caller may not edit
* cannot leave half the batch applied. Rows that already carry the status are
* counted as untouched rather than rewritten, which keeps updated_at honest.
*/
setStatusMany(userId: number, ids: number[], status: CollectionStatus, socketId?: string): { updated: number } {
const touched = new Set<number>();
for (const id of ids) {
const collectionId = this.collectionIdOfPlace(id);
this.assertCanEdit(userId, collectionId);
touched.add(collectionId);
}
let updated = 0;
this.db.transaction(() => {
for (const id of ids) {
const res = this.db.run(
"UPDATE collection_places SET status = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ? AND status IS NOT ?",
status, id, status,
);
updated += res.changes;
}
});
if (updated > 0) touched.forEach(cid => this.notifyCollectionUsers(cid, socketId, 'collections:updated'));
return { updated };
}
/**
* Set a status on every saved copy of the given trip places (#1469): "I have
* been here now" said once from the trip, instead of hunting the place down in
* each list it was saved to.
*
* The match is findMembership's, so what gets updated is exactly what the
* place dialog listed as "saved in". Lists the caller may only read are left
* alone rather than refused — a shared list you cannot edit should not stop
* you marking your own.
*/
setStatusFromTrip(
userId: number,
tripId: number,
placeIds: number[],
status: CollectionStatus,
socketId?: string,
): { updated: number; places: number } {
if (!this.db.canAccessTrip(tripId, userId)) httpError(404, 'Trip not found');
const sources = placeIds.length
? this.db.all<{ id: number; name: string; lat: number | null; lng: number | null; google_place_id: string | null; google_ftid: string | null; osm_id: string | null }>(`
SELECT id, name, lat, lng, google_place_id, google_ftid, osm_id
FROM places WHERE trip_id = ? AND id IN (${placeIds.map(() => '?').join(',')})
`, tripId, ...placeIds)
: [];
if (sources.length === 0) return { updated: 0, places: 0 };
const editable = this.accessibleCollectionIds(userId).filter(cid => {
const role = this.roleOf(userId, cid);
return role !== null && role !== 'viewer';
});
if (editable.length === 0) return { updated: 0, places: 0 };
const matched = new Set<number>();
const placesWithMatch = new Set<number>();
for (const src of sources) {
for (const row of this.matchingCollectionPlaces(editable, tripId, src)) {
matched.add(row.id);
placesWithMatch.add(src.id);
}
}
if (matched.size === 0) return { updated: 0, places: 0 };
const { updated } = this.setStatusMany(userId, [...matched], status, socketId);
return { updated, places: placesWithMatch.size };
}
/**
* The saved copies of one trip place. Same signals findMembership uses — a
* provider id, or the same spot within the dedup tolerance — plus the
* source link a place saved out of this very trip already carries, which beats
* inferring anything. A bare name match is left out here for the same reason
* as there: every "Starbucks" in the library would answer to it.
*/
private matchingCollectionPlaces(
collectionIds: number[],
tripId: number,
place: { id: number; lat: number | null; lng: number | null; google_place_id: string | null; google_ftid: string | null; osm_id: string | null },
): Array<{ id: number }> {
const conditions: string[] = ['(cp.source_trip_id = ? AND cp.source_place_id = ?)'];
const params: (string | number)[] = [...collectionIds, tripId, place.id];
if (place.google_place_id) { conditions.push('cp.google_place_id = ?'); params.push(place.google_place_id); }
if (place.google_ftid) { conditions.push('cp.google_ftid = ?'); params.push(place.google_ftid); }
if (place.osm_id) { conditions.push('cp.osm_id = ?'); params.push(place.osm_id); }
if (place.lat != null && place.lng != null) {
conditions.push('(cp.lat IS NOT NULL AND cp.lng IS NOT NULL AND abs(cp.lat - ?) <= ? AND abs(cp.lng - ?) <= ?)');
params.push(place.lat, COORD_DEDUP_TOLERANCE, place.lng, COORD_DEDUP_TOLERANCE);
}
return this.db.all<{ id: number }>(`
SELECT cp.id FROM collection_places cp
WHERE cp.collection_id IN (${collectionIds.map(() => '?').join(',')}) AND (${conditions.join(' OR ')})
`, ...params);
}
/** Set (or clear) a saved place's custom thumbnail, reclaiming the previous upload. */
async setPlaceImage(userId: number, placeId: number, imageUrl: string | null, socketId?: string): Promise<CollectionPlace> {
const collectionId = this.collectionIdOfPlace(placeId);
this.assertCanEdit(userId, collectionId);
const prev = this.db.get<{ image_url: string | null }>('SELECT image_url FROM collection_places WHERE id = ?', placeId)?.image_url ?? null;
this.db.run('UPDATE collection_places SET image_url = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?', imageUrl, placeId);
if (prev !== imageUrl) await reclaimPlaceImage(this.storage, prev);
this.notifyCollectionUsers(collectionId, socketId, 'collections:updated');
return this.getPlaceById(placeId);
}
// -------------------------------------------------------------------------
// Copy to trip
// -------------------------------------------------------------------------
copyToTrip(userId: number, body: CollectionCopyToTripRequest): { copied: number; skipped: { id: number; name: string }[] } {
const trip = this.db.canAccessTrip(body.trip_id, userId);
if (!trip) httpError(404, 'Trip not found');
const role = this.db.get<{ role: string }>('SELECT role FROM users WHERE id = ?', userId)?.role ?? 'user';
if (!this.permissions.checkPermission('place_edit', role, trip.user_id, userId, trip.user_id !== userId)) {
httpError(403, 'Not allowed to edit this trip');
}
// Votes only travel for users who are members of THIS trip (owner + members),
// so copying never surfaces a collection member with no tie to the trip.
// Symmetric with copyTripRatings' collection-member filter (#1435).
const tripMemberIds = new Set<number>([trip.user_id]);
for (const r of this.db.all<{ user_id: number }>('SELECT user_id FROM trip_members WHERE trip_id = ?', body.trip_id)) {
tripMemberIds.add(r.user_id);
}
// Visibility on every SOURCE place — no cross-user exfiltration via copy.
const sources: Array<{ id: number; name: string; description: string | null; lat: number | null; lng: number | null;
address: string | null; category_id: number | null; price: number | null; currency: string | null;
notes: string | null; image_url: string | null; google_place_id: string | null; google_ftid: string | null;
osm_id: string | null; website: string | null; phone: string | null; collection_id: number }> = [];
for (const pid of body.place_ids) {
const row = this.db.get<(typeof sources)[number]>(`
SELECT id, collection_id, name, description, lat, lng, address, category_id, price, currency,
notes, image_url, google_place_id, google_ftid, osm_id, website, phone
FROM collection_places WHERE id = ?
`, pid);
if (!row) httpError(404, 'Place not found');
this.assertAccess(userId, row.collection_id);
sources.push(row);
}
// Trip dedup set — same helpers the importers use, so a place renamed in the
// trip is still recognised by its provider id when it is copied again (#1550).
const existing = this.db.all<{
name: string | null; lat: number | null; lng: number | null;
google_place_id: string | null; google_ftid: string | null; osm_id: string | null;
}>('SELECT name, lat, lng, google_place_id, google_ftid, osm_id FROM places WHERE trip_id = ?', body.trip_id);
const dedup: DedupSet = { names: new Set(), coords: [], externalIds: new Set() };
for (const r of existing) {
for (const id of externalIdsOf(r)) dedup.externalIds.add(id);
if (r.name) dedup.names.add(r.name.trim().toLowerCase());
else if (r.lat != null && r.lng != null) dedup.coords.push({ lat: r.lat, lng: r.lng });
}
const insertPlace = this.db.prepare(`
INSERT INTO places (trip_id, name, description, lat, lng, address, category_id, price,
currency, notes, image_url, google_place_id, google_ftid, website, phone, osm_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`);
const insertTag = this.db.prepare('INSERT OR IGNORE INTO place_tags (place_id, tag_id) VALUES (?, ?)');
const insertRating = this.db.prepare('INSERT OR IGNORE INTO place_ratings (place_id, user_id, rating) VALUES (?, ?, ?)');
let copied = 0;
const skipped: { id: number; name: string }[] = [];
// The whole copy is one logical write — atomic since the post-fold quirk pass.
this.db.transaction(() => {
for (const s of sources) {
if (!body.force && isPlaceDuplicate({
name: s.name, lat: s.lat, lng: s.lng,
google_place_id: s.google_place_id, google_ftid: s.google_ftid, osm_id: s.osm_id,
}, dedup)) {
skipped.push({ id: s.id, name: s.name });
continue;
}
const res = insertPlace.run(
body.trip_id, s.name, s.description, s.lat, s.lng, s.address, s.category_id, s.price,
s.currency, s.notes, s.image_url, s.google_place_id, s.google_ftid, s.website, s.phone, s.osm_id,
);
const newPlaceId = Number(res.lastInsertRowid);
const tagIds = this.db.all<{ tag_id: number }>('SELECT tag_id FROM collection_place_tags WHERE collection_place_id = ?', s.id);
for (const t of tagIds) insertTag.run(newPlaceId, t.tag_id);
// Ratings travel into the trip too (#1435), but only for trip members — a
// collection voter who isn't on the trip stays out of it. Trip members keep
// voting there; nothing is mirrored back.
const votes = this.db.all<{ user_id: number; rating: number }>('SELECT user_id, rating FROM collection_place_ratings WHERE collection_place_id = ?', s.id);
for (const v of votes) if (tripMemberIds.has(v.user_id)) insertRating.run(newPlaceId, v.user_id, v.rating);
trackInsertedInDedupSet(s, dedup);
copied++;
}
});
return { copied, skipped };
}
// -------------------------------------------------------------------------
// Library-wide membership lookup (inspector indicator)
// -------------------------------------------------------------------------
findMembership(
userId: number,
query: { google_place_id?: string; google_ftid?: string; name?: string; lat?: number; lng?: number },
): CollectionMembership {
const ids = this.accessibleCollectionIds(userId);
if (ids.length === 0) return { saved: false, lists: [] };
const placeholders = ids.map(() => '?').join(',');
const conditions: string[] = [];
const params: (string | number)[] = [...ids];
if (query.google_place_id) { conditions.push('cp.google_place_id = ?'); params.push(query.google_place_id); }
if (query.google_ftid) { conditions.push('cp.google_ftid = ?'); params.push(query.google_ftid); }
// Coordinate proximity is the location signal. A bare NAME match is deliberately
// NOT a condition on its own — "Starbucks" (or any repeated name) would otherwise
// false-positive the inspector's "already saved" bookmark. When coords are given
// the name still effectively matches via the same-location row below; without an
// id or coords there is nothing strong enough to claim it's the same place.
if (query.lat != null && query.lng != null) {
conditions.push('(cp.lat IS NOT NULL AND cp.lng IS NOT NULL AND abs(cp.lat - ?) <= ? AND abs(cp.lng - ?) <= ?)');
params.push(query.lat, COORD_DEDUP_TOLERANCE, query.lng, COORD_DEDUP_TOLERANCE);
}
if (conditions.length === 0) return { saved: false, lists: [] };
const rows = this.db.all<{ place_id: number; collection_id: number; name: string; status: CollectionStatus }>(`
SELECT cp.id AS place_id, cp.collection_id, c.name, cp.status
FROM collection_places cp
JOIN collections c ON c.id = cp.collection_id
WHERE cp.collection_id IN (${placeholders}) AND (${conditions.join(' OR ')})
`, ...params);
return {
saved: rows.length > 0,
lists: rows.map(r => {
const role = this.roleOf(userId, r.collection_id);
return {
collection_id: r.collection_id,
name: r.name,
place_id: r.place_id,
status: r.status ?? 'idea',
can_edit: role !== null && role !== 'viewer',
};
}),
};
}
// -------------------------------------------------------------------------
// WebSocket notify
// -------------------------------------------------------------------------
notifyCollectionUsers(
collectionId: number,
excludeSid: string | undefined,
event: 'collections:updated' | 'collections:accepted' | 'collections:declined' | 'collections:left' = 'collections:updated',
): void {
const owner = this.db.get<{ owner_id: number }>('SELECT owner_id FROM collections WHERE id = ?', collectionId);
if (!owner) return;
const userIds = [owner.owner_id];
const members = this.db.all<{ user_id: number }>("SELECT user_id FROM collection_members WHERE collection_id = ? AND status = 'accepted'", collectionId);
members.forEach(m => userIds.push(m.user_id));
userIds.forEach(id => this.realtime.broadcastToUser(id, { type: event, collectionId }, excludeSid));
}
// -------------------------------------------------------------------------
// Labels — per-collection custom labels. Managing + assigning both require
// edit rights (owner/admin/editor); filtering is a read available to every
// member.
// -------------------------------------------------------------------------
private collectionIdOfLabel(labelId: number): number {
const row = this.db.get<{ collection_id: number }>('SELECT collection_id FROM collection_labels WHERE id = ?', labelId);
if (!row) httpError(404, 'Label not found');
return row.collection_id;
}
private getLabelById(labelId: number): CollectionLabel {
return this.db.get<CollectionLabel>('SELECT id, collection_id, name, color, sort_order FROM collection_labels WHERE id = ?', labelId) as CollectionLabel;
}
/** Replace a place's label assignments, keeping only labels of `collectionId`. */
private setPlaceLabels(placeId: number, collectionId: number, labelIds: number[]): void {
this.db.run('DELETE FROM collection_place_labels WHERE collection_place_id = ?', placeId);
if (labelIds.length === 0) return;
const valid = new Set(this.loadLabelsByCollection(collectionId).map(l => l.id));
const stmt = this.db.prepare('INSERT OR IGNORE INTO collection_place_labels (collection_place_id, label_id) VALUES (?, ?)');
for (const id of labelIds) if (valid.has(id)) stmt.run(placeId, id);
}
createLabel(userId: number, collectionId: number, name: string, color?: string, socketId?: string): CollectionLabel {
this.assertCanEdit(userId, collectionId);
const trimmed = name.trim();
if (!trimmed) httpError(400, 'Label name is required');
const count = this.db.get<{ n: number }>('SELECT COUNT(*) AS n FROM collection_labels WHERE collection_id = ?', collectionId)!.n;
if (count >= MAX_LABELS_PER_COLLECTION) httpError(400, `A list can have at most ${MAX_LABELS_PER_COLLECTION} labels`);
if (this.db.get('SELECT 1 FROM collection_labels WHERE collection_id = ? AND lower(name) = lower(?)', collectionId, trimmed)) {
httpError(409, 'A label with this name already exists');
}
const nextSort = this.db.get<{ m: number }>('SELECT COALESCE(MAX(sort_order), -1) AS m FROM collection_labels WHERE collection_id = ?', collectionId)!.m + 1;
const res = this.db.run('INSERT INTO collection_labels (collection_id, name, color, sort_order) VALUES (?, ?, ?, ?)',
collectionId, trimmed, color ?? '#6366f1', nextSort);
this.notifyCollectionUsers(collectionId, socketId, 'collections:updated');
return this.getLabelById(Number(res.lastInsertRowid));
}
updateLabel(userId: number, labelId: number, body: { name?: string; color?: string; sort_order?: number }, socketId?: string): CollectionLabel {
const collectionId = this.collectionIdOfLabel(labelId);
this.assertCanEdit(userId, collectionId);
const updates: string[] = [];
const params: (string | number)[] = [];
if (body.name !== undefined) {
const trimmed = body.name.trim();
if (!trimmed) httpError(400, 'Label name is required');
if (this.db.get('SELECT 1 FROM collection_labels WHERE collection_id = ? AND lower(name) = lower(?) AND id != ?', collectionId, trimmed, labelId)) {
httpError(409, 'A label with this name already exists');
}
updates.push('name = ?'); params.push(trimmed);
}
if (body.color !== undefined) { updates.push('color = ?'); params.push(body.color); }
if (body.sort_order !== undefined) { updates.push('sort_order = ?'); params.push(body.sort_order); }
if (updates.length > 0) {
params.push(labelId);
this.db.run(`UPDATE collection_labels SET ${updates.join(', ')} WHERE id = ?`, ...params);
}
this.notifyCollectionUsers(collectionId, socketId, 'collections:updated');
return this.getLabelById(labelId);
}
deleteLabel(userId: number, labelId: number, socketId?: string): void {
const collectionId = this.collectionIdOfLabel(labelId);
this.assertCanEdit(userId, collectionId);
this.db.run('DELETE FROM collection_labels WHERE id = ?', labelId); // CASCADE clears place assignments
this.notifyCollectionUsers(collectionId, socketId, 'collections:updated');
}
/** Bulk add (or remove) one or more labels across a selection of places.
* Places are grouped by list so each list is permission-checked once, and only
* labels that belong to that list are applied. */
assignLabels(userId: number, labelIds: number[], placeIds: number[], remove: boolean, socketId?: string): { changed: number } {
const byCollection = new Map<number, number[]>();
for (const pid of placeIds) {
const cid = this.collectionIdOfPlace(pid);
if (!byCollection.has(cid)) byCollection.set(cid, []);
byCollection.get(cid)!.push(pid);
}
// All-or-nothing since the post-fold quirk pass: every touched list is
// permission-checked BEFORE any write (the relocated legacy checked inside
// the write loop, so a later list's 403 left earlier lists modified), and
// the writes then run in one transaction. Broadcasts still skip lists where
// no provided label applied.
for (const cid of byCollection.keys()) this.assertCanEdit(userId, cid);
let changed = 0;
const notified: number[] = [];
this.db.transaction(() => {
for (const [cid, pids] of byCollection) {
const valid = new Set(this.loadLabelsByCollection(cid).map(l => l.id));
const applicable = labelIds.filter(id => valid.has(id));
if (applicable.length === 0) continue;
if (remove) {
const del = this.db.prepare('DELETE FROM collection_place_labels WHERE collection_place_id = ? AND label_id = ?');
for (const pid of pids) for (const lid of applicable) changed += del.run(pid, lid).changes;
} else {
const ins = this.db.prepare('INSERT OR IGNORE INTO collection_place_labels (collection_place_id, label_id) VALUES (?, ?)');
for (const pid of pids) for (const lid of applicable) changed += ins.run(pid, lid).changes;
}
notified.push(cid);
}
});
for (const cid of notified) this.notifyCollectionUsers(cid, socketId, 'collections:updated');
return { changed };
}
// -------------------------------------------------------------------------
// Fusion invitations (mirror vacayService, dropping the one-fusion guards)
// -------------------------------------------------------------------------
sendInvite(
collectionId: number, inviterId: number, inviterUsername: string, inviterEmail: string, targetUserId: number,
role: 'viewer' | 'editor' | 'admin' = 'editor',
): { error?: string; status?: number } {
if (!this.isOwner(inviterId, collectionId)) return { error: 'Not allowed', status: 403 };
if (targetUserId === inviterId) return { error: 'Cannot invite yourself', status: 400 };
const targetUser = this.db.get('SELECT id, username FROM users WHERE id = ?', targetUserId);
if (!targetUser) return { error: 'User not found', status: 404 };
const existing = this.db.get<{ id: number; status: string }>('SELECT id, status FROM collection_members WHERE collection_id = ? AND user_id = ?', collectionId, targetUserId);
if (existing) {
if (existing.status === 'accepted') return { error: 'Already a member', status: 400 };
if (existing.status === 'pending') return { error: 'Invite already pending', status: 400 };
}
this.db.run("INSERT INTO collection_members (collection_id, user_id, status, role) VALUES (?, ?, 'pending', ?)", collectionId, targetUserId, role);
this.realtime.broadcastToUser(targetUserId, { type: 'collections:invite', from: { id: inviterId, username: inviterUsername }, collectionId });
// Injected, not a lazy import of the old notifications bridge. The laziness bought
// nothing the module graph does not already give — NotificationsModule
// reaches nothing in this direction — and it hid the edge while handing the
// send a second NotificationsService built outside the container.
this.notifications.send({ event: 'collection_invite', actorId: inviterId, scope: 'user', targetId: targetUserId, params: { actor: inviterEmail, collectionId: String(collectionId) } }).catch(() => {});
return {};
}
acceptInvite(userId: number, collectionId: number, socketId: string | undefined): { error?: string; status?: number } {
const invite = this.db.get<{ id: number }>("SELECT id FROM collection_members WHERE collection_id = ? AND user_id = ? AND status = 'pending'", collectionId, userId);
if (!invite) return { error: 'No pending invite', status: 404 };
this.db.run("UPDATE collection_members SET status = 'accepted' WHERE id = ?", invite.id);
this.notifyCollectionUsers(collectionId, socketId, 'collections:accepted');
return {};
}
declineInvite(userId: number, collectionId: number, socketId: string | undefined): void {
this.db.run("DELETE FROM collection_members WHERE collection_id = ? AND user_id = ? AND status = 'pending'", collectionId, userId);
this.notifyCollectionUsers(collectionId, socketId, 'collections:declined');
}
cancelInvite(collectionId: number, ownerId: number, targetUserId: number): void {
if (!this.isOwner(ownerId, collectionId)) httpError(403, 'Not allowed');
this.db.run("DELETE FROM collection_members WHERE collection_id = ? AND user_id = ? AND status = 'pending'", collectionId, targetUserId);
this.realtime.broadcastToUser(targetUserId, { type: 'collections:cancelled', collectionId });
}
leaveCollection(userId: number, collectionId: number, socketId: string | undefined): void {
if (this.isOwner(userId, collectionId)) httpError(400, 'Owner cannot leave; delete the list');
this.db.run("DELETE FROM collection_members WHERE collection_id = ? AND user_id = ? AND status = 'accepted'", collectionId, userId);
this.notifyCollectionUsers(collectionId, socketId, 'collections:left');
}
/** Owner removes an already-accepted member (a "kick"). */
removeMember(ownerId: number, collectionId: number, targetUserId: number): void {
if (!this.isOwner(ownerId, collectionId)) httpError(403, 'Not allowed');
if (targetUserId === ownerId) httpError(400, 'Owner cannot be removed');
const res = this.db.run("DELETE FROM collection_members WHERE collection_id = ? AND user_id = ? AND status = 'accepted'", collectionId, targetUserId);
if (res.changes === 0) httpError(404, 'Member not found');
this.notifyCollectionUsers(collectionId, undefined, 'collections:left'); // refresh remaining members
this.realtime.broadcastToUser(targetUserId, { type: 'collections:removed', collectionId }); // bounce the removed user
}
/** Owner changes an accepted member's permission role (viewer/editor/admin). */
setMemberRole(ownerId: number, collectionId: number, targetUserId: number, role: 'viewer' | 'editor' | 'admin'): void {
if (!this.isOwner(ownerId, collectionId)) httpError(403, 'Not allowed');
const res = this.db.run("UPDATE collection_members SET role = ? WHERE collection_id = ? AND user_id = ? AND status = 'accepted'", role, collectionId, targetUserId);
if (res.changes === 0) httpError(404, 'Member not found');
this.notifyCollectionUsers(collectionId, undefined, 'collections:updated'); // re-gate the member live
this.realtime.broadcastToUser(targetUserId, { type: 'collections:updated', collectionId });
}
availableUsers(ownerId: number, collectionId: number): { id: number; username: string }[] {
return this.db.all<{ id: number; username: string }>(`
SELECT u.id, u.username FROM users u
WHERE u.id != ?
AND u.id NOT IN (SELECT user_id FROM collection_members WHERE collection_id = ?)
AND u.is_guest = 0
ORDER BY u.username
`, ownerId, collectionId);
}
findMembershipForUser(userId: number, collectionId: number): { is_member: boolean; is_owner: boolean; status: string | null } {
if (this.isOwner(userId, collectionId)) return { is_member: true, is_owner: true, status: 'accepted' };
const row = this.db.get<{ status: string }>('SELECT status FROM collection_members WHERE collection_id = ? AND user_id = ?', collectionId, userId);
return { is_member: row?.status === 'accepted', is_owner: false, status: row?.status ?? null };
}
}