@blogmcp/community 0.4.1 → 0.5.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.
Files changed (150) hide show
  1. package/dist/blogmcp-community-private.global.js +4 -0
  2. package/dist/blogmcp-community.global.js +3 -3
  3. package/dist/chunks/{mount-Ts_r2fwt.js → mount-SKmc4NgD.js} +39 -9
  4. package/dist/chunks/private-mount-bhCQeScD.js +5397 -0
  5. package/dist/core/attrs.d.ts +15 -0
  6. package/dist/core/settings.d.ts +5 -0
  7. package/dist/index.js +2 -2
  8. package/dist/private/core/dock.d.ts +38 -0
  9. package/dist/private/core/push.d.ts +49 -0
  10. package/dist/private/core/realtime.d.ts +57 -0
  11. package/dist/private/core/setup.d.ts +43 -0
  12. package/dist/private/core/view.d.ts +179 -0
  13. package/dist/private/dm/composer.d.ts +34 -0
  14. package/dist/private/dm/controller.d.ts +162 -0
  15. package/dist/private/dm/index.d.ts +14 -0
  16. package/dist/private/dm/links.d.ts +33 -0
  17. package/dist/private/dm/muted.d.ts +37 -0
  18. package/dist/private/dm/search.d.ts +7 -0
  19. package/dist/private/dm/text.d.ts +27 -0
  20. package/dist/private/dm/ui/DmBlocks.vue.d.ts +8 -0
  21. package/dist/private/dm/ui/DmCases.vue.d.ts +8 -0
  22. package/dist/private/dm/ui/DmComposer.vue.d.ts +20 -0
  23. package/dist/private/dm/ui/DmInbox.vue.d.ts +12 -0
  24. package/dist/private/dm/ui/DmLinkWarning.vue.d.ts +8 -0
  25. package/dist/private/dm/ui/DmLocalAids.vue.d.ts +8 -0
  26. package/dist/private/dm/ui/DmMessageText.vue.d.ts +8 -0
  27. package/dist/private/dm/ui/DmNew.vue.d.ts +12 -0
  28. package/dist/private/dm/ui/DmReport.vue.d.ts +8 -0
  29. package/dist/private/dm/ui/DmRequests.vue.d.ts +12 -0
  30. package/dist/private/dm/ui/DmScreens.vue.d.ts +9 -0
  31. package/dist/private/dm/ui/DmThread.vue.d.ts +9 -0
  32. package/dist/private/element.d.ts +23 -0
  33. package/dist/private/global.d.ts +10 -0
  34. package/dist/private/i18n.d.ts +41 -0
  35. package/dist/private/index.d.ts +32 -0
  36. package/dist/private/index.js +57 -0
  37. package/dist/private/mount.d.ts +55 -0
  38. package/dist/private/pages/api.d.ts +44 -0
  39. package/dist/private/pages/element.d.ts +6 -0
  40. package/dist/private/pages/fragment.d.ts +43 -0
  41. package/dist/private/pages/i18n.d.ts +25 -0
  42. package/dist/private/pages/index.d.ts +1 -0
  43. package/dist/private/pages/pages.d.ts +276 -0
  44. package/dist/private/pages/proof.d.ts +36 -0
  45. package/dist/private/pages/provider-view.d.ts +33 -0
  46. package/dist/private/pages/setup.d.ts +42 -0
  47. package/dist/private/pages/ui/AccountPageRoot.vue.d.ts +9 -0
  48. package/dist/private/pages/ui/PageActivation.vue.d.ts +9 -0
  49. package/dist/private/pages/ui/PageAddress.vue.d.ts +8 -0
  50. package/dist/private/pages/ui/PageDeviceApprove.vue.d.ts +8 -0
  51. package/dist/private/pages/ui/PageDevices.vue.d.ts +8 -0
  52. package/dist/private/pages/ui/PageExport.vue.d.ts +8 -0
  53. package/dist/private/pages/ui/PageMessages.vue.d.ts +9 -0
  54. package/dist/private/react.d.ts +12 -0
  55. package/dist/private/react.js +36 -0
  56. package/dist/private/ui/ChatDockRoot.vue.d.ts +20 -0
  57. package/dist/private/ui/LegalText.vue.d.ts +13 -0
  58. package/dist/private/ui/PrivateScreens.vue.d.ts +14 -0
  59. package/dist/private/ui/PrivateViewRoot.vue.d.ts +23 -0
  60. package/dist/private/ui/PromiseBlock.vue.d.ts +9 -0
  61. package/dist/private/ui/ProviderLine.vue.d.ts +8 -0
  62. package/dist/private/ui/ScreenActivation.vue.d.ts +8 -0
  63. package/dist/private/ui/ScreenAddress.vue.d.ts +8 -0
  64. package/dist/private/ui/ScreenApproval.vue.d.ts +8 -0
  65. package/dist/private/ui/ScreenDevices.vue.d.ts +8 -0
  66. package/dist/private/ui/ScreenHome.vue.d.ts +12 -0
  67. package/dist/private/ui/ScreenNotifications.vue.d.ts +8 -0
  68. package/dist/private/ui/ScreenReads.vue.d.ts +8 -0
  69. package/dist/private/ui/ScreenSettings.vue.d.ts +8 -0
  70. package/dist/private/ui/root.d.ts +35 -0
  71. package/dist/private/ui/text.d.ts +22 -0
  72. package/dist/private/vue.d.ts +88 -0
  73. package/dist/private/vue.js +72 -0
  74. package/dist/react.js +14 -14
  75. package/dist/vue.js +14 -14
  76. package/package.json +23 -6
  77. package/src/core/attrs.ts +19 -0
  78. package/src/core/settings.ts +55 -12
  79. package/src/private/core/dock.ts +105 -0
  80. package/src/private/core/push.ts +166 -0
  81. package/src/private/core/realtime.ts +109 -0
  82. package/src/private/core/setup.ts +75 -0
  83. package/src/private/core/view.ts +920 -0
  84. package/src/private/dm/composer.ts +74 -0
  85. package/src/private/dm/controller.ts +636 -0
  86. package/src/private/dm/index.ts +14 -0
  87. package/src/private/dm/links.ts +55 -0
  88. package/src/private/dm/muted.ts +160 -0
  89. package/src/private/dm/search.ts +18 -0
  90. package/src/private/dm/text.ts +60 -0
  91. package/src/private/dm/ui/DmBlocks.vue +27 -0
  92. package/src/private/dm/ui/DmCases.vue +58 -0
  93. package/src/private/dm/ui/DmComposer.vue +68 -0
  94. package/src/private/dm/ui/DmInbox.vue +34 -0
  95. package/src/private/dm/ui/DmLinkWarning.vue +27 -0
  96. package/src/private/dm/ui/DmLocalAids.vue +43 -0
  97. package/src/private/dm/ui/DmMessageText.vue +16 -0
  98. package/src/private/dm/ui/DmNew.vue +46 -0
  99. package/src/private/dm/ui/DmReport.vue +93 -0
  100. package/src/private/dm/ui/DmRequests.vue +50 -0
  101. package/src/private/dm/ui/DmScreens.vue +66 -0
  102. package/src/private/dm/ui/DmThread.vue +235 -0
  103. package/src/private/element.ts +90 -0
  104. package/src/private/global.ts +14 -0
  105. package/src/private/i18n.ts +127 -0
  106. package/src/private/index.ts +68 -0
  107. package/src/private/messages/cs.json +311 -0
  108. package/src/private/messages/en.json +319 -0
  109. package/src/private/mount.ts +103 -0
  110. package/src/private/pages/api.ts +96 -0
  111. package/src/private/pages/build.d.mts +2 -0
  112. package/src/private/pages/build.mjs +72 -0
  113. package/src/private/pages/element.ts +49 -0
  114. package/src/private/pages/fragment.ts +63 -0
  115. package/src/private/pages/i18n.ts +82 -0
  116. package/src/private/pages/index.ts +10 -0
  117. package/src/private/pages/messages/cs.json +97 -0
  118. package/src/private/pages/messages/en.json +97 -0
  119. package/src/private/pages/pages.ts +410 -0
  120. package/src/private/pages/proof.ts +110 -0
  121. package/src/private/pages/provider-view.ts +121 -0
  122. package/src/private/pages/setup.ts +79 -0
  123. package/src/private/pages/ui/AccountPageRoot.vue +45 -0
  124. package/src/private/pages/ui/PageActivation.vue +42 -0
  125. package/src/private/pages/ui/PageAddress.vue +39 -0
  126. package/src/private/pages/ui/PageDeviceApprove.vue +62 -0
  127. package/src/private/pages/ui/PageDevices.vue +84 -0
  128. package/src/private/pages/ui/PageExport.vue +35 -0
  129. package/src/private/pages/ui/PageMessages.vue +39 -0
  130. package/src/private/pages/ui/account.css +6 -0
  131. package/src/private/react.ts +49 -0
  132. package/src/private/shims.d.ts +6 -0
  133. package/src/private/ui/ChatDockRoot.vue +119 -0
  134. package/src/private/ui/LegalText.vue +18 -0
  135. package/src/private/ui/PrivateScreens.vue +61 -0
  136. package/src/private/ui/PrivateViewRoot.vue +70 -0
  137. package/src/private/ui/PromiseBlock.vue +18 -0
  138. package/src/private/ui/ProviderLine.vue +20 -0
  139. package/src/private/ui/ScreenActivation.vue +82 -0
  140. package/src/private/ui/ScreenAddress.vue +37 -0
  141. package/src/private/ui/ScreenApproval.vue +71 -0
  142. package/src/private/ui/ScreenDevices.vue +42 -0
  143. package/src/private/ui/ScreenHome.vue +29 -0
  144. package/src/private/ui/ScreenNotifications.vue +44 -0
  145. package/src/private/ui/ScreenReads.vue +23 -0
  146. package/src/private/ui/ScreenSettings.vue +44 -0
  147. package/src/private/ui/private.css +81 -0
  148. package/src/private/ui/root.ts +89 -0
  149. package/src/private/ui/text.ts +40 -0
  150. package/src/private/vue.ts +87 -0
