Add view counters per album, recipient and browser, per-browser sign-out in admin, and photo hearts
This commit is contained in:
@@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -207,3 +207,47 @@ describe('public shares', () => {
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('activity and hearts', () => {
|
||||
it('counts views per share, recipient and browser, and lists browsers newest first', () => {
|
||||
store.upsertShare({ key: 'k1', slug: null, title: 'A', password: null }, T0)
|
||||
const ann = recipient()
|
||||
const older = store.createSession(ann.id, T0, 'Firefox')
|
||||
const newer = store.createSession(ann.id, T0, 'Chrome')
|
||||
store.recordView('k1', {}, T1)
|
||||
store.recordView('k1', { recipientId: ann.id, sessionToken: older, userAgent: 'Firefox 2' }, T1)
|
||||
store.recordView('k1', { recipientId: ann.id, sessionToken: newer }, T2)
|
||||
|
||||
expect(store.shareActivity().get('k1')).toEqual({ views: 3, anonymousViews: 1, lastViewedAt: T2, hearts: 0 })
|
||||
expect(store.recipientActivity(ann.id)).toEqual({ views: 2, lastViewedAt: T2 })
|
||||
const [first, second] = store.listSessions(ann.id)
|
||||
expect(first).toMatchObject({ views: 1, lastViewedAt: T2, userAgent: 'Chrome' })
|
||||
expect(second).toMatchObject({ views: 1, lastViewedAt: T1, userAgent: 'Firefox 2' })
|
||||
})
|
||||
|
||||
it('signs out one browser, only for its own recipient', () => {
|
||||
const ann = recipient()
|
||||
const bob = recipient('bob@example.com')
|
||||
store.createSession(ann.id, T0)
|
||||
const [session] = store.listSessions(ann.id)
|
||||
expect(store.deleteSessionById(bob.id, session.id)).toBe(false)
|
||||
expect(store.deleteSessionById(ann.id, session.id)).toBe(true)
|
||||
expect(store.listSessions(ann.id)).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('hearts once per recipient per photo, and counts them', () => {
|
||||
store.upsertShare({ key: 'k1', slug: null, title: 'A', password: null }, T0)
|
||||
const ann = recipient()
|
||||
const bob = recipient('bob@example.com')
|
||||
expect(store.setHeart('k1', 'p1', ann.id, true, T0)).toBe(1)
|
||||
expect(store.setHeart('k1', 'p1', ann.id, true, T0)).toBe(1)
|
||||
expect(store.setHeart('k1', 'p1', bob.id, true, T0)).toBe(2)
|
||||
expect(store.countHearts('k1')).toEqual({ p1: 2 })
|
||||
expect(store.heartedBy('k1', ann.id)).toEqual(['p1'])
|
||||
expect(store.shareActivity().get('k1')?.hearts).toBe(2)
|
||||
expect(store.setHeart('k1', 'p1', ann.id, false, T0)).toBe(1)
|
||||
// Deleting the share takes its hearts with it
|
||||
store.deleteShare('k1')
|
||||
expect(store.countHearts('k1')).toEqual({})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user