@estiva-app/interop 0.108.1 → 0.109.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30,6 +30,7 @@ export type FileLook = 'row' | 'card'
30
30
 
31
31
  const ORDER = 'order:'
32
32
  const LOOK = 'look:'
33
+ const LISTS = 'lists:'
33
34
  /** The field a file's place on a topic is kept in: `order:<d>`. */
34
35
  export const orderField = (file: string): string => `${ORDER}${file}`
35
36
  /** The field a file's look on a topic is kept in: `look:<d>`. */
@@ -107,7 +108,16 @@ export interface FileArrangement {
107
108
  * on the relay, one a reader skips included (the relay chains them all),
108
109
  * or the topic's first record when the field never changed.
109
110
  */
110
- prev: { order: string; look: string }
111
+ prev: {
112
+ order: string
113
+ look: string
114
+ /**
115
+ * Its entry on the list, `lists:<d>`: what taking it off follows where
116
+ * the relay keeps topics in order (`planRemoveFromTopic`'s `prev`).
117
+ * Optional only so an arrangement built by hand still type-checks.
118
+ */
119
+ lists?: string
120
+ }
111
121
  /** The latest time (ms) among those, so a change is stamped after it. */
112
122
  prevAt: number
113
123
  }
@@ -166,12 +176,14 @@ export function arrangementHeads(
166
176
  const list = tagOf(event, 'a')
167
177
  if (!list) continue
168
178
  for (const { field } of fieldsSetBy(event)) {
169
- if (!isArrangeField(field)) continue
179
+ const isEntry = field.startsWith(LISTS)
180
+ if (!isArrangeField(field) && !isEntry) continue
170
181
  const heads = out.get(list) ?? { good: new Map(), any: new Map() }
171
182
  out.set(list, heads)
172
183
  const held = heads.any.get(field)
173
184
  if (!held || byOrder(held, event) < 0) heads.any.set(field, event)
174
- if (!readsAs(event, field, list, teamOfList(list))) continue
185
+ // An entry is folded by `listEntries`; here only its head is kept, as the next `prev`.
186
+ if (isEntry || !readsAs(event, field, list, teamOfList(list))) continue
175
187
  const good = heads.good.get(field)
176
188
  if (!good || byOrder(good, event) < 0) heads.good.set(field, event)
177
189
  }
@@ -193,10 +205,12 @@ export function arrangementOf(heads: Heads | undefined, file: string, listed: Si
193
205
  const look = looked ? valueIn(looked, lookField(file)) : undefined
194
206
  const orderHead = heads?.any.get(orderField(file))
195
207
  const lookHead = heads?.any.get(lookField(file))
208
+ // The relay chains every entry, one a reader skips included; `listed` is the newest one that counts.
209
+ const listsHead = heads?.any.get(`${LISTS}${file}`) ?? listed
196
210
  return {
197
211
  ...(isPlaceKey(key) ? { order: key } : {}),
198
212
  ...(look === 'row' || look === 'card' ? { look } : {}),
199
- prev: { order: orderHead?.id ?? root.id, look: lookHead?.id ?? root.id },
213
+ prev: { order: orderHead?.id ?? root.id, look: lookHead?.id ?? root.id, lists: listsHead.id },
200
214
  prevAt: Math.max(changedAt(root), orderHead ? changedAt(orderHead) : 0, lookHead ? changedAt(lookHead) : 0),
201
215
  }
202
216
  }
@@ -322,15 +336,20 @@ export function planLookInTopic(
322
336
  * over the list read again, so a refusal plans the move again rather than
323
337
  * leaving the file back but last. The move is stamped after the listing, so
324
338
  * its place is not older than it.
339
+ *
340
+ * `listsPrev`, where the relay keeps topics in order, is the change the
341
+ * listing follows: the Remove it undoes (the id the relay accepted), not the
342
+ * file's `prev.lists`, which was read before it.
325
343
  */
326
344
  export function planPutBackInTopic(
327
345
  pubkey: string,
328
346
  createdAtMs: number,
329
- args: { topic: ArrangeHolder; file: ArrangedFile; after: string | null },
347
+ args: { topic: ArrangeHolder; file: ArrangedFile; after: string | null; listsPrev?: string },
330
348
  ): { relist: UnsignedEvent; move: (files: readonly ArrangedFile[], createdAtMs: number) => UnsignedEvent | null } {
331
349
  const listedAt = stamp(createdAtMs, [args.file])
332
350
  const holder = ADDRESS.exec(args.topic.address)
333
351
  if (!holder) throw new Error(`not a file address: ${JSON.stringify(args.topic.address)}`)
352
+ if (args.listsPrev !== undefined && !/^[0-9a-f]{64}$/.test(args.listsPrev)) throw new Error(`not an event id: ${JSON.stringify(args.listsPrev)}`)
334
353
  const relist: UnsignedEvent = {
335
354
  pubkey,
336
355
  created_at: Math.floor(listedAt / 1000),
@@ -342,11 +361,12 @@ export function planPutBackInTopic(
342
361
  ['value', 'true'],
343
362
  ['h', args.topic.folder],
344
363
  ['ts', String(listedAt)],
364
+ ...(args.listsPrev ? [['prev', args.listsPrev]] : []),
345
365
  ['o', args.file.file],
346
366
  ],
347
367
  content: '',
348
368
  }
349
- const move = (files: readonly ArrangedFile[], ms: number) => {
369
+ const move =(files: readonly ArrangedFile[], ms: number) => {
350
370
  // Back on the list it has no place yet; a list read before the listing landed does not hold it.
351
371
  const held = files.find((f) => f.file === args.file.file)
352
372
  const back: ArrangedFile = { ...(held ?? args.file), order: undefined, prevAt: Math.max((held ?? args.file).prevAt, listedAt) }
package/src/topics.ts CHANGED
@@ -5,8 +5,9 @@
5
5
  * A topic lists files from any Folder, one entry per file, by the file's id.
6
6
  * An entry is a `kind:1851` change on the topic: `a` and `O` name the topic,
7
7
  * `field` is `lists:<d>`, `value` is `true` or `false`, `h` is the topic's
8
- * Folder, then `ts`, then `["o", <d>]` naming the listed file. The newest
9
- * entry per topic and file counts (§6.3).
8
+ * Folder, then `ts`, then — where the relay keeps topics in order — `prev`,
9
+ * the change it follows (§6.13), then `["o", <d>]` naming the listed file.
10
+ * The newest entry per topic and file counts (§6.3).
10
11
  *
11
12
  * `O` is the file the change is about and `o` another file it points at
12
13
  * (why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-change-is-about-its-file-as-a-comment-is.md),
@@ -84,10 +85,27 @@ export interface ListHolder {
84
85
  folder: string
85
86
  }
86
87
 
88
+ /**
89
+ * What a list entry is handed. `prev` is the change it follows (§6.13): the
90
+ * newest `lists:<d>` change on the topic for that file, or the topic's first
91
+ * record when the file was never on it — a listed file's `prev.lists` from
92
+ * {@link topicList}, or `prevsAfterSwitch(query, topic, [listsField(file)])`.
93
+ * Pass it where the relay keeps topics in order (`fileFieldsOf(reads,
94
+ * TOPIC_TYPE, …)`), and send through `writeChange` so a stale one is followed
95
+ * again; leave it out elsewhere, and the entry is written as before. Two
96
+ * people adding different files never clash: each file is its own field.
97
+ */
98
+ export interface ListEntryArgs {
99
+ topic: ListHolder
100
+ file: string
101
+ note?: string
102
+ prev?: string
103
+ }
104
+
87
105
  function entry(
88
106
  pubkey: string,
89
107
  createdAtMs: number,
90
- args: { list: ListHolder; file: string; listed: boolean; note?: string },
108
+ args: { list: ListHolder; file: string; listed: boolean; note?: string; prev?: string },
91
109
  ): UnsignedEvent {
92
110
  const holder = ADDRESS.exec(args.list.address)
93
111
  if (!holder) throw new Error(`not a file address: ${JSON.stringify(args.list.address)}`)
@@ -96,6 +114,7 @@ function entry(
96
114
  // A reader ignores a file on its own list (§6.14): refuse it here rather than publish an entry that shows nothing.
97
115
  if (args.file === holder[3]) throw new Error('a file is not on its own list')
98
116
  if (!Number.isSafeInteger(createdAtMs) || createdAtMs < 0) throw new Error(`not epoch milliseconds: ${createdAtMs}`)
117
+ if (args.prev !== undefined && !/^[0-9a-f]{64}$/.test(args.prev)) throw new Error(`not an event id: ${JSON.stringify(args.prev)}`)
99
118
  return {
100
119
  pubkey,
101
120
  created_at: Math.floor(createdAtMs / 1000),
@@ -107,6 +126,8 @@ function entry(
107
126
  ['value', args.listed ? 'true' : 'false'],
108
127
  ['h', args.list.folder],
109
128
  ['ts', String(createdAtMs)],
129
+ // Where `order:` and `look:` carry theirs (§6.14).
130
+ ...(args.prev ? [['prev', args.prev]] : []),
110
131
  ['o', args.file],
111
132
  ],
112
133
  // Empty unless the person wrote a note: each app says "added" and "removed" in its own words (§6.14).
@@ -118,21 +139,13 @@ function entry(
118
139
  * Put a file on a topic's list: `[kind:1851 lists:<d> = true]`, signed by
119
140
  * whoever adds it. `file` is the listed file's id (its `d`), from any Folder.
120
141
  */
121
- export function planListInTopic(
122
- pubkey: string,
123
- createdAtMs: number,
124
- args: { topic: ListHolder; file: string; note?: string },
125
- ): UnsignedEvent[] {
126
- return [entry(pubkey, createdAtMs, { list: args.topic, file: args.file, listed: true, note: args.note })]
142
+ export function planListInTopic(pubkey: string, createdAtMs: number, args: ListEntryArgs): UnsignedEvent[] {
143
+ return [entry(pubkey, createdAtMs, { list: args.topic, file: args.file, listed: true, note: args.note, prev: args.prev })]
127
144
  }
128
145
 
129
146
  /** Take a file off a topic's list: `[kind:1851 lists:<d> = false]`. {@link planListInTopic} in reverse. */
130
- export function planRemoveFromTopic(
131
- pubkey: string,
132
- createdAtMs: number,
133
- args: { topic: ListHolder; file: string; note?: string },
134
- ): UnsignedEvent[] {
135
- return [entry(pubkey, createdAtMs, { list: args.topic, file: args.file, listed: false, note: args.note })]
147
+ export function planRemoveFromTopic(pubkey: string, createdAtMs: number, args: ListEntryArgs): UnsignedEvent[] {
148
+ return [entry(pubkey, createdAtMs, { list: args.topic, file: args.file, listed: false, note: args.note, prev: args.prev })]
136
149
  }
137
150
 
138
151
  /** Which topics list a file: every entry naming its id in `o`. Fold with {@link listEntries}. */