@estiva-app/interop 0.94.0 → 0.96.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.
- package/CHANGELOG.md +41 -0
- package/README.md +1 -0
- package/dist/activity.d.ts +13 -7
- package/dist/activity.d.ts.map +1 -1
- package/dist/activity.js +138 -104
- package/dist/activity.js.map +1 -1
- package/dist/folders.d.ts +22 -6
- package/dist/folders.d.ts.map +1 -1
- package/dist/folders.js +39 -12
- package/dist/folders.js.map +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -1
- package/dist/projection.d.ts +36 -0
- package/dist/projection.d.ts.map +1 -1
- package/dist/projection.js +95 -2
- package/dist/projection.js.map +1 -1
- package/dist/topics.d.ts +91 -0
- package/dist/topics.d.ts.map +1 -1
- package/dist/topics.js +173 -56
- package/dist/topics.js.map +1 -1
- package/dist/yourTopics.d.ts +35 -0
- package/dist/yourTopics.d.ts.map +1 -0
- package/dist/yourTopics.js +177 -0
- package/dist/yourTopics.js.map +1 -0
- package/package.json +1 -1
- package/src/activity.ts +133 -94
- package/src/folders.ts +46 -13
- package/src/index.ts +9 -0
- package/src/projection.ts +103 -2
- package/src/topics.ts +202 -51
- package/src/yourTopics.ts +239 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Your topics, lean: only the topics the viewer is in, newest activity first —
|
|
3
|
+
* what a sidebar draws (ALO-17), where {@link readTopics} reads every topic the
|
|
4
|
+
* viewer can open, the viewer's whole part in each and their text.
|
|
5
|
+
*
|
|
6
|
+
* The same answer as {@link readTopics}, the filter every app applies (in it,
|
|
7
|
+
* not archived, its own or with a parent, not muted, not a space) and
|
|
8
|
+
* {@link readTopicActivity} over what is left, for fewer requests and bytes:
|
|
9
|
+
*
|
|
10
|
+
* 1. every topic's root, and the viewer's mute list, in one request;
|
|
11
|
+
* 2. the changes naming the viewer on any topic and the team lists naming
|
|
12
|
+
* them (one request), beside every topic's current fields by the list read
|
|
13
|
+
* (another: a relay refuses a list read mixed with other filters) — each
|
|
14
|
+
* topic's title, archive and parent come from that answer, and only the
|
|
15
|
+
* topics the viewer is in are read further;
|
|
16
|
+
* 3. their listed ids, what the list read skipped and the manifests of their
|
|
17
|
+
* parents, in one request;
|
|
18
|
+
* 4. their parents, a level a request by the list read, to see whether one is
|
|
19
|
+
* archived, while their activity is read.
|
|
20
|
+
*
|
|
21
|
+
* Without the list read (the relay does not advertise it), round 2 reads each
|
|
22
|
+
* topic's changes plain, as {@link readTopics} does. A topic the list read
|
|
23
|
+
* does not answer (deleted) is read plain, as a parent is.
|
|
24
|
+
*/
|
|
25
|
+
import { mutedFilter, parseMutedBlob, MUTED_D_TAG } from '@estiva-app/conversation'
|
|
26
|
+
import { FILE_KINDS, type SignedEvent } from '@estiva-app/protocol'
|
|
27
|
+
import {
|
|
28
|
+
addressPointerOf,
|
|
29
|
+
createProjectionCache,
|
|
30
|
+
isArchived,
|
|
31
|
+
markTopicsArchivedWith,
|
|
32
|
+
projectTopics,
|
|
33
|
+
seedManifests,
|
|
34
|
+
type ForeignObject,
|
|
35
|
+
type ProjectionCache,
|
|
36
|
+
type QueryFn,
|
|
37
|
+
} from './projection.js'
|
|
38
|
+
import {
|
|
39
|
+
answeredBy,
|
|
40
|
+
changesNamingFilter,
|
|
41
|
+
eventsByTopic,
|
|
42
|
+
listReadPlan,
|
|
43
|
+
listsFrom,
|
|
44
|
+
newestChanges,
|
|
45
|
+
readAll,
|
|
46
|
+
readTopicOf,
|
|
47
|
+
resolveIds,
|
|
48
|
+
spacesOf,
|
|
49
|
+
teamListFilters,
|
|
50
|
+
topicListFilter,
|
|
51
|
+
topicRoots,
|
|
52
|
+
type ListReadOptions,
|
|
53
|
+
type ReadTopic,
|
|
54
|
+
} from './topics.js'
|
|
55
|
+
import { activityTopics, byActivity, readTopicActivity } from './activity.js'
|
|
56
|
+
|
|
57
|
+
const KIND_TOPIC = 30840
|
|
58
|
+
const KIND_CHANGE = 1851
|
|
59
|
+
const KIND_APP_DATA = 30078
|
|
60
|
+
/** As {@link resolveForeignRoots} reads a file's changes when it reads them plain. */
|
|
61
|
+
const PLAIN_CHANGES_LIMIT = 500
|
|
62
|
+
|
|
63
|
+
/** {@link readYourTopics}' answer. */
|
|
64
|
+
export interface YourTopicsRead {
|
|
65
|
+
me: string
|
|
66
|
+
/**
|
|
67
|
+
* The viewer's topics: in it (`following.in`), not archived (its own, or
|
|
68
|
+
* with a parent), not muted, and not a space unless `spaces` was asked.
|
|
69
|
+
* Newest activity first ({@link byActivity} over `at`). Each as
|
|
70
|
+
* {@link readTopics} reads it, its `object` without its text when read by
|
|
71
|
+
* the list read and without `openUrl`.
|
|
72
|
+
*/
|
|
73
|
+
topics: ReadTopic[]
|
|
74
|
+
/** {@link readTopicActivity}'s `at` for exactly these topics. */
|
|
75
|
+
at: Map<string, number>
|
|
76
|
+
/** The viewer's muted keys, from their mute list; empty with none; `undefined` when it would not decrypt (then nothing is left out as muted). */
|
|
77
|
+
muted: ReadonlySet<string> | undefined
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The viewer's topics, newest activity first, in about ten requests on a
|
|
82
|
+
* workspace like Miky's (53 topics, 12 his): see the module's rounds.
|
|
83
|
+
*
|
|
84
|
+
* `listRead`, where the relay advertises the list read (NIP-11
|
|
85
|
+
* `file_list.newest`): the topics and their parents are read as their
|
|
86
|
+
* current fields (§6.13); `readTopics(query, me, { listRead, parentsListRead:
|
|
87
|
+
* listRead })` reads the same. `decrypt` opens the mute list (NIP-44 to
|
|
88
|
+
* self); `cache` keeps the parents' manifests between reads.
|
|
89
|
+
*/
|
|
90
|
+
export async function readYourTopics(
|
|
91
|
+
query: QueryFn,
|
|
92
|
+
me: string,
|
|
93
|
+
options: ListReadOptions & {
|
|
94
|
+
decrypt: (ciphertext: string) => Promise<string | undefined>
|
|
95
|
+
spaces?: boolean
|
|
96
|
+
cache?: ProjectionCache
|
|
97
|
+
nowSeconds?: number
|
|
98
|
+
},
|
|
99
|
+
): Promise<YourTopicsRead> {
|
|
100
|
+
const listRead = options.listRead ?? false
|
|
101
|
+
const cache = options.cache ?? createProjectionCache()
|
|
102
|
+
|
|
103
|
+
// 1. Every topic's root, and the mute list.
|
|
104
|
+
const first = gathered(query)
|
|
105
|
+
const rootsRead = readAll(first.query, [{ kinds: [KIND_TOPIC] }])
|
|
106
|
+
const mutedOpened = first.query([mutedFilter(me)]).then((events) => mutedOf(events, me, options.decrypt))
|
|
107
|
+
// Its failure is the roots' too, and said by them.
|
|
108
|
+
mutedOpened.catch(() => {})
|
|
109
|
+
first.send()
|
|
110
|
+
const roots = topicRoots(await rootsRead)
|
|
111
|
+
const addresses = [...roots.keys()]
|
|
112
|
+
if (addresses.length === 0) return { me, topics: [], at: new Map(), muted: await mutedOpened }
|
|
113
|
+
|
|
114
|
+
// 2. How the viewer is in each, which are spaces, and each topic's fields.
|
|
115
|
+
const second = gathered(query)
|
|
116
|
+
const partRead = readAll(second.query, [changesNamingFilter(me, addresses)])
|
|
117
|
+
const statesRead = readAll(second.query, teamListFilters(roots))
|
|
118
|
+
// Without the list read: each topic's changes as `resolveForeignRoots` and `topicLists` read them.
|
|
119
|
+
const objectChangesRead = listRead ? Promise.resolve([]) : second.query(addresses.map(plainChangesFilter))
|
|
120
|
+
const listChangesRead = listRead ? Promise.resolve([]) : readAll(second.query, addresses.map(topicListFilter))
|
|
121
|
+
second.send()
|
|
122
|
+
const [part, states, listed, objectChanges, listChanges, muted] = await Promise.all([
|
|
123
|
+
partRead,
|
|
124
|
+
statesRead,
|
|
125
|
+
listRead ? newestChanges(query, addresses) : Promise.resolve([]),
|
|
126
|
+
objectChangesRead,
|
|
127
|
+
listChangesRead,
|
|
128
|
+
mutedOpened,
|
|
129
|
+
])
|
|
130
|
+
const spaces = spacesOf(states, addresses)
|
|
131
|
+
const byTopic = eventsByTopic(part)
|
|
132
|
+
const facts = { roots, byTopic, states, spaces }
|
|
133
|
+
const mine = (object: ForeignObject) => {
|
|
134
|
+
const topic = readTopicOf(object, me, facts)
|
|
135
|
+
return topic && topic.yours && (options.spaces || !topic.space) && !muted?.has(topic.address) ? topic : undefined
|
|
136
|
+
}
|
|
137
|
+
// A topic the list read did not answer (deleted) is read plain below, as a parent is.
|
|
138
|
+
const answered = new Set(topicRoots(listed).keys())
|
|
139
|
+
const byList = listRead ? addresses.filter((address) => answered.has(address)) : addresses
|
|
140
|
+
const objects = projectTopics([...byList.map((address) => roots.get(address)!), ...(listRead ? listed : objectChanges)], listRead)
|
|
141
|
+
if (listRead) {
|
|
142
|
+
const unanswered = addresses.filter((address) => !answered.has(address))
|
|
143
|
+
// Asked about only when one would be the viewer's, by its root alone: whether they are in it does not depend on its fields.
|
|
144
|
+
const maybe = projectTopics(unanswered.map((address) => roots.get(address)!), false).flatMap((object) => (mine(object) ? [object.address!] : []))
|
|
145
|
+
if (maybe.length > 0) objects.push(...projectTopics([...maybe.map((address) => roots.get(address)!), ...(await query(maybe.map(plainChangesFilter)))], false))
|
|
146
|
+
}
|
|
147
|
+
const candidates: Omit<ReadTopic, 'listed'>[] = []
|
|
148
|
+
for (const object of objects) {
|
|
149
|
+
const topic = mine(object)
|
|
150
|
+
if (topic && !isArchived(topic.object)) candidates.push(topic)
|
|
151
|
+
}
|
|
152
|
+
if (candidates.length === 0) return { me, topics: [], at: new Map(), muted }
|
|
153
|
+
|
|
154
|
+
// 3. Their listed ids, what the list read skipped, and their parents' manifests.
|
|
155
|
+
const asked = candidates.map((topic) => topic.address)
|
|
156
|
+
const listEvents = listRead ? listed : listChanges
|
|
157
|
+
const plan = listReadPlan(asked, roots, listEvents, listRead)
|
|
158
|
+
const parents = candidates.flatMap((topic) => {
|
|
159
|
+
const parent = topic.object.parentRef ? addressPointerOf(topic.object.parentRef) : null
|
|
160
|
+
return parent ? [parent] : []
|
|
161
|
+
})
|
|
162
|
+
const third = gathered(query)
|
|
163
|
+
const resolvedRead = plan.ids.length ? resolveIds(third.query, plan.ids, options.fileKinds) : Promise.resolve(new Map())
|
|
164
|
+
const againRead = readAll(third.query, plan.again)
|
|
165
|
+
const seeded = seedManifests(parents, third.query, cache)
|
|
166
|
+
third.send()
|
|
167
|
+
const [resolved, again] = await Promise.all([resolvedRead, againRead, seeded])
|
|
168
|
+
const lists = listsFrom(asked, roots, [...listEvents, ...again], resolved)
|
|
169
|
+
const topics: ReadTopic[] = candidates.map((topic) => ({ ...topic, listed: lists.get(topic.address) ?? [] }))
|
|
170
|
+
|
|
171
|
+
// 4. Their parents, while their activity is read: one archived with a parent is left out after.
|
|
172
|
+
const [activity] = await Promise.all([
|
|
173
|
+
readTopicActivity(query, activityTopics(topics), { nowSeconds: options.nowSeconds }),
|
|
174
|
+
markTopicsArchivedWith(
|
|
175
|
+
topics.map((topic) => topic.object),
|
|
176
|
+
query,
|
|
177
|
+
cache,
|
|
178
|
+
listRead ? FILE_KINDS : [],
|
|
179
|
+
),
|
|
180
|
+
])
|
|
181
|
+
const open = topics.filter((topic) => !isArchived(topic.object))
|
|
182
|
+
const newest = byActivity(activity.at)
|
|
183
|
+
open.sort((a, b) => newest(a.address, b.address))
|
|
184
|
+
return { me, topics: open, at: new Map(open.map((topic) => [topic.address, activity.at.get(topic.address) ?? 0])), muted }
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** A topic's changes as {@link resolveForeignRoots} reads them plain: the newest 500 naming it by `a`. */
|
|
188
|
+
function plainChangesFilter(address: string): Record<string, unknown> {
|
|
189
|
+
return { kinds: [KIND_CHANGE], '#a': [address], limit: PLAIN_CHANGES_LIMIT }
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** The viewer's muted keys from a read of their mute list: none without one, `undefined` when it does not open. */
|
|
193
|
+
async function mutedOf(
|
|
194
|
+
events: readonly SignedEvent[],
|
|
195
|
+
me: string,
|
|
196
|
+
decrypt: (ciphertext: string) => Promise<string | undefined>,
|
|
197
|
+
): Promise<ReadonlySet<string> | undefined> {
|
|
198
|
+
const list = events
|
|
199
|
+
.filter((e) => e.pubkey === me && e.kind === KIND_APP_DATA && e.tags.some((t) => t[0] === 'd' && t[1] === MUTED_D_TAG))
|
|
200
|
+
.sort((a, b) => b.created_at - a.created_at)[0]
|
|
201
|
+
if (!list) return new Set()
|
|
202
|
+
const parsed = parseMutedBlob((await decrypt(list.content).catch(() => undefined)) ?? '')
|
|
203
|
+
return parsed ? new Set(parsed.keys) : undefined
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* A query that holds every request made through it until {@link send}, then
|
|
208
|
+
* asks them all as one request and hands each caller what its own filters
|
|
209
|
+
* would have been answered alone ({@link answeredBy}). A request made after
|
|
210
|
+
* `send` (a later page), and a list read, which a relay takes only alone, go
|
|
211
|
+
* straight through.
|
|
212
|
+
*/
|
|
213
|
+
function gathered(query: QueryFn): { query: QueryFn; send(): void } {
|
|
214
|
+
let held: { filters: Record<string, unknown>[]; resolve: (events: SignedEvent[]) => void; reject: (error: unknown) => void }[] | null = []
|
|
215
|
+
return {
|
|
216
|
+
query: (filters) => {
|
|
217
|
+
if (!held || filters.some((filter) => 'fields' in filter)) return query(filters)
|
|
218
|
+
const asks = held
|
|
219
|
+
return new Promise((resolve, reject) => asks.push({ filters, resolve, reject }))
|
|
220
|
+
},
|
|
221
|
+
send() {
|
|
222
|
+
const asks = held ?? []
|
|
223
|
+
held = null
|
|
224
|
+
if (asks.length === 0) return
|
|
225
|
+
query(asks.flatMap((ask) => ask.filters)).then(
|
|
226
|
+
(events) => {
|
|
227
|
+
for (const ask of asks) {
|
|
228
|
+
const page = new Map<string, SignedEvent>()
|
|
229
|
+
for (const filter of ask.filters) for (const event of answeredBy(events, filter)) page.set(event.id, event)
|
|
230
|
+
ask.resolve([...page.values()])
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
(error: unknown) => {
|
|
234
|
+
for (const ask of asks) ask.reject(error)
|
|
235
|
+
},
|
|
236
|
+
)
|
|
237
|
+
},
|
|
238
|
+
}
|
|
239
|
+
}
|