@blogmcp/sdk 0.8.0 → 0.9.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/dist/community/channels.d.ts +12 -0
- package/dist/community/channels.js +7 -0
- package/dist/community/client.d.ts +211 -0
- package/dist/community/client.js +188 -0
- package/dist/community/index.d.ts +34 -0
- package/dist/community/index.js +92 -0
- package/dist/community/links.d.ts +65 -0
- package/dist/community/links.js +81 -0
- package/dist/community/messages.cs.d.ts +331 -0
- package/dist/community/messages.cs.js +342 -0
- package/dist/community/messages.d.ts +40 -0
- package/dist/community/messages.en.d.ts +315 -0
- package/dist/community/messages.en.js +313 -0
- package/dist/community/messages.js +66 -0
- package/dist/community/outcome.d.ts +96 -0
- package/dist/community/outcome.js +211 -0
- package/dist/community/poll.d.ts +35 -0
- package/dist/community/poll.js +70 -0
- package/dist/community/routes.d.ts +110 -0
- package/dist/community/routes.js +66 -0
- package/dist/community/server/bff.d.ts +123 -0
- package/dist/community/server/bff.js +214 -0
- package/dist/community/server/env.d.ts +20 -0
- package/dist/community/server/env.js +16 -0
- package/dist/community/server/host-routes.d.ts +15 -0
- package/dist/community/server/host-routes.js +35 -0
- package/dist/community/server/http.d.ts +64 -0
- package/dist/community/server/http.js +90 -0
- package/dist/community/server/index.d.ts +45 -0
- package/dist/community/server/index.js +19 -0
- package/dist/community/server/route-decision.d.ts +70 -0
- package/dist/community/server/route-decision.js +89 -0
- package/dist/community/server/unknown-path.d.ts +21 -0
- package/dist/community/server/unknown-path.js +11 -0
- package/dist/community/shapes.d.ts +457 -0
- package/dist/community/shapes.js +51 -0
- package/dist/community/store.d.ts +183 -0
- package/dist/community/store.js +423 -0
- package/dist/community/transport.d.ts +42 -0
- package/dist/community/transport.js +59 -0
- package/dist/core/client.d.ts +17 -0
- package/dist/core/client.js +58 -55
- package/dist/core/comments-legacy.d.ts +19 -0
- package/dist/core/comments-legacy.js +18 -0
- package/dist/core/index.d.ts +3 -0
- package/dist/core/index.js +107 -101
- package/dist/core/queries.d.ts +46 -0
- package/dist/core/queries.js +128 -84
- package/dist/core/review/page.d.ts +1 -1
- package/dist/core/review/page.js +1 -1
- package/dist/core/review/plan.d.ts +1 -1
- package/dist/core/review/plan.js +8 -4
- package/dist/core/review/stamp.d.ts +4 -2
- package/dist/core/review/stamp.js +47 -47
- package/dist/core/seo.js +40 -39
- package/dist/core/translations/cs.json.js +6 -6
- package/dist/core/translations/en.json.js +1 -1
- package/dist/core/types.d.ts +23 -1
- package/dist/core/view-model.d.ts +7 -0
- package/dist/core/view-model.js +29 -25
- package/dist/react/KbMdxContent.js +4 -3
- package/dist/vue/components/KbMdxContent.vue2.js +3 -2
- package/dist/vue/composables/useKbComments.d.ts +8 -0
- package/package.json +13 -4
- package/src/community/channels.ts +12 -0
- package/src/community/client.ts +437 -0
- package/src/community/index.ts +190 -0
- package/src/community/links.ts +166 -0
- package/src/community/messages.cs.ts +371 -0
- package/src/community/messages.en.ts +341 -0
- package/src/community/messages.ts +112 -0
- package/src/community/outcome.ts +317 -0
- package/src/community/poll.ts +154 -0
- package/src/community/routes.ts +139 -0
- package/src/community/server/bff.ts +530 -0
- package/src/community/server/env.ts +42 -0
- package/src/community/server/host-routes.ts +48 -0
- package/src/community/server/http.ts +175 -0
- package/src/community/server/index.ts +55 -0
- package/src/community/server/route-decision.ts +226 -0
- package/src/community/server/unknown-path.ts +32 -0
- package/src/community/shapes.ts +513 -0
- package/src/community/store.ts +786 -0
- package/src/community/transport.ts +111 -0
- package/src/core/client.ts +24 -1
- package/src/core/comments-legacy.ts +39 -0
- package/src/core/index.ts +8 -0
- package/src/core/queries.ts +93 -0
- package/src/core/review/page.ts +1 -1
- package/src/core/review/plan.ts +4 -2
- package/src/core/review/stamp.ts +18 -5
- package/src/core/seo.ts +5 -1
- package/src/core/translations/cs.json +3 -1
- package/src/core/translations/en.json +3 -1
- package/src/core/types.ts +19 -1
- package/src/core/view-model.ts +12 -1
- package/src/vue/composables/useKbComments.ts +8 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The envelope every community call answers with, and the one mapping from kb-api's codes to the codes a page
|
|
3
|
+
* shows (docs/internal/m1-contracts.md §7.12, §10.2).
|
|
4
|
+
*
|
|
5
|
+
* ⚠️ A reader never sees kb-api's text. kb-api refuses with a code and a fixed English sentence
|
|
6
|
+
* (`{ ok: false, code, message }`); the client turns the code into one of COMMUNITY_UI_CODES, and the page turns
|
|
7
|
+
* that into its own text (`communityMessage(locale, 'error.<code>')`). The mapping is total: every code kb-api
|
|
8
|
+
* answers (COMMUNITY_API_CODES — a copy of kb-api's `routes/community-codes.ts`, pinned by
|
|
9
|
+
* test/community/codes-drift.spec.ts) and every code a host's BFF answers itself has a UI code, checked by the
|
|
10
|
+
* compiler; a code neither list knows is `submit`.
|
|
11
|
+
*
|
|
12
|
+
* Browser-safe and server-safe: no I/O, no globals.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { KnownNeed } from './shapes'
|
|
16
|
+
|
|
17
|
+
/** The codes a page can be told. */
|
|
18
|
+
export const COMMUNITY_UI_CODES = [
|
|
19
|
+
/** The host refused the method (a programming error: every operation is a POST). */
|
|
20
|
+
'method',
|
|
21
|
+
/** The host refused the request's origin (the page and its API are on different sites). */
|
|
22
|
+
'origin',
|
|
23
|
+
/** The host or kb-api could not be reached, or asked to retry later. */
|
|
24
|
+
'unavailable',
|
|
25
|
+
/** The request did not validate (a field's shape, a body too large). */
|
|
26
|
+
'invalid',
|
|
27
|
+
/** A signed-in reader is needed — or the session cannot take part (not a reader's, or not admitted here). */
|
|
28
|
+
'signIn',
|
|
29
|
+
/** The member has to finish joining first (`needs` says what). */
|
|
30
|
+
'onboarding',
|
|
31
|
+
/** The community is not open to the member's age group. */
|
|
32
|
+
'age',
|
|
33
|
+
/** The display name is reserved or not accepted. */
|
|
34
|
+
'name',
|
|
35
|
+
/** The handle is taken, or changed too recently. */
|
|
36
|
+
'handle',
|
|
37
|
+
/** Posting is restricted (a sanction; `until` when it ends) or the member is read-only. */
|
|
38
|
+
'restricted',
|
|
39
|
+
/** The community account is suspended (`until`). */
|
|
40
|
+
'suspended',
|
|
41
|
+
/** The community account is closed. */
|
|
42
|
+
'terminated',
|
|
43
|
+
/** Too many links in one message. */
|
|
44
|
+
'links',
|
|
45
|
+
/** Too fast — wait `retryAfterS`. */
|
|
46
|
+
'fast',
|
|
47
|
+
/** The filters refused the text: nothing was stored. */
|
|
48
|
+
'blocked',
|
|
49
|
+
/** The author must confirm `codes` (e.g. writing about themselves) and send again with `confirmations`. */
|
|
50
|
+
'confirm',
|
|
51
|
+
/** The message was removed: it cannot be edited. */
|
|
52
|
+
'removed',
|
|
53
|
+
/** A daily or per-network quota — `retryAfterS` when it frees. */
|
|
54
|
+
'quota',
|
|
55
|
+
/** The Turnstile check failed: a fresh token, then again. */
|
|
56
|
+
'captcha',
|
|
57
|
+
/** No such thing, or not visible to this viewer (the same answer for both). */
|
|
58
|
+
'notFound',
|
|
59
|
+
/** Discussions are switched off here: render nothing. */
|
|
60
|
+
'surfaceOff',
|
|
61
|
+
/** The discussion does not take writes yet (the legacy comments' cutover). */
|
|
62
|
+
'notMoved',
|
|
63
|
+
/** kb-api could not record the change; nothing changed — retry. */
|
|
64
|
+
'ledger',
|
|
65
|
+
/** Anything else: "that did not work, try again". */
|
|
66
|
+
'submit',
|
|
67
|
+
] as const
|
|
68
|
+
|
|
69
|
+
export type CommunityUiCode = (typeof COMMUNITY_UI_CODES)[number]
|
|
70
|
+
|
|
71
|
+
const UI_CODES: ReadonlySet<string> = new Set(COMMUNITY_UI_CODES)
|
|
72
|
+
|
|
73
|
+
/** True for one of COMMUNITY_UI_CODES. */
|
|
74
|
+
export function isCommunityUiCode(code: unknown): code is CommunityUiCode {
|
|
75
|
+
return typeof code === 'string' && UI_CODES.has(code)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** What a refusal may carry besides its code — codes, needs, a time, field paths; never anything a member wrote. */
|
|
79
|
+
export interface CommunityFailure {
|
|
80
|
+
ok: false
|
|
81
|
+
code: CommunityUiCode
|
|
82
|
+
/** Seconds before a retry may succeed (a quota, the burst, the rate limiter, `Retry-After`). */
|
|
83
|
+
retryAfterS?: number
|
|
84
|
+
/** ONBOARDING_REQUIRED: what is still missing (an unknown need is dropped). */
|
|
85
|
+
needs?: KnownNeed[]
|
|
86
|
+
/** FILTER_BLOCKED, CONFIRMATION_REQUIRED: the filter codes (CONFIRMATION_REQUIRED: the ones to confirm). */
|
|
87
|
+
codes?: string[]
|
|
88
|
+
/** POSTING_RESTRICTED, ACCOUNT_SUSPENDED: when it ends (absent: no end set). */
|
|
89
|
+
until?: string
|
|
90
|
+
/** VALIDATION: the paths of the refused fields (`displayName`, `body`, …), never their values. */
|
|
91
|
+
fields?: string[]
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** What every client method resolves to; none of them throws. */
|
|
95
|
+
export type CommunityOutcome<T> = { ok: true; value: T } | CommunityFailure
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Every code kb-api's community routers answer: their own, and the member, trust and quota codes they pass
|
|
99
|
+
* through. A copy of `COMMUNITY_API_CODES` in kb-api's `routes/community-codes.ts`, in its order.
|
|
100
|
+
*/
|
|
101
|
+
export const COMMUNITY_API_CODES = [
|
|
102
|
+
'SURFACE_OFF',
|
|
103
|
+
'CUTOVER_NOT_MOVED',
|
|
104
|
+
'NOT_FOUND',
|
|
105
|
+
'ONBOARDING_REQUIRED',
|
|
106
|
+
'AGE_BELOW_MINIMUM',
|
|
107
|
+
'NAME_RESERVED',
|
|
108
|
+
'NAME_BLOCKED',
|
|
109
|
+
'HANDLE_TAKEN',
|
|
110
|
+
'HANDLE_RATE',
|
|
111
|
+
'POSTING_RESTRICTED',
|
|
112
|
+
'ACCOUNT_SUSPENDED',
|
|
113
|
+
'ACCOUNT_TERMINATED',
|
|
114
|
+
'TOO_MANY_LINKS',
|
|
115
|
+
'TOO_FAST',
|
|
116
|
+
'FILTER_BLOCKED',
|
|
117
|
+
'CONFIRMATION_REQUIRED',
|
|
118
|
+
'MESSAGE_REMOVED',
|
|
119
|
+
'REACTION_NOT_ALLOWED',
|
|
120
|
+
'CAPTCHA_FAILED',
|
|
121
|
+
'UNAUTHORIZED',
|
|
122
|
+
'VALIDATION',
|
|
123
|
+
'UNAVAILABLE',
|
|
124
|
+
'STALE_LEASE',
|
|
125
|
+
'STAFF_POSTING_OFF',
|
|
126
|
+
'PRESET_CONFLICT',
|
|
127
|
+
'INTERNAL',
|
|
128
|
+
'NOT_A_MEMBER_SESSION',
|
|
129
|
+
'IDENTITY_NOT_ADMITTED',
|
|
130
|
+
'ACCOUNT_NOT_ELIGIBLE',
|
|
131
|
+
'MEMBER_READ_ONLY',
|
|
132
|
+
'MEMBER_DELETION_REQUESTED',
|
|
133
|
+
'PRIVATE_SPACE',
|
|
134
|
+
'WRONG_PROVIDER',
|
|
135
|
+
'CASE_CLOSED',
|
|
136
|
+
'NOTIFIER_NOT_ALLOWED',
|
|
137
|
+
'UNSUPPORTED_RESTRICTION',
|
|
138
|
+
'LADDER_EXCEEDED',
|
|
139
|
+
'PERSON_REQUIRED',
|
|
140
|
+
'APPEAL_OPEN',
|
|
141
|
+
'APPEAL_WINDOW_CLOSED',
|
|
142
|
+
'APPEAL_NEEDS_ANOTHER_PERSON',
|
|
143
|
+
'GAG_PLATFORM_ONLY',
|
|
144
|
+
'ORDER_FINAL',
|
|
145
|
+
'BODY_JSON_INVALID',
|
|
146
|
+
'AUDIT_DETAIL_NOT_CODE',
|
|
147
|
+
'LEDGER_UNAVAILABLE',
|
|
148
|
+
'INVALID_INPUT',
|
|
149
|
+
'LEGAL_HOLD',
|
|
150
|
+
'TEXT_MISSING',
|
|
151
|
+
'QUOTA_DAILY',
|
|
152
|
+
'QUOTA_IP_HOURLY',
|
|
153
|
+
] as const
|
|
154
|
+
|
|
155
|
+
/** A code kb-api's community routers answer. */
|
|
156
|
+
export type CommunityApiCode = (typeof COMMUNITY_API_CODES)[number]
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Every kb-api code → the page's code. `satisfies Record` over the whole union: a code kb-api adds (and the drift
|
|
160
|
+
* spec makes this list add) fails the type check here instead of reaching a page as an unknown string.
|
|
161
|
+
*
|
|
162
|
+
* Codes no public or notice operation can produce (the team's, the adapter's, the decision engine's) map to
|
|
163
|
+
* `submit`: should one arrive anyway, the page gets the generic text and the log shows the kb-api code.
|
|
164
|
+
*/
|
|
165
|
+
export const COMMUNITY_OUTCOME_OF_KB: Readonly<Record<CommunityApiCode, CommunityUiCode>> = Object.freeze({
|
|
166
|
+
SURFACE_OFF: 'surfaceOff',
|
|
167
|
+
CUTOVER_NOT_MOVED: 'notMoved',
|
|
168
|
+
NOT_FOUND: 'notFound',
|
|
169
|
+
ONBOARDING_REQUIRED: 'onboarding',
|
|
170
|
+
AGE_BELOW_MINIMUM: 'age',
|
|
171
|
+
NAME_RESERVED: 'name',
|
|
172
|
+
NAME_BLOCKED: 'name',
|
|
173
|
+
HANDLE_TAKEN: 'handle',
|
|
174
|
+
HANDLE_RATE: 'handle',
|
|
175
|
+
POSTING_RESTRICTED: 'restricted',
|
|
176
|
+
ACCOUNT_SUSPENDED: 'suspended',
|
|
177
|
+
ACCOUNT_TERMINATED: 'terminated',
|
|
178
|
+
TOO_MANY_LINKS: 'links',
|
|
179
|
+
TOO_FAST: 'fast',
|
|
180
|
+
FILTER_BLOCKED: 'blocked',
|
|
181
|
+
CONFIRMATION_REQUIRED: 'confirm',
|
|
182
|
+
MESSAGE_REMOVED: 'removed',
|
|
183
|
+
// An emoji outside the tenant's set: the page offered a stale set, the request was malformed.
|
|
184
|
+
REACTION_NOT_ALLOWED: 'invalid',
|
|
185
|
+
CAPTCHA_FAILED: 'captcha',
|
|
186
|
+
UNAUTHORIZED: 'signIn',
|
|
187
|
+
VALIDATION: 'invalid',
|
|
188
|
+
// The notice routes without kb-api's capability key, and a BFF's failed upstream call.
|
|
189
|
+
UNAVAILABLE: 'unavailable',
|
|
190
|
+
STALE_LEASE: 'submit',
|
|
191
|
+
STAFF_POSTING_OFF: 'submit',
|
|
192
|
+
PRESET_CONFLICT: 'submit',
|
|
193
|
+
INTERNAL: 'unavailable',
|
|
194
|
+
// A session that is not a reader's (the team, a key) or an identity the tenant does not admit: sign in as a reader.
|
|
195
|
+
NOT_A_MEMBER_SESSION: 'signIn',
|
|
196
|
+
IDENTITY_NOT_ADMITTED: 'signIn',
|
|
197
|
+
ACCOUNT_NOT_ELIGIBLE: 'signIn',
|
|
198
|
+
// Writes are closed for the member (a 16–17 member under an 18 minimum, a pending deletion); MyState says which.
|
|
199
|
+
MEMBER_READ_ONLY: 'restricted',
|
|
200
|
+
MEMBER_DELETION_REQUESTED: 'restricted',
|
|
201
|
+
PRIVATE_SPACE: 'submit',
|
|
202
|
+
WRONG_PROVIDER: 'submit',
|
|
203
|
+
// An appeal that came too late or twice (another tab, a window that closed meanwhile): refetch the statement.
|
|
204
|
+
CASE_CLOSED: 'submit',
|
|
205
|
+
// A child-at-risk notice with a name or e-mail: the form hides those fields, so this is a wrong form.
|
|
206
|
+
NOTIFIER_NOT_ALLOWED: 'invalid',
|
|
207
|
+
UNSUPPORTED_RESTRICTION: 'submit',
|
|
208
|
+
LADDER_EXCEEDED: 'submit',
|
|
209
|
+
PERSON_REQUIRED: 'submit',
|
|
210
|
+
APPEAL_OPEN: 'submit',
|
|
211
|
+
APPEAL_WINDOW_CLOSED: 'submit',
|
|
212
|
+
APPEAL_NEEDS_ANOTHER_PERSON: 'submit',
|
|
213
|
+
GAG_PLATFORM_ONLY: 'submit',
|
|
214
|
+
ORDER_FINAL: 'submit',
|
|
215
|
+
BODY_JSON_INVALID: 'invalid',
|
|
216
|
+
AUDIT_DETAIL_NOT_CODE: 'submit',
|
|
217
|
+
LEDGER_UNAVAILABLE: 'ledger',
|
|
218
|
+
INVALID_INPUT: 'invalid',
|
|
219
|
+
LEGAL_HOLD: 'submit',
|
|
220
|
+
// The tenant has not published the text a join needs: the community cannot be joined yet.
|
|
221
|
+
TEXT_MISSING: 'unavailable',
|
|
222
|
+
QUOTA_DAILY: 'quota',
|
|
223
|
+
QUOTA_IP_HOURLY: 'quota',
|
|
224
|
+
} satisfies Record<CommunityApiCode, CommunityUiCode>)
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* The codes a host's BFF answers ITSELF, in kb-api's envelope, before kb-api is asked (m1-contracts.md §10.3):
|
|
228
|
+
* a method other than POST, a foreign origin, a body over 64 KB, a body that is not JSON.
|
|
229
|
+
*/
|
|
230
|
+
export const COMMUNITY_HOST_CODES = ['METHOD', 'ORIGIN', 'TOO_LARGE', 'INVALID_JSON'] as const
|
|
231
|
+
export type CommunityHostCode = (typeof COMMUNITY_HOST_CODES)[number]
|
|
232
|
+
|
|
233
|
+
export const COMMUNITY_OUTCOME_OF_HOST: Readonly<Record<CommunityHostCode, CommunityUiCode>> = Object.freeze({
|
|
234
|
+
METHOD: 'method',
|
|
235
|
+
ORIGIN: 'origin',
|
|
236
|
+
TOO_LARGE: 'invalid',
|
|
237
|
+
INVALID_JSON: 'invalid',
|
|
238
|
+
} satisfies Record<CommunityHostCode, CommunityUiCode>)
|
|
239
|
+
|
|
240
|
+
const own = (table: object, key: string): boolean => Object.prototype.hasOwnProperty.call(table, key)
|
|
241
|
+
|
|
242
|
+
/** A kb-api or host code → the page's code. Unknown and absent codes are `submit`. */
|
|
243
|
+
export function communityOutcomeOf(code: string | null | undefined): CommunityUiCode {
|
|
244
|
+
if (typeof code !== 'string' || code === '') return 'submit'
|
|
245
|
+
if (own(COMMUNITY_OUTCOME_OF_KB, code)) return COMMUNITY_OUTCOME_OF_KB[code as CommunityApiCode]
|
|
246
|
+
if (own(COMMUNITY_OUTCOME_OF_HOST, code)) return COMMUNITY_OUTCOME_OF_HOST[code as CommunityHostCode]
|
|
247
|
+
return 'submit'
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// ───────────────────────── the transport's failure ─────────────────────────
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Brand shared by every copy of the SDK loaded in one process (a bundler can load the src and the dist build side
|
|
254
|
+
* by side), so the check works where `instanceof` would not.
|
|
255
|
+
*/
|
|
256
|
+
const TRANSPORT_ERROR_BRAND = Symbol.for('@blogmcp/sdk/CommunityTransportError')
|
|
257
|
+
|
|
258
|
+
function httpStatusOrNull(value: unknown): number | null {
|
|
259
|
+
return typeof value === 'number' && Number.isInteger(value) && value >= 100 && value <= 599 ? value : null
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function secondsOrNull(value: unknown): number | null {
|
|
263
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? Math.ceil(value) : null
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* The host could not be asked: the network failed, the request timed out or was aborted. A replaceable transport
|
|
268
|
+
* throws it (or anything else — every throw is a failure the client maps, never a crash of the page).
|
|
269
|
+
*
|
|
270
|
+
* Its message must stay free of anything a member sent (bodies, names, tokens): it names what failed.
|
|
271
|
+
*/
|
|
272
|
+
export class CommunityTransportError extends Error {
|
|
273
|
+
/** The HTTP status, or null when there was no response (network, timeout). */
|
|
274
|
+
readonly status: number | null
|
|
275
|
+
/** Seconds to wait before retrying (`Retry-After`), or null. */
|
|
276
|
+
readonly retryAfterS: number | null
|
|
277
|
+
|
|
278
|
+
constructor(message: string, init: { status?: number | null; retryAfterS?: number | null } = {}) {
|
|
279
|
+
super(message)
|
|
280
|
+
this.name = 'CommunityTransportError'
|
|
281
|
+
this.status = httpStatusOrNull(init.status)
|
|
282
|
+
this.retryAfterS = secondsOrNull(init.retryAfterS)
|
|
283
|
+
// Non-enumerable, so it stays out of serialised errors and of the .d.ts.
|
|
284
|
+
Object.defineProperty(this, TRANSPORT_ERROR_BRAND, { value: true })
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** True for a CommunityTransportError, also one thrown by another copy of the SDK. */
|
|
289
|
+
export function isCommunityTransportError(err: unknown): err is CommunityTransportError {
|
|
290
|
+
if (err instanceof CommunityTransportError) return true
|
|
291
|
+
return err instanceof Error && (err as unknown as Record<symbol, unknown>)[TRANSPORT_ERROR_BRAND] === true
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** Anything a transport threw → `unavailable`, with the error's `retryAfterS` when it carries one. */
|
|
295
|
+
export function communityFailureOf(err: unknown): CommunityFailure {
|
|
296
|
+
if (isCommunityTransportError(err) && err.retryAfterS !== null) return { ok: false, code: 'unavailable', retryAfterS: err.retryAfterS }
|
|
297
|
+
return { ok: false, code: 'unavailable' }
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// ───────────────────────── logging ─────────────────────────
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Where the client reports what operators should see — an outage, a host misconfiguration, something unexpected
|
|
304
|
+
* (isLoggedCommunityOutcome). It gets the operation, the outcome, kb-api's code and the HTTP status: never a body,
|
|
305
|
+
* a name, a handle, an address, a receipt, a token, a session or an IP.
|
|
306
|
+
*/
|
|
307
|
+
export type CommunityLogger = (e: {
|
|
308
|
+
op: string
|
|
309
|
+
outcome: CommunityUiCode
|
|
310
|
+
kbCode?: string | null
|
|
311
|
+
status?: number | null
|
|
312
|
+
}) => void
|
|
313
|
+
|
|
314
|
+
/** The outcomes worth an operator's attention; the rest are ordinary member states (a quota, a needed sign-in). */
|
|
315
|
+
export function isLoggedCommunityOutcome(code: CommunityUiCode): boolean {
|
|
316
|
+
return code === 'unavailable' || code === 'submit' || code === 'method' || code === 'origin' || code === 'invalid' || code === 'ledger'
|
|
317
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Polls `discussions/head` while the page shows the discussion (docs/internal/m1-contracts.md §10.2; M1 has no
|
|
3
|
+
* realtime off Pro): the review layer's poller rules — only while visible, `Retry-After` honoured on 429, every
|
|
4
|
+
* failure doubles the wait up to 5 minutes, stopped when the element goes. A page shown again, or `now()`, asks at
|
|
5
|
+
* once — except while a failure's wait runs: neither cuts a `Retry-After` or the backoff short.
|
|
6
|
+
*
|
|
7
|
+
* const poller = pollDiscussionHead(client, [articleId], {
|
|
8
|
+
* onChange: (heads) => { if (heads.some((h) => store.applyHead(h))) void refreshDiscussion(store, client, input) },
|
|
9
|
+
* })
|
|
10
|
+
* // on unmount: poller.stop()
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { CommunityCallOptions, CommunityClient } from './client'
|
|
14
|
+
import type { CommunityFailure } from './outcome'
|
|
15
|
+
import { COMMUNITY_HEAD_ARTICLES_MAX } from './routes'
|
|
16
|
+
import type { DiscussionHead } from './shapes'
|
|
17
|
+
|
|
18
|
+
export interface PollDiscussionHeadOptions {
|
|
19
|
+
/** Whether the discussion is seen now; default: the document is visible. Polls only while true. */
|
|
20
|
+
visible?: () => boolean
|
|
21
|
+
/** Default 20 s (at least 1 s). */
|
|
22
|
+
intervalMs?: number
|
|
23
|
+
/** The heads that differ from the previous answer (the first answer: every head). */
|
|
24
|
+
onChange(heads: DiscussionHead[]): void
|
|
25
|
+
/** A poll that failed; the poller goes on, slower — `surfaceOff` stops it (discussions are switched off). */
|
|
26
|
+
onFailure?(failure: CommunityFailure): void
|
|
27
|
+
/** Aborting it stops the poller (the element's lifetime). */
|
|
28
|
+
signal?: AbortSignal
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface DiscussionHeadPoller {
|
|
32
|
+
/** Stops for good: no timer, no listener, the request in flight aborted. */
|
|
33
|
+
stop(): void
|
|
34
|
+
/** Polls now (the element came into view) — or when a failure's wait ends, if one runs. */
|
|
35
|
+
now(): void
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const POLL_INTERVAL_MS = 20_000
|
|
39
|
+
export const POLL_MAX_BACKOFF_MS = 5 * 60_000
|
|
40
|
+
/** A 429 without `Retry-After`. */
|
|
41
|
+
const RATE_LIMITED_WAIT_MS = 60_000
|
|
42
|
+
|
|
43
|
+
const sameHead = (a: DiscussionHead, b: DiscussionHead): boolean =>
|
|
44
|
+
a.lastSeq === b.lastSeq && a.count === b.count && a.lastChangeAt === b.lastChangeAt
|
|
45
|
+
|
|
46
|
+
export function pollDiscussionHead(
|
|
47
|
+
client: Pick<CommunityClient, 'discussions'>,
|
|
48
|
+
articleIds: readonly string[],
|
|
49
|
+
options: PollDiscussionHeadOptions,
|
|
50
|
+
): DiscussionHeadPoller {
|
|
51
|
+
const base = Math.max(1000, options.intervalMs ?? POLL_INTERVAL_MS)
|
|
52
|
+
const ids = [...new Set((Array.isArray(articleIds) ? articleIds : []).filter((id): id is string => typeof id === 'string' && id !== ''))].slice(
|
|
53
|
+
0,
|
|
54
|
+
COMMUNITY_HEAD_ARTICLES_MAX,
|
|
55
|
+
)
|
|
56
|
+
const visible =
|
|
57
|
+
options.visible ?? ((): boolean => typeof document === 'undefined' || document.visibilityState !== 'hidden')
|
|
58
|
+
const last = new Map<string, DiscussionHead>()
|
|
59
|
+
let timer: ReturnType<typeof setTimeout> | null = null
|
|
60
|
+
let inFlight: AbortController | null = null
|
|
61
|
+
let failureDelay = 0
|
|
62
|
+
/** A failure's wait (Retry-After, the backoff) runs: the page shown again or now() do not cut it short. */
|
|
63
|
+
let holding = false
|
|
64
|
+
let stopped = false
|
|
65
|
+
|
|
66
|
+
const schedule = (ms: number): void => {
|
|
67
|
+
if (stopped) return
|
|
68
|
+
if (timer) clearTimeout(timer)
|
|
69
|
+
timer = setTimeout(() => void tick(), ms)
|
|
70
|
+
}
|
|
71
|
+
const hold = (ms: number): void => {
|
|
72
|
+
schedule(ms)
|
|
73
|
+
holding = true
|
|
74
|
+
}
|
|
75
|
+
/** At once, unless a failure's wait runs (its timer asks when it ends). */
|
|
76
|
+
const soon = (): void => {
|
|
77
|
+
if (!stopped && !inFlight && !holding) schedule(0)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function tick(): Promise<void> {
|
|
81
|
+
timer = null
|
|
82
|
+
holding = false
|
|
83
|
+
if (stopped || inFlight) return
|
|
84
|
+
if (!visible()) {
|
|
85
|
+
// Seen again on visibilitychange, on now(), or at the next look.
|
|
86
|
+
schedule(base)
|
|
87
|
+
return
|
|
88
|
+
}
|
|
89
|
+
const controller = new AbortController()
|
|
90
|
+
inFlight = controller
|
|
91
|
+
const call: CommunityCallOptions = { signal: controller.signal }
|
|
92
|
+
const r = await client.discussions.head({ articleIds: ids }, call)
|
|
93
|
+
inFlight = null
|
|
94
|
+
if (stopped) return
|
|
95
|
+
if (r.ok) {
|
|
96
|
+
failureDelay = 0
|
|
97
|
+
const changed: DiscussionHead[] = []
|
|
98
|
+
for (const head of Array.isArray(r.value.heads) ? r.value.heads : []) {
|
|
99
|
+
if (typeof head?.articleId !== 'string') continue
|
|
100
|
+
const key = head.articleId.toLowerCase()
|
|
101
|
+
const before = last.get(key)
|
|
102
|
+
last.set(key, head)
|
|
103
|
+
if (!before || !sameHead(before, head)) changed.push(head)
|
|
104
|
+
}
|
|
105
|
+
if (changed.length > 0) {
|
|
106
|
+
try {
|
|
107
|
+
options.onChange(changed)
|
|
108
|
+
} catch {
|
|
109
|
+
// The page's handler must not stop the polling.
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
schedule(base)
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
options.onFailure?.(r)
|
|
117
|
+
} catch {
|
|
118
|
+
// As above.
|
|
119
|
+
}
|
|
120
|
+
if (r.code === 'surfaceOff') return stop()
|
|
121
|
+
if (r.code === 'fast' || r.code === 'quota') {
|
|
122
|
+
hold(Math.max(base, r.retryAfterS !== undefined ? r.retryAfterS * 1000 : RATE_LIMITED_WAIT_MS))
|
|
123
|
+
return
|
|
124
|
+
}
|
|
125
|
+
failureDelay = Math.min(POLL_MAX_BACKOFF_MS, (failureDelay || base) * 2)
|
|
126
|
+
hold(failureDelay)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const onVisibility = (): void => {
|
|
130
|
+
if (visible()) soon()
|
|
131
|
+
}
|
|
132
|
+
const onAbort = (): void => stop()
|
|
133
|
+
|
|
134
|
+
function stop(): void {
|
|
135
|
+
if (stopped) return
|
|
136
|
+
stopped = true
|
|
137
|
+
if (timer) clearTimeout(timer)
|
|
138
|
+
timer = null
|
|
139
|
+
inFlight?.abort()
|
|
140
|
+
inFlight = null
|
|
141
|
+
if (typeof document !== 'undefined') document.removeEventListener('visibilitychange', onVisibility)
|
|
142
|
+
options.signal?.removeEventListener('abort', onAbort)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (ids.length === 0 || options.signal?.aborted) {
|
|
146
|
+
stopped = true
|
|
147
|
+
return { stop: () => undefined, now: () => undefined }
|
|
148
|
+
}
|
|
149
|
+
if (typeof document !== 'undefined') document.addEventListener('visibilitychange', onVisibility)
|
|
150
|
+
options.signal?.addEventListener('abort', onAbort, { once: true })
|
|
151
|
+
schedule(base)
|
|
152
|
+
|
|
153
|
+
return { stop, now: soon }
|
|
154
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The community's operations — the one place in `@blogmcp/sdk/community` that spells kb-api's paths
|
|
3
|
+
* (docs/internal/m1-contracts.md §7.1, §7.4, §7.7, §8.3, §10.2) — and the limits a page checks before it posts.
|
|
4
|
+
*
|
|
5
|
+
* Every operation is a POST of a JSON body to one literal path; ids travel in the body, never in a path or a query,
|
|
6
|
+
* so a host's proxy or BFF forwards a list of literal routes and nothing else. COMMUNITY_OPS is kb-api's
|
|
7
|
+
* `routes/community-public-routes.json` (the allowlist kb-api generates from its routers) keyed by the client's
|
|
8
|
+
* name for each operation; test/community/ops-drift.spec.ts reads that file and fails on any difference.
|
|
9
|
+
*
|
|
10
|
+
* A host serves the operations under ONE relative base (the blog's proxy, a headless site's BFF): the public ones
|
|
11
|
+
* at `${base}/v1/public/<resource>/<verb>`, the notice form's at `${base}/trust/v1/notices/<verb>`
|
|
12
|
+
* (`communityOpPath`). The private surfaces have no operation here: their client is its own entry, later
|
|
13
|
+
* (community.md §3.7).
|
|
14
|
+
*
|
|
15
|
+
* Browser-safe: nothing here takes a key, and nothing imports `node:*`.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** Which kb-api router serves an operation: the public API (`/community/v1/public`) or the notice form (`/trust/v1`). */
|
|
19
|
+
export type CommunitySurface = 'public' | 'notice'
|
|
20
|
+
|
|
21
|
+
/** One operation: the client's name, the method and kb-api's literal path. */
|
|
22
|
+
export interface CommunityOp<N extends string = CommunityOpName> {
|
|
23
|
+
readonly name: N
|
|
24
|
+
readonly method: 'POST'
|
|
25
|
+
/** kb-api's path, exactly as `routes/community-public-routes.json` lists it. */
|
|
26
|
+
readonly path: string
|
|
27
|
+
readonly surface: CommunitySurface
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const op = <N extends string>(name: N, surface: CommunitySurface, path: string): CommunityOp<N> =>
|
|
31
|
+
Object.freeze({ name, method: 'POST', path, surface })
|
|
32
|
+
|
|
33
|
+
/** Every operation of the public client, by name. */
|
|
34
|
+
export const COMMUNITY_OPS = Object.freeze({
|
|
35
|
+
'discussions.list': op('discussions.list', 'public', '/community/v1/public/discussions/list'),
|
|
36
|
+
'discussions.head': op('discussions.head', 'public', '/community/v1/public/discussions/head'),
|
|
37
|
+
'messages.post': op('messages.post', 'public', '/community/v1/public/messages/post'),
|
|
38
|
+
'messages.edit': op('messages.edit', 'public', '/community/v1/public/messages/edit'),
|
|
39
|
+
'messages.delete': op('messages.delete', 'public', '/community/v1/public/messages/delete'),
|
|
40
|
+
'messages.report': op('messages.report', 'public', '/community/v1/public/messages/report'),
|
|
41
|
+
'reactions.set': op('reactions.set', 'public', '/community/v1/public/reactions/set'),
|
|
42
|
+
'blocks.set': op('blocks.set', 'public', '/community/v1/public/blocks/set'),
|
|
43
|
+
'blocks.list': op('blocks.list', 'public', '/community/v1/public/blocks/list'),
|
|
44
|
+
'profile.get': op('profile.get', 'public', '/community/v1/public/profile/get'),
|
|
45
|
+
'me.get': op('me.get', 'public', '/community/v1/public/me/get'),
|
|
46
|
+
'me.onboard': op('me.onboard', 'public', '/community/v1/public/me/onboard'),
|
|
47
|
+
'me.updateProfile': op('me.updateProfile', 'public', '/community/v1/public/me/profile/update'),
|
|
48
|
+
'me.notifications.list': op('me.notifications.list', 'public', '/community/v1/public/me/notifications/list'),
|
|
49
|
+
'me.notifications.markRead': op('me.notifications.markRead', 'public', '/community/v1/public/me/notifications/mark-read'),
|
|
50
|
+
'me.decisions.list': op('me.decisions.list', 'public', '/community/v1/public/me/decisions/list'),
|
|
51
|
+
'me.decisions.get': op('me.decisions.get', 'public', '/community/v1/public/me/decisions/get'),
|
|
52
|
+
'me.appeals.file': op('me.appeals.file', 'public', '/community/v1/public/me/appeals/file'),
|
|
53
|
+
'me.appeals.withdraw': op('me.appeals.withdraw', 'public', '/community/v1/public/me/appeals/withdraw'),
|
|
54
|
+
'me.appeals.list': op('me.appeals.list', 'public', '/community/v1/public/me/appeals/list'),
|
|
55
|
+
'me.reports.list': op('me.reports.list', 'public', '/community/v1/public/me/reports/list'),
|
|
56
|
+
'me.consents.withdraw': op('me.consents.withdraw', 'public', '/community/v1/public/me/consents/withdraw'),
|
|
57
|
+
'me.export': op('me.export', 'public', '/community/v1/public/me/export'),
|
|
58
|
+
'me.erase': op('me.erase', 'public', '/community/v1/public/me/erase'),
|
|
59
|
+
'me.cancelErase': op('me.cancelErase', 'public', '/community/v1/public/me/erase/cancel'),
|
|
60
|
+
'legal.current': op('legal.current', 'public', '/community/v1/public/legal/current'),
|
|
61
|
+
'notices.submit': op('notices.submit', 'notice', '/trust/v1/notices/submit'),
|
|
62
|
+
'notices.status': op('notices.status', 'notice', '/trust/v1/notices/status'),
|
|
63
|
+
'notices.appeal': op('notices.appeal', 'notice', '/trust/v1/notices/appeal'),
|
|
64
|
+
'notices.withdrawAppeal': op('notices.withdrawAppeal', 'notice', '/trust/v1/notices/appeal/withdraw'),
|
|
65
|
+
} as const)
|
|
66
|
+
|
|
67
|
+
/** The client's name of an operation: `discussions.list`, `me.notifications.markRead`, … */
|
|
68
|
+
export type CommunityOpName = keyof typeof COMMUNITY_OPS
|
|
69
|
+
|
|
70
|
+
/** The public router's mount: its operations' paths start with it, and a host's base stands in for it. */
|
|
71
|
+
const PUBLIC_MOUNT = '/community'
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Where an operation is served under a host's base: `/v1/public/<resource>/<verb>` for the public API (the base
|
|
75
|
+
* stands in for kb-api's `/community`), `/trust/v1/notices/<verb>` for the notice form.
|
|
76
|
+
*/
|
|
77
|
+
export function communityOpPath(o: Pick<CommunityOp, 'path' | 'surface'>): string {
|
|
78
|
+
return o.surface === 'public' && o.path.startsWith(`${PUBLIC_MOUNT}/`) ? o.path.slice(PUBLIC_MOUNT.length) : o.path
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ───────────────────────── limits (kb-api's, checked before a request) ─────────────────────────
|
|
82
|
+
|
|
83
|
+
/** A message body, in code points (kb-api's MESSAGE_MAX_CHARS and the DB's CHECK). */
|
|
84
|
+
export const COMMUNITY_MESSAGE_MAX = 4000
|
|
85
|
+
/** Links in one message (TOO_MANY_LINKS above it). */
|
|
86
|
+
export const COMMUNITY_MESSAGE_LINKS_MAX = 2
|
|
87
|
+
/** A report's note (`messages/report`). */
|
|
88
|
+
export const COMMUNITY_REPORT_NOTE_MAX = 1000
|
|
89
|
+
/** An appeal's text (`me/appeals/file`, `notices/appeal`). */
|
|
90
|
+
export const COMMUNITY_APPEAL_MAX = 4000
|
|
91
|
+
/** A notice's description (`notices/submit`). */
|
|
92
|
+
export const COMMUNITY_NOTICE_DESCRIPTION_MAX = 4000
|
|
93
|
+
/** A notifier's optional name. */
|
|
94
|
+
export const COMMUNITY_NOTIFIER_NAME_MAX = 120
|
|
95
|
+
/** A display name, in characters after kb-api's NFKC fold. */
|
|
96
|
+
export const COMMUNITY_DISPLAY_NAME_MIN = 2
|
|
97
|
+
export const COMMUNITY_DISPLAY_NAME_MAX = 80
|
|
98
|
+
/** A profile's bio. */
|
|
99
|
+
export const COMMUNITY_BIO_MAX = 280
|
|
100
|
+
/** A handle (kb-api's HANDLE_RE): lower case, digits and `_`, 3–30 characters, not starting with `_`. */
|
|
101
|
+
export const COMMUNITY_HANDLE_RE = /^[a-z0-9][a-z0-9_]{2,29}$/
|
|
102
|
+
/** Days between two handle changes (HANDLE_RATE). */
|
|
103
|
+
export const COMMUNITY_HANDLE_CHANGE_DAYS = 30
|
|
104
|
+
/** Articles one `discussions/head` asks about. */
|
|
105
|
+
export const COMMUNITY_HEAD_ARTICLES_MAX = 50
|
|
106
|
+
/** Top-level messages in one `discussions/list` page (the cursor is the last one's `seq`). */
|
|
107
|
+
export const COMMUNITY_PAGE_SIZE = 50
|
|
108
|
+
/** Replies one top-level message carries before `repliesTruncated`. */
|
|
109
|
+
export const COMMUNITY_REPLIES_MAX = 200
|
|
110
|
+
|
|
111
|
+
/** The Turnstile action the notice form's widget declares; kb-api refuses a token issued for any other. */
|
|
112
|
+
export const COMMUNITY_NOTICE_TURNSTILE_ACTION = 'community_notice'
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The confirmation a notice carries (DSA art. 16(2)(d): the notifier's good-faith statement). Without it in
|
|
116
|
+
* `confirmations`, `notices/submit` answers CONFIRMATION_REQUIRED with this code.
|
|
117
|
+
*/
|
|
118
|
+
export const COMMUNITY_GOOD_FAITH = 'good_faith'
|
|
119
|
+
|
|
120
|
+
/** A `clientMsgId`: a UUID (the pattern kb-api validates). */
|
|
121
|
+
export const COMMUNITY_CLIENT_MSG_ID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* A fresh `clientMsgId` for one post (a v4 UUID). Mint it when the composer submits and send the SAME id again on
|
|
125
|
+
* a retry: kb-api answers a repeat with the message it already stored instead of posting twice.
|
|
126
|
+
*
|
|
127
|
+
* `crypto.randomUUID` exists only in secure contexts; a page opened over plain http on a LAN address (a phone
|
|
128
|
+
* during development) gets a UUID from `crypto.getRandomValues`, which exists everywhere.
|
|
129
|
+
*/
|
|
130
|
+
export function newClientMsgId(): string {
|
|
131
|
+
const c = globalThis.crypto
|
|
132
|
+
if (typeof c?.randomUUID === 'function') return c.randomUUID()
|
|
133
|
+
const bytes = c.getRandomValues(new Uint8Array(16))
|
|
134
|
+
// `?? 0` only for consumers compiling with noUncheckedIndexedAccess: the array has 16 bytes.
|
|
135
|
+
bytes[6] = ((bytes[6] ?? 0) & 0x0f) | 0x40
|
|
136
|
+
bytes[8] = ((bytes[8] ?? 0) & 0x3f) | 0x80
|
|
137
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('')
|
|
138
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`
|
|
139
|
+
}
|