From dc1ea7af27ee6ccd67b6e96c5c0ff8c5e42a2bb3 Mon Sep 17 00:00:00 2001 From: Void Date: Tue, 10 Nov 2026 17:26:21 +0000 Subject: [PATCH] Albums: event link beats an earlier albums choice; buildAlbums tolerates bad input - zoneView: an eventId always shows the timeline; the back-to-albums button is hidden then. - buildAlbums: null/non-object inputs and replies are skipped, non-numeric timestamps count as 0, an empty-body root is titled "Untitled album", m.replace events are never files or albums. Co-Authored-By: Claude Sonnet 5.5 --- src/app/features/albums/buildAlbums.test.ts | 56 ++++++++++++++++++++- src/app/features/albums/buildAlbums.ts | 19 +++++-- src/app/features/albums/zoneView.test.ts | 7 ++- src/app/features/albums/zoneView.ts | 4 +- src/app/features/room/RoomView.tsx | 2 +- 5 files changed, 77 insertions(+), 11 deletions(-) diff --git a/src/app/features/albums/buildAlbums.test.ts b/src/app/features/albums/buildAlbums.test.ts index 68ab6fc3..3a27bed6 100644 --- a/src/app/features/albums/buildAlbums.test.ts +++ b/src/app/features/albums/buildAlbums.test.ts @@ -120,7 +120,7 @@ describe('buildAlbums', () => { const a = one({ event_id: '$r', content: { msgtype: 'm.text' } }, [ { event_id: '$a', content: { msgtype: 'm.audio' } }, ]); - expect(a).toMatchObject({ title: '', author: '', ts: 0, count: 1 }); + expect(a).toMatchObject({ title: 'Untitled album', author: '', ts: 0, count: 1 }); }); it('does not read inherited object keys as file kinds', () => { @@ -159,4 +159,58 @@ describe('buildAlbums', () => { const emptyEdit = root('$q', 1, { unsigned: { 'm.relations': { 'm.replace': {} } } }); expect(one(emptyEdit, []).title).toBe('album $q'); }); + + it('survives null or non-object entries in inputs and replies', () => { + const bad = [null, undefined, 'x', 7] as unknown as ZoneEvent[]; + const a = buildAlbums([ + null as unknown as AlbumInput, + { root: null as unknown as ZoneEvent, replies: [] }, + { root: root('$r', 1), replies: [...bad, reply('$ok', 2, 'm.audio')] }, + { root: root('$s', 2), replies: null as unknown as ZoneEvent[] }, + ]); + expect(a.map((x) => x.id)).toEqual(['$s', '$r']); + expect(a[1].files.map((f) => f.id)).toEqual(['$ok']); + }); + + it('treats a non-numeric timestamp as 0 so it sorts first, not by string order', () => { + const a = one(root('$r', 1), [ + reply('$b', 20, 'm.audio'), + { ...reply('$bad', 0, 'm.audio'), origin_server_ts: '5' as unknown as number }, + { ...reply('$nan', 0, 'm.audio'), origin_server_ts: NaN }, + reply('$a', 10, 'm.audio'), + ]); + expect(a.files.map((f) => f.id)).toEqual(['$bad', '$nan', '$a', '$b']); + const albums = buildAlbums([ + { root: root('$x', 100), replies: [] }, + { root: root('$y', 0, { origin_server_ts: '999' as unknown as number }), replies: [] }, + ]); + expect(albums.map((x) => [x.id, x.ts])).toEqual([ + ['$x', 100], + ['$y', 0], + ]); + }); + + it('gives an album with an empty body a fallback title', () => { + expect(one(root('$r', 1, { content: { msgtype: 'm.text', body: '' } }), []).title).toBe( + 'Untitled album' + ); + expect(one(root('$q', 1, { content: { msgtype: 'm.text' } }), []).title).toBe('Untitled album'); + }); + + it('never counts an edit event as a file or an album', () => { + const edit = reply('$e', 9, 'm.audio', '* x', { + 'm.relates_to': { rel_type: 'm.replace', event_id: '$a' }, + 'm.new_content': { msgtype: 'm.audio', body: 'x' }, + }); + const a = one(root('$r', 1), [reply('$a', 5, 'm.audio'), edit]); + expect(a.files.map((f) => f.id)).toEqual(['$a']); + const rootEdit = root('$re', 1, { + content: { msgtype: 'm.text', body: '* t', 'm.relates_to': { rel_type: 'm.replace' } }, + }); + expect(buildAlbums([{ root: rootEdit, replies: [] }])).toEqual([]); + const threaded = reply('$t', 6, 'm.audio', 't', { + 'm.relates_to': { rel_type: 'm.thread', event_id: '$r' }, + }); + expect(one(root('$q', 1), [threaded]).count).toBe(1); + }); }); diff --git a/src/app/features/albums/buildAlbums.ts b/src/app/features/albums/buildAlbums.ts index 1f8c944e..8e57262b 100644 --- a/src/app/features/albums/buildAlbums.ts +++ b/src/app/features/albums/buildAlbums.ts @@ -47,11 +47,17 @@ const KIND_OF_MSGTYPE = new Map([ const live = ( e: ZoneEvent ): e is ZoneEvent & { event_id: string; content: Record } => + !!e && + typeof e === 'object' && !!e.event_id && + // An edit is an m.room.message too, but it revises an event; it is never a file or an album. + (e.content?.['m.relates_to'] as { rel_type?: unknown } | undefined)?.rel_type !== 'm.replace' && !e.unsigned?.redacted_because && (e.type === undefined || e.type === 'm.room.message') && typeof e.content?.msgtype === 'string'; +const tsOf = (v: unknown): number => (typeof v === 'number' && Number.isFinite(v) ? v : 0); + const asString = (v: unknown): string => (typeof v === 'string' ? v : ''); const toFile = (e: ZoneEvent): AlbumFile | undefined => { @@ -62,24 +68,27 @@ const toFile = (e: ZoneEvent): AlbumFile | undefined => { id: e.event_id, kind, name: asString(e.content.filename) || asString(e.content.body), - ts: e.origin_server_ts ?? 0, + ts: tsOf(e.origin_server_ts), content: e.content, }; }; +export const UNTITLED = 'Untitled album'; + // An edited root carries its latest edit under unsigned["m.relations"]["m.replace"]; the title is // that edit's m.new_content.body, else the original body. const titleOf = (root: ZoneEvent & { content: Record }): string => { const relations = root.unsigned?.['m.relations'] as Record | undefined; const edit = relations?.['m.replace'] as ZoneEvent | undefined; const edited = (edit?.content?.['m.new_content'] as Record | undefined)?.body; - return asString(edited) || asString(root.content.body); + return asString(edited) || asString(root.content.body) || UNTITLED; }; -const toAlbum = ({ root, replies }: AlbumInput): Album | undefined => { +const toAlbum = (input: AlbumInput): Album | undefined => { + const { root, replies } = input ?? {}; if (!live(root)) return undefined; const seen = new Set(); - const files = replies + const files = (Array.isArray(replies) ? replies : []) .map(toFile) .filter((f): f is AlbumFile => { if (!f || seen.has(f.id)) return false; @@ -91,7 +100,7 @@ const toAlbum = ({ root, replies }: AlbumInput): Album | undefined => { id: root.event_id, title: titleOf(root), author: root.sender ?? '', - ts: root.origin_server_ts ?? 0, + ts: tsOf(root.origin_server_ts), cover: files.find((f) => f.kind === 'image'), files, count: files.length, diff --git a/src/app/features/albums/zoneView.test.ts b/src/app/features/albums/zoneView.test.ts index 9774fb25..65beb9cb 100644 --- a/src/app/features/albums/zoneView.test.ts +++ b/src/app/features/albums/zoneView.test.ts @@ -6,8 +6,11 @@ describe('zoneView', () => { expect(zoneView(undefined, undefined)).toBe('albums'); expect(zoneView(undefined, '$e')).toBe('timeline'); }); - it('lets the user go back to albums or on to the timeline whatever the link said', () => { - expect(zoneView('albums', '$e')).toBe('albums'); + it('lets an event link win over an earlier choice of albums', () => { + expect(zoneView('albums', '$e')).toBe('timeline'); + }); + it('honours the choice when there is no event link', () => { + expect(zoneView('albums', undefined)).toBe('albums'); expect(zoneView('timeline', undefined)).toBe('timeline'); }); }); diff --git a/src/app/features/albums/zoneView.ts b/src/app/features/albums/zoneView.ts index 2532bcc5..1af7f705 100644 --- a/src/app/features/albums/zoneView.ts +++ b/src/app/features/albums/zoneView.ts @@ -1,5 +1,5 @@ export type ZoneView = 'albums' | 'timeline'; -/** The view a file-zone room shows: the user's choice, else the timeline for an event deep link, else albums. */ +/** The view a file-zone room shows. A link to an event always wins; else the user's choice; else albums. */ export const zoneView = (choice: ZoneView | undefined, eventId: string | undefined): ZoneView => - choice ?? (eventId ? 'timeline' : 'albums'); + eventId ? 'timeline' : choice ?? 'albums'; diff --git a/src/app/features/room/RoomView.tsx b/src/app/features/room/RoomView.tsx index b61f54de..0137fd95 100644 --- a/src/app/features/room/RoomView.tsx +++ b/src/app/features/room/RoomView.tsx @@ -117,7 +117,7 @@ export function RoomView({ eventId }: { eventId?: string }) { return ( - {isZone && ( + {isZone && !eventId && (