Add view counters per album, recipient and browser, per-browser sign-out in admin, and photo hearts

This commit is contained in:
zyphlar
2026-11-07 22:55:29 -08:00
parent 17bf59b218
commit 1db425d493
17 changed files with 511 additions and 26 deletions
+108
View File
@@ -6,6 +6,7 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { loadConfig } from '@ipp/core'
import { Recipient, setStore, Store } from '../src/magic/store'
import { Mail, setMailSender } from '../src/magic/mailer'
import { browserName } from '../src/magic/views'
/*
End to end: the real app in front of a fake Immich. Proves that nothing is
@@ -144,6 +145,7 @@ afterAll(async () => {
interface Options {
cookie?: string
userAgent?: string
page?: boolean
method?: string
json?: unknown
@@ -153,6 +155,7 @@ interface Options {
async function request (path: string, options: Options = {}) {
const headers: Record<string, string> = {}
if (options.cookie) headers.Cookie = options.cookie
if (options.userAgent) headers['User-Agent'] = options.userAgent
if (options.page) headers.Accept = 'text/html,application/xhtml+xml'
let body: string | undefined
if (options.json !== undefined) {
@@ -529,3 +532,108 @@ describe('public shares', () => {
expect(store.getShare(KEY_D)?.public).toBe(true)
})
})
describe('activity', () => {
const IPHONE = 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1'
it('counts gallery views against the album, the recipient and the browser', async () => {
const shareBefore = store.shareActivity().get(KEY_A)!
const annBefore = store.recipientActivity(ann.id).views
const { res } = await request(annLink, { page: true, userAgent: IPHONE })
const cookie = cookieFrom(res, 'imp_session')!
await request('/share/' + KEY_A, { page: true, cookie, userAgent: IPHONE })
await request('/share/' + KEY_A, { page: true, cookie, userAgent: IPHONE })
// Photos and metadata aren't views; only the gallery page is
await request('/share/photo/' + KEY_A + '/' + ASSET_1 + '/thumbnail', { cookie })
const share = store.shareActivity().get(KEY_A)!
expect(share.views - shareBefore.views).toBe(2)
expect(share.anonymousViews).toBe(shareBefore.anonymousViews)
expect(share.lastViewedAt).not.toBeNull()
expect(store.recipientActivity(ann.id).views - annBefore).toBe(2)
const browser = store.listSessions(ann.id).find(s => s.userAgent === IPHONE)!
expect(browser.views).toBe(2)
expect(browserName(browser.userAgent)).toBe('Safari on iPhone')
})
it('counts views of a public share by visitors who aren\'t signed in', async () => {
const before = store.shareActivity().get(KEY_D)!
await request('/share/' + KEY_D, { page: true })
const after = store.shareActivity().get(KEY_D)!
expect(after.views - before.views).toBe(1)
expect(after.anonymousViews - before.anonymousViews).toBe(1)
})
it('lists each browser for the admin, who can sign one out', async () => {
await signIn()
const others = store.listSessions(ann.id).map(s => s.id)
const kept = await signIn()
const keptId = store.listSessions(ann.id).map(s => s.id).find(id => !others.includes(id))!
const login = await request('/admin/login', { form: { password: 'admin-secret' } })
const admin = cookieFrom(login.res, 'imp_admin')!
const page = await request('/admin/recipients/' + ann.id, { page: true, cookie: admin })
expect(page.text).toContain('Safari on iPhone')
expect(page.text).toMatch(/album views?, last/)
// Sign out one browser; the others stay signed in
const target = others[0]
await request('/admin/recipients/' + ann.id + '/sessions/' + target + '/revoke', { cookie: admin, method: 'POST' })
const left = store.listSessions(ann.id).map(s => s.id)
expect(left).not.toContain(target)
expect(left).toContain(keptId)
expect((await request('/share/' + KEY_A, { page: true, cookie: kept })).res.status).toBe(200)
// Another recipient's browser can't be signed out from this page
const bobs = store.listSessions(bob.id)
if (bobs.length) {
await request('/admin/recipients/' + ann.id + '/sessions/' + bobs[0].id + '/revoke', { cookie: admin, method: 'POST' })
expect(store.listSessions(bob.id)).toHaveLength(bobs.length)
}
// Not without the admin session
await request('/admin/recipients/' + ann.id + '/sessions/' + keptId + '/revoke', { method: 'POST' })
expect(store.listSessions(ann.id).map(s => s.id)).toContain(keptId)
})
it('shows views and hearts on the dashboard', async () => {
const login = await request('/admin/login', { form: { password: 'admin-secret' } })
const admin = cookieFrom(login.res, 'imp_admin')!
const page = await request('/admin', { page: true, cookie: admin })
expect(page.text).toContain('<th>Views</th>')
expect(page.text).toContain('not signed in')
})
})
describe('hearts', () => {
let cookie: string
beforeAll(async () => { cookie = await signIn() })
const heart = (key: string, id: string, method: string, as = cookie) => request('/share/hearts/' + key + '/' + id, { cookie: as, method })
it('a recipient hearts a photo once, and can take it back', async () => {
let res = await heart(KEY_A, ASSET_1, 'PUT')
expect(res.res.status).toBe(200)
expect(JSON.parse(res.text)).toEqual({ count: 1, mine: true })
res = await heart(KEY_A, ASSET_1, 'PUT')
expect(JSON.parse(res.text)).toEqual({ count: 1, mine: true })
const page = await request('/share/' + KEY_A, { page: true, cookie })
expect(page.text).toContain('/share/hearts/' + KEY_A)
expect(page.text).toContain('"mine":["' + ASSET_1 + '"]')
res = await heart(KEY_A, ASSET_1, 'DELETE')
expect(JSON.parse(res.text)).toEqual({ count: 0, mine: false })
})
it('only on photos in the album', async () => {
expect((await heart(KEY_A, ASSET_B, 'PUT')).res.status).toBe(404)
})
it('not for someone without the album, or not signed in', async () => {
const bobCookie = await signIn('/m/' + store.createMagicLink(bob.id, { purpose: 'invite', expiresAt: null }, new Date().toISOString()))
expect((await heart(KEY_A, ASSET_1, 'PUT', bobCookie)).res.status).toBe(404)
expect((await request('/share/hearts/' + KEY_A + '/' + ASSET_1, { method: 'PUT' })).res.status).toBe(404)
// A public share's anonymous visitors don't get hearts at all
const page = await request('/share/' + KEY_D, { page: true })
expect(page.text).not.toContain('/share/hearts/')
expect((await request('/share/hearts/' + KEY_D + '/' + ASSET_2, { method: 'PUT' })).res.status).toBe(404)
})
})