fix(memories): give the photo picker a chronological order it can rely on

The picker got its photos back in whatever order the provider happened to
return them. It groups by day and appends pages lazily, so an unordered page
lands under the wrong heading and pushes photos out of the fold the reader
is looking at.

Immich now asks for order: desc on both the search and the album path. An
older server that does not know the property drops it silently rather than
failing, so each page is sorted again here; the album paths drain every page
before sorting and are covered outright. getAssetInfo reports mediaType, the
same mapping the listing already used, so the detail route and the picker
agree on what an asset is.

Synology gets no sort parameter upstream on either path. Search.Search
documents none, and _fetchSynologyJson maps every app code except 106/107/119
onto a 400, so a parameter one DSM build rejects would take listing down
entirely for that user. Both paths read every page anyway, so ordering the
mapped result costs nothing and cannot fail.

No media type for Synology yet, deliberately. streamSynologyAsset serves
every asset as a JPEG out of SYNO.Foto.Thumbnail, original included; there is
no video byte path. Labelling an asset a video would make the lightbox mount
a player on a still image, which is worse than the missing badge. The badge,
the byte path and the persisted media_type belong together in their own
change.

Five album cases in the Immich integration suite pinned the old arrival
order. Where the position was incidental they now look the asset up by id;
where the order is the point they state the chronological one.
This commit is contained in:
Maurice
2026-09-08 14:04:45 +02:00
committed by Maurice
parent cfae0a1f78
commit 2d7889c237
7 changed files with 259 additions and 15 deletions
+13 -3
View File
@@ -8,7 +8,7 @@ import fsPromises from 'node:fs/promises';
import path from 'node:path';
import { DatabaseService } from '../database/database.service';
import { MemoriesAccessService } from './memories-access.service';
import { fail, handleServiceResult, pipeAsset, type Selection } from './memories.helpers';
import { fail, handleServiceResult, pipeAsset, sortAssetsByTakenAtDesc, type Selection } from './memories.helpers';
const ALBUM_PAGE_SIZE = 1000;
const ALBUM_MAX_PAGES = 20;
@@ -226,6 +226,12 @@ export class ImmichService {
// below is the only guard on those versions. Do not remove it.
visibility: 'timeline',
withExif: true,
// Immich's own default order has moved between versions, and the picker
// pages lazily: an unordered page 2 lands in the wrong day heading.
// Same whitelist reasoning as `visibility` above — an older server that
// does not know the property drops it instead of failing the request,
// which is why the result is sorted again below.
order: 'desc',
size,
page,
}),
@@ -249,7 +255,7 @@ export class ImmichService {
lng: typeof a.exifInfo?.longitude === 'number' ? a.exifInfo.longitude : null,
mediaType: a.type === 'VIDEO' ? 'video' : 'image',
}));
return { assets, hasMore: items.length >= size };
return { assets: sortAssetsByTakenAtDesc(assets), hasMore: items.length >= size };
} catch {
return { error: 'Could not reach Immich', status: 502 };
}
@@ -279,6 +285,7 @@ export class ImmichService {
data: {
id: asset.id,
takenAt: asset.fileCreatedAt || asset.createdAt,
mediaType: asset.type === 'VIDEO' ? 'video' as const : 'image' as const,
width: asset.exifInfo?.exifImageWidth || null,
height: asset.exifInfo?.exifImageHeight || null,
camera: asset.exifInfo?.make && asset.exifInfo?.model ? `${asset.exifInfo.make} ${asset.exifInfo.model}` : null,
@@ -487,6 +494,7 @@ export class ImmichService {
albumIds: [albumId],
withExif: true,
withDeleted: false,
order: 'desc',
size: ALBUM_PAGE_SIZE,
page,
}),
@@ -529,7 +537,9 @@ export class ImmichService {
lng: typeof a.exifInfo?.longitude === 'number' ? a.exifInfo.longitude : null,
mediaType: a.type === 'VIDEO' ? 'video' : 'image',
}));
return { assets };
// The v2 branch reads /api/albums/{id} and never passes a search at all, so
// this is the only ordering an album ever gets there.
return { assets: sortAssetsByTakenAtDesc(assets) };
} catch {
return { error: 'Could not reach Immich', status: 502 };
}
@@ -87,10 +87,46 @@ export type AssetsList = {
hasMore: boolean
};
/**
* Newest first, by capture time, with a stable fallback.
*
* Neither provider guarantees an order: Immich sorts by whatever its version
* defaults to and Synology's search API documents none at all. The picker groups
* by day and lazily appends pages, so an unordered page puts photos in the wrong
* day heading and, worse, drops them above the fold the reader is looking at.
* Asking upstream for `desc` is the fix; this is the belt to that pair of braces,
* and it is load-bearing for the album paths, which do not run through a sorted
* search at all.
*
* Assets without a usable timestamp keep their relative order at the end, which
* matches how the client groups them under its unknown-date heading. That, and
* the order of assets sharing a timestamp, rests on Array.prototype.sort being
* stable, which it has been since ES2019.
*
* Timestamps are parsed once up front rather than inside the comparator, which
* would re-parse the same string O(n log n) times.
*/
export function sortAssetsByTakenAtDesc<T extends { takenAt?: string | null }>(assets: T[]): T[] {
return assets
.map(asset => {
const parsed = asset.takenAt ? Date.parse(asset.takenAt) : Number.NaN;
return { asset, at: Number.isNaN(parsed) ? null : parsed };
})
.sort((a, b) => {
if (a.at === null && b.at === null) return 0;
if (a.at === null) return 1;
if (b.at === null) return -1;
return b.at - a.at;
})
.map(entry => entry.asset);
}
export type AssetInfo = {
id: string;
takenAt: string | null;
/** What the provider says this is. Absent means the provider does not tell us. */
mediaType?: 'image' | 'video';
city: string | null;
country: string | null;
state?: string | null;
+10 -2
View File
@@ -9,6 +9,7 @@ import {
handleServiceResult,
success,
pipeAsset,
sortAssetsByTakenAtDesc,
type AlbumsList,
type AssetInfo,
type AssetsList,
@@ -526,7 +527,8 @@ export class SynologyService {
lng: typeof item.additional?.gps?.longitude === 'number' ? item.additional.gps.longitude : null,
})).filter(a => a.id);
return success({ assets, total: assets.length, hasMore: false });
const ordered = sortAssetsByTakenAtDesc(assets);
return success({ assets: ordered, total: ordered.length, hasMore: false });
}
/**
@@ -593,7 +595,13 @@ export class SynologyService {
if (!result.success) return result as ServiceResult<AssetsList>;
const allItems = result.data.list || [];
const assets = allItems.map(item => this._normalizeSynologyPhotoInfo(item));
// Deliberately no sort parameter upstream, on this path or the album one.
// Synology documents none for Search.Search, and _fetchSynologyJson maps
// every app code except 106/107/119 onto a 400 — a parameter one DSM build
// rejects would take listing down entirely for that user. Both paths read
// every page anyway, so ordering the mapped result costs nothing and
// cannot fail.
const assets = sortAssetsByTakenAtDesc(allItems.map(item => this._normalizeSynologyPhotoInfo(item)));
return success({
assets,