Albums: event link beats an earlier albums choice; buildAlbums tolerates bad input
All checks were successful
Build pull request / Build pull request (pull_request) Successful in 3m41s

- 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 <noreply@anthropic.com>
This commit is contained in:
Void 2026-11-10 17:26:21 +00:00
commit dc1ea7af27
5 changed files with 77 additions and 11 deletions

View file

@ -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);
});
});

View file

@ -47,11 +47,17 @@ const KIND_OF_MSGTYPE = new Map<string, FileKind>([
const live = (
e: ZoneEvent
): e is ZoneEvent & { event_id: string; content: Record<string, unknown> } =>
!!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, unknown> }): string => {
const relations = root.unsigned?.['m.relations'] as Record<string, unknown> | undefined;
const edit = relations?.['m.replace'] as ZoneEvent | undefined;
const edited = (edit?.content?.['m.new_content'] as Record<string, unknown> | 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<string>();
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,

View file

@ -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');
});
});

View file

@ -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';

View file

@ -117,7 +117,7 @@ export function RoomView({ eventId }: { eventId?: string }) {
return (
<Page ref={roomViewRef}>
<Box grow="Yes" direction="Column">
{isZone && (
{isZone && !eventId && (
<button
type="button"
className={albumCss.Back}