diff --git a/server/src/routes/memories/immich.ts b/server/src/routes/memories/immich.ts index af9b4d0..fa89258 100644 --- a/server/src/routes/memories/immich.ts +++ b/server/src/routes/memories/immich.ts @@ -136,7 +136,8 @@ router.get('/albums', authenticate, async (req: Request, res: Response) => { router.post('/trips/:tripId/album-links/:linkId/sync', authenticate, async (req: Request, res: Response) => { const authReq = req as AuthRequest; const { tripId, linkId } = req.params; - const result = await syncAlbumAssets(tripId, linkId, authReq.user.id); + const sid = req.headers['x-socket-id'] as string; + const result = await syncAlbumAssets(tripId, linkId, authReq.user.id, sid); if (result.error) return res.status(result.status!).json({ error: result.error }); res.json({ success: true, added: result.added, total: result.total }); if (result.added! > 0) { diff --git a/server/src/routes/memories/synology.ts b/server/src/routes/memories/synology.ts index 94ddd88..1c7eb4c 100644 --- a/server/src/routes/memories/synology.ts +++ b/server/src/routes/memories/synology.ts @@ -72,8 +72,9 @@ router.get('/albums', authenticate, async (req: Request, res: Response) => { router.post('/trips/:tripId/album-links/:linkId/sync', authenticate, async (req: Request, res: Response) => { const authReq = req as AuthRequest; const { tripId, linkId } = req.params; + const sid = req.headers['x-socket-id'] as string; - handleServiceResult(res, await syncSynologyAlbumLink(authReq.user.id, tripId, linkId)); + handleServiceResult(res, await syncSynologyAlbumLink(authReq.user.id, tripId, sid, linkId)); }); router.post('/search', authenticate, async (req: Request, res: Response) => { diff --git a/server/src/services/memories/immichService.ts b/server/src/services/memories/immichService.ts index da0dca0..5f8138d 100644 --- a/server/src/services/memories/immichService.ts +++ b/server/src/services/memories/immichService.ts @@ -343,7 +343,8 @@ export function deleteAlbumLink(linkId: string, tripId: string, userId: number) export async function syncAlbumAssets( tripId: string, linkId: string, - userId: number + userId: number, + sid: string, ): Promise<{ success?: boolean; added?: number; total?: number; error?: string; status?: number }> { const response = getAlbumIdFromLink(tripId, linkId, userId); if (!response.success) return { error: 'Album link not found', status: 404 }; @@ -365,7 +366,7 @@ export async function syncAlbumAssets( asset_ids: assets.map((a: any) => a.id), }; - const result = await addTripPhotos(tripId, userId, true, [selection]); + const result = await addTripPhotos(tripId, userId, true, [selection], sid, linkId); if ('error' in result) return { error: result.error.message, status: result.error.status }; updateSyncTimeForAlbumLink(linkId); diff --git a/server/src/services/memories/synologyService.ts b/server/src/services/memories/synologyService.ts index 76b8916..b9bdff2 100644 --- a/server/src/services/memories/synologyService.ts +++ b/server/src/services/memories/synologyService.ts @@ -348,7 +348,7 @@ export async function listSynologyAlbums(userId: number): Promise> { +export async function syncSynologyAlbumLink(userId: number, tripId: string, linkId: string, sid: string): Promise> { const response = getAlbumIdFromLink(tripId, linkId, userId); if (!response.success) return response as ServiceResult; @@ -380,11 +380,12 @@ export async function syncSynologyAlbumLink(userId: number, tripId: string, link asset_ids: allItems.map(item => String(item.additional?.thumbnail?.cache_key || '')).filter(id => id), }; - updateSyncTimeForAlbumLink(linkId); - - const result = await addTripPhotos(tripId, userId, true, [selection]); + + const result = await addTripPhotos(tripId, userId, true, [selection], sid, linkId); if (!result.success) return result as ServiceResult; - + + updateSyncTimeForAlbumLink(linkId); + return success({ added: result.data.added, total: allItems.length }); } diff --git a/server/src/services/memories/unifiedService.ts b/server/src/services/memories/unifiedService.ts index 35d47eb..b3918b8 100644 --- a/server/src/services/memories/unifiedService.ts +++ b/server/src/services/memories/unifiedService.ts @@ -67,10 +67,10 @@ export function listTripAlbumLinks(tripId: string, userId: number): ServiceResul //----------------------------------------------- // managing photos in trip -function _addTripPhoto(tripId: string, userId: number, provider: string, assetId: string, shared: boolean): boolean { +function _addTripPhoto(tripId: string, userId: number, provider: string, assetId: string, shared: boolean, albumLinkId?: string): boolean { const result = db.prepare( - 'INSERT OR IGNORE INTO trip_photos (trip_id, user_id, asset_id, provider, shared) VALUES (?, ?, ?, ?, ?)' - ).run(tripId, userId, assetId, provider, shared ? 1 : 0); + 'INSERT OR IGNORE INTO trip_photos (trip_id, user_id, asset_id, provider, shared, album_link_id) VALUES (?, ?, ?, ?, ?, ?)' + ).run(tripId, userId, assetId, provider, shared ? 1 : 0, albumLinkId || null); return result.changes > 0; } @@ -79,7 +79,8 @@ export async function addTripPhotos( userId: number, shared: boolean, selections: Selection[], - sid?: string, + sid: string, + albumLinkId?: string, ): Promise> { const access = canAccessTrip(tripId, userId); if (!access) { @@ -96,7 +97,7 @@ export async function addTripPhotos( for (const raw of selection.asset_ids) { const assetId = String(raw || '').trim(); if (!assetId) continue; - if (_addTripPhoto(tripId, userId, selection.provider, assetId, shared)) { + if (_addTripPhoto(tripId, userId, selection.provider, assetId, shared, albumLinkId)) { added++; } } @@ -213,9 +214,10 @@ export function removeAlbumLink(tripId: string, linkId: string, userId: number): } try { + db.prepare('DELETE FROM trip_photos WHERE trip_id = ? AND album_link_id = ?') + .run(tripId, linkId); db.prepare('DELETE FROM trip_album_links WHERE id = ? AND trip_id = ? AND user_id = ?') .run(linkId, tripId, userId); - return success(true); } catch (error) { return mapDbError(error, 'Failed to remove album link');