Gate shares behind magic-link sessions, add comments and new-photo email digests

This commit is contained in:
zyphlar
2026-10-19 15:10:57 -07:00
parent c8d8dc9dbc
commit f598200e24
34 changed files with 3387 additions and 375 deletions
+147
View File
@@ -0,0 +1,147 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { Store } from '../src/magic/store'
import { nextDigestDue, scanShares, sendDueDigests } from '../src/magic/digest'
import { Mail } from '../src/magic/mailer'
const HOUR = 8
// Digests are scheduled in server-local time, so build every time locally
const at = (month: number, day: number, hour: number, minute = 0) => new Date(2026, month - 1, day, hour, minute)
const iso = (month: number, day: number, hour: number, minute = 0) => at(month, day, hour, minute).toISOString()
describe('nextDigestDue', () => {
it('lands on the next daily / weekly / monthly boundary at the digest hour', () => {
const start = iso(3, 10, 15, 30)
expect(nextDigestDue('daily', start, HOUR)).toEqual(at(3, 11, HOUR))
expect(nextDigestDue('weekly', start, HOUR)).toEqual(at(3, 17, HOUR))
expect(nextDigestDue('monthly', start, HOUR)).toEqual(at(4, 10, HOUR))
})
it('is never due when off or without a window', () => {
expect(nextDigestDue('off', iso(3, 10, 0))).toBeUndefined()
expect(nextDigestDue('daily', null)).toBeUndefined()
})
})
describe('scan and digest', () => {
let store: Store
let sent: Mail[]
const send = async (mail: Mail) => { sent.push(mail) }
const baseUrl = 'https://photos.example.com'
beforeEach(() => {
store = new Store(':memory:')
sent = []
})
afterEach(() => store.close())
function setup (frequency: 'daily' | 'weekly' | 'off' = 'daily') {
const share = store.upsertShare({ key: 'key-a', slug: null, title: 'Holiday', password: null }, iso(3, 1, 0))
const ann = store.createRecipient({ name: 'Ann', email: 'ann@example.com', frequency }, iso(3, 2, 10))
store.setRecipientShares(ann.id, [share.key], iso(3, 2, 10))
return { share, ann }
}
const digest = (now: Date, current: Map<string, Set<string>>, sender = send) =>
sendDueDigests({ store, send: sender, baseUrl, now, hour: HOUR }, current)
it('treats the first scan as a baseline, then reports only later photos', async () => {
setup()
let assets = ['a', 'b']
const list = async () => assets
// Baseline: photos already there are dated to when the share was added
await scanShares(store, list, iso(3, 2, 12))
assets = ['a', 'b', 'c']
const current = await scanShares(store, list, iso(3, 2, 13))
// Next morning, after the digest hour
expect(await digest(at(3, 3, HOUR, 5), current)).toBe(1)
expect(sent[0].to).toBe('ann@example.com')
expect(sent[0].subject).toContain('1 new photo')
expect(sent[0].text).toContain('Holiday')
// Links sign the recipient in and land on the album
expect(sent[0].text).toMatch(/https:\/\/photos\.example\.com\/m\/[\w-]+\?next=%2Fshare%2Fkey-a/)
})
it('waits until the digest is due, then does not repeat it', async () => {
const { ann } = setup()
await scanShares(store, async () => ['a'], iso(3, 2, 12))
const current = await scanShares(store, async () => ['a', 'b'], iso(3, 2, 13))
expect(await digest(at(3, 3, HOUR - 1), current)).toBe(0)
expect(await digest(at(3, 3, HOUR), current)).toBe(1)
expect(store.getRecipient(ann.id)?.lastDigestAt).toBe(iso(3, 3, HOUR))
expect(await digest(at(3, 3, HOUR + 5), current)).toBe(0)
})
it('sends nothing to recipients who opted out', async () => {
setup('off')
await scanShares(store, async () => ['a'], iso(3, 2, 12))
const current = await scanShares(store, async () => ['a', 'b'], iso(3, 2, 13))
expect(await digest(at(6, 1, HOUR), current)).toBe(0)
expect(sent).toEqual([])
})
it('sends nothing when nothing is new, but still moves the window on', async () => {
const { ann } = setup()
const current = await scanShares(store, async () => ['a'], iso(3, 2, 12))
expect(await digest(at(3, 3, HOUR), current)).toBe(0)
expect(store.getRecipient(ann.id)?.lastDigestAt).toBe(iso(3, 3, HOUR))
})
it('does not count photos removed before the digest', async () => {
setup()
await scanShares(store, async () => ['a'], iso(3, 2, 12))
await scanShares(store, async () => ['a', 'b'], iso(3, 2, 13))
const current = await scanShares(store, async () => ['a'], iso(3, 2, 14))
expect(await digest(at(3, 3, HOUR), current)).toBe(0)
})
it('keeps the window open when sending fails, so the next run retries', async () => {
const { ann } = setup()
await scanShares(store, async () => ['a'], iso(3, 2, 12))
const current = await scanShares(store, async () => ['a', 'b'], iso(3, 2, 13))
const failing = async () => { throw new Error('SMTP down') }
expect(await digest(at(3, 3, HOUR), current, failing)).toBe(0)
expect(store.getRecipient(ann.id)?.lastDigestAt).toBe(iso(3, 2, 10))
expect(await digest(at(3, 3, HOUR + 1), current)).toBe(1)
})
it('picks up photos added while Immich was unreachable on the next good scan', async () => {
setup()
await scanShares(store, async () => ['a'], iso(3, 2, 12))
const failed = await scanShares(store, async () => null, iso(3, 2, 13))
expect(failed.size).toBe(0)
const current = await scanShares(store, async () => ['a', 'b'], iso(3, 3, 7))
expect(await digest(at(3, 3, HOUR), current)).toBe(1)
})
it('does not report photos that were in an album before the recipient got it', async () => {
const { ann } = setup()
// An album added and granted to Ann after her window opened
store.upsertShare({ key: 'key-b', slug: null, title: 'Wedding', password: null }, iso(3, 2, 14))
const list = async (share: { key: string }) => share.key === 'key-a' ? ['a'] : ['b1', 'b2', 'b3']
await scanShares(store, list, iso(3, 2, 14))
store.setRecipientShares(ann.id, ['key-a', 'key-b'], iso(3, 2, 15))
const current = await scanShares(store, list, iso(3, 2, 16))
expect(await digest(at(3, 3, HOUR), current)).toBe(0)
})
it('groups several albums into one email', async () => {
const { ann } = setup()
store.upsertShare({ key: 'key-b', slug: null, title: 'Wedding', password: null }, iso(3, 1, 0))
store.setRecipientShares(ann.id, ['key-a', 'key-b'], iso(3, 2, 10))
let a = ['a1']
let b = ['b1']
const list = async (share: { key: string }) => share.key === 'key-a' ? a : b
await scanShares(store, list, iso(3, 2, 12))
a = ['a1', 'a2', 'a3']
b = ['b1', 'b2']
const current = await scanShares(store, list, iso(3, 2, 13))
expect(await digest(at(3, 3, HOUR), current)).toBe(1)
expect(sent[0].subject).toContain('3 new photos')
expect(sent[0].text).toContain('2 new photos in Holiday')
expect(sent[0].text).toContain('1 new photo in Wedding')
})
})