Fix #260 empty shares when Postgres timezone is not UTC

This commit is contained in:
Alan Grainger
2026-09-03 15:49:28 +12:00
parent 24e8228d9f
commit 02b30e0176
2 changed files with 31 additions and 2 deletions
+14 -1
View File
@@ -419,6 +419,19 @@ function timelineBucketToAssets (bucket: TimelineBucketAssets): Asset[] {
return assets
}
/**
* Normalise a `/timeline/buckets` key for the `/timeline/bucket` lookup.
*
* Immich lists buckets as bare UTC dates (`2026-06-01`) but matches the
* lookup by casting the string back to a timestamp in Postgres's session
* timezone. On a non-UTC database that lands in the previous month and the
* bucket comes back empty - every album shows zero photos #260. The explicit
* `T00:00:00.000Z` form is what Immich's web client sends: (immich-app/immich#22672).
*/
export function utcBucketKey (timeBucket: string): string {
return /^\d{4}-\d{2}-\d{2}$/.test(timeBucket) ? timeBucket + 'T00:00:00.000Z' : timeBucket
}
/**
* Enumerate an album's assets via Immich's timeline API, scoped by album id +
* shared-link key. `GET /timeline/buckets` lists the time buckets (months),
@@ -446,7 +459,7 @@ async function fetchAlbumAssets (albumId: string, keyType: KeyType, key: string,
const perBucket = await Promise.all((buckets || []).map(async (bucket) => {
const res = await fetch(buildUrl(apiUrl() + '/timeline/bucket', {
albumId,
timeBucket: bucket.timeBucket,
timeBucket: utcBucketKey(bucket.timeBucket),
[keyType]: key
}), { headers })
if (!res.ok) {
+17 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { getShareByKey } from '../src/immich'
import { getShareByKey, utcBucketKey } from '../src/immich'
import { KeyType } from '../src/types'
// 3.0-shaped responses: album `/shared-links/me` returns an empty assets[],
@@ -101,6 +101,22 @@ describe('album timeline enumeration (Immich 3.0)', () => {
expect(assets[0].width!).toBeGreaterThan(assets[0].height!)
})
it('requests each bucket with an explicit UTC key so non-UTC databases match (#260)', async () => {
const fetchMock = routeFetch(sharedLinkResponse())
vi.stubGlobal('fetch', fetchMock)
await getShareByKey(uniqueKey(), undefined, KeyType.key)
const bucketCalls = fetchMock.mock.calls.map(c => String(c[0])).filter(u => u.includes('/timeline/bucket?'))
expect(bucketCalls).toHaveLength(1)
expect(new URL(bucketCalls[0]).searchParams.get('timeBucket')).toBe('2026-06-01T00:00:00.000Z')
})
it('utcBucketKey pads bare dates and leaves anything else alone', () => {
expect(utcBucketKey('2026-06-01')).toBe('2026-06-01T00:00:00.000Z')
expect(utcBucketKey('2026-06-01T00:00:00.000Z')).toBe('2026-06-01T00:00:00.000Z')
expect(utcBucketKey('')).toBe('')
})
it('synthesises localDateTime from fileCreatedAt + localOffsetHours', async () => {
vi.stubGlobal('fetch', routeFetch(sharedLinkResponse()))
const result = await getShareByKey(uniqueKey(), undefined, KeyType.key)