@blogmcp/community 0.4.1 → 0.5.1
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/blogmcp-community-private.global.js +8 -0
- package/dist/blogmcp-community.global.js +3 -3
- package/dist/chunks/{mount-Ts_r2fwt.js → mount-SKmc4NgD.js} +39 -9
- package/dist/chunks/private-mount-BgfUSd3g.js +5548 -0
- package/dist/core/attrs.d.ts +15 -0
- package/dist/core/settings.d.ts +5 -0
- package/dist/index.js +2 -2
- package/dist/private/core/dock.d.ts +38 -0
- package/dist/private/core/push.d.ts +49 -0
- package/dist/private/core/realtime.d.ts +57 -0
- package/dist/private/core/setup.d.ts +43 -0
- package/dist/private/core/view.d.ts +179 -0
- package/dist/private/dm/composer.d.ts +34 -0
- package/dist/private/dm/controller.d.ts +162 -0
- package/dist/private/dm/index.d.ts +14 -0
- package/dist/private/dm/links.d.ts +33 -0
- package/dist/private/dm/muted.d.ts +37 -0
- package/dist/private/dm/search.d.ts +7 -0
- package/dist/private/dm/text.d.ts +27 -0
- package/dist/private/dm/ui/DmBlocks.vue.d.ts +8 -0
- package/dist/private/dm/ui/DmCases.vue.d.ts +8 -0
- package/dist/private/dm/ui/DmComposer.vue.d.ts +20 -0
- package/dist/private/dm/ui/DmInbox.vue.d.ts +12 -0
- package/dist/private/dm/ui/DmLinkWarning.vue.d.ts +8 -0
- package/dist/private/dm/ui/DmLocalAids.vue.d.ts +8 -0
- package/dist/private/dm/ui/DmMessageText.vue.d.ts +8 -0
- package/dist/private/dm/ui/DmNew.vue.d.ts +12 -0
- package/dist/private/dm/ui/DmReport.vue.d.ts +8 -0
- package/dist/private/dm/ui/DmRequests.vue.d.ts +12 -0
- package/dist/private/dm/ui/DmScreens.vue.d.ts +9 -0
- package/dist/private/dm/ui/DmThread.vue.d.ts +9 -0
- package/dist/private/element.d.ts +23 -0
- package/dist/private/global.d.ts +10 -0
- package/dist/private/i18n.d.ts +41 -0
- package/dist/private/index.d.ts +32 -0
- package/dist/private/index.js +57 -0
- package/dist/private/mount.d.ts +55 -0
- package/dist/private/pages/api.d.ts +44 -0
- package/dist/private/pages/element.d.ts +6 -0
- package/dist/private/pages/fragment.d.ts +43 -0
- package/dist/private/pages/i18n.d.ts +25 -0
- package/dist/private/pages/index.d.ts +1 -0
- package/dist/private/pages/pages.d.ts +276 -0
- package/dist/private/pages/proof.d.ts +36 -0
- package/dist/private/pages/provider-view.d.ts +33 -0
- package/dist/private/pages/setup.d.ts +42 -0
- package/dist/private/pages/ui/AccountPageRoot.vue.d.ts +9 -0
- package/dist/private/pages/ui/PageActivation.vue.d.ts +9 -0
- package/dist/private/pages/ui/PageAddress.vue.d.ts +8 -0
- package/dist/private/pages/ui/PageDeviceApprove.vue.d.ts +8 -0
- package/dist/private/pages/ui/PageDevices.vue.d.ts +8 -0
- package/dist/private/pages/ui/PageExport.vue.d.ts +8 -0
- package/dist/private/pages/ui/PageMessages.vue.d.ts +9 -0
- package/dist/private/react.d.ts +12 -0
- package/dist/private/react.js +36 -0
- package/dist/private/ui/ChatDockRoot.vue.d.ts +20 -0
- package/dist/private/ui/LegalBlocks.vue.d.ts +10 -0
- package/dist/private/ui/LegalRun.vue.d.ts +7 -0
- package/dist/private/ui/LegalRuns.vue.d.ts +6 -0
- package/dist/private/ui/LegalText.vue.d.ts +13 -0
- package/dist/private/ui/PrivateScreens.vue.d.ts +14 -0
- package/dist/private/ui/PrivateViewRoot.vue.d.ts +23 -0
- package/dist/private/ui/PromiseBlock.vue.d.ts +9 -0
- package/dist/private/ui/ProviderLine.vue.d.ts +8 -0
- package/dist/private/ui/ScreenActivation.vue.d.ts +8 -0
- package/dist/private/ui/ScreenAddress.vue.d.ts +8 -0
- package/dist/private/ui/ScreenApproval.vue.d.ts +8 -0
- package/dist/private/ui/ScreenDevices.vue.d.ts +8 -0
- package/dist/private/ui/ScreenHome.vue.d.ts +12 -0
- package/dist/private/ui/ScreenNotifications.vue.d.ts +8 -0
- package/dist/private/ui/ScreenReads.vue.d.ts +8 -0
- package/dist/private/ui/ScreenSettings.vue.d.ts +8 -0
- package/dist/private/ui/legal.d.ts +15 -0
- package/dist/private/ui/root.d.ts +35 -0
- package/dist/private/ui/text.d.ts +22 -0
- package/dist/private/vue.d.ts +88 -0
- package/dist/private/vue.js +72 -0
- package/dist/react.js +14 -14
- package/dist/vue.js +14 -14
- package/package.json +23 -6
- package/src/core/attrs.ts +19 -0
- package/src/core/settings.ts +55 -12
- package/src/private/core/dock.ts +105 -0
- package/src/private/core/push.ts +166 -0
- package/src/private/core/realtime.ts +109 -0
- package/src/private/core/setup.ts +75 -0
- package/src/private/core/view.ts +920 -0
- package/src/private/dm/composer.ts +74 -0
- package/src/private/dm/controller.ts +636 -0
- package/src/private/dm/index.ts +14 -0
- package/src/private/dm/links.ts +55 -0
- package/src/private/dm/muted.ts +160 -0
- package/src/private/dm/search.ts +18 -0
- package/src/private/dm/text.ts +60 -0
- package/src/private/dm/ui/DmBlocks.vue +27 -0
- package/src/private/dm/ui/DmCases.vue +58 -0
- package/src/private/dm/ui/DmComposer.vue +68 -0
- package/src/private/dm/ui/DmInbox.vue +34 -0
- package/src/private/dm/ui/DmLinkWarning.vue +27 -0
- package/src/private/dm/ui/DmLocalAids.vue +43 -0
- package/src/private/dm/ui/DmMessageText.vue +16 -0
- package/src/private/dm/ui/DmNew.vue +46 -0
- package/src/private/dm/ui/DmReport.vue +93 -0
- package/src/private/dm/ui/DmRequests.vue +50 -0
- package/src/private/dm/ui/DmScreens.vue +66 -0
- package/src/private/dm/ui/DmThread.vue +235 -0
- package/src/private/element.ts +90 -0
- package/src/private/global.ts +14 -0
- package/src/private/i18n.ts +127 -0
- package/src/private/index.ts +68 -0
- package/src/private/messages/cs.json +311 -0
- package/src/private/messages/en.json +319 -0
- package/src/private/mount.ts +103 -0
- package/src/private/pages/api.ts +96 -0
- package/src/private/pages/build.d.mts +2 -0
- package/src/private/pages/build.mjs +72 -0
- package/src/private/pages/element.ts +49 -0
- package/src/private/pages/fragment.ts +63 -0
- package/src/private/pages/i18n.ts +82 -0
- package/src/private/pages/index.ts +10 -0
- package/src/private/pages/messages/cs.json +97 -0
- package/src/private/pages/messages/en.json +97 -0
- package/src/private/pages/pages.ts +410 -0
- package/src/private/pages/proof.ts +110 -0
- package/src/private/pages/provider-view.ts +121 -0
- package/src/private/pages/setup.ts +79 -0
- package/src/private/pages/ui/AccountPageRoot.vue +45 -0
- package/src/private/pages/ui/PageActivation.vue +42 -0
- package/src/private/pages/ui/PageAddress.vue +39 -0
- package/src/private/pages/ui/PageDeviceApprove.vue +62 -0
- package/src/private/pages/ui/PageDevices.vue +84 -0
- package/src/private/pages/ui/PageExport.vue +35 -0
- package/src/private/pages/ui/PageMessages.vue +39 -0
- package/src/private/pages/ui/account.css +6 -0
- package/src/private/react.ts +49 -0
- package/src/private/shims.d.ts +6 -0
- package/src/private/ui/ChatDockRoot.vue +119 -0
- package/src/private/ui/LegalBlocks.vue +30 -0
- package/src/private/ui/LegalRun.vue +13 -0
- package/src/private/ui/LegalRuns.vue +20 -0
- package/src/private/ui/LegalText.vue +25 -0
- package/src/private/ui/PrivateScreens.vue +61 -0
- package/src/private/ui/PrivateViewRoot.vue +70 -0
- package/src/private/ui/PromiseBlock.vue +18 -0
- package/src/private/ui/ProviderLine.vue +20 -0
- package/src/private/ui/ScreenActivation.vue +82 -0
- package/src/private/ui/ScreenAddress.vue +37 -0
- package/src/private/ui/ScreenApproval.vue +71 -0
- package/src/private/ui/ScreenDevices.vue +42 -0
- package/src/private/ui/ScreenHome.vue +29 -0
- package/src/private/ui/ScreenNotifications.vue +44 -0
- package/src/private/ui/ScreenReads.vue +23 -0
- package/src/private/ui/ScreenSettings.vue +44 -0
- package/src/private/ui/legal.ts +29 -0
- package/src/private/ui/private.css +90 -0
- package/src/private/ui/root.ts +89 -0
- package/src/private/ui/text.ts +40 -0
- package/src/private/vue.ts +87 -0
|
@@ -0,0 +1,920 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The private view's brain (m4-contracts.md §6.2): one controller per mounted root — `<blogmcp-private-view>` holds
|
|
3
|
+
* one, `<blogmcp-chat-dock>` holds one for all its windows — over the SDK's private client
|
|
4
|
+
* (`@blogmcp/sdk/community/private`), the only way `src/private/**` reaches kb-api. Framework-free but for Vue's
|
|
5
|
+
* reactivity, as the public controllers.
|
|
6
|
+
*
|
|
7
|
+
* **What it mounts, and when.** The host's own `me/get` (the public client, through the host's relative base) decides
|
|
8
|
+
* first, as `privateMountOf` reads it: no member → the sign-in sentence; no private block, or a state other than `live`
|
|
9
|
+
* and `read_only` → nothing at all (a flag-false instance shows and calls nothing private, the M0d rule); under
|
|
10
|
+
* `provider_page` → only the link to the account host's `/zpravy` (§6.4). Only then is the private client started:
|
|
11
|
+
* `session.start()` (another member's stored session on this browser ended, §5.9), `state`, `activation/state`.
|
|
12
|
+
*
|
|
13
|
+
* **The screens** (§6.2's M2b list): the address step, the activation, the approval of this device (a pending one) or
|
|
14
|
+
* of others (an approved one), the devices, the read log, the private notifications with this browser's private push,
|
|
15
|
+
* the private settings (a stubbed client until DM3 serves `settings/get|set`), and on every one the provider line
|
|
16
|
+
* (§5.11: the current `private_contact` text's `body_json.provider`) and, on the activation and the overview, the
|
|
17
|
+
* promise rendered from PU1's matrix (`privatePromise`, invariant 12). The order a member meets them is the order
|
|
18
|
+
* kb-api enforces: the address first (every other operation answers `ADDRESS_REQUIRED` until it is verified), then —
|
|
19
|
+
* on a pending device — the approval (kb-api accepts an activation only from an approved device), then the activation.
|
|
20
|
+
*
|
|
21
|
+
* **The art. 9 refusal stores nothing.** kb-api's `activation/accept` answers `ACTIVATION_REQUIRED` to `{ refused:
|
|
22
|
+
* true }` and keeps nothing of it (services/community/private/activation.ts), so the refusal button posts nothing: the
|
|
23
|
+
* screen says that nothing was recorded and that the private surfaces stay closed.
|
|
24
|
+
*
|
|
25
|
+
* **One controller, several windows.** The dock's windows share this controller, so a screen's reads (`open`) do not
|
|
26
|
+
* wait for another window's action, and a refusal is kept per screen (`screenFailures`): one window's refusal is not
|
|
27
|
+
* shown in another.
|
|
28
|
+
*
|
|
29
|
+
* **The live inbox** (§6.3, C.8): the host's `realtime` (core/realtime.ts) starts once the view is ready, with this
|
|
30
|
+
* controller's private client, and stops with it. Its events are content-free codes: `resync` reloads the view (in
|
|
31
|
+
* place: the phase stays `ready` while it runs, `reloading`, so nothing flashes and the events keep reaching the
|
|
32
|
+
* conversations; a second `resync` meanwhile asks for one more pass, never a second load beside it), an
|
|
33
|
+
* account event (`device_approved`, `address_verified`, `activation_done`) re-reads the member's state, and every one
|
|
34
|
+
* refreshes the unread dot. A connection the server or kb-api ends for this device (a sign-out, a denial) is not
|
|
35
|
+
* opened again here: the view re-reads its state once, which says what happened, and the next load starts anew.
|
|
36
|
+
*
|
|
37
|
+
* **Final refusals** (`FINAL_REFUSALS`): a refusal no retry cures ends the view's private part on this page — said
|
|
38
|
+
* once, without a retry, and without the private-root mark or the dock's launcher (`isFinalFailure`).
|
|
39
|
+
*
|
|
40
|
+
* **The conversations** (DM8b, `src/private/dm/**`): once the view is ready it makes the DM controller over the SDK's
|
|
41
|
+
* DM client (`createPrivateDmClient`) with this controller's private client, shown on the `messages` screen and in the
|
|
42
|
+
* dock; every inbox event goes to it too, and it ends with the view, a sign-in change (the host's `me/get` answering
|
|
43
|
+
* `signIn` included) or a forgotten device (its drafts, its outbox and every decrypted message with it). Of the inbox
|
|
44
|
+
* events only those that can move the unread count refresh it (`refreshesUnread`, C.11 RF8-C (h)): typing, receipts and
|
|
45
|
+
* edits do not. kb-api reads a conversation's own notifications (`dm_message`, `dm_request`) with its read mark, so a
|
|
46
|
+
* read that read any refreshes the dot (`notificationsRead`) and nothing else is asked.
|
|
47
|
+
*
|
|
48
|
+
* Every text is a catalogue key (i18n.ts); kb-api's texts (`activation/state`) are rendered as text, never as markup.
|
|
49
|
+
*/
|
|
50
|
+
import { reactive } from 'vue'
|
|
51
|
+
import {
|
|
52
|
+
createPrivateDmClient,
|
|
53
|
+
privateBlockOf,
|
|
54
|
+
privatePromise,
|
|
55
|
+
type PrivateActivationState,
|
|
56
|
+
type PrivateApprovalRequest,
|
|
57
|
+
type PrivateApprovalStart,
|
|
58
|
+
type PrivateClient,
|
|
59
|
+
type PrivateDevice,
|
|
60
|
+
type PrivateFailure,
|
|
61
|
+
type PrivateLegalText,
|
|
62
|
+
type PrivateModuleBlock,
|
|
63
|
+
type PrivateOutcome,
|
|
64
|
+
type PrivatePromiseKey,
|
|
65
|
+
type PrivateReadHour,
|
|
66
|
+
type PrivateStateAnswer,
|
|
67
|
+
} from '@blogmcp/sdk/community/private'
|
|
68
|
+
import type { CommunityClient } from '@blogmcp/sdk/community'
|
|
69
|
+
import { FINAL_REFUSALS } from '../i18n'
|
|
70
|
+
import { createDmView, type DmViewController } from '../dm/controller'
|
|
71
|
+
import { indexedDbMutedStore, type MutedStore } from '../dm/muted'
|
|
72
|
+
import type { PrivatePushPort } from './push'
|
|
73
|
+
import { refreshesUnread, startRealtime, type PrivateInboxEvent, type PrivateRealtimeInput, type PrivateRealtimeState, type RunningRealtime } from './realtime'
|
|
74
|
+
|
|
75
|
+
/** The screens of M2b, and `messages` — the conversations, whose own screens are DM8b's (src/private/dm/**). */
|
|
76
|
+
export const PRIVATE_SCREENS = ['home', 'messages', 'address', 'activation', 'approval', 'devices', 'reads', 'notifications', 'settings'] as const
|
|
77
|
+
export type PrivateScreen = (typeof PRIVATE_SCREENS)[number]
|
|
78
|
+
|
|
79
|
+
export type PrivatePhase = 'idle' | 'loading' | 'ready' | 'signIn' | 'dark' | 'link' | 'failed'
|
|
80
|
+
|
|
81
|
+
/** A text and its params, turned into words by the view. */
|
|
82
|
+
export interface PrivateTextRef {
|
|
83
|
+
readonly msg: string
|
|
84
|
+
readonly params?: Record<string, string | number>
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The provider line (§5.11): `body_json.provider` of the current `private_contact` text. */
|
|
88
|
+
export interface ProviderLine {
|
|
89
|
+
readonly name: string
|
|
90
|
+
readonly ico: string
|
|
91
|
+
readonly address: string
|
|
92
|
+
readonly links: Readonly<Partial<Record<'contact' | 'rules' | 'terms' | 'privacy', string>>>
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** One private notification as the screen shows it (PU3 serves `notifications/list`). */
|
|
96
|
+
export interface PrivateNotificationItem {
|
|
97
|
+
readonly id: string
|
|
98
|
+
readonly code: string
|
|
99
|
+
readonly createdAt: string | null
|
|
100
|
+
readonly readAt: string | null
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export type DmAudience = 'nobody' | 'shared' | 'anyone'
|
|
104
|
+
export const DM_AUDIENCES: readonly DmAudience[] = ['nobody', 'shared', 'anyone']
|
|
105
|
+
|
|
106
|
+
/** The private settings (DM3's `settings/get|set`: `dm_audience` within the ceiling, read receipts, typing). */
|
|
107
|
+
export interface PrivateSettingsValues {
|
|
108
|
+
dmAudience: DmAudience
|
|
109
|
+
dmReadReceipts: boolean
|
|
110
|
+
dmTyping: boolean
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The codes the account's notification screen words: Appendix A.5's private codes but the conversations' own
|
|
115
|
+
* (`dm_message`, `dm_request`), which DM8b's conversation screens word. Any other code is left out, never shown raw;
|
|
116
|
+
* „mark all read" marks every row (`{ all: true }`), so a row left out never keeps the unread dot on.
|
|
117
|
+
*/
|
|
118
|
+
export const PRIVATE_NOTIFICATION_CODES = [
|
|
119
|
+
'device_new',
|
|
120
|
+
'device_request',
|
|
121
|
+
'private_decision',
|
|
122
|
+
'private_statement',
|
|
123
|
+
'private_terms_change',
|
|
124
|
+
'private_read_only',
|
|
125
|
+
'private_erasure_pending',
|
|
126
|
+
] as const
|
|
127
|
+
|
|
128
|
+
/** The account events after which the member's state is read again (§6.3's inbox codes of M2b). */
|
|
129
|
+
export const ACCOUNT_STATE_EVENTS: readonly string[] = Object.freeze(['device_approved', 'address_verified', 'activation_done'])
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* `approval/start`'s rate refusals (HTTP 429): the member's daily cap, the network's hourly cap, and a device's failed
|
|
133
|
+
* requests (§5.10). The approval screen says which and when to try again instead of the generic failure.
|
|
134
|
+
*/
|
|
135
|
+
export const APPROVAL_LIMIT_CODES: Readonly<Record<string, 'day' | 'hour' | 'device'>> = Object.freeze({
|
|
136
|
+
QUOTA_DAILY: 'day',
|
|
137
|
+
QUOTA_IP_HOURLY: 'hour',
|
|
138
|
+
TOO_FAST: 'device',
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
export interface PrivateViewUi {
|
|
142
|
+
phase: PrivatePhase
|
|
143
|
+
/**
|
|
144
|
+
* A ready view reloading (a `resync`, a forget): the phase stays `ready` meanwhile, so the screens, the dock's panel
|
|
145
|
+
* and the conversation windows stay as they are — no loading placeholder flashes — and the inbox events reach the
|
|
146
|
+
* conversations; it ends with the reload, which then sets the phase it found.
|
|
147
|
+
*/
|
|
148
|
+
reloading: boolean
|
|
149
|
+
failure: PrivateTextRef | null
|
|
150
|
+
block: PrivateModuleBlock | null
|
|
151
|
+
screen: PrivateScreen
|
|
152
|
+
state: PrivateStateAnswer | null
|
|
153
|
+
activation: PrivateActivationState | null
|
|
154
|
+
provider: ProviderLine | null
|
|
155
|
+
promise: { keys: readonly PrivatePromiseKey[]; delegated: boolean } | null
|
|
156
|
+
/** The texts' placeholders the screen fills (`display` of activation/state, §5.11). */
|
|
157
|
+
busy: boolean
|
|
158
|
+
announcement: PrivateTextRef | null
|
|
159
|
+
announceId: number
|
|
160
|
+
/** Each screen's own refusal, shown on that screen only (cleared when the screen opens again or acts again). */
|
|
161
|
+
screenFailures: Partial<Record<PrivateScreen, PrivateTextRef>>
|
|
162
|
+
/** The member declined the art. 9 consent on this screen: nothing was recorded (see the module comment). */
|
|
163
|
+
art9Declined: boolean
|
|
164
|
+
devices: readonly PrivateDevice[] | null
|
|
165
|
+
reads: readonly PrivateReadHour[] | null
|
|
166
|
+
approvals: readonly PrivateApprovalRequest[] | null
|
|
167
|
+
approvalStart: PrivateApprovalStart | null
|
|
168
|
+
/** A start the rate limits refused (APPROVAL_LIMIT_CODES), until the member tries again. */
|
|
169
|
+
approvalLimited: 'day' | 'hour' | 'device' | null
|
|
170
|
+
/** The code an approved device shows for a request, and until when. */
|
|
171
|
+
shownCode: { approvalId: string; code: string; expiresAt: string } | null
|
|
172
|
+
addressLink: { url: string; expiresAt: string } | null
|
|
173
|
+
notifications: readonly PrivateNotificationItem[] | null
|
|
174
|
+
/** The next page of the notifications (`nextCursor`), or null when the list is complete. */
|
|
175
|
+
notificationsCursor: string | null
|
|
176
|
+
unread: number
|
|
177
|
+
settings: { values: PrivateSettingsValues; ceiling: DmAudience } | null
|
|
178
|
+
settingsUnavailable: boolean
|
|
179
|
+
/**
|
|
180
|
+
* `stale`: the browser's subscription was made for a VAPID key the tenant no longer serves (a rotation). The private
|
|
181
|
+
* view never ends a subscription (B-55), so it asks the member to switch the website's notifications off and on —
|
|
182
|
+
* the public element ends a subscription made for another key — and the next start re-posts the new endpoint.
|
|
183
|
+
*/
|
|
184
|
+
push: { offered: boolean; status: 'off' | 'on' | 'busy' | 'unsupported'; note: 'denied' | 'refused' | 'stale' | null }
|
|
185
|
+
/** The live inbox (see the module comment): `off` when the host passed none. */
|
|
186
|
+
realtime: PrivateRealtimeState
|
|
187
|
+
/** The conversations (DM8b), once the view is ready; null before, and where the private client offers no calls. */
|
|
188
|
+
dm: DmViewController | null
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export interface PrivateViewController {
|
|
192
|
+
readonly state: PrivateViewUi
|
|
193
|
+
load(): Promise<void>
|
|
194
|
+
open(screen: PrivateScreen): Promise<void>
|
|
195
|
+
/** The screen a member should see first (address → approval of a pending device → activation → overview). */
|
|
196
|
+
firstScreen(): PrivateScreen
|
|
197
|
+
refreshUnread(): Promise<void>
|
|
198
|
+
approvalStart(via: 'code' | 'email'): Promise<void>
|
|
199
|
+
/** Clears a rate refusal of `approvalStart`, so the two ways to start are offered again. */
|
|
200
|
+
approvalRetry(): void
|
|
201
|
+
approvalRedeem(code: string): Promise<void>
|
|
202
|
+
approvalShow(approvalId: string): Promise<void>
|
|
203
|
+
approvalDeny(approvalId: string): Promise<void>
|
|
204
|
+
requestAddressLink(): Promise<void>
|
|
205
|
+
activate(input: { acceptTerms: boolean; art9: 'accept' | 'refuse'; art9Tenant: boolean }): Promise<void>
|
|
206
|
+
withdraw(): Promise<void>
|
|
207
|
+
signOutDevice(deviceId: string): Promise<void>
|
|
208
|
+
signOutAll(): Promise<void>
|
|
209
|
+
forget(): Promise<void>
|
|
210
|
+
markAllRead(): Promise<void>
|
|
211
|
+
moreNotifications(): Promise<void>
|
|
212
|
+
setSetting<K extends keyof PrivateSettingsValues>(key: K, value: PrivateSettingsValues[K]): Promise<void>
|
|
213
|
+
enablePush(): Promise<void>
|
|
214
|
+
disablePush(): Promise<void>
|
|
215
|
+
/** The sentence the activation's `private_activation` text shows once notifications are on (§7.5), or null. */
|
|
216
|
+
pushSentence(): string | null
|
|
217
|
+
/** The promise's `{site}`, `{entity}`, `{ico}`. */
|
|
218
|
+
promiseParams(): Record<string, string>
|
|
219
|
+
dispose(): void
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export interface PrivateViewOptions {
|
|
223
|
+
/** The host's public client (its relative base): `me/get` says who is signed in and what the module allows. */
|
|
224
|
+
host: CommunityClient
|
|
225
|
+
/** The private client, made once the host's `me/get` admits it (never before: a dark instance is not asked). */
|
|
226
|
+
client: () => PrivateClient
|
|
227
|
+
/** This browser's push glue (null where the host offers no worker). */
|
|
228
|
+
push: PrivatePushPort | null
|
|
229
|
+
/** The website's name for the promise's `{site}` (the host's `site-name`; default the page's host name). */
|
|
230
|
+
siteName: string
|
|
231
|
+
/** The host's live inbox (core/realtime.ts): the SDK's `createPrivateRealtime`, or a source of its own. */
|
|
232
|
+
realtime?: PrivateRealtimeInput | null
|
|
233
|
+
/** The site's poradna (`routes.consultationPath`), for a report's „Nahlásit výrobci“ (§7.5). */
|
|
234
|
+
consultationPath?: string | null
|
|
235
|
+
/** The member's muted words' store (default: this browser's IndexedDB, dm/muted.ts); a test passes its own. */
|
|
236
|
+
mutedStore?: (memberId: string) => MutedStore
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Whether the view ended on a refusal no retry cures (FINAL_REFUSALS): nothing private shows on this page. */
|
|
240
|
+
export function isFinalFailure(state: Pick<PrivateViewUi, 'phase' | 'failure'>): boolean {
|
|
241
|
+
const msg = state.phase === 'failed' ? (state.failure?.msg ?? '') : ''
|
|
242
|
+
return msg.startsWith('error:') && FINAL_REFUSALS.includes(msg.slice('error:'.length))
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
type Rec = Record<string, unknown>
|
|
246
|
+
const isRec = (v: unknown): v is Rec => typeof v === 'object' && v !== null && !Array.isArray(v)
|
|
247
|
+
const str = (v: unknown): string | null => (typeof v === 'string' && v !== '' ? v : null)
|
|
248
|
+
|
|
249
|
+
/** The provider line of a `private_contact` text, or null when its `body_json.provider` is incomplete. */
|
|
250
|
+
export function providerOf(texts: readonly PrivateLegalText[] | null | undefined): ProviderLine | null {
|
|
251
|
+
const contact = (texts ?? []).find((t) => t.kind === 'private_contact')
|
|
252
|
+
const p = contact?.bodyJson?.provider
|
|
253
|
+
if (!isRec(p)) return null
|
|
254
|
+
const name = str(p.name)
|
|
255
|
+
const ico = str(p.ico)
|
|
256
|
+
if (!name || !ico) return null
|
|
257
|
+
const links: Partial<Record<'contact' | 'rules' | 'terms' | 'privacy', string>> = {}
|
|
258
|
+
const raw = isRec(p.links) ? p.links : {}
|
|
259
|
+
for (const k of ['contact', 'rules', 'terms', 'privacy'] as const) {
|
|
260
|
+
const href = str(raw[k])
|
|
261
|
+
// Only an https address is a link; anything else is left out (the text is data the T&S console published).
|
|
262
|
+
if (href && /^https:\/\/[^\s/?#]+(?:[/?#][^\s]*)?$/i.test(href)) links[k] = href
|
|
263
|
+
}
|
|
264
|
+
return Object.freeze({ name, ico, address: str(p.address) ?? '', links: Object.freeze(links) })
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** The variant ids kb-api applied (§5.11's table), across every served text. */
|
|
268
|
+
function variantsOf(a: PrivateActivationState | null): Set<string> {
|
|
269
|
+
const out = new Set<string>()
|
|
270
|
+
for (const t of [...(a?.texts ?? []), ...(a?.announced ?? []), ...(a?.art9 ? [a.art9] : [])]) for (const v of t.variants ?? []) out.add(v)
|
|
271
|
+
return out
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* The promise for this member, from the variants kb-api resolved for this session (§5.11): `delegated` (a delegated
|
|
276
|
+
* identity, or a native one through the tenant's host), `same_entity`, the rendering and the host kind — the same facts
|
|
277
|
+
* the texts were rendered from, so the promise and the accepted texts cannot disagree.
|
|
278
|
+
*/
|
|
279
|
+
export function promiseFor(a: PrivateActivationState | null, block: PrivateModuleBlock | null) {
|
|
280
|
+
const v = variantsOf(a)
|
|
281
|
+
const rendering = block?.rendering === 'provider_page' || v.has('rendering_provider_page') ? 'provider_page' : 'tenant'
|
|
282
|
+
return privatePromise({
|
|
283
|
+
space: 'dm',
|
|
284
|
+
identity: v.has('delegated') ? 'delegated' : 'native',
|
|
285
|
+
rendering,
|
|
286
|
+
sameEntity: v.has('same_entity'),
|
|
287
|
+
hostKind: v.has('host_tenant') ? 'tenant' : 'blogmcp',
|
|
288
|
+
})
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** The `push` section of the `private_activation` text (§7.5: the sentence shown once notifications are on). */
|
|
292
|
+
export function pushSectionOf(a: PrivateActivationState | null): string | null {
|
|
293
|
+
const text = (a?.texts ?? []).find((t) => t.kind === 'private_activation')
|
|
294
|
+
const sections = text?.bodyJson?.sections
|
|
295
|
+
if (!Array.isArray(sections)) return null
|
|
296
|
+
for (const s of sections) {
|
|
297
|
+
if (!isRec(s)) continue
|
|
298
|
+
const id = str(s.id) ?? str(s.key)
|
|
299
|
+
if (id !== 'push' || (s.surface !== undefined && s.surface !== 'screen')) continue
|
|
300
|
+
return str(s.text) ?? str(s.body)
|
|
301
|
+
}
|
|
302
|
+
return null
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const failureRef = (f: PrivateFailure | { code: string }): PrivateTextRef => {
|
|
306
|
+
const code = f.code
|
|
307
|
+
return { msg: `error:${code}` }
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function settingsOf(value: unknown): { values: PrivateSettingsValues; ceiling: DmAudience } | null {
|
|
311
|
+
if (!isRec(value)) return null
|
|
312
|
+
const s = isRec(value.settings) ? value.settings : value
|
|
313
|
+
const audience = DM_AUDIENCES.includes(s.dmAudience as DmAudience) ? (s.dmAudience as DmAudience) : null
|
|
314
|
+
if (audience === null) return null
|
|
315
|
+
const ceilingRaw = isRec(value.ceiling) ? value.ceiling.dmAudience : value.ceiling
|
|
316
|
+
const ceiling = DM_AUDIENCES.includes(ceilingRaw as DmAudience) ? (ceilingRaw as DmAudience) : audience
|
|
317
|
+
return { values: { dmAudience: audience, dmReadReceipts: s.dmReadReceipts === true, dmTyping: s.dmTyping === true }, ceiling }
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** The next page's cursor of a `notifications/list` answer, or null. */
|
|
321
|
+
function cursorOf(value: unknown): string | null {
|
|
322
|
+
return isRec(value) ? str(value.nextCursor) : null
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function notificationsOf(value: unknown): PrivateNotificationItem[] {
|
|
326
|
+
const items = isRec(value) && Array.isArray(value.items) ? value.items : []
|
|
327
|
+
const out: PrivateNotificationItem[] = []
|
|
328
|
+
for (const n of items) {
|
|
329
|
+
if (!isRec(n) || !str(n.id) || !(PRIVATE_NOTIFICATION_CODES as readonly string[]).includes(n.code as string)) continue
|
|
330
|
+
out.push({ id: n.id as string, code: n.code as string, createdAt: str(n.createdAt), readAt: str(n.readAt) })
|
|
331
|
+
}
|
|
332
|
+
return out
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export function createPrivateView(o: PrivateViewOptions): PrivateViewController {
|
|
336
|
+
let client: PrivateClient | null = null
|
|
337
|
+
let disposed = false
|
|
338
|
+
|
|
339
|
+
const state = reactive<PrivateViewUi>({
|
|
340
|
+
phase: 'idle',
|
|
341
|
+
reloading: false,
|
|
342
|
+
failure: null,
|
|
343
|
+
block: null,
|
|
344
|
+
screen: 'home',
|
|
345
|
+
state: null,
|
|
346
|
+
activation: null,
|
|
347
|
+
provider: null,
|
|
348
|
+
promise: null,
|
|
349
|
+
busy: false,
|
|
350
|
+
announcement: null,
|
|
351
|
+
announceId: 0,
|
|
352
|
+
screenFailures: {},
|
|
353
|
+
art9Declined: false,
|
|
354
|
+
devices: null,
|
|
355
|
+
reads: null,
|
|
356
|
+
approvals: null,
|
|
357
|
+
approvalStart: null,
|
|
358
|
+
approvalLimited: null,
|
|
359
|
+
shownCode: null,
|
|
360
|
+
addressLink: null,
|
|
361
|
+
notifications: null,
|
|
362
|
+
notificationsCursor: null,
|
|
363
|
+
unread: 0,
|
|
364
|
+
settings: null,
|
|
365
|
+
settingsUnavailable: false,
|
|
366
|
+
push: { offered: false, status: 'off', note: null },
|
|
367
|
+
realtime: 'off',
|
|
368
|
+
dm: null,
|
|
369
|
+
}) as PrivateViewUi
|
|
370
|
+
|
|
371
|
+
/** The signed-in member as the host's `me/get` named them (the muted words' scope, the DM client's `self`). */
|
|
372
|
+
let memberId: string | null = null
|
|
373
|
+
const onOnline = (): void => state.dm?.online()
|
|
374
|
+
|
|
375
|
+
/** The conversations for the ready view, once (see the module comment). */
|
|
376
|
+
function ensureDm(): void {
|
|
377
|
+
if (state.dm || !client || disposed || memberId === null) return
|
|
378
|
+
const me = memberId
|
|
379
|
+
let dmClient
|
|
380
|
+
try {
|
|
381
|
+
dmClient = createPrivateDmClient(client, { self: () => me })
|
|
382
|
+
} catch {
|
|
383
|
+
// A private client without the session's `call` (a host's own, or a test's stub) offers no conversations.
|
|
384
|
+
return
|
|
385
|
+
}
|
|
386
|
+
state.dm = createDmView({
|
|
387
|
+
dm: dmClient,
|
|
388
|
+
promise: () => promiseFor(state.activation, state.block),
|
|
389
|
+
rulesHref: () => state.provider?.links.rules ?? null,
|
|
390
|
+
consultationPath: o.consultationPath ?? null,
|
|
391
|
+
typingAllowed: () => state.settings?.values.dmTyping !== false,
|
|
392
|
+
readOnly: () => state.block?.state === 'read_only',
|
|
393
|
+
muted: (o.mutedStore ?? indexedDbMutedStore)(me),
|
|
394
|
+
// kb-api's dm/read marked some of the conversation's notifications read: the dot follows.
|
|
395
|
+
notificationsRead: () => void refreshUnread(),
|
|
396
|
+
})
|
|
397
|
+
if (typeof window !== 'undefined' && typeof window.addEventListener === 'function') window.addEventListener('online', onOnline)
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** The conversations end: their drafts, outbox and decrypted messages go (a sign-out, a forgotten key, the view). */
|
|
401
|
+
function endDm(): void {
|
|
402
|
+
if (typeof window !== 'undefined' && typeof window.removeEventListener === 'function') window.removeEventListener('online', onOnline)
|
|
403
|
+
state.dm?.dispose()
|
|
404
|
+
state.dm = null
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
let live: RunningRealtime | null = null
|
|
408
|
+
/** The connection ended for this device once: re-read the state once, never reopen it from this load. */
|
|
409
|
+
let liveEnded = false
|
|
410
|
+
|
|
411
|
+
function stopRealtime(): void {
|
|
412
|
+
live?.stop()
|
|
413
|
+
live = null
|
|
414
|
+
if (state.realtime !== 'off') state.realtime = o.realtime ? 'ended' : 'off'
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function onInbox(event: PrivateInboxEvent): void {
|
|
418
|
+
if (disposed || state.phase !== 'ready') return
|
|
419
|
+
state.dm?.apply(event)
|
|
420
|
+
if (event.k === 'resync') {
|
|
421
|
+
void load()
|
|
422
|
+
return
|
|
423
|
+
}
|
|
424
|
+
if (ACCOUNT_STATE_EVENTS.includes(event.k)) {
|
|
425
|
+
void afterChange().then(() => {
|
|
426
|
+
if (disposed || state.phase !== 'ready') return
|
|
427
|
+
// The approval, the address or the activation this screen waited for has happened elsewhere.
|
|
428
|
+
if (state.screen === 'approval' || state.screen === 'address' || state.screen === 'activation') state.screen = firstScreen()
|
|
429
|
+
})
|
|
430
|
+
}
|
|
431
|
+
if (refreshesUnread(event)) void refreshUnread()
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/** Start the live inbox for the ready view, once per load chain (see the module comment). */
|
|
435
|
+
function ensureRealtime(): void {
|
|
436
|
+
if (!o.realtime || !client || live || liveEnded || disposed) return
|
|
437
|
+
live = startRealtime(o.realtime, client, onInbox, (next, end) => {
|
|
438
|
+
if (disposed) return
|
|
439
|
+
state.realtime = next
|
|
440
|
+
if (next !== 'ended') return
|
|
441
|
+
live = null
|
|
442
|
+
liveEnded = true
|
|
443
|
+
// kb-api refused this device's token (signed out, denied, removed): the state says what happened.
|
|
444
|
+
if (end?.reason === 'refused' && state.phase === 'ready') void readState()
|
|
445
|
+
})
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
const announce = (ref: PrivateTextRef): void => {
|
|
449
|
+
state.announcement = ref
|
|
450
|
+
state.announceId += 1
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Runs one of the member's actions while the view is mounted: one at a time (a second click while one is in flight
|
|
455
|
+
* does nothing), and a refusal is said on the screen the action belongs to; returns null then.
|
|
456
|
+
*/
|
|
457
|
+
async function run<T>(screen: PrivateScreen, call: () => Promise<PrivateOutcome<T>>): Promise<T | null> {
|
|
458
|
+
if (!client || state.busy) return null
|
|
459
|
+
state.busy = true
|
|
460
|
+
delete state.screenFailures[screen]
|
|
461
|
+
try {
|
|
462
|
+
return settle(screen, await call())
|
|
463
|
+
} finally {
|
|
464
|
+
state.busy = false
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** Runs a screen's read: never blocked by an action in another window, its refusal said on its own screen. */
|
|
469
|
+
async function read<T>(screen: PrivateScreen, call: () => Promise<PrivateOutcome<T>>): Promise<T | null> {
|
|
470
|
+
if (!client) return null
|
|
471
|
+
return settle(screen, await call())
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
function settle<T>(screen: PrivateScreen, r: PrivateOutcome<T>): T | null {
|
|
475
|
+
if (disposed) return null
|
|
476
|
+
if (!r.ok) {
|
|
477
|
+
state.screenFailures[screen] = failureRef(r)
|
|
478
|
+
return null
|
|
479
|
+
}
|
|
480
|
+
return r.value
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
async function readActivation(): Promise<void> {
|
|
484
|
+
if (!client) return
|
|
485
|
+
const a = await client.activation.state()
|
|
486
|
+
if (disposed || !a.ok) return
|
|
487
|
+
state.activation = a.value
|
|
488
|
+
state.provider = providerOf(a.value.texts)
|
|
489
|
+
const p = promiseFor(a.value, state.block)
|
|
490
|
+
state.promise = { keys: p.keys, delegated: p.delegated }
|
|
491
|
+
state.push.offered = o.push !== null && variantsOf(a.value).has('private_push')
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
async function readState(): Promise<boolean> {
|
|
495
|
+
if (!client) return false
|
|
496
|
+
const s = await client.state()
|
|
497
|
+
if (disposed) return false
|
|
498
|
+
if (!s.ok) {
|
|
499
|
+
if (s.code === 'NO_MEMBER') state.phase = 'signIn'
|
|
500
|
+
// A dark instance answers no private route at all: show nothing (the M0d rule).
|
|
501
|
+
else if (s.code === 'UNKNOWN_PATH') state.phase = 'dark'
|
|
502
|
+
else {
|
|
503
|
+
state.phase = 'failed'
|
|
504
|
+
state.failure = failureRef(s)
|
|
505
|
+
}
|
|
506
|
+
stopRealtime()
|
|
507
|
+
return false
|
|
508
|
+
}
|
|
509
|
+
state.state = s.value
|
|
510
|
+
return true
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function firstScreen(): PrivateScreen {
|
|
514
|
+
const s = state.state
|
|
515
|
+
if (!s) return 'home'
|
|
516
|
+
if (s.address.state !== 'verified') return 'address'
|
|
517
|
+
// A pending device cannot activate (kb-api refuses activation/accept from it): it is approved first.
|
|
518
|
+
if (s.device.state === 'pending') return 'approval'
|
|
519
|
+
if (state.activation && state.activation.activation.state !== 'active') return 'activation'
|
|
520
|
+
return 'home'
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/** The screens a member is sent to before anything else (firstScreen): kept by a reload only while they still apply. */
|
|
524
|
+
const GATE_SCREENS: readonly PrivateScreen[] = ['address', 'approval', 'activation']
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* One load at a time: a `resync` (or a retry, a forget) while a load runs asks for one more pass after it rather than
|
|
528
|
+
* a second load beside it — two overlapping loads would each read the other's `loading` as a view that was never
|
|
529
|
+
* ready, and lose the open conversation and the reload's place.
|
|
530
|
+
*/
|
|
531
|
+
let loading: Promise<void> | null = null
|
|
532
|
+
let loadAgain = false
|
|
533
|
+
function load(): Promise<void> {
|
|
534
|
+
if (loading) {
|
|
535
|
+
loadAgain = true
|
|
536
|
+
return loading
|
|
537
|
+
}
|
|
538
|
+
const run = (async () => {
|
|
539
|
+
try {
|
|
540
|
+
do {
|
|
541
|
+
loadAgain = false
|
|
542
|
+
await loadOnce()
|
|
543
|
+
} while (loadAgain && !disposed)
|
|
544
|
+
} finally {
|
|
545
|
+
loading = null
|
|
546
|
+
state.reloading = false
|
|
547
|
+
}
|
|
548
|
+
})()
|
|
549
|
+
loading = run
|
|
550
|
+
return run
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/** The way out of the private view's ready part (a sign-out, a dark instance, a refusal): nothing of it stays. */
|
|
554
|
+
function leave(phase: PrivatePhase): void {
|
|
555
|
+
state.phase = phase
|
|
556
|
+
state.reloading = false
|
|
557
|
+
stopRealtime()
|
|
558
|
+
endDm()
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
async function loadOnce(): Promise<void> {
|
|
562
|
+
const wasReady = state.phase === 'ready'
|
|
563
|
+
// The gate the member was on, if any: a reload keeps the screen only while that gate still applies.
|
|
564
|
+
const gateBefore = wasReady && GATE_SCREENS.includes(state.screen) && state.screen === firstScreen() ? state.screen : null
|
|
565
|
+
if (!wasReady) liveEnded = false
|
|
566
|
+
// A ready view keeps its phase while it reloads (see `reloading`); a first load says it is loading.
|
|
567
|
+
if (wasReady) state.reloading = true
|
|
568
|
+
else state.phase = 'loading'
|
|
569
|
+
state.failure = null
|
|
570
|
+
const me = await o.host.me.get()
|
|
571
|
+
if (disposed) return
|
|
572
|
+
if (!me.ok) {
|
|
573
|
+
if (me.code === 'signIn') {
|
|
574
|
+
// Signed out on the host since the last load: the conversations go with their drafts and decrypted messages.
|
|
575
|
+
leave('signIn')
|
|
576
|
+
return
|
|
577
|
+
}
|
|
578
|
+
state.phase = 'failed'
|
|
579
|
+
state.reloading = false
|
|
580
|
+
state.failure = { msg: 'private.failed' }
|
|
581
|
+
return
|
|
582
|
+
}
|
|
583
|
+
if (me.value.needs.includes('sign_in') || !me.value.member) {
|
|
584
|
+
leave('signIn')
|
|
585
|
+
return
|
|
586
|
+
}
|
|
587
|
+
// Another member on this browser since the last load: the previous member's conversations go first.
|
|
588
|
+
if (memberId !== null && memberId !== me.value.member.memberId) endDm()
|
|
589
|
+
memberId = me.value.member.memberId
|
|
590
|
+
const block = privateBlockOf(me.value)
|
|
591
|
+
state.block = block
|
|
592
|
+
if (block === null || (block.state !== 'live' && block.state !== 'read_only')) {
|
|
593
|
+
leave('dark')
|
|
594
|
+
return
|
|
595
|
+
}
|
|
596
|
+
if (block.rendering === 'provider_page') {
|
|
597
|
+
// The conversations render on the account host now: this page's drafts, outbox and messages go.
|
|
598
|
+
leave('link')
|
|
599
|
+
return
|
|
600
|
+
}
|
|
601
|
+
try {
|
|
602
|
+
client = client ?? o.client()
|
|
603
|
+
} catch {
|
|
604
|
+
state.phase = 'failed'
|
|
605
|
+
state.reloading = false
|
|
606
|
+
state.failure = { msg: 'private.failed' }
|
|
607
|
+
return
|
|
608
|
+
}
|
|
609
|
+
const started = await client.session.start()
|
|
610
|
+
if (disposed) return
|
|
611
|
+
if (!started.ok && started.code === 'NO_MEMBER') {
|
|
612
|
+
leave('signIn')
|
|
613
|
+
return
|
|
614
|
+
}
|
|
615
|
+
// `start()` asks kb-api nothing but the push and session ends of other members' records: a refusal of THIS
|
|
616
|
+
// member (the tenant's `private_identity = provider_only`, PRIVATE_IDENTITY; ACTIVATION_REQUIRED, …) comes from
|
|
617
|
+
// the grant, through the first private call (`state`), and readState says it.
|
|
618
|
+
if (!(await readState())) {
|
|
619
|
+
leave(state.phase)
|
|
620
|
+
return
|
|
621
|
+
}
|
|
622
|
+
await readActivation()
|
|
623
|
+
if (disposed) return
|
|
624
|
+
state.phase = 'ready'
|
|
625
|
+
state.reloading = false
|
|
626
|
+
// A reload of a ready view (a `resync`) keeps the member where they were — an open conversation included — unless
|
|
627
|
+
// a gate (the address, the approval, the activation) now comes first, they were on a gate (one that no longer
|
|
628
|
+
// applies, since `first` is home: they go home rather than stay on a screen that asks for what is done), or the
|
|
629
|
+
// conversations ended in between (another member, a sign-out): `state.dm` survives a reload only for the same member.
|
|
630
|
+
const first = firstScreen()
|
|
631
|
+
const keep = wasReady && first === 'home' && state.dm !== null && gateBefore === null
|
|
632
|
+
if (!keep) state.screen = first
|
|
633
|
+
ensureDm()
|
|
634
|
+
ensureRealtime()
|
|
635
|
+
// B-55: the browser's one subscription may have a new endpoint since the last visit; re-post it for this device.
|
|
636
|
+
if (o.push && state.push.offered) {
|
|
637
|
+
const on = await o.push.resync(client)
|
|
638
|
+
if (disposed) return
|
|
639
|
+
state.push.status = on === true ? 'on' : o.push.supported() ? 'off' : 'unsupported'
|
|
640
|
+
state.push.note = on === 'stale' ? 'stale' : null
|
|
641
|
+
} else if (o.push && !o.push.supported()) state.push.status = 'unsupported'
|
|
642
|
+
await refreshUnread()
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
async function refreshUnread(): Promise<void> {
|
|
646
|
+
if (!client || state.phase !== 'ready') return
|
|
647
|
+
const r = await client.notifications.unreadCount()
|
|
648
|
+
if (disposed) return
|
|
649
|
+
state.unread = r.ok && typeof r.value.count === 'number' && r.value.count > 0 ? r.value.count : 0
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
async function open(screen: PrivateScreen): Promise<void> {
|
|
653
|
+
if (state.phase !== 'ready' || !client) return
|
|
654
|
+
state.screen = screen
|
|
655
|
+
delete state.screenFailures[screen]
|
|
656
|
+
const c = client
|
|
657
|
+
switch (screen) {
|
|
658
|
+
case 'devices': {
|
|
659
|
+
const v = await read(screen, () => c.devices.list())
|
|
660
|
+
if (v) state.devices = v.devices
|
|
661
|
+
return
|
|
662
|
+
}
|
|
663
|
+
case 'reads': {
|
|
664
|
+
const v = await read(screen, () => c.devices.reads())
|
|
665
|
+
if (v) state.reads = v.reads
|
|
666
|
+
// The rows name devices by id: the list gives their labels.
|
|
667
|
+
if (v && state.devices === null) {
|
|
668
|
+
const d = await read(screen, () => c.devices.list())
|
|
669
|
+
if (d) state.devices = d.devices
|
|
670
|
+
}
|
|
671
|
+
return
|
|
672
|
+
}
|
|
673
|
+
case 'approval': {
|
|
674
|
+
state.shownCode = null
|
|
675
|
+
if (state.state?.device.state === 'pending') return
|
|
676
|
+
const v = await read(screen, () => c.approval.pending())
|
|
677
|
+
if (v) state.approvals = v.approvals
|
|
678
|
+
return
|
|
679
|
+
}
|
|
680
|
+
case 'notifications': {
|
|
681
|
+
const v = await read(screen, () => c.notifications.list({}))
|
|
682
|
+
if (v) {
|
|
683
|
+
state.notifications = notificationsOf(v)
|
|
684
|
+
state.notificationsCursor = cursorOf(v)
|
|
685
|
+
}
|
|
686
|
+
return
|
|
687
|
+
}
|
|
688
|
+
case 'settings': {
|
|
689
|
+
state.settingsUnavailable = false
|
|
690
|
+
const r = await c.settings.get()
|
|
691
|
+
if (disposed) return
|
|
692
|
+
const parsed = r.ok ? settingsOf(r.value) : null
|
|
693
|
+
state.settings = parsed
|
|
694
|
+
// Until DM3 serves it, kb-api answers the unknown path: the screen says the settings come with DMs.
|
|
695
|
+
state.settingsUnavailable = parsed === null
|
|
696
|
+
return
|
|
697
|
+
}
|
|
698
|
+
case 'activation':
|
|
699
|
+
state.art9Declined = false
|
|
700
|
+
await readActivation()
|
|
701
|
+
return
|
|
702
|
+
default:
|
|
703
|
+
return
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
async function afterChange(): Promise<void> {
|
|
708
|
+
await readState()
|
|
709
|
+
await readActivation()
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
return {
|
|
713
|
+
state,
|
|
714
|
+
load,
|
|
715
|
+
open,
|
|
716
|
+
firstScreen,
|
|
717
|
+
refreshUnread,
|
|
718
|
+
async approvalStart(via) {
|
|
719
|
+
const c = client
|
|
720
|
+
if (!c) return
|
|
721
|
+
state.approvalLimited = null
|
|
722
|
+
const v = await run('approval', async () => {
|
|
723
|
+
const out = await c.approval.start(via)
|
|
724
|
+
// A rate refusal gets its own words on the screen (when to try again), not the generic failure.
|
|
725
|
+
if (!out.ok && APPROVAL_LIMIT_CODES[out.code]) state.approvalLimited = APPROVAL_LIMIT_CODES[out.code]!
|
|
726
|
+
return out
|
|
727
|
+
})
|
|
728
|
+
if (!v) {
|
|
729
|
+
if (state.approvalLimited) delete state.screenFailures.approval
|
|
730
|
+
return
|
|
731
|
+
}
|
|
732
|
+
state.approvalStart = v
|
|
733
|
+
if (v.state === 'approved') {
|
|
734
|
+
announce({ msg: 'private.approval.approved' })
|
|
735
|
+
await afterChange()
|
|
736
|
+
}
|
|
737
|
+
},
|
|
738
|
+
approvalRetry() {
|
|
739
|
+
state.approvalLimited = null
|
|
740
|
+
delete state.screenFailures.approval
|
|
741
|
+
},
|
|
742
|
+
async approvalRedeem(code) {
|
|
743
|
+
const c = client
|
|
744
|
+
const trimmed = code.trim()
|
|
745
|
+
if (!c || trimmed === '') return
|
|
746
|
+
const v = await run('approval', () => c.approval.redeem(trimmed))
|
|
747
|
+
if (!v) {
|
|
748
|
+
// An exhausted or expired request takes no more codes: the screen offers a new request (its refusal stays said).
|
|
749
|
+
if (state.screenFailures.approval?.msg === 'error:APPROVAL_EXHAUSTED') state.approvalStart = null
|
|
750
|
+
return
|
|
751
|
+
}
|
|
752
|
+
state.approvalStart = null
|
|
753
|
+
announce({ msg: 'private.approval.approved' })
|
|
754
|
+
await afterChange()
|
|
755
|
+
state.screen = firstScreen()
|
|
756
|
+
},
|
|
757
|
+
async approvalShow(approvalId) {
|
|
758
|
+
const c = client
|
|
759
|
+
if (!c) return
|
|
760
|
+
const v = await run('approval', () => c.approval.show(approvalId))
|
|
761
|
+
if (v) state.shownCode = { approvalId, code: v.code, expiresAt: v.expiresAt }
|
|
762
|
+
},
|
|
763
|
+
async approvalDeny(approvalId) {
|
|
764
|
+
const c = client
|
|
765
|
+
if (!c) return
|
|
766
|
+
const v = await run('approval', () => c.approval.deny(approvalId))
|
|
767
|
+
if (!v) return
|
|
768
|
+
state.approvals = (state.approvals ?? []).filter((a) => a.approvalId !== approvalId)
|
|
769
|
+
if (state.shownCode?.approvalId === approvalId) state.shownCode = null
|
|
770
|
+
announce({ msg: 'private.approval.denied' })
|
|
771
|
+
},
|
|
772
|
+
async requestAddressLink() {
|
|
773
|
+
const c = client
|
|
774
|
+
if (!c) return
|
|
775
|
+
const v = await run('address', () => c.addressLink())
|
|
776
|
+
if (v) state.addressLink = { url: v.url, expiresAt: v.expiresAt }
|
|
777
|
+
},
|
|
778
|
+
async activate(input) {
|
|
779
|
+
const c = client
|
|
780
|
+
const a = state.activation
|
|
781
|
+
if (!c || !a) return
|
|
782
|
+
if (input.art9 === 'refuse') {
|
|
783
|
+
// kb-api stores no refusal (it answers ACTIVATION_REQUIRED and keeps nothing): nothing is posted, and the
|
|
784
|
+
// screen says so instead of claiming a record.
|
|
785
|
+
state.art9Declined = true
|
|
786
|
+
announce({ msg: 'private.activation.declined' })
|
|
787
|
+
return
|
|
788
|
+
}
|
|
789
|
+
const terms = a.texts.find((t) => t.kind === 'private_terms')
|
|
790
|
+
if (!terms || !a.art9 || !input.acceptTerms) return
|
|
791
|
+
const art9 = a.art9
|
|
792
|
+
const proof = (t: PrivateLegalText) => ({ id: t.id, contentSha256: t.contentSha256, renderedSha256: t.renderedSha256 })
|
|
793
|
+
const shownKinds = ['private_privacy', 'private_notice_63e', 'private_contact', 'private_activation']
|
|
794
|
+
const v = await run('activation', () =>
|
|
795
|
+
c.activation.accept({
|
|
796
|
+
accepted: [proof(terms)],
|
|
797
|
+
art9: proof(art9),
|
|
798
|
+
...(a.art9Tenant && input.art9Tenant ? { art9Tenant: proof(a.art9Tenant) } : {}),
|
|
799
|
+
shown: a.texts.filter((t) => shownKinds.includes(t.kind)).map(proof),
|
|
800
|
+
}),
|
|
801
|
+
)
|
|
802
|
+
if (!v) return
|
|
803
|
+
state.art9Declined = false
|
|
804
|
+
if (v.activation.state === 'active') announce({ msg: 'private.activation.done' })
|
|
805
|
+
await afterChange()
|
|
806
|
+
state.screen = firstScreen()
|
|
807
|
+
},
|
|
808
|
+
async withdraw() {
|
|
809
|
+
const c = client
|
|
810
|
+
if (!c) return
|
|
811
|
+
const v = await run('activation', () => c.activation.withdraw())
|
|
812
|
+
if (!v) return
|
|
813
|
+
announce({ msg: 'private.activation.withdrawn' })
|
|
814
|
+
await afterChange()
|
|
815
|
+
},
|
|
816
|
+
async signOutDevice(deviceId) {
|
|
817
|
+
const c = client
|
|
818
|
+
if (!c) return
|
|
819
|
+
const v = await run('devices', () => c.devices.signOut(deviceId))
|
|
820
|
+
if (!v) return
|
|
821
|
+
state.devices = (state.devices ?? []).filter((d) => d.id !== deviceId)
|
|
822
|
+
announce({ msg: 'private.devices.signedOut' })
|
|
823
|
+
},
|
|
824
|
+
async signOutAll() {
|
|
825
|
+
const c = client
|
|
826
|
+
if (!c) return
|
|
827
|
+
const v = await run('devices', () => c.devices.signOutAll())
|
|
828
|
+
if (!v) return
|
|
829
|
+
state.devices = (state.devices ?? []).filter((d) => d.this)
|
|
830
|
+
announce({ msg: 'private.devices.signedOutAll', params: { count: v.removed } })
|
|
831
|
+
},
|
|
832
|
+
async forget() {
|
|
833
|
+
const c = client
|
|
834
|
+
if (!c || state.busy) return
|
|
835
|
+
const v = await run('devices', () => c.devices.forget())
|
|
836
|
+
if (v === null && state.screenFailures.devices) return
|
|
837
|
+
announce({ msg: 'private.devices.forgotten' })
|
|
838
|
+
// The key is gone: the next load is a new, pending device, with a connection and conversations of its own.
|
|
839
|
+
state.devices = null
|
|
840
|
+
stopRealtime()
|
|
841
|
+
endDm()
|
|
842
|
+
liveEnded = false
|
|
843
|
+
await load()
|
|
844
|
+
},
|
|
845
|
+
async markAllRead() {
|
|
846
|
+
const c = client
|
|
847
|
+
const shownUnread = (state.notifications ?? []).some((n) => n.readAt === null)
|
|
848
|
+
if (!c || (!shownUnread && state.unread === 0)) return
|
|
849
|
+
// Every row, not the ids shown: a row of a code this screen leaves out, or one on a later page, would otherwise
|
|
850
|
+
// keep the unread dot on with nothing to show or mark.
|
|
851
|
+
const v = await run('notifications', () => c.notifications.markRead({ all: true }))
|
|
852
|
+
if (!v) return
|
|
853
|
+
const now = new Date().toISOString()
|
|
854
|
+
state.notifications = (state.notifications ?? []).map((n) => (n.readAt === null ? { ...n, readAt: now } : n))
|
|
855
|
+
state.unread = 0
|
|
856
|
+
},
|
|
857
|
+
async moreNotifications() {
|
|
858
|
+
const c = client
|
|
859
|
+
const cursor = state.notificationsCursor
|
|
860
|
+
if (!c || cursor === null) return
|
|
861
|
+
const v = await read('notifications', () => c.notifications.list({ cursor }))
|
|
862
|
+
if (!v || state.notificationsCursor !== cursor) return
|
|
863
|
+
const known = new Set((state.notifications ?? []).map((n) => n.id))
|
|
864
|
+
state.notifications = [...(state.notifications ?? []), ...notificationsOf(v).filter((n) => !known.has(n.id))]
|
|
865
|
+
state.notificationsCursor = cursorOf(v)
|
|
866
|
+
},
|
|
867
|
+
async setSetting(key, value) {
|
|
868
|
+
const c = client
|
|
869
|
+
const s = state.settings
|
|
870
|
+
if (!c || !s || s.values[key] === value) return
|
|
871
|
+
if (key === 'dmAudience' && DM_AUDIENCES.indexOf(value as DmAudience) > DM_AUDIENCES.indexOf(s.ceiling)) return
|
|
872
|
+
const before = s.values[key]
|
|
873
|
+
s.values[key] = value
|
|
874
|
+
const v = await run('settings', () => c.settings.set({ [key]: value }))
|
|
875
|
+
if (!v) {
|
|
876
|
+
if (state.settings) state.settings.values[key] = before
|
|
877
|
+
return
|
|
878
|
+
}
|
|
879
|
+
const parsed = settingsOf(v)
|
|
880
|
+
if (parsed) state.settings = parsed
|
|
881
|
+
announce({ msg: 'private.settings.saved' })
|
|
882
|
+
},
|
|
883
|
+
async enablePush() {
|
|
884
|
+
const c = client
|
|
885
|
+
const port = o.push
|
|
886
|
+
if (!c || !port || state.push.status === 'busy') return
|
|
887
|
+
state.push.status = 'busy'
|
|
888
|
+
state.push.note = null
|
|
889
|
+
const r = await port.enable(c)
|
|
890
|
+
if (disposed) return
|
|
891
|
+
state.push.status = r === 'on' ? 'on' : r === 'unsupported' ? 'unsupported' : 'off'
|
|
892
|
+
state.push.note = r === 'denied' || r === 'refused' || r === 'stale' ? r : null
|
|
893
|
+
},
|
|
894
|
+
async disablePush() {
|
|
895
|
+
const c = client
|
|
896
|
+
const port = o.push
|
|
897
|
+
if (!c || !port || state.push.status === 'busy') return
|
|
898
|
+
state.push.status = 'busy'
|
|
899
|
+
state.push.note = null
|
|
900
|
+
const off = await port.disable(c).catch(() => false)
|
|
901
|
+
if (disposed) return
|
|
902
|
+
// A row kb-api kept still receives pushes: the switch stays on and the refusal is said, so the member can retry.
|
|
903
|
+
state.push.status = off ? 'off' : 'on'
|
|
904
|
+
if (off) delete state.screenFailures.notifications
|
|
905
|
+
else state.screenFailures.notifications = { msg: 'private.push.offFailed' }
|
|
906
|
+
},
|
|
907
|
+
pushSentence: () => (state.push.status === 'on' ? pushSectionOf(state.activation) : null),
|
|
908
|
+
promiseParams() {
|
|
909
|
+
const p = state.provider
|
|
910
|
+
return { site: o.siteName, entity: p?.name ?? '', ico: p?.ico ?? '' }
|
|
911
|
+
},
|
|
912
|
+
dispose() {
|
|
913
|
+
endDm()
|
|
914
|
+
disposed = true
|
|
915
|
+
live?.stop()
|
|
916
|
+
live = null
|
|
917
|
+
client?.session.close()
|
|
918
|
+
},
|
|
919
|
+
}
|
|
920
|
+
}
|