Files desk cut 3: show the latest version of a file chain, earlier versions folded under it
A file event naming org.gamecult.publish.replaces supersedes the event it names. foldVersions reads the chain from the events alone; buildAlbums lists heads (count too) and AlbumView unfolds 'N earlier versions'. Both events stay ordinary files for other clients. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
143bcc2dc6
commit
ac6018b57c
5 changed files with 154 additions and 4 deletions
|
|
@ -68,6 +68,26 @@ function FileRow({ room, file, author }: FileRowProps) {
|
|||
);
|
||||
}
|
||||
|
||||
/** A file, and under it a row that unfolds the versions it supersedes. */
|
||||
function VersionedFileRow({ room, file, author }: FileRowProps) {
|
||||
const [shown, setShown] = useState(false);
|
||||
const n = file.earlier.length;
|
||||
return (
|
||||
<>
|
||||
<FileRow room={room} file={file} author={author} />
|
||||
{n > 0 && (
|
||||
<button type="button" className={css.Row} onClick={() => setShown(!shown)}>
|
||||
<Text size="T200" className={css.Dim}>
|
||||
{`${shown ? '▾' : '▸'} ${n} earlier ${n === 1 ? 'version' : 'versions'}`}
|
||||
</Text>
|
||||
</button>
|
||||
)}
|
||||
{shown &&
|
||||
file.earlier.map((e) => <FileRow key={e.id} room={room} file={e} author={author} />)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
const countLabel = (n: number) => `${n} ${n === 1 ? 'file' : 'files'}`;
|
||||
|
||||
/** The zone never mounts RoomTimeline, so it sends the read receipt the timeline would. */
|
||||
|
|
@ -124,7 +144,7 @@ export function AlbumView({ room, onTimeline }: AlbumViewProps) {
|
|||
{open.count === 0 && <Text>This album has no files.</Text>}
|
||||
<div className={images ? `${css.Files} ${css.FilesWide}` : css.Files}>
|
||||
{open.files.map((f) => (
|
||||
<FileRow key={f.id} room={room} file={f} author={authorOf(open)} />
|
||||
<VersionedFileRow key={f.id} room={room} file={f} author={authorOf(open)} />
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
|
|
|
|||
|
|
@ -215,6 +215,20 @@ describe('buildAlbums', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('versions', () => {
|
||||
const v = (id: string, ts: number, replaces?: string) =>
|
||||
reply(id, ts, 'm.file', id, replaces ? { 'org.gamecult.publish': { replaces } } : {});
|
||||
|
||||
it('lists the latest of a chain and folds the earlier versions under it', () => {
|
||||
const a = one(root('$r', 1), [v('$v1', 10), v('$v2', 20, '$v1'), reply('$x', 30, 'm.file')]);
|
||||
expect(a.files.map((f) => [f.id, f.earlier.map((e) => e.id)])).toEqual([
|
||||
['$v2', ['$v1']],
|
||||
['$x', []],
|
||||
]);
|
||||
expect(a.count).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Mind receipts', () => {
|
||||
const HASH = 'a'.repeat(64);
|
||||
const file = (id: string, sha?: string) =>
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
// into albums. See docs/file-zone.md for the rulings this encodes.
|
||||
import { readersOf } from './fileReceipt';
|
||||
import type { FileReader } from './fileReceipt';
|
||||
import { foldVersions } from './versions';
|
||||
|
||||
/** The slice of a Matrix event this module reads. Every field may be missing. */
|
||||
export type ZoneEvent = {
|
||||
|
|
@ -27,6 +28,8 @@ export type AlbumFile = {
|
|||
content: Record<string, unknown>;
|
||||
/** Receipts in this album that name this file, one per sender (the latest), oldest first. */
|
||||
readBy: FileReader[];
|
||||
/** The versions this file supersedes, nearest first (`org.gamecult.publish.replaces`). */
|
||||
earlier: AlbumFile[];
|
||||
};
|
||||
|
||||
export type Album = {
|
||||
|
|
@ -75,6 +78,7 @@ const toFile = (e: ZoneEvent): AlbumFile | undefined => {
|
|||
ts: tsOf(e.origin_server_ts),
|
||||
content: e.content,
|
||||
readBy: [],
|
||||
earlier: [],
|
||||
};
|
||||
};
|
||||
|
||||
|
|
@ -103,14 +107,15 @@ const toAlbum = (input: AlbumInput, server: string): Album | undefined => {
|
|||
})
|
||||
.sort((a, b) => a.ts - b.ts)
|
||||
.map((f) => ({ ...f, readBy: readersOf(list, f.id, f.content, server) }));
|
||||
const heads = foldVersions(files);
|
||||
return {
|
||||
id: root.event_id,
|
||||
title: titleOf(root),
|
||||
author: root.sender ?? '',
|
||||
ts: tsOf(root.origin_server_ts),
|
||||
cover: files.find((f) => f.kind === 'image'),
|
||||
files,
|
||||
count: files.length,
|
||||
cover: heads.find((f) => f.kind === 'image'),
|
||||
files: heads,
|
||||
count: heads.length,
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
|||
65
src/app/features/albums/versions.test.ts
Normal file
65
src/app/features/albums/versions.test.ts
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import { describe, expect, it } from 'vitest';
|
||||
import { PUBLISH_KEY, foldVersions, replacesOf } from './versions';
|
||||
|
||||
type F = { id: string; content: Record<string, unknown>; earlier: F[] };
|
||||
const f = (id: string, replaces?: string): F => ({
|
||||
id,
|
||||
content: replaces ? { [PUBLISH_KEY]: { replaces } } : {},
|
||||
earlier: [],
|
||||
});
|
||||
const shape = (files: F[]): unknown => files.map((x) => [x.id, x.earlier.map((e) => e.id)]);
|
||||
|
||||
describe('replacesOf', () => {
|
||||
it('reads the id the publisher key names', () => {
|
||||
expect(replacesOf({ [PUBLISH_KEY]: { replaces: '$a' } })).toBe('$a');
|
||||
});
|
||||
it('anything else says nothing', () => {
|
||||
expect(replacesOf({})).toBeUndefined();
|
||||
expect(replacesOf({ [PUBLISH_KEY]: {} })).toBeUndefined();
|
||||
expect(replacesOf({ [PUBLISH_KEY]: { replaces: 3 } })).toBeUndefined();
|
||||
expect(replacesOf({ [PUBLISH_KEY]: { replaces: '' } })).toBeUndefined();
|
||||
expect(replacesOf({ [PUBLISH_KEY]: 'x' })).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('foldVersions', () => {
|
||||
it('files that replace nothing pass through in order, with nothing folded', () => {
|
||||
expect(shape(foldVersions([f('$a'), f('$b')]))).toEqual([
|
||||
['$a', []],
|
||||
['$b', []],
|
||||
]);
|
||||
});
|
||||
|
||||
it('shows the latest of a chain with earlier versions nearest first', () => {
|
||||
expect(shape(foldVersions([f('$a'), f('$b', '$a'), f('$c', '$b')]))).toEqual([
|
||||
['$c', ['$b', '$a']],
|
||||
]);
|
||||
});
|
||||
|
||||
it('keeps unrelated files beside a chain, in order', () => {
|
||||
expect(shape(foldVersions([f('$a'), f('$x'), f('$b', '$a')]))).toEqual([
|
||||
['$x', []],
|
||||
['$b', ['$a']],
|
||||
]);
|
||||
});
|
||||
|
||||
it('a replacement whose target is not in the list stands alone', () => {
|
||||
expect(shape(foldVersions([f('$b', '$gone')]))).toEqual([['$b', []]]);
|
||||
});
|
||||
|
||||
it('a file two others replace sits under both', () => {
|
||||
expect(shape(foldVersions([f('$a'), f('$b', '$a'), f('$c', '$a')]))).toEqual([
|
||||
['$b', ['$a']],
|
||||
['$c', ['$a']],
|
||||
]);
|
||||
});
|
||||
|
||||
it('a malformed loop shows no head and does not hang', () => {
|
||||
const out = foldVersions([f('$a', '$b'), f('$b', '$a'), f('$s', '$s')]);
|
||||
expect(out.map((x) => x.id)).toEqual(['$s']);
|
||||
});
|
||||
|
||||
it('an empty list is empty', () => {
|
||||
expect(foldVersions([])).toEqual([]);
|
||||
});
|
||||
});
|
||||
46
src/app/features/albums/versions.ts
Normal file
46
src/app/features/albums/versions.ts
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
// Pure: a file event may name the event it supersedes in `org.gamecult.publish.replaces` (the
|
||||
// publisher writes it on a later final version; docs/commons-files-desk.md, "replaces, not
|
||||
// m.replace"). Both events stay ordinary files for every other client; this view shows the latest
|
||||
// of a chain with the earlier ones folded under it. A chain is read from the events alone.
|
||||
|
||||
/** Key the publisher writes its provenance under. */
|
||||
export const PUBLISH_KEY = 'org.gamecult.publish';
|
||||
|
||||
/** The event id a file says it replaces, if it says one. */
|
||||
export const replacesOf = (content: Record<string, unknown>): string | undefined => {
|
||||
const id = (content[PUBLISH_KEY] as { replaces?: unknown } | undefined)?.replaces;
|
||||
return typeof id === 'string' && id ? id : undefined;
|
||||
};
|
||||
|
||||
type Versioned<F> = F & { earlier: F[] };
|
||||
|
||||
/**
|
||||
* Files not replaced by another file in the list, oldest first as given, each carrying the files it
|
||||
* supersedes (nearest first) in `earlier`. A replacement whose target is missing from the list (a
|
||||
* redacted event, another thread) replaces nothing and stands as a head. A file two others claim to
|
||||
* replace sits under both. The walk refuses to revisit a file, so a malformed chain cannot loop (an event id hashes its content, so a real one cannot cycle).
|
||||
*/
|
||||
export const foldVersions = <F extends { id: string; content: Record<string, unknown> }>(
|
||||
files: Versioned<F>[]
|
||||
): Versioned<F>[] => {
|
||||
const byId = new Map(files.map((f) => [f.id, f]));
|
||||
const target = (f: F): F | undefined => {
|
||||
const id = replacesOf(f.content);
|
||||
return id && id !== f.id ? byId.get(id) : undefined;
|
||||
};
|
||||
const replaced = new Set<string>();
|
||||
files.forEach((f) => {
|
||||
const t = target(f);
|
||||
if (t) replaced.add(t.id);
|
||||
});
|
||||
const chainOf = (head: Versioned<F>): Versioned<F>[] => {
|
||||
const out: Versioned<F>[] = [];
|
||||
const seen = new Set([head.id]);
|
||||
for (let t = target(head); t && !seen.has(t.id); t = target(t)) {
|
||||
seen.add(t.id);
|
||||
out.push({ ...t, earlier: [] });
|
||||
}
|
||||
return out;
|
||||
};
|
||||
return files.filter((f) => !replaced.has(f.id)).map((h) => ({ ...h, earlier: chainOf(h) }));
|
||||
};
|
||||
Loading…
Reference in a new issue