Add the optional Immich integration and manual gallery push

Immich connections stay server-side with an encrypted API key, albums can be
imported, and photos are only sent back when an event's push button is pressed:
originals upload once, edited photos re-render, and edits hashes plus checksums
skip anything already in Immich. This also lands the accompanying editor, cloud
and administration work that was still uncommitted.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
2026-11-25 17:43:05 +00:00
parent e0c76ba965
commit 0b3ab76405
26 changed files with 4323 additions and 144 deletions
+446
View File
@@ -114,6 +114,76 @@ db.exec(`
created_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS immich_albums (
event_id TEXT PRIMARY KEY,
owner_id TEXT NOT NULL,
server_url TEXT NOT NULL,
album_id TEXT NOT NULL,
album_name TEXT NOT NULL DEFAULT '',
asset_count INTEGER NOT NULL DEFAULT 0,
kind TEXT NOT NULL DEFAULT 'import',
last_sync_at INTEGER,
created_at INTEGER NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS immich_albums_key
ON immich_albums(owner_id, server_url, album_id);
CREATE TABLE IF NOT EXISTS immich_assets (
event_id TEXT NOT NULL,
asset_id TEXT NOT NULL,
photo_id TEXT NOT NULL,
owner_id TEXT NOT NULL,
album_id TEXT NOT NULL,
checksum TEXT NOT NULL DEFAULT '',
file_name TEXT NOT NULL DEFAULT '',
imported_at INTEGER NOT NULL,
PRIMARY KEY (event_id, asset_id)
);
CREATE INDEX IF NOT EXISTS immich_assets_photo ON immich_assets(photo_id);
CREATE TABLE IF NOT EXISTS immich_settings (
owner_id TEXT PRIMARY KEY,
server_url TEXT NOT NULL DEFAULT '',
api_key TEXT NOT NULL DEFAULT '',
enabled INTEGER NOT NULL DEFAULT 0,
auto_sync INTEGER NOT NULL DEFAULT 0,
sync_minutes INTEGER NOT NULL DEFAULT 60,
trash_original INTEGER NOT NULL DEFAULT 0,
sync_metadata INTEGER NOT NULL DEFAULT 1,
last_sync_at INTEGER,
last_error TEXT,
created_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS immich_pushes (
id TEXT PRIMARY KEY,
owner_id TEXT NOT NULL,
photo_id TEXT NOT NULL,
source_asset_id TEXT NOT NULL,
new_asset_id TEXT,
checksum TEXT NOT NULL,
edits_hash TEXT NOT NULL DEFAULT '',
state TEXT NOT NULL,
trashed_source INTEGER NOT NULL DEFAULT 0,
error TEXT,
created_at INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS immich_pushes_photo ON immich_pushes(photo_id, checksum);
CREATE TABLE IF NOT EXISTS immich_meta (
photo_id TEXT PRIMARY KEY,
owner_id TEXT NOT NULL,
asset_id TEXT NOT NULL,
local_fingerprint TEXT NOT NULL DEFAULT '',
remote_fingerprint TEXT NOT NULL DEFAULT '',
remote_updated_at INTEGER,
last_synced_at INTEGER,
last_error TEXT
);
CREATE TABLE IF NOT EXISTS user_quota (
user_id TEXT PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
bytes_limit INTEGER NOT NULL DEFAULT (100 * 1024 * 1024 * 1024),
@@ -162,6 +232,24 @@ if (!eventColumns.includes('share_token')) {
}
db.exec('CREATE UNIQUE INDEX IF NOT EXISTS events_share_token ON events(share_token)');
/* Metadata round-tripping arrived after the first Immich release. */
const immichSettingsColumns = db.prepare('PRAGMA table_info(immich_settings)').all().map((c) => c.name);
if (!immichSettingsColumns.includes('sync_metadata')) {
db.exec('ALTER TABLE immich_settings ADD COLUMN sync_metadata INTEGER NOT NULL DEFAULT 1');
}
/* Manual pushes remember which edit version reached Immich. */
const immichPushColumns = db.prepare('PRAGMA table_info(immich_pushes)').all().map((c) => c.name);
if (!immichPushColumns.includes('edits_hash')) {
db.exec("ALTER TABLE immich_pushes ADD COLUMN edits_hash TEXT NOT NULL DEFAULT ''");
}
/* Galleries created from an event are destinations, not import sources. */
const immichAlbumColumns = db.prepare('PRAGMA table_info(immich_albums)').all().map((c) => c.name);
if (!immichAlbumColumns.includes('kind')) {
db.exec("ALTER TABLE immich_albums ADD COLUMN kind TEXT NOT NULL DEFAULT 'import'");
}
const q = {
upsertEvent: db.prepare(`
INSERT INTO events (id, owner_id, sort_index, created_at, data) VALUES (?, ?, ?, ?, ?)
@@ -255,6 +343,115 @@ const q = {
photo: db.prepare('SELECT data FROM photos WHERE id = ?'),
photoCount: db.prepare('SELECT COUNT(*) AS n FROM photos'),
event: db.prepare('SELECT data FROM events WHERE id = ?'),
immichAlbum: db.prepare(
'SELECT * FROM immich_albums WHERE owner_id = ? AND server_url = ? AND album_id = ?',
),
immichAlbumForEvent: db.prepare('SELECT * FROM immich_albums WHERE event_id = ?'),
immichAlbumByAlbum: db.prepare(
'SELECT * FROM immich_albums WHERE owner_id = ? AND album_id = ? LIMIT 1',
),
immichAlbumsByOwner: db.prepare(
'SELECT * FROM immich_albums WHERE owner_id = ? ORDER BY last_sync_at DESC, created_at DESC',
),
upsertImmichAlbum: db.prepare(`
INSERT INTO immich_albums (event_id, owner_id, server_url, album_id, album_name, asset_count, kind, last_sync_at, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(event_id) DO UPDATE SET
server_url = excluded.server_url,
album_id = excluded.album_id,
album_name = excluded.album_name,
asset_count = excluded.asset_count,
kind = excluded.kind,
last_sync_at = excluded.last_sync_at
`),
deleteImmichAlbum: db.prepare('DELETE FROM immich_albums WHERE event_id = ?'),
immichAssetsByEvent: db.prepare('SELECT * FROM immich_assets WHERE event_id = ?'),
upsertImmichAsset: db.prepare(`
INSERT INTO immich_assets (event_id, asset_id, photo_id, owner_id, album_id, checksum, file_name, imported_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(event_id, asset_id) DO UPDATE SET
photo_id = excluded.photo_id,
checksum = excluded.checksum,
file_name = excluded.file_name,
imported_at = excluded.imported_at
`),
deleteImmichAsset: db.prepare('DELETE FROM immich_assets WHERE event_id = ? AND asset_id = ?'),
deleteImmichAssetsForPhoto: db.prepare('DELETE FROM immich_assets WHERE photo_id = ?'),
immichAssetForPhoto: db.prepare('SELECT * FROM immich_assets WHERE photo_id = ? LIMIT 1'),
immichSettings: db.prepare('SELECT * FROM immich_settings WHERE owner_id = ?'),
allImmichSettings: db.prepare('SELECT * FROM immich_settings'),
upsertImmichSettings: db.prepare(`
INSERT INTO immich_settings (owner_id, server_url, api_key, enabled, auto_sync, sync_minutes, trash_original, sync_metadata, last_sync_at, last_error, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(owner_id) DO UPDATE SET
server_url = excluded.server_url,
api_key = excluded.api_key,
enabled = excluded.enabled,
auto_sync = excluded.auto_sync,
sync_minutes = excluded.sync_minutes,
trash_original = excluded.trash_original,
sync_metadata = excluded.sync_metadata,
last_sync_at = excluded.last_sync_at,
last_error = excluded.last_error
`),
deleteImmichSettings: db.prepare('DELETE FROM immich_settings WHERE owner_id = ?'),
setImmichSyncState: db.prepare(
'UPDATE immich_settings SET last_sync_at = ?, last_error = ? WHERE owner_id = ?',
),
immichPush: db.prepare(
'SELECT * FROM immich_pushes WHERE photo_id = ? AND checksum = ? ORDER BY created_at DESC LIMIT 1',
),
immichPushedForPhoto: db.prepare(
"SELECT * FROM immich_pushes WHERE photo_id = ? AND state = 'done' AND new_asset_id IS NOT NULL ORDER BY created_at DESC LIMIT 1",
),
immichPhotoIdsByEvent: db.prepare(
'SELECT id FROM photos WHERE event_id = ? AND owner_id = ? ORDER BY sort_index ASC, updated_at ASC',
),
immichPendingByEvent: db.prepare(`
SELECT p.data FROM photos p
WHERE p.event_id = ? AND p.owner_id = ?
AND NOT EXISTS (SELECT 1 FROM immich_assets a WHERE a.photo_id = p.id)
AND NOT EXISTS (
SELECT 1 FROM immich_pushes u
WHERE u.photo_id = p.id AND u.state = 'done' AND u.new_asset_id IS NOT NULL
)
ORDER BY p.sort_index ASC, p.updated_at ASC
`),
upsertImmichPush: db.prepare(`
INSERT INTO immich_pushes (id, owner_id, photo_id, source_asset_id, new_asset_id, checksum, edits_hash, state, trashed_source, error, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET
new_asset_id = excluded.new_asset_id,
edits_hash = excluded.edits_hash,
state = excluded.state,
trashed_source = excluded.trashed_source,
error = excluded.error
`),
deleteImmichPushesForPhoto: db.prepare('DELETE FROM immich_pushes WHERE photo_id = ?'),
immichMeta: db.prepare('SELECT * FROM immich_meta WHERE photo_id = ?'),
upsertImmichMeta: db.prepare(`
INSERT INTO immich_meta (photo_id, owner_id, asset_id, local_fingerprint, remote_fingerprint, remote_updated_at, last_synced_at, last_error)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(photo_id) DO UPDATE SET
owner_id = excluded.owner_id,
asset_id = excluded.asset_id,
local_fingerprint = excluded.local_fingerprint,
remote_fingerprint = excluded.remote_fingerprint,
remote_updated_at = excluded.remote_updated_at,
last_synced_at = excluded.last_synced_at,
last_error = excluded.last_error
`),
deleteImmichMeta: db.prepare('DELETE FROM immich_meta WHERE photo_id = ?'),
immichPushAssets: db.prepare(
"SELECT DISTINCT new_asset_id FROM immich_pushes WHERE photo_id = ? AND state = 'done' AND new_asset_id IS NOT NULL",
),
patchPhotoMeta: db.prepare(`
UPDATE photos
SET data = json_set(data, '$.rating', ?, '$.flag', ?, '$.keywords', json(?)), updated_at = ?
WHERE id = ?
`),
findUserByEmail: db.prepare('SELECT * FROM users WHERE email = ?'),
insertUser: db.prepare(
'INSERT INTO users (id, email, name, pass_hash, role, created_at) VALUES (?, ?, ?, ?, ?, ?)',
@@ -361,6 +558,8 @@ export function saveEvent(record, sortIndex = 0, ownerId = '') {
export function removeEvent(id) {
q.deleteEvent.run(id);
// An album link must not outlive the event it points at.
q.deleteImmichAlbum.run(id);
}
export function shareTokenFor(id) {
@@ -479,6 +678,250 @@ export function getPhoto(id) {
return row ? parse(row.data) : null;
}
export function getEvent(id) {
const row = q.event.get(id);
return row ? parse(row.data) : null;
}
/** The Luminary event already linked to an Immich album, if any. */
export function immichAlbumFor(ownerId, serverUrl, albumId) {
const row = q.immichAlbum.get(ownerId, serverUrl, albumId);
return row ? albumLinkFromRow(row) : null;
}
export function immichAlbumLinks(ownerId) {
return q.immichAlbumsByOwner.all(ownerId).map(albumLinkFromRow);
}
export function immichAlbumForEvent(eventId) {
const row = q.immichAlbumForEvent.get(eventId);
return row ? albumLinkFromRow(row) : null;
}
export function immichAlbumByAlbum(ownerId, albumId) {
const row = q.immichAlbumByAlbum.get(ownerId, albumId);
return row ? albumLinkFromRow(row) : null;
}
function albumLinkFromRow(row) {
return {
eventId: row.event_id,
ownerId: row.owner_id,
serverUrl: row.server_url,
albumId: row.album_id,
albumName: row.album_name,
assetCount: Number(row.asset_count ?? 0),
kind: row.kind ?? 'import',
lastSyncAt: row.last_sync_at ?? null,
createdAt: row.created_at,
};
}
export function saveImmichAlbum(link) {
q.upsertImmichAlbum.run(
link.eventId,
link.ownerId,
link.serverUrl,
link.albumId,
link.albumName ?? '',
Number(link.assetCount ?? 0),
link.kind === 'gallery' ? 'gallery' : 'import',
link.lastSyncAt ?? Date.now(),
link.createdAt ?? Date.now(),
);
}
export function immichAssetsForEvent(eventId) {
return q.immichAssetsByEvent.all(eventId).map((row) => ({
eventId: row.event_id,
assetId: row.asset_id,
photoId: row.photo_id,
ownerId: row.owner_id,
albumId: row.album_id,
checksum: row.checksum,
fileName: row.file_name,
importedAt: row.imported_at,
}));
}
export function saveImmichAsset(asset) {
q.upsertImmichAsset.run(
asset.eventId,
asset.assetId,
asset.photoId,
asset.ownerId,
asset.albumId,
asset.checksum ?? '',
asset.fileName ?? '',
asset.importedAt ?? Date.now(),
);
}
export function removeImmichAsset(eventId, assetId) {
q.deleteImmichAsset.run(eventId, assetId);
}
/** The Immich source row for a photo, used when sending an edit back. */
export function immichAssetForPhoto(photoId) {
const row = q.immichAssetForPhoto.get(photoId);
if (!row) return null;
return {
eventId: row.event_id,
assetId: row.asset_id,
photoId: row.photo_id,
ownerId: row.owner_id,
albumId: row.album_id,
checksum: row.checksum,
fileName: row.file_name,
importedAt: row.imported_at,
};
}
function immichSettingsFromRow(row) {
if (!row) return null;
return {
ownerId: row.owner_id,
serverUrl: row.server_url,
apiKey: row.api_key,
enabled: Boolean(row.enabled),
autoSync: Boolean(row.auto_sync),
syncMinutes: Number(row.sync_minutes ?? 60),
trashOriginal: Boolean(row.trash_original),
syncMetadata: Boolean(row.sync_metadata ?? 1),
lastSyncAt: row.last_sync_at ?? null,
lastError: row.last_error ?? null,
createdAt: row.created_at,
};
}
export function immichSettingsFor(ownerId) {
return immichSettingsFromRow(q.immichSettings.get(ownerId));
}
export function allImmichSettings() {
return q.allImmichSettings.all().map(immichSettingsFromRow);
}
export function saveImmichSettingsRecord(record) {
q.upsertImmichSettings.run(
record.ownerId,
record.serverUrl ?? '',
record.apiKey ?? '',
record.enabled ? 1 : 0,
record.autoSync ? 1 : 0,
Math.max(15, Math.min(1440, Number(record.syncMinutes) || 60)),
record.trashOriginal ? 1 : 0,
record.syncMetadata === false ? 0 : 1,
record.lastSyncAt ?? null,
record.lastError ?? null,
record.createdAt ?? Date.now(),
);
}
export function deleteImmichSettingsRecord(ownerId) {
q.deleteImmichSettings.run(ownerId);
}
export function setImmichSyncState(ownerId, lastSyncAt, lastError) {
q.setImmichSyncState.run(lastSyncAt ?? null, lastError ?? null, ownerId);
}
export function immichPushFor(photoId, checksum) {
const row = q.immichPush.get(photoId, checksum);
if (!row) return null;
return {
id: row.id,
ownerId: row.owner_id,
photoId: row.photo_id,
sourceAssetId: row.source_asset_id,
newAssetId: row.new_asset_id ?? null,
checksum: row.checksum,
state: row.state,
trashedSource: Boolean(row.trashed_source),
error: row.error ?? null,
createdAt: row.created_at,
};
}
export function saveImmichPush(record) {
q.upsertImmichPush.run(
record.id,
record.ownerId,
record.photoId,
record.sourceAssetId,
record.newAssetId ?? null,
record.checksum,
record.editsHash ?? '',
record.state,
record.trashedSource ? 1 : 0,
record.error ?? null,
record.createdAt ?? Date.now(),
);
}
export function immichMetaFor(photoId) {
const row = q.immichMeta.get(photoId);
if (!row) return null;
return {
photoId: row.photo_id,
ownerId: row.owner_id,
assetId: row.asset_id,
localFingerprint: row.local_fingerprint,
remoteFingerprint: row.remote_fingerprint,
remoteUpdatedAt: row.remote_updated_at ?? null,
lastSyncedAt: row.last_synced_at ?? null,
lastError: row.last_error ?? null,
};
}
export function saveImmichMeta(record) {
q.upsertImmichMeta.run(
record.photoId,
record.ownerId,
record.assetId,
record.localFingerprint,
record.remoteFingerprint,
record.remoteUpdatedAt ?? null,
record.lastSyncedAt ?? Date.now(),
record.lastError ?? null,
);
}
export function immichPushAssetsForPhoto(photoId) {
return q.immichPushAssets.all(photoId).map((row) => row.new_asset_id);
}
/** The first successful upload for a photo, whatever checksum produced it. */
export function immichPushDoneForPhoto(photoId) {
return q.immichPushedForPhoto.get(photoId) ?? null;
}
/** Every photo id in an event, in the order it is shown. */
export function photoIdsForEvent(eventId, ownerId) {
return q.immichPhotoIdsByEvent.all(eventId, ownerId).map((row) => row.id);
}
/** Live photo ids in an event that still need to reach its Immich gallery. */
export function immichPendingForEvent(eventId, ownerId) {
return q.immichPendingByEvent
.all(eventId, ownerId)
.map((row) => parse(row.data))
.filter((photo) => photo && !photo.deletedAt)
.map((photo) => photo.id);
}
/** Updates only metadata fields in the stored record, preserving sort order. */
export function patchPhotoMeta(photoId, patch, updatedAt = Date.now()) {
q.patchPhotoMeta.run(
Math.max(0, Math.min(5, Number(patch.rating) || 0)),
patch.flag ?? 'none',
JSON.stringify(Array.isArray(patch.keywords) ? patch.keywords : []),
updatedAt,
photoId,
);
return getPhoto(photoId);
}
export function photoOwner(id) {
return q.photoOwner.get(id)?.owner_id ?? null;
}
@@ -501,6 +944,9 @@ export function removePhoto(id) {
q.deletePicksForPhoto.run(id);
q.deleteCommentsForPhoto.run(id);
q.deleteReviewsForPhoto.run(id);
q.deleteImmichAssetsForPhoto.run(id);
q.deleteImmichPushesForPhoto.run(id);
q.deleteImmichMeta.run(id);
}
export function countPhotos() {