@@ -0,0 +1,636 @@
1
+ /**
2
+ * The conversations' brain (m4-contracts.md §7.8 DM8b): one per private view (core/view.ts makes it once the view is
3
+ * ready), over the SDK's DM client (`createPrivateDmClient`, DM8a) — the inbox, the requests the member received, a
4
+ * new conversation by handle, the threads with their composer, the report flow with its preview, private blocks, the
5
+ * member's cases with their statements and appeals. Framework-free but for Vue's reactivity, as core/view.ts.
6
+ *
7
+ * **What stays on the device.** Drafts are the DM client's (`dm.drafts`, this tab's memory); the muted words are
8
+ * muted.ts's (this browser's IndexedDB); search is search.ts's (the loaded messages only). None of them reaches a
9
+ * request: the only calls are the DM client's operations, and their bodies are the member's acts — a message sent, an
10
+ * edit, a report. Decrypted messages are kept per open thread (`watch`) and dropped with its last window
11
+ * (`thread.close()`), so a closed conversation leaves no text in this page's memory.
12
+ *
13
+ * **The inbox events** (A.3's `m`, `e`, `d`, `t`, `rq`, `rd`, `resync`) go to `dm.apply`, which moves the open threads;
14
+ * only those that can change the list (`dmEventEffect` = `inbox`) re-read it, coalesced, and only `rq` and `resync`
15
+ * re-read the requests — typing and receipts read nothing (C.11 RF8-C (h)).
16
+ *
17
+ * **The conversations' notifications.** kb-api writes a private `dm_message` / `dm_request` row (§6.2) that the account's
18
+ * notification screen does not word: reading the thread is what reads them. kb-api marks them read with the cursor
19
+ * (`dm/read` answers how many, `notificationsRead`), so a read that read any tells the view (`notificationsRead`),
20
+ * which refreshes the unread dot — no list of the notifications, no mark-read of its own.
21
+ *
22
+ * Every refusal is kept as its code for the area it belongs to (`failures`) and worded by the screens (`dmErrorKey`).
23
+ */
24
+ import { markRaw, reactive } from 'vue'
25
+ import type { CaseCategory } from '@blogmcp/sdk/community'
26
+ import {
27
+ dmEventEffect,
28
+ type PrivateDmAudience,
29
+ type PrivateDmBlock,
30
+ type PrivateDmCase,
31
+ type PrivateDmClient,
32
+ type PrivateDmConversation,
33
+ type PrivateDmCounterpart,
34
+ type PrivateDmMessage,
35
+ type PrivateDmReportLine,
36
+ type PrivateDmReportPreview,
37
+ type PrivateDmRequest,
38
+ type PrivateDmStatement,
39
+ type PrivateDmThread,
40
+ type PrivateDmThreadState,
41
+ type PrivateOutcome,
42
+ type PrivatePromise,
43
+ } from '@blogmcp/sdk/community/private'
44
+ import { composerKeys, type DmComposerKey } from './composer'
45
+ import { cleanMutedWords, mutedWordOf, type MutedStore } from './muted'
46
+
47
+ /** The conversations' own screens inside the private view's `messages` screen. */
48
+ export const DM_SCREENS = ['inbox', 'conversation', 'new', 'requests', 'blocks', 'cases'] as const
49
+ export type DmScreen = (typeof DM_SCREENS)[number]
50
+
51
+ /** The areas a refusal is said in (one screen's refusal never shows in another). */
52
+ export type DmArea = 'inbox' | 'requests' | 'new' | 'thread' | 'report' | 'blocks' | 'cases' | 'statement' | 'appeal' | 'muted'
53
+
54
+ /** The rules line's categories (kb-api's `lineOf`: every other report category is the illegal line's). */
55
+ export const DM_RULES_CATEGORIES: readonly CaseCategory[] = Object.freeze(['rules', 'adverse_event', 'third_party_health_data', 'suspected_minor'])
56
+
57
+ /**
58
+ * The illegal line's categories: the SDK's `ESCALATION_CATEGORIES`, spelt out so that the private bundles need not load
59
+ * the public SDK entry for one list (test/private/dm/screens.spec.ts holds the two equal).
60
+ */
61
+ export const DM_ILLEGAL_CATEGORIES: readonly CaseCategory[] = Object.freeze([
62
+ 'fraud_spam',
63
+ 'harassment',
64
+ 'hate',
65
+ 'threats',
66
+ 'sexual_content',
67
+ 'child_at_risk',
68
+ 'self_harm',
69
+ 'doxxing',
70
+ 'copyright',
71
+ 'terrorism',
72
+ 'drugs',
73
+ 'other_illegal',
74
+ ])
75
+
76
+ /** The categories a line offers, in the SDK's order. */
77
+ export function categoriesOf(line: PrivateDmReportLine): readonly CaseCategory[] {
78
+ return line === 'rules' ? DM_RULES_CATEGORIES : DM_ILLEGAL_CATEGORIES
79
+ }
80
+
81
+ /** The one category whose report also points to the manufacturer's poradna (§7.5: „Nahlásit výrobci“). */
82
+ export const DM_ADVERSE_EVENT: CaseCategory = 'adverse_event'
83
+
84
+ /** The report in progress (§7.5): what is reported, on which line, what goes to whom, and the reporter's choices. */
85
+ export interface DmReportFlow {
86
+ /**
87
+ * A message of a conversation, or a request (`requests/block-report`: the request is the package). Both choose the
88
+ * line: block-report takes no line, kb-api derives it from the category (`lineOf`), so a request's illegal content
89
+ * reaches blogmcp's T&S only when the reporter can pick the illegal line's categories.
90
+ */
91
+ readonly kind: 'message' | 'request'
92
+ readonly conversationId: string
93
+ readonly seq: number | null
94
+ line: PrivateDmReportLine | null
95
+ category: CaseCategory | null
96
+ /** „Kdo to dostane“: the range and the recipients, read before the reporter sends. */
97
+ preview: PrivateDmReportPreview | null
98
+ /** Ticked by default (§7.5: the sender is blocked unless the reporter unticks it). */
99
+ block: boolean
100
+ note: string
101
+ done: { caseId: string } | null
102
+ }
103
+
104
+ export interface DmUi {
105
+ screen: DmScreen
106
+ /** The conversation the full view shows (`screen = 'conversation'`). */
107
+ current: string | null
108
+ inbox: PrivateDmConversation[] | null
109
+ inboxCursor: string | null
110
+ requests: PrivateDmRequest[] | null
111
+ requestsCursor: string | null
112
+ blocks: PrivateDmBlock[] | null
113
+ cases: PrivateDmCase[] | null
114
+ casesCursor: string | null
115
+ statement: PrivateDmStatement | null
116
+ appealDone: boolean
117
+ /** Each watched thread's state (the SDK thread's snapshot), by conversation id. */
118
+ threads: Record<string, PrivateDmThreadState>
119
+ /** The counterpart of a conversation, as the inbox or the requests named it (for a window's title). */
120
+ counterparts: Record<string, PrivateDmCounterpart>
121
+ /** The handle check of a new conversation. */
122
+ audience: { handle: string; canWrite: PrivateDmAudience['canWrite'] } | null
123
+ report: DmReportFlow | null
124
+ /** A link a member clicked in a message, waiting for the warning's „Pokračovat“. */
125
+ linkWarning: { href: string; host: string } | null
126
+ muted: string[]
127
+ mutedPersistent: boolean
128
+ /** Folded messages the member chose to show anyway (`<conversationId>:<seq>`), for this page only. */
129
+ unfolded: Record<string, true>
130
+ failures: Partial<Record<DmArea, string>>
131
+ busy: boolean
132
+ announcement: { msg: string; params?: Record<string, string | number> } | null
133
+ announceId: number
134
+ }
135
+
136
+ export interface DmViewController {
137
+ readonly state: DmUi
138
+ /** The words the composer shows for this member (composer.ts). */
139
+ composerKeys(): DmComposerKey[]
140
+ /** The website's rules (the provider line's `rules` link), for the reminder to the sender. */
141
+ rulesHref(): string | null
142
+ /** The site's poradna for „Nahlásit výrobci“ (§7.5), or null when the host named none. */
143
+ consultationPath(): string | null
144
+ /** The private surfaces are read-only (`read_only`): the history shows, the composer does not. */
145
+ readOnly(): boolean
146
+ readonly drafts: PrivateDmClient['drafts']
147
+ show(screen: DmScreen): Promise<void>
148
+ loadInbox(more?: boolean): Promise<void>
149
+ loadRequests(more?: boolean): Promise<void>
150
+ /** Watch a conversation's thread (a window or the full view): loads it; the returned function stops watching. */
151
+ watch(conversationId: string): () => void
152
+ openConversation(conversationId: string): Promise<void>
153
+ checkHandle(handle: string): Promise<void>
154
+ openHandle(handle: string): Promise<string | null>
155
+ send(conversationId: string, text: string, replyToSeq?: number | null): void
156
+ typing(conversationId: string): void
157
+ markRead(conversationId: string): void
158
+ loadOlder(conversationId: string): Promise<void>
159
+ edit(conversationId: string, m: PrivateDmMessage, text: string): Promise<boolean>
160
+ deleteForAll(conversationId: string, seq: number): Promise<boolean>
161
+ hide(conversationId: string, seq: number): Promise<boolean>
162
+ clear(conversationId: string): Promise<boolean>
163
+ retry(clientMsgId: string): void
164
+ discard(clientMsgId: string): void
165
+ acceptRequest(conversationId: string): Promise<boolean>
166
+ deleteRequest(conversationId: string): Promise<boolean>
167
+ block(memberId: string): Promise<boolean>
168
+ unblock(memberId: string): Promise<boolean>
169
+ startReport(kind: 'message' | 'request', conversationId: string, seq?: number): void
170
+ reportLine(line: PrivateDmReportLine): Promise<void>
171
+ reportCategory(category: CaseCategory): void
172
+ submitReport(): Promise<boolean>
173
+ cancelReport(): void
174
+ loadCases(more?: boolean): Promise<void>
175
+ openStatement(decisionId: string): Promise<void>
176
+ appeal(decisionId: string, text: string): Promise<boolean>
177
+ warnLink(link: { href: string; host: string }): void
178
+ dismissLink(): void
179
+ addMuted(word: string): Promise<void>
180
+ removeMuted(word: string): Promise<void>
181
+ unfold(conversationId: string, seq: number): void
182
+ /** One inbox event (core/view.ts hands every one over). */
183
+ apply(event: { readonly k: string; readonly c?: string; readonly s?: number; readonly r?: string }): void
184
+ /** The browser is online again: wake every waiting send. */
185
+ online(): void
186
+ dispose(): void
187
+ }
188
+
189
+ export interface DmViewOptions {
190
+ dm: PrivateDmClient
191
+ /** The matrix row for this member (core/view.ts `promiseFor`), for the composer sentence. */
192
+ promise: () => Pick<PrivatePromise, 'receivers' | 'keys' | 'delegated'> | null
193
+ rulesHref: () => string | null
194
+ consultationPath?: string | null
195
+ /** False when the member switched typing off (the private settings): no `dm/typing` is sent then. */
196
+ typingAllowed?: () => boolean
197
+ /** True while the private state is `read_only` (§5.11): kb-api refuses writes, so the composer is not offered. */
198
+ readOnly?: () => boolean
199
+ muted: MutedStore
200
+ /**
201
+ * A read of this conversation also read some of its private notifications (`dm_message`, `dm_request`; kb-api marks
202
+ * them with the cursor): the view refreshes its unread dot with its private client, which the DM controller does not
203
+ * hold.
204
+ */
205
+ notificationsRead?: (conversationId: string) => void
206
+ }
207
+
208
+ type Outcome<T> = PrivateOutcome<T> | { ok: false; code: string; status: number | null }
209
+
210
+ export function createDmView(o: DmViewOptions): DmViewController {
211
+ const dm = o.dm
212
+ let disposed = false
213
+ const state = reactive<DmUi>({
214
+ screen: 'inbox',
215
+ current: null,
216
+ inbox: null,
217
+ inboxCursor: null,
218
+ requests: null,
219
+ requestsCursor: null,
220
+ blocks: null,
221
+ cases: null,
222
+ casesCursor: null,
223
+ statement: null,
224
+ appealDone: false,
225
+ threads: {},
226
+ counterparts: {},
227
+ audience: null,
228
+ report: null,
229
+ linkWarning: null,
230
+ muted: [],
231
+ mutedPersistent: o.muted.persistent(),
232
+ unfolded: {},
233
+ failures: {},
234
+ busy: false,
235
+ announcement: null,
236
+ announceId: 0,
237
+ }) as DmUi
238
+
239
+ /** The open threads and how many windows watch each (the last one gone closes the thread). */
240
+ const open = new Map<string, { thread: PrivateDmThread; watchers: number; off: () => void }>()
241
+
242
+ const announce = (msg: string, params?: Record<string, string | number>): void => {
243
+ state.announcement = params ? { msg, params } : { msg }
244
+ state.announceId += 1
245
+ }
246
+
247
+ function settle<T>(area: DmArea, out: Outcome<T>): T | null {
248
+ if (disposed) return null
249
+ if (!out.ok) {
250
+ state.failures[area] = out.code
251
+ return null
252
+ }
253
+ delete state.failures[area]
254
+ return out.value
255
+ }
256
+
257
+ /** One of the member's acts: one at a time, its refusal said in its area. */
258
+ async function act<T>(area: DmArea, call: () => Promise<Outcome<T>>): Promise<T | null> {
259
+ if (state.busy || disposed) return null
260
+ state.busy = true
261
+ delete state.failures[area]
262
+ try {
263
+ return settle(area, await call())
264
+ } finally {
265
+ state.busy = false
266
+ }
267
+ }
268
+
269
+ function remember(conversationId: string, counterpart: PrivateDmCounterpart): void {
270
+ state.counterparts[conversationId] = counterpart
271
+ }
272
+
273
+ async function loadInbox(more = false): Promise<void> {
274
+ const cursor = more ? state.inboxCursor : null
275
+ if (more && cursor === null) return
276
+ const v = settle('inbox', await dm.inbox(cursor ?? undefined))
277
+ if (!v) return
278
+ for (const c of v.items) remember(c.conversationId, c.counterpart)
279
+ if (more && state.inbox) {
280
+ const known = new Set(state.inbox.map((c) => c.conversationId))
281
+ state.inbox = [...state.inbox, ...v.items.filter((c) => !known.has(c.conversationId))]
282
+ } else state.inbox = [...v.items]
283
+ state.inboxCursor = v.nextCursor
284
+ }
285
+
286
+ async function loadRequests(more = false): Promise<void> {
287
+ const cursor = more ? state.requestsCursor : null
288
+ if (more && cursor === null) return
289
+ const v = settle('requests', await dm.requests.list(cursor ?? undefined))
290
+ if (!v) return
291
+ for (const r of v.items) remember(r.conversationId, r.from)
292
+ if (more && state.requests) {
293
+ const known = new Set(state.requests.map((r) => r.conversationId))
294
+ state.requests = [...state.requests, ...v.items.filter((r) => !known.has(r.conversationId))]
295
+ } else state.requests = [...v.items]
296
+ state.requestsCursor = v.nextCursor
297
+ }
298
+
299
+ // The inbox events re-read the list at most once at a time, and once more when another came in meanwhile.
300
+ let inboxReading: Promise<void> | null = null
301
+ let inboxAgain = false
302
+ function rereadInbox(): void {
303
+ if (inboxReading) {
304
+ inboxAgain = true
305
+ return
306
+ }
307
+ inboxReading = (async () => {
308
+ try {
309
+ do {
310
+ inboxAgain = false
311
+ await loadInbox()
312
+ } while (inboxAgain && !disposed)
313
+ } finally {
314
+ inboxReading = null
315
+ }
316
+ })()
317
+ }
318
+
319
+ function watch(conversationId: string): () => void {
320
+ const cid = conversationId.toLowerCase()
321
+ let entry = open.get(cid)
322
+ if (!entry) {
323
+ const thread = dm.thread(cid)
324
+ const off = thread.subscribe((s) => {
325
+ if (!disposed) state.threads[cid] = s
326
+ })
327
+ entry = { thread, watchers: 0, off }
328
+ open.set(cid, entry)
329
+ void thread.loadLatest()
330
+ }
331
+ entry.watchers += 1
332
+ let stopped = false
333
+ return () => {
334
+ if (stopped) return
335
+ stopped = true
336
+ const e = open.get(cid)
337
+ if (!e) return
338
+ e.watchers -= 1
339
+ if (e.watchers > 0) return
340
+ // The last window of this conversation went: its decrypted messages go from this page's memory.
341
+ e.off()
342
+ e.thread.close()
343
+ open.delete(cid)
344
+ delete state.threads[cid]
345
+ for (const k of Object.keys(state.unfolded)) if (k.startsWith(`${cid}:`)) delete state.unfolded[k]
346
+ }
347
+ }
348
+
349
+ const threadOf = (cid: string): PrivateDmThread | null => open.get(cid.toLowerCase())?.thread ?? null
350
+
351
+ // The muted words load once, from this browser.
352
+ void o.muted.load().then((words) => {
353
+ if (disposed) return
354
+ state.muted = words
355
+ state.mutedPersistent = o.muted.persistent()
356
+ })
357
+
358
+ async function saveMuted(words: string[]): Promise<void> {
359
+ const clean = cleanMutedWords(words)
360
+ state.muted = clean
361
+ const kept = await o.muted.save(clean)
362
+ if (disposed) return
363
+ state.mutedPersistent = kept
364
+ }
365
+
366
+ const controller: DmViewController = {
367
+ state,
368
+ composerKeys: () => composerKeys(o.promise()),
369
+ rulesHref: () => o.rulesHref(),
370
+ consultationPath: () => (typeof o.consultationPath === 'string' && o.consultationPath !== '' ? o.consultationPath : null),
371
+ readOnly: () => o.readOnly?.() === true,
372
+ drafts: dm.drafts,
373
+ async show(screen) {
374
+ state.screen = screen
375
+ if (screen !== 'new') state.audience = null
376
+ if (screen === 'inbox') {
377
+ await Promise.all([loadInbox(), loadRequests()])
378
+ } else if (screen === 'requests') await loadRequests()
379
+ else if (screen === 'blocks') {
380
+ const v = settle('blocks', await dm.blocks.list())
381
+ if (v) state.blocks = [...v.blocks]
382
+ } else if (screen === 'cases') {
383
+ state.statement = null
384
+ await controller.loadCases()
385
+ }
386
+ },
387
+ loadInbox,
388
+ loadRequests,
389
+ watch,
390
+ async openConversation(conversationId) {
391
+ state.current = conversationId.toLowerCase()
392
+ state.screen = 'conversation'
393
+ },
394
+ async checkHandle(handle) {
395
+ const h = handle.trim()
396
+ state.audience = null
397
+ if (h === '') return
398
+ const v = await act('new', () => dm.audience.check(h))
399
+ if (v) state.audience = { handle: h, canWrite: v.canWrite }
400
+ },
401
+ async openHandle(handle) {
402
+ const v = await act('new', () => dm.open(handle))
403
+ if (!v) return null
404
+ state.audience = null
405
+ await controller.openConversation(v.conversationId)
406
+ void loadInbox()
407
+ return v.conversationId
408
+ },
409
+ send(conversationId, text, replyToSeq) {
410
+ const thread = threadOf(conversationId)
411
+ if (!thread) {
412
+ dm.send({ conversationId, text, ...(replyToSeq ? { replyToSeq } : {}) })
413
+ return
414
+ }
415
+ thread.send(text, replyToSeq ? { replyToSeq } : {})
416
+ },
417
+ typing(conversationId) {
418
+ if (o.typingAllowed && !o.typingAllowed()) return
419
+ void dm.typing(conversationId)
420
+ },
421
+ markRead(conversationId) {
422
+ const cid = conversationId.toLowerCase()
423
+ const entry = open.get(cid)
424
+ if (!entry) return
425
+ void entry.thread.markRead().then((r) => {
426
+ if (disposed || !r.ok) return
427
+ // The inbox's unread count of this conversation is now 0; the next read of the list says so too.
428
+ if (r.value.sent && state.inbox) {
429
+ state.inbox = state.inbox.map((c) => (c.conversationId === cid ? { ...c, unread: 0 } : c))
430
+ }
431
+ // kb-api read the conversation's notifications with the cursor: only then is the dot asked again.
432
+ if (r.value.notificationsRead > 0) o.notificationsRead?.(cid)
433
+ })
434
+ },
435
+ async loadOlder(conversationId) {
436
+ const thread = threadOf(conversationId)
437
+ if (thread) settle('thread', await thread.loadOlder())
438
+ },
439
+ async edit(conversationId, m, text) {
440
+ if (text.trim() === '' || text === m.text) return false
441
+ const v = await act('thread', () => dm.edit({ conversationId, seq: m.seq, rev: m.rev, text }))
442
+ if (v) announce('private.dm.message.edited')
443
+ return v !== null
444
+ },
445
+ async deleteForAll(conversationId, seq) {
446
+ const v = await act('thread', () => dm.delete({ conversationId, seq }))
447
+ if (v) announce('private.dm.message.deleted')
448
+ return v !== null
449
+ },
450
+ async hide(conversationId, seq) {
451
+ const v = await act('thread', () => dm.hide({ conversationId, seq }))
452
+ if (v !== null) announce('private.dm.message.hidden')
453
+ return v !== null
454
+ },
455
+ async clear(conversationId) {
456
+ const v = await act('thread', () => dm.clear(conversationId))
457
+ if (v !== null) announce('private.dm.thread.cleared')
458
+ return v !== null
459
+ },
460
+ retry(clientMsgId) {
461
+ dm.outbox.retry(clientMsgId)
462
+ },
463
+ discard(clientMsgId) {
464
+ dm.outbox.discard(clientMsgId)
465
+ },
466
+ async acceptRequest(conversationId) {
467
+ const v = await act('requests', () => dm.requests.accept(conversationId))
468
+ if (v === null) return false
469
+ state.requests = (state.requests ?? []).filter((r) => r.conversationId !== conversationId)
470
+ announce('private.dm.request.accepted')
471
+ void loadInbox()
472
+ return true
473
+ },
474
+ async deleteRequest(conversationId) {
475
+ const v = await act('requests', () => dm.requests.delete(conversationId))
476
+ if (v === null) return false
477
+ state.requests = (state.requests ?? []).filter((r) => r.conversationId !== conversationId)
478
+ announce('private.dm.request.deleted')
479
+ return true
480
+ },
481
+ async block(memberId) {
482
+ const v = await act('blocks', () => dm.blocks.add(memberId))
483
+ if (v === null) return false
484
+ announce('private.dm.block.done')
485
+ return true
486
+ },
487
+ async unblock(memberId) {
488
+ const v = await act('blocks', () => dm.blocks.remove(memberId))
489
+ if (v === null) return false
490
+ state.blocks = (state.blocks ?? []).filter((b) => b.memberId !== memberId)
491
+ announce('private.dm.block.removed')
492
+ return true
493
+ },
494
+ startReport(kind, conversationId, seq) {
495
+ delete state.failures.report
496
+ state.report = { kind, conversationId: conversationId.toLowerCase(), seq: seq ?? null, line: null, category: null, preview: null, block: true, note: '', done: null }
497
+ },
498
+ async reportLine(line) {
499
+ const r = state.report
500
+ if (!r) return
501
+ r.line = line
502
+ r.category = null
503
+ r.preview = null
504
+ if (r.kind !== 'message' || r.seq === null) return
505
+ const target = { conversationId: r.conversationId, seq: r.seq, line }
506
+ const v = settle('report', await dm.reports.preview(target))
507
+ // The answer of a line the reporter has since changed is not the one shown.
508
+ if (v && state.report === r && r.line === line) r.preview = v
509
+ },
510
+ reportCategory(category) {
511
+ if (state.report) state.report.category = category
512
+ },
513
+ async submitReport() {
514
+ const r = state.report
515
+ if (!r || !r.category || r.done) return false
516
+ const note = r.note.trim() === '' ? undefined : r.note
517
+ // A message's report is sent only after its preview said what goes to whom (§7.5).
518
+ if (r.kind === 'message' && (r.seq === null || !r.line || !r.preview)) return false
519
+ const v =
520
+ r.kind === 'message'
521
+ ? await act('report', () => dm.reports.submit({ conversationId: r.conversationId, seq: r.seq!, line: r.line!, category: r.category!, block: r.block, ...(note !== undefined ? { text: note } : {}) }))
522
+ : await act('report', () => dm.requests.blockReport({ conversationId: r.conversationId, category: r.category!, ...(note !== undefined ? { text: note } : {}) }))
523
+ if (!v) return false
524
+ r.done = { caseId: v.caseId }
525
+ if (r.kind === 'request') state.requests = (state.requests ?? []).filter((q) => q.conversationId !== r.conversationId)
526
+ announce('private.dm.report.sent')
527
+ return true
528
+ },
529
+ cancelReport() {
530
+ state.report = null
531
+ delete state.failures.report
532
+ },
533
+ async loadCases(more = false) {
534
+ const cursor = more ? state.casesCursor : null
535
+ if (more && cursor === null) return
536
+ const v = settle('cases', await dm.cases.list(cursor ?? undefined))
537
+ if (!v) return
538
+ state.cases = more && state.cases ? [...state.cases, ...v.items.filter((c) => !state.cases!.some((k) => k.caseId === c.caseId))] : [...v.items]
539
+ state.casesCursor = v.nextCursor
540
+ },
541
+ async openStatement(decisionId) {
542
+ state.appealDone = false
543
+ const v = settle('statement', await dm.statements.get(decisionId))
544
+ if (v) state.statement = v
545
+ },
546
+ async appeal(decisionId, text) {
547
+ if (text.trim() === '') return false
548
+ const v = await act('appeal', () => dm.appeals.submit({ decisionId, text }))
549
+ if (!v) return false
550
+ state.appealDone = true
551
+ announce('private.dm.appeal.sent')
552
+ return true
553
+ },
554
+ warnLink(link) {
555
+ state.linkWarning = { href: link.href, host: link.host }
556
+ },
557
+ dismissLink() {
558
+ state.linkWarning = null
559
+ },
560
+ async addMuted(word) {
561
+ const w = mutedWordOf(word)
562
+ if (w === null || state.muted.includes(w)) return
563
+ await saveMuted([...state.muted, w])
564
+ },
565
+ async removeMuted(word) {
566
+ await saveMuted(state.muted.filter((w) => w !== word))
567
+ },
568
+ unfold(conversationId, seq) {
569
+ state.unfolded[`${conversationId.toLowerCase()}:${seq}`] = true
570
+ },
571
+ apply(event) {
572
+ if (disposed) return
573
+ const effect = dm.apply(event)
574
+ if (effect !== 'inbox') return
575
+ rereadInbox()
576
+ if (event.k === 'rq' || event.k === 'resync') void loadRequests()
577
+ },
578
+ online() {
579
+ dm.outbox.retryNow()
580
+ },
581
+ dispose() {
582
+ if (disposed) return
583
+ disposed = true
584
+ for (const e of open.values()) {
585
+ e.off()
586
+ e.thread.close()
587
+ }
588
+ open.clear()
589
+ // Drafts, the outbox and every decrypted message go with the view (the DM client's close()) — and so does what
590
+ // this controller copied of them for the screens: the threads' snapshots, the requests' texts, a report's flow,
591
+ // the member's muted words, so an ended controller (a sign-out, another member) holds nothing of the member's.
592
+ dm.close()
593
+ Object.assign(state, {
594
+ current: null,
595
+ inbox: null,
596
+ inboxCursor: null,
597
+ requests: null,
598
+ requestsCursor: null,
599
+ blocks: null,
600
+ cases: null,
601
+ casesCursor: null,
602
+ statement: null,
603
+ threads: {},
604
+ counterparts: {},
605
+ audience: null,
606
+ report: null,
607
+ linkWarning: null,
608
+ muted: [],
609
+ unfolded: {},
610
+ })
611
+ },
612
+ }
613
+ return markRaw(controller) as DmViewController
614
+ }
615
+
616
+ /** The catalogue key of a refusal in the conversations: their own codes by name, the account's through the view's. */
617
+ export function dmErrorKey(code: string | null | undefined, fallback: (code: string | null | undefined) => string): string {
618
+ switch (code) {
619
+ case 'DM_NOT_ALLOWED':
620
+ return 'private.dm.error.notAllowed'
621
+ case 'EDIT_WINDOW':
622
+ return 'private.dm.error.editWindow'
623
+ case 'DELETE_WINDOW':
624
+ return 'private.dm.error.deleteWindow'
625
+ case 'NOT_FOUND':
626
+ return 'private.dm.error.notFound'
627
+ case 'TOO_FAST':
628
+ return 'private.dm.error.tooFast'
629
+ case 'UNKNOWN_PATH':
630
+ return 'private.dm.error.notYet'
631
+ case 'CANCELLED':
632
+ return 'private.dm.error.cancelled'
633
+ default:
634
+ return fallback(code)
635
+ }
636
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The DM elements (m4-contracts.md §7.8 DM8b): the conversations inside `<blogmcp-private-view>` (its `messages`
3
+ * screen) and `<blogmcp-chat-dock>` (the panel's list and a window per conversation), over the SDK's DM client. The
4
+ * private entry re-exports what a host that builds its own screens around the controller may use.
5
+ */
6
+ export { createDmView, dmErrorKey, categoriesOf, DM_SCREENS, DM_RULES_CATEGORIES, DM_ADVERSE_EVENT } from './controller'
7
+ export type { DmViewController, DmViewOptions, DmUi, DmScreen, DmArea, DmReportFlow } from './controller'
8
+ export { composerKeys, canEdit, canDeleteForAll, canReport, DM_COMPOSER_KEYS, DM_EDIT_WINDOW_MS, DM_DELETE_FOR_ALL_MS } from './composer'
9
+ export type { DmComposerKey } from './composer'
10
+ export { indexedDbMutedStore, memoryMutedStore, mutedMatch, foldText, MUTED_DB_NAME, MUTED_DB_STORE, MUTED_MAX_WORDS } from './muted'
11
+ export type { MutedStore } from './muted'
12
+ export { searchLoaded, DM_SEARCH_MIN_LENGTH } from './search'
13
+ export { segmentsOf, linkOf } from './links'
14
+ export type { DmTextSegment } from './links'