@coda-console/friends 0.12.0 → 0.14.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/README.md +16 -9
- package/THIRD-PARTY.md +32 -0
- package/lib/friends.mjs +49 -1
- package/lib/machine-record.mjs +67 -0
- package/lib/view-members.mjs +31 -10
- package/main.mjs +8 -1
- package/package.json +55 -4
- package/view/app.mjs +216 -135
- package/view/style.css +18 -13
- package/view/vendor/qrcode.mjs +2291 -0
package/view/app.mjs
CHANGED
|
@@ -1,22 +1,28 @@
|
|
|
1
|
-
// 好友页:各台电脑的 Accord 身份(Hero
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// 好友页:各台电脑的 Accord 身份(Hero)、按电脑分节的好友列表与详情、添加好友。目录聚合视图(清单 directoryViews)。
|
|
2
|
+
// 读:一个不带 machine 的 coda.watch 读目录里各台模块写下的 friends/machine@1(本机 Accord 身份与好友摘要,lib/machine-record.mjs),先读齐一次再画,
|
|
3
|
+
// 按条目的 owner 对到视图里的成员(lib/view-members.mjs);身份按公钥归组、好友按电脑分节;离线的电脑照样显示它最后写下的,并标出离线(看 coda/machine@1 的 offline);
|
|
4
|
+
// 没有这条记录的成员不出节。写:添加好友、互认、移除等(coda/accord@1 的 accept、handshake、remove)都带 { machine } 发往它所在的那一台,那台离线时失败并写出原因。
|
|
5
|
+
// 「共享」(授给好友的、好友授给本机的远程执行、最近的操作)经 postMessage(…, { machine }) 问各台的模块。
|
|
4
6
|
// 网络节点(friendNodes / removeFriendNode,试用):分节标题行的入口打开抽屉,列出入了网却没完成互认的残留节点,删除由客户端弹确认框;
|
|
5
7
|
// 移除好友后留在网络里的节点(status.networkRemovals)显示在分节里,用户点「重试」才读、才删,页面不会自己调用。
|
|
6
8
|
// 昵称、头像都由 Accord 公钥推出(WebView 标准库 coda-view://lib/v1/accord.js、avatar.mjs),没有可编辑的名片;
|
|
7
9
|
// 节点名等外来数据一律用 textContent / DOM API 渲染,从不拼 HTML 字符串。
|
|
8
|
-
import { shortAuthor } from 'coda-view://lib/v1/accord.js';
|
|
10
|
+
import { identityTitles, isAuthor, shortAuthor } from 'coda-view://lib/v1/accord.js';
|
|
9
11
|
import { accordStatusSummary, resolveAvatar } from '../lib/avatar.mjs';
|
|
10
|
-
import {
|
|
11
|
-
import { friendSections, identities, listMembers, memberTitles } from '../lib/view-members.mjs';
|
|
12
|
+
import { encodeInvite, parseInvite } from '../lib/friends.mjs';
|
|
13
|
+
import { applyEntry, applyOffline, friendSections, identities, listMembers, memberOf, memberTitles } from '../lib/view-members.mjs';
|
|
12
14
|
import { fill, h, identiconSvg, svg } from './dom.mjs';
|
|
15
|
+
import qrcode from './vendor/qrcode.mjs';
|
|
13
16
|
|
|
14
17
|
const ACCORD = 'coda/accord@1', LOCAL = { kind: ACCORD, id: 'local' };
|
|
18
|
+
const MACHINE_RECORD = 'friends/machine@1', MACHINE = 'coda/machine@1';
|
|
15
19
|
const coda = window.coda;
|
|
16
20
|
const root = document.getElementById('root');
|
|
17
21
|
const STATUS_TEXT = { bound: '已互认', pending: '待互认', conflict: '密钥冲突' };
|
|
18
22
|
const MACHINE_KIND = 'remote-exec/machine@1';
|
|
19
23
|
const RECOVER_MS = 3000;
|
|
24
|
+
const OFFLINE_HINT = '这台电脑离线,显示的是它最后一次写下的身份与好友;添加、互认、移除等操作要等它回来才能做。';
|
|
25
|
+
const OFFLINE_POLL_MS = 30_000;
|
|
20
26
|
// 结果不明的撤销:连续读到这么多遍有效列表、授权仍可用,就不再自动重发。
|
|
21
27
|
const REVOKE_RETRIES = 3;
|
|
22
28
|
// 结果不明的授予:连续读到这么多遍有效列表、里面都没有这条授权,就回滚这一行,允许重试(持有方对同一目标的授予幂等)。
|
|
@@ -24,11 +30,11 @@ const GRANT_CONFIRMS = 3;
|
|
|
24
30
|
// 仅限目录的授予、改范围要先到目标机探测(持有方最长等 30 秒):页面多等一会儿再算没有回应。
|
|
25
31
|
const PROBE_WAIT_MS = 45_000;
|
|
26
32
|
|
|
27
|
-
//
|
|
33
|
+
// 视图里的每台电脑(成员在页面生命周期内不变)各存一份数据;没有 friends/machine@1 记录的 `loaded` 为 false,不出节。
|
|
28
34
|
// 节点的名字、忙碌、错误、已收回按(电脑, 控制服务器节点 ID)记在 m.fn 里,不与好友行的 state.busy / state.notice 混用(那两个按 Accord 的 node 记)。
|
|
29
|
-
const members = listMembers(coda).map(m => ({ ...m, entry: null, author: null, friends: [], loaded: false,
|
|
30
|
-
const live = members.filter(m => !m.unreachable);
|
|
35
|
+
const members = listMembers(coda).map(m => ({ ...m, entry: null, author: null, friends: [], loaded: false, fn: { names: new Map(), done: new Set(), busy: new Set(), errors: new Map(), readErrors: new Map(), blocks: new Map(), noCreds: false } }));
|
|
31
36
|
const multi = members.length > 1;
|
|
37
|
+
let firstRead = false, loadError = null; // firstRead:第一次读完(成功或失败)才画,之后记录变了才重画
|
|
32
38
|
const state = { busy: new Set(), notice: new Map(), heroNotice: new Map(), detail: null, removing: null, menu: null };
|
|
33
39
|
let netBusy = false, netView = null; // 同一页同一时间只有一个好友节点操作在途;netView 是开着的抽屉里读到的内容
|
|
34
40
|
const via = m => ({ machine: m.id });
|
|
@@ -60,23 +66,30 @@ addEventListener('message', event => {
|
|
|
60
66
|
if (wait.late) { pendingReplies.delete(rid); wait.late(ok ? { value } : { failure }); } else if (ok) wait.resolve(value); else wait.reject(failure);
|
|
61
67
|
});
|
|
62
68
|
|
|
63
|
-
// ----
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
m
|
|
67
|
-
m
|
|
68
|
-
m
|
|
69
|
+
// ---- 读目录里各台的 friends/machine@1 ----
|
|
70
|
+
// 条目没了(DELETED)当这台没有记录。记录里的好友表一变,已成功移除而等推送确认的通道就可以清理了。
|
|
71
|
+
function onChange({ type, entry }) {
|
|
72
|
+
const m = memberOf(members, entry?.owner);
|
|
73
|
+
if (!m) return;
|
|
74
|
+
applyEntry(m, type === 'DELETED' ? null : entry);
|
|
69
75
|
for (const one of gone) {
|
|
70
76
|
if (one.m !== m || m.friends.some(f => f.node === one.node)) continue;
|
|
71
77
|
gone.delete(one); state.busy.delete(one.key); state.notice.delete(one.key);
|
|
72
78
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
async function
|
|
77
|
-
try {
|
|
78
|
-
|
|
79
|
+
if (firstRead) draw();
|
|
80
|
+
}
|
|
81
|
+
// 哪几台离线:只有 coda/machine@1 的 offline 说得出,它到点不推送,所以打开时读一次,之后隔一会儿再读;读不到就都当在线。
|
|
82
|
+
async function readOffline() {
|
|
83
|
+
try { return applyOffline(members, await coda.list(MACHINE)); } catch { return false; }
|
|
84
|
+
}
|
|
85
|
+
async function start() {
|
|
86
|
+
await Promise.all([
|
|
87
|
+
Promise.resolve(coda.watch(MACHINE_RECORD, onChange)).catch(e => { loadError = `读不到好友信息:${e.message}`; }),
|
|
88
|
+
readOffline(),
|
|
89
|
+
]);
|
|
90
|
+
firstRead = true;
|
|
79
91
|
draw();
|
|
92
|
+
setInterval(async () => { if (await readOffline()) draw(); }, OFFLINE_POLL_MS);
|
|
80
93
|
}
|
|
81
94
|
|
|
82
95
|
// ---- 头像 ----
|
|
@@ -135,7 +148,6 @@ function tile(label, value, sub, dot) {
|
|
|
135
148
|
}
|
|
136
149
|
// 一台电脑的好友数与证书状态。
|
|
137
150
|
function statusTiles(m, rows) {
|
|
138
|
-
if (!m.loaded) return h('div', { class: 'bento' }, tile('好友', '读取中…'), tile('身份', '读取中…'));
|
|
139
151
|
const summary = accordStatusSummary(m.entry, rows), { bound, pending, conflict, total } = summary.friends, cert = CERT[summary.cert];
|
|
140
152
|
const sub = summary.cert === 'active' ? (summary.certUntil ? `证书有效至 ${formatTime(summary.certUntil)}` : '可正常发布记录') : cert.sub;
|
|
141
153
|
return h('div', { class: 'bento' },
|
|
@@ -149,21 +161,22 @@ function drawHero(id, sections, titles) {
|
|
|
149
161
|
const { author, signer } = id, ready = Boolean(signer);
|
|
150
162
|
const summary = accordStatusSummary(signer?.entry, []);
|
|
151
163
|
const notice = state.heroNotice.get(author ?? '');
|
|
152
|
-
const
|
|
153
|
-
const name = author ? titles.get(author) : !settled ? '读取中…' : ready ? '未建身份' : '读不到身份';
|
|
164
|
+
const name = author ? titles.get(author) : ready ? '未建身份' : '读不到身份';
|
|
154
165
|
const single = !multi && sections[0];
|
|
155
166
|
return h('section', { class: 'hero' },
|
|
156
167
|
h('div', { class: 'banner' }, h('div', { class: 'banner-acts' },
|
|
157
|
-
single
|
|
168
|
+
single ? h('button', { class: 'pill strong', type: 'button', onclick: () => openInvite(single.member) }, '添加好友') : null)),
|
|
158
169
|
h('div', { class: 'hero-body' },
|
|
159
170
|
h('div', { class: 'hero-id' },
|
|
160
171
|
h('span', { class: 'av-ring' }, avatar(resolveAvatar({ author, name }), 'lg')),
|
|
161
172
|
h('div', { class: 'hero-names' }, h('div', { class: 'name-line' }, h('h2', { class: 'hero-name selectable' }, name), h('span', { class: 'tag' }, '我的身份')),
|
|
162
173
|
h('button', { class: 'fp mono', type: 'button', disabled: !author, title: author ? '复制完整公钥' : '', onclick: () => copyKey(author),
|
|
163
|
-
}, author ? `ed25519:${summary.short}… · 复制` : !ready ?
|
|
164
|
-
!author && ready
|
|
174
|
+
}, author ? `ed25519:${summary.short}… · 复制` : !ready ? '读不到公钥' : summary.cert === 'no_identity' ? '未建身份' : '未取得公钥'))),
|
|
175
|
+
!author && ready ? h('p', { class: 'hint' }, '这台电脑还没有 Accord 身份:前往客户端「设置 › 身份」创建后,好友才能认出你。') : null,
|
|
176
|
+
loadError ? h('p', { class: 'error selectable' }, loadError) : null,
|
|
177
|
+
single?.offline ? h('p', { class: 'hint' }, OFFLINE_HINT) : null,
|
|
165
178
|
notice ? h('p', { class: notice.kind }, notice.text) : null,
|
|
166
|
-
single
|
|
179
|
+
single ? statusTiles(single.member, single.rows) : null));
|
|
167
180
|
}
|
|
168
181
|
async function copyKey(author) {
|
|
169
182
|
try { await coda.copy(author); state.heroNotice.set(author, { kind: 'ok', text: '公钥已复制' }); } catch (e) { state.heroNotice.set(author, { kind: 'error', text: `复制失败:${e.message}` }); }
|
|
@@ -171,133 +184,165 @@ async function copyKey(author) {
|
|
|
171
184
|
}
|
|
172
185
|
|
|
173
186
|
// ---- 添加好友 ----
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
h('label', { class: 'field' }, h('span', { class: 'lbl' }, 'authKey(入网码)'), invite.authKey),
|
|
190
|
-
h('label', { class: 'field' }, h('span', { class: 'lbl' }, 'linkId'), invite.linkId),
|
|
191
|
-
h('label', { class: 'field' }, h('span', { class: 'lbl' }, 'scope'), invite.scope),
|
|
192
|
-
h('label', { class: 'field' }, h('span', { class: 'lbl' }, 'peerNode'), invite.peerNode));
|
|
193
|
-
invite.fields.hidden = true;
|
|
194
|
-
invite.dialog = makeDialog({ title: '添加好友', close: '关闭', onClose: resetGenerated });
|
|
187
|
+
// 弹层分「邀请对方」「接受邀请」两页,切换时各自的状态保留。邀请码是凭据:生成的只放在 generated.code 里显示,
|
|
188
|
+
// 识别出的放在 accept.parsed 里,都不进 setState、存储或日志;生成的和识别出的都随弹层关闭、换目标电脑清掉,识别出的在交给 accept 时也清掉。
|
|
189
|
+
const invite = { target: null, tab: 'invite', message: h('p', { class: 'hint' }) };
|
|
190
|
+
invite.dialog = makeDialog({ title: '添加好友', close: '关闭', onClose: resetInvite });
|
|
191
|
+
const TABS = [['invite', '邀请对方'], ['accept', '接受邀请']];
|
|
192
|
+
const tabButtons = TABS.map(([id, label]) => h('button', { type: 'button', role: 'tab', onclick: () => showTab(id) }, label));
|
|
193
|
+
const panes = { invite: h('section', { class: 'pane' }), accept: h('section', { class: 'pane' }) };
|
|
194
|
+
function showTab(id) {
|
|
195
|
+
invite.tab = id;
|
|
196
|
+
TABS.forEach(([name], i) => {
|
|
197
|
+
tabButtons[i].className = name === id ? 'on' : '';
|
|
198
|
+
tabButtons[i].setAttribute('aria-selected', String(name === id));
|
|
199
|
+
panes[name].hidden = name !== id;
|
|
200
|
+
});
|
|
201
|
+
}
|
|
195
202
|
// 接受邀请和随后的互认都发给打开它的那一节的电脑。
|
|
196
203
|
function openInvite(m) {
|
|
197
|
-
if (invite.target !== m) say(invite.message, '');
|
|
204
|
+
if (invite.target !== m) { say(invite.message, ''); resetInvite(); }
|
|
198
205
|
invite.target = m;
|
|
199
206
|
invite.dialog.heading.textContent = multi ? `添加好友 · ${m.name}` : '添加好友';
|
|
200
207
|
showDialog(invite.dialog);
|
|
201
208
|
}
|
|
202
|
-
|
|
203
|
-
//
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
};
|
|
209
|
+
const inviterTitle = author => memberTitles(members).get(author) ?? identityTitles([author]).get(author);
|
|
210
|
+
// 头像加名字(几何纹头像由公钥推出;没有公钥的旧邀请写「对方」)。
|
|
211
|
+
const personView = (author, name, ...more) => h('div', { class: 'person' }, avatar(resolveAvatar({ author, name }), 'lg'), h('div', { class: 'person-info' }, h('div', { class: 'nm' }, name), ...more));
|
|
212
|
+
|
|
213
|
+
// 「邀请对方」:生成 coda.inviteFriend 的邀请,编成一行邀请码,配二维码。
|
|
214
|
+
const generated = { state: 'idle', code: '', author: null, error: '', copied: false, copyError: '', token: 0, timer: 0 };
|
|
215
|
+
const generate = { button: h('button', { class: 'btn', type: 'button', onclick: generateInvite }, '生成邀请码'), view: h('div', { class: 'stack', role: 'status', 'aria-live': 'polite' }) };
|
|
209
216
|
const canInvite = () => typeof coda.inviteFriend === 'function';
|
|
217
|
+
const VALID = '24 小时内有效,只能用一次';
|
|
218
|
+
function qrSvg(text) {
|
|
219
|
+
const qr = qrcode(0, 'M');
|
|
220
|
+
qr.addData(text);
|
|
221
|
+
qr.make();
|
|
222
|
+
const count = qr.getModuleCount(), edge = count + 8;
|
|
223
|
+
let path = '';
|
|
224
|
+
for (let r = 0; r < count; r++) for (let c = 0; c < count; c++) if (qr.isDark(r, c)) path += `M${c} ${r}h1v1h-1z`;
|
|
225
|
+
return svg('svg', { viewBox: `-4 -4 ${edge} ${edge}`, 'shape-rendering': 'crispEdges', role: 'img', 'aria-label': '邀请二维码' },
|
|
226
|
+
svg('rect', { x: -4, y: -4, width: edge, height: edge, fill: '#fff' }), svg('path', { d: path, fill: '#000' }));
|
|
227
|
+
}
|
|
210
228
|
function drawGenerated() {
|
|
211
229
|
const s = canInvite() ? generated.state : 'na';
|
|
212
230
|
generate.button.disabled = s === 'busy' || s === 'na';
|
|
213
|
-
generate.button.textContent = s === '
|
|
231
|
+
generate.button.textContent = s === 'fail' ? '重试' : '生成邀请码';
|
|
214
232
|
if (s === 'done') {
|
|
215
|
-
const
|
|
216
|
-
return fill(generate.
|
|
217
|
-
|
|
233
|
+
const { code, author } = generated;
|
|
234
|
+
return fill(generate.view,
|
|
235
|
+
personView(author, author ? inviterTitle(author) : '我', h('div', { class: 'secondary' }, '邀请你成为好友')),
|
|
236
|
+
h('div', { class: 'qr', 'aria-hidden': 'false' }, qrSvg(code)),
|
|
237
|
+
h('div', { class: 'code-line mono selectable', 'aria-label': '好友邀请码' }, h('span', { class: 'truncate' }, code)),
|
|
238
|
+
h('div', { class: 'line' },
|
|
239
|
+
h('button', { class: 'btn', type: 'button', onclick: copyInvite }, generated.copied ? '已复制' : '复制邀请码'),
|
|
240
|
+
h('button', { class: 'btn quiet', type: 'button', onclick: generateInvite }, '换一个')),
|
|
241
|
+
generated.copyError ? h('p', { class: 'error' }, generated.copyError) : h('p', { class: 'hint center' }, VALID));
|
|
218
242
|
}
|
|
219
|
-
const text = { busy: '正在生成…', fail: generated.error, na: '请在 Coda 客户端里打开好友页来生成邀请' }[s];
|
|
220
|
-
fill(generate.
|
|
243
|
+
const text = { idle: '生成一个邀请码,发给对方或让对方扫码', busy: '正在生成…', fail: generated.error, na: '请在 Coda 客户端里打开好友页来生成邀请' }[s];
|
|
244
|
+
fill(generate.view, h('p', { class: s === 'fail' ? 'error center' : 'hint center' }, text), generate.button, s === 'idle' ? h('p', { class: 'hint center' }, VALID) : null);
|
|
221
245
|
}
|
|
222
246
|
async function generateInvite() {
|
|
223
247
|
if (generated.state === 'busy' || !canInvite()) return;
|
|
224
248
|
const mine = ++generated.token, target = invite.target;
|
|
225
|
-
Object.assign(generated, { state: 'busy',
|
|
249
|
+
Object.assign(generated, { state: 'busy', code: '', author: null, error: '', copied: false, copyError: '' });
|
|
226
250
|
drawGenerated();
|
|
227
251
|
let outcome;
|
|
228
|
-
try {
|
|
252
|
+
try {
|
|
253
|
+
const { text, expires } = await coda.inviteFriend({ machine: target.id });
|
|
254
|
+
const { control, authKey, scope, peerNode, bindingToken } = JSON.parse(text);
|
|
255
|
+
outcome = { state: 'done', author: target.author, code: encodeInvite({ control, authKey, peerNode, bindingToken, scope, author: target.author ?? undefined, expires }) };
|
|
229
256
|
// 在确认框里取消:回到空闲,不显示错误。
|
|
230
|
-
catch (e) { outcome = e?.code === 'canceled' ? { state: 'idle' } : { state: 'fail', error: e?.message ?? String(e) }; }
|
|
257
|
+
} catch (e) { outcome = e?.code === 'canceled' ? { state: 'idle' } : { state: 'fail', error: e?.message ?? String(e) }; }
|
|
231
258
|
if (mine !== generated.token) return;
|
|
232
259
|
Object.assign(generated, outcome);
|
|
233
260
|
drawGenerated();
|
|
234
261
|
}
|
|
235
262
|
async function copyInvite() {
|
|
236
263
|
clearTimeout(generated.timer);
|
|
237
|
-
try { await coda.copy(generated.
|
|
264
|
+
try { await coda.copy(generated.code); Object.assign(generated, { copied: true, copyError: '' }); }
|
|
238
265
|
catch (e) { Object.assign(generated, { copied: false, copyError: `复制失败:${e.message}` }); }
|
|
239
266
|
drawGenerated();
|
|
240
267
|
generated.timer = setTimeout(() => { generated.copied = false; drawGenerated(); }, 1500);
|
|
241
268
|
}
|
|
242
269
|
function resetGenerated() {
|
|
243
270
|
clearTimeout(generated.timer);
|
|
244
|
-
Object.assign(generated, { state: 'idle',
|
|
271
|
+
Object.assign(generated, { state: 'idle', code: '', author: null, error: '', copied: false, copyError: '', token: generated.token + 1 });
|
|
245
272
|
drawGenerated();
|
|
246
273
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
//
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
274
|
+
|
|
275
|
+
// 生成侧和接受侧一起清。接受进行中(凭据已交出)不动,等它自己走完。
|
|
276
|
+
function resetInvite() {
|
|
277
|
+
resetGenerated();
|
|
278
|
+
if (accept.state === 'busy') return;
|
|
279
|
+
accept.input.value = '';
|
|
280
|
+
Object.assign(accept, { state: 'empty', parsed: null, card: null });
|
|
281
|
+
drawAccept();
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// 「接受邀请」:粘贴或扫码得到邀请码,识别后只给人看「谁邀请你、还有多久过期」,点「接受」才发请求。
|
|
285
|
+
// state:empty 等邀请码,found 已识别,busy 正在接受,done 接受完成(互认没成时 card.kind 是 error)。
|
|
286
|
+
const accept = { state: 'empty', parsed: null, card: null };
|
|
287
|
+
accept.input = h('textarea', { placeholder: '粘贴对方发来的邀请码', autocomplete: 'off', spellcheck: 'false', oninput: parsePaste });
|
|
288
|
+
const pickButtons = [h('button', { class: 'btn quiet', type: 'button', onclick: () => readCode(() => coda.paste()) }, '粘贴'),
|
|
289
|
+
typeof coda.scanCode === 'function' ? h('button', { class: 'btn quiet', type: 'button', onclick: () => readCode(() => coda.scanCode()) }, '扫码') : null].filter(Boolean);
|
|
290
|
+
accept.pick = h('div', { class: 'line' }, ...pickButtons);
|
|
291
|
+
accept.cardBox = h('div', { class: 'found' });
|
|
292
|
+
accept.button = h('button', { class: 'btn', type: 'button', onclick: submitInvite }, '接受');
|
|
293
|
+
accept.again = h('button', { class: 'link', type: 'button', onclick: resetAccept }, '换一个邀请码');
|
|
294
|
+
const left = ms => (ms <= 3_600_000 ? `${Math.max(1, Math.ceil(ms / 60_000))} 分钟` : `${Math.ceil(ms / 3_600_000)} 小时`);
|
|
295
|
+
const EXPIRED = '这个邀请码已经过期,请对方重新生成';
|
|
296
|
+
function drawAccept() {
|
|
297
|
+
const { state, card } = accept, empty = state === 'empty';
|
|
298
|
+
accept.input.hidden = accept.pick.hidden = !empty;
|
|
299
|
+
accept.button.hidden = !(state === 'found' || state === 'busy');
|
|
300
|
+
// 接受进行中(点「接受」到互认结束)输入框、粘贴、扫码、接受都不能动。
|
|
301
|
+
for (const control of [accept.input, accept.button, ...pickButtons]) control.disabled = state === 'busy';
|
|
302
|
+
accept.again.hidden = empty || state === 'busy';
|
|
303
|
+
accept.again.textContent = state === 'done' ? '再加一位' : '换一个邀请码';
|
|
304
|
+
accept.cardBox.hidden = !card;
|
|
305
|
+
if (!card) return;
|
|
306
|
+
const line = card.kind ? h('div', { class: card.kind }, card.text)
|
|
307
|
+
: h('div', { class: 'secondary' }, `邀请你成为好友${card.expires ? ` · 还有 ${left(card.expires - Date.now())}过期` : ''}`);
|
|
308
|
+
fill(accept.cardBox, personView(card.inviter, card.inviter ? inviterTitle(card.inviter) : '对方', line));
|
|
309
|
+
}
|
|
310
|
+
// 读到的文字放进输入框并识别;在扫码页取消不算错误;接受进行中或弹层已关时回来的结果直接丢掉。
|
|
311
|
+
async function readCode(read) {
|
|
312
|
+
const got = await Promise.resolve().then(read).then(text => ({ text }), error => ({ error }));
|
|
313
|
+
if (accept.state === 'busy' || openDialog !== invite.dialog) return;
|
|
314
|
+
if (got.error) { if (got.error?.code === 'canceled') return; return say(invite.message, `读取失败:${got.error?.message ?? got.error}`, 'error'); }
|
|
315
|
+
const { text } = got;
|
|
316
|
+
accept.input.value = typeof text === 'string' ? text : text?.text ?? '';
|
|
317
|
+
parsePaste();
|
|
262
318
|
}
|
|
263
319
|
function parsePaste() {
|
|
264
|
-
|
|
265
|
-
const text =
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
invite.
|
|
273
|
-
say(invite.message, '已识别邀请,核对后点「接受邀请」', 'ok');
|
|
274
|
-
} catch (e) {
|
|
275
|
-
// 提交时报的是这份的解析错误。
|
|
276
|
-
closeFields();
|
|
277
|
-
say(invite.message, e.message, 'error');
|
|
320
|
+
Object.assign(accept, { state: 'empty', parsed: null, card: null });
|
|
321
|
+
const text = accept.input.value;
|
|
322
|
+
say(invite.message, '');
|
|
323
|
+
if (text.trim()) {
|
|
324
|
+
try {
|
|
325
|
+
const { inviter, expires, ...input } = parseInvite(text);
|
|
326
|
+
if (expires <= Date.now()) throw new Error(EXPIRED);
|
|
327
|
+
Object.assign(accept, { state: 'found', parsed: input, card: { inviter: isAuthor(inviter) ? inviter : null, expires } });
|
|
328
|
+
} catch (e) { say(invite.message, e.message, 'error'); }
|
|
278
329
|
}
|
|
330
|
+
drawAccept();
|
|
279
331
|
}
|
|
280
|
-
function
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
invite.hidden = {};
|
|
332
|
+
function resetAccept() {
|
|
333
|
+
accept.input.value = '';
|
|
334
|
+
parsePaste();
|
|
284
335
|
}
|
|
285
336
|
async function submitInvite() {
|
|
286
|
-
let input;
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
// 粘贴了却还没识别成功,才按粘贴原文再解析一次,成不了就报它的错。
|
|
293
|
-
if (invite.fields.hidden && !invite.paste.value.trim()) return say(invite.message, ASK_PASTE, 'error');
|
|
294
|
-
if (invite.hidden.bindingToken || !invite.paste.value.trim()) return say(invite.message, e.message, 'error');
|
|
295
|
-
try { input = parseInvite(invite.paste.value); } catch (pasted) { return say(invite.message, pasted.message, 'error'); }
|
|
296
|
-
}
|
|
297
|
-
// 入网码和一次性口令是凭据:取出后立刻清空所有输入框和记着的口令,不写进任何存储或日志。
|
|
298
|
-
clearInvite();
|
|
337
|
+
let input = accept.parsed;
|
|
338
|
+
if (!input) return;
|
|
339
|
+
// 入网码和一次性口令是凭据:取出后立刻清空输入框和记着的邀请,不写进任何存储或日志。
|
|
340
|
+
accept.parsed = null;
|
|
341
|
+
accept.input.value = '';
|
|
342
|
+
if (accept.card.expires <= Date.now()) { resetAccept(); return say(invite.message, EXPIRED, 'error'); }
|
|
299
343
|
const target = invite.target, opts = via(target), show = (text, kind) => { if (invite.target === target) say(invite.message, text, kind); };
|
|
300
|
-
|
|
344
|
+
accept.state = 'busy';
|
|
345
|
+
drawAccept();
|
|
301
346
|
show('正在接受邀请,加入对方的通道…', 'progress');
|
|
302
347
|
const peerNode = input.peerNode;
|
|
303
348
|
let node;
|
|
@@ -308,15 +353,49 @@ async function submitInvite() {
|
|
|
308
353
|
({ node } = await pending);
|
|
309
354
|
} catch (e) {
|
|
310
355
|
input = null;
|
|
311
|
-
|
|
356
|
+
Object.assign(accept, { state: 'empty', card: null });
|
|
357
|
+
drawAccept();
|
|
312
358
|
return show(`接受邀请失败:${e.message}。入网码和口令已清空,请重新粘贴邀请再试`, 'error');
|
|
313
359
|
}
|
|
314
360
|
show('已加入通道,正在与对方互认…', 'progress');
|
|
315
361
|
try {
|
|
316
|
-
await coda.call(LOCAL, 'handshake', { node, ...(peerNode ? { peerNode } : {}) }, opts);
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
362
|
+
const { author } = await coda.call(LOCAL, 'handshake', { node, ...(peerNode ? { peerNode } : {}) }, opts) ?? {};
|
|
363
|
+
const { card } = accept, coded = card.inviter;
|
|
364
|
+
// 按互认出的真实身份显示;和邀请码里写的不是同一个人时写明,不说成码里那个人成了好友。
|
|
365
|
+
if (isAuthor(author)) card.inviter = author;
|
|
366
|
+
if (isAuthor(author) && coded && coded !== author) {
|
|
367
|
+
const titles = identityTitles([coded, author]);
|
|
368
|
+
Object.assign(card, { kind: 'error', text: `互认出的身份是 ${titles.get(author)},和邀请码里写的 ${titles.get(coded)} 不一样` });
|
|
369
|
+
} else Object.assign(card, { kind: 'ok', text: '已成为好友' });
|
|
370
|
+
show('', 'hint');
|
|
371
|
+
} catch (e) {
|
|
372
|
+
accept.card.kind = 'error';
|
|
373
|
+
accept.card.text = '已加入通道,但互认暂未完成';
|
|
374
|
+
show(`互认暂未完成(${e.message})。可稍后在好友详情里点「重新互认」重试`, 'error');
|
|
375
|
+
}
|
|
376
|
+
accept.state = 'done';
|
|
377
|
+
drawAccept();
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
drawGenerated();
|
|
381
|
+
drawAccept();
|
|
382
|
+
panes.invite.append(generate.view);
|
|
383
|
+
panes.accept.append(accept.input, accept.pick, accept.cardBox, accept.button, accept.again, invite.message);
|
|
384
|
+
showTab('invite');
|
|
385
|
+
invite.dialog.body.append(h('div', { class: 'segs', role: 'tablist' }, ...tabButtons), panes.invite, panes.accept);
|
|
386
|
+
|
|
387
|
+
// 带 { add: true } 打开好友页(或页面开着时又被这样打开):等页面第一次画完,自动弹出第一节那台电脑的添加弹层;tab 为 accept 停在「接受邀请」。
|
|
388
|
+
let pendingAdd = null;
|
|
389
|
+
function requestAdd(args) {
|
|
390
|
+
if (args?.add !== true) return;
|
|
391
|
+
pendingAdd = { tab: args.tab === 'accept' ? 'accept' : 'invite' };
|
|
392
|
+
flushAdd();
|
|
393
|
+
}
|
|
394
|
+
function flushAdd() {
|
|
395
|
+
if (!pendingAdd || !firstRead || !drawn.length) return;
|
|
396
|
+
showTab(pendingAdd.tab);
|
|
397
|
+
pendingAdd = null;
|
|
398
|
+
openInvite(drawn[0].member);
|
|
320
399
|
}
|
|
321
400
|
|
|
322
401
|
// ---- 好友详情:重新互认、移除都发给这一行所在的电脑 ----
|
|
@@ -985,37 +1064,39 @@ function friendTile(m, row) {
|
|
|
985
1064
|
}
|
|
986
1065
|
|
|
987
1066
|
// 每台电脑一节;只有一台时没有节标题,也不重复画身份卡上已有的状态条和「添加好友」。
|
|
988
|
-
function drawSection({ member: m, title,
|
|
989
|
-
const
|
|
990
|
-
const summary = !unreachable && m.loaded ? accordStatusSummary(m.entry, rows) : null;
|
|
1067
|
+
function drawSection({ member: m, title, offline, rows }) {
|
|
1068
|
+
const summary = accordStatusSummary(m.entry, rows);
|
|
991
1069
|
// 几台时状态条只在有需要提示的状态(证书有问题、待互认、冲突)时出;只有一台时状态条在身份卡上。
|
|
992
|
-
const attention = summary
|
|
993
|
-
const empty = !m.loaded ? '正在读取…' : '还没有好友。点上方「添加好友」,粘贴对方发来的邀请即可。';
|
|
1070
|
+
const attention = summary.cert !== 'active' || summary.friends.pending || summary.friends.conflict;
|
|
994
1071
|
return h('section', {},
|
|
995
|
-
h('h2', { class: 'sec' }, title ?? '好友',
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1072
|
+
h('h2', { class: 'sec' }, title ?? '好友', h('span', { class: 'count', hidden: !rows.length }, String(rows.length)),
|
|
1073
|
+
offline && multi ? h('span', { class: 'badge pending' }, '离线') : null,
|
|
1074
|
+
canNet() ? [h('span', { class: 'spacer' }), h('button', { class: 'kebab', type: 'button', title: '网络节点', 'aria-label': '网络节点', disabled: netBusy, onclick: () => openNet(m) }, netIcon())] : null),
|
|
1075
|
+
multi && offline ? h('p', { class: 'hint' }, OFFLINE_HINT) : null,
|
|
1076
|
+
multi && attention ? statusTiles(m, rows) : null,
|
|
1077
|
+
multi ? h('div', { class: 'line' }, h('button', { class: 'pill strong', type: 'button', onclick: () => openInvite(m) }, '添加好友')) : null,
|
|
1078
|
+
rows.length ? h('ul', { class: 'friends' }, rows.map(row => friendTile(m, row))) : multi ? null : h('p', { class: 'empty' }, '还没有好友。点上方「添加好友」,粘贴对方发来的邀请即可。'),
|
|
1079
|
+
removalBlock(m));
|
|
1002
1080
|
}
|
|
1003
1081
|
|
|
1004
1082
|
const page = h('div', { class: 'page' });
|
|
1005
1083
|
function draw() {
|
|
1084
|
+
if (!firstRead) return;
|
|
1006
1085
|
const titles = memberTitles(members), sections = friendSections(members, titles);
|
|
1007
1086
|
drawn = sections;
|
|
1008
|
-
const ids = identities(
|
|
1087
|
+
const ids = identities(members);
|
|
1009
1088
|
if (!ids.length) ids.push({ author: null, signer: null, members: [] });
|
|
1010
1089
|
fill(page, ids.map(id => drawHero(id, sections, titles)), sections.map(drawSection));
|
|
1011
1090
|
if (state.detail) { if (detailRow()) drawDetail(); else closeDialog(); }
|
|
1012
1091
|
drawPicker();
|
|
1013
1092
|
if (netView) drawNet();
|
|
1093
|
+
flushAdd();
|
|
1014
1094
|
}
|
|
1015
1095
|
|
|
1016
1096
|
picker.dialog.scrim.hidden = true;
|
|
1017
1097
|
root.replaceChildren(page, detail.dialog.scrim, invite.dialog.scrim, picker.dialog.scrim, net.dialog.scrim);
|
|
1018
|
-
draw();
|
|
1019
1098
|
|
|
1020
1099
|
coda.header({ text: '好友' }).catch(() => {});
|
|
1021
|
-
|
|
1100
|
+
requestAdd(coda.args);
|
|
1101
|
+
coda.on('context', ({ args }) => requestAdd(args));
|
|
1102
|
+
start();
|
package/view/style.css
CHANGED
|
@@ -37,10 +37,6 @@ button:disabled { opacity: .5; cursor: default; }
|
|
|
37
37
|
.stack { display: flex; flex-direction: column; gap: 12px; }
|
|
38
38
|
.actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
|
|
39
39
|
.actions.split { padding-top: 12px; border-top: 1px solid var(--line); }
|
|
40
|
-
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
|
|
41
|
-
.lbl { display: block; color: var(--fg2); font-size: .88em; font-weight: 600; }
|
|
42
|
-
.fields { display: flex; flex-wrap: wrap; gap: 10px; }
|
|
43
|
-
.fields > .field { flex: 1 1 140px; }
|
|
44
40
|
input, textarea { width: 100%; min-width: 0; padding: 8px 10px; border-radius: var(--coda-radius, 9px); border: 1px solid var(--line); background: var(--bg); outline: none; }
|
|
45
41
|
input:focus, textarea:focus { border-color: var(--accent); }
|
|
46
42
|
textarea { min-height: 84px; resize: vertical; }
|
|
@@ -145,15 +141,24 @@ details[open] > summary { margin-bottom: 10px; }
|
|
|
145
141
|
.key-full { overflow-wrap: anywhere; word-break: break-all; }
|
|
146
142
|
.warn-box { margin: 0 0 8px; padding: 9px 11px; border-radius: 11px; background: color-mix(in srgb, var(--red) 11%, transparent); color: var(--red); font-size: .92em; }
|
|
147
143
|
|
|
148
|
-
/*
|
|
149
|
-
.
|
|
150
|
-
.
|
|
151
|
-
.
|
|
152
|
-
.
|
|
153
|
-
.
|
|
154
|
-
.
|
|
155
|
-
.
|
|
156
|
-
.
|
|
144
|
+
/* 添加好友:分段「邀请对方 / 接受邀请」,邀请页是头像、二维码、一行邀请码,接受页是输入框和识别出的邀请人 */
|
|
145
|
+
.segs { display: flex; padding: 2px; margin-bottom: 14px; border-radius: 10px; background: var(--fill); }
|
|
146
|
+
.segs button { flex: 1; padding: 5px 0; border: 0; border-radius: 8px; background: none; font-weight: 500; }
|
|
147
|
+
.segs button.on { background: var(--bg); box-shadow: 0 1px 3px rgba(0, 0, 0, .14); font-weight: 650; }
|
|
148
|
+
.pane { display: flex; flex-direction: column; gap: 14px; }
|
|
149
|
+
.person { display: flex; align-items: center; gap: 10px; min-width: 0; }
|
|
150
|
+
.person > .av { width: 40px; height: 40px; border-radius: 50%; }
|
|
151
|
+
.person-info { min-width: 0; }
|
|
152
|
+
.nm { font-weight: 650; overflow-wrap: anywhere; }
|
|
153
|
+
.qr { align-self: center; padding: 10px; border-radius: 16px; background: #fff; box-shadow: 0 0 0 1px var(--line); }
|
|
154
|
+
.qr svg { display: block; width: 216px; height: 216px; }
|
|
155
|
+
.code-line { display: flex; padding: 9px 12px; border-radius: 10px; background: var(--card); color: var(--fg2); }
|
|
156
|
+
.code-line > span { flex: 1; }
|
|
157
|
+
.center { text-align: center; }
|
|
158
|
+
.found { display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 16px; border-radius: 14px; background: var(--card); text-align: center; }
|
|
159
|
+
.found .person { flex-direction: column; }
|
|
160
|
+
.found .person > .av { width: 56px; height: 56px; }
|
|
161
|
+
.link { align-self: center; padding: 4px; border: 0; background: none; color: var(--accent); }
|
|
157
162
|
|
|
158
163
|
/* 好友详情里的「共享」 */
|
|
159
164
|
.share { padding-top: 12px; border-top: 1px solid var(--line); display: flex; flex-direction: column; gap: 16px; }
|