@coda-console/friends 0.4.0 → 0.6.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 +25 -34
- package/lib/avatar.mjs +5 -27
- package/lib/friends.mjs +9 -142
- package/lib/gateway.mjs +16 -165
- package/lib/nickname.mjs +43 -0
- package/lib/outbox.mjs +14 -12
- package/lib/view-members.mjs +38 -0
- package/main.mjs +5 -31
- package/package.json +7 -175
- package/view/app.mjs +148 -460
- package/view/style.css +12 -60
- package/lib/notes.mjs +0 -65
- package/lib/social-streams.mjs +0 -218
- package/lib/view-machine.mjs +0 -42
- package/view/cropper.mjs +0 -106
package/view/app.mjs
CHANGED
|
@@ -1,142 +1,58 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { fill, h, identiconSvg
|
|
1
|
+
// 好友页:各台电脑的 Accord 身份(Hero)、按电脑分节的好友列表与详情、添加好友。
|
|
2
|
+
// 页面不自己选机器:视图里有几台就都读,身份按公钥归组、好友按电脑分节(lib/view-members.mjs);写操作发往它所在的那一台。
|
|
3
|
+
// Accord 的读写(coda/accord@1)都在这里按交互发起。昵称、头像都由 Accord 公钥推出(nickname.mjs、avatar.mjs),没有可编辑的名片;
|
|
4
|
+
// 节点名等外来数据一律用 textContent / DOM API 渲染,从不拼 HTML 字符串。
|
|
5
|
+
import { accordStatusSummary, resolveAvatar } from '../lib/avatar.mjs';
|
|
6
|
+
import { checked, parseInvite } from '../lib/friends.mjs';
|
|
7
|
+
import { identityTitle } from '../lib/nickname.mjs';
|
|
8
|
+
import { friendSections, identities, listMembers } from '../lib/view-members.mjs';
|
|
9
|
+
import { fill, h, identiconSvg } from './dom.mjs';
|
|
10
10
|
|
|
11
11
|
const ACCORD = 'coda/accord@1', LOCAL = { kind: ACCORD, id: 'local' };
|
|
12
12
|
const coda = window.coda;
|
|
13
13
|
const root = document.getElementById('root');
|
|
14
|
-
const COLOR_NAMES = { blue: '蓝', orange: '橙', green: '绿', purple: '紫', pink: '粉', teal: '青', gray: '灰' };
|
|
15
14
|
const STATUS_TEXT = { bound: '已互认', pending: '待互认', conflict: '密钥冲突' };
|
|
16
|
-
const MODES = [['photo', '照片'], ['identicon', '几何纹'], ['emoji', 'Emoji'], ['monogram', '字标']];
|
|
17
|
-
const EMOJIS = ['😀', '😎', '🦊', '🐱', '🐼', '🌿', '☕', '🎧', '🚀', '⚡', '🌙', '✨'];
|
|
18
|
-
const UPLOAD_ERRORS = { 404: '当前客户端还不支持上传头像,请升级 Coda 后再试', 413: '图片太大(超过 5 MiB),请换一张' };
|
|
19
15
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const via =
|
|
26
|
-
|
|
27
|
-
const multiMachine = () => Array.isArray(coda.machines) && coda.machines.length > 1;
|
|
28
|
-
const blobMachine = () => (multiMachine() ? currentMachine : null);
|
|
29
|
-
// 每次选机自增(A → B → A 也不同):旧机器的订阅回调、异步回写,凭它丢弃。
|
|
30
|
-
let selectionGen = 0;
|
|
31
|
-
// 编辑名片的会话代际:每次打开、关闭编辑弹层或切机自增;旧会话的发布做完后不碰新会话的弹层、按钮、提示和裁剪。
|
|
32
|
-
let editorGen = 0;
|
|
16
|
+
// 视图里的每台电脑(成员在页面生命周期内不变)各存一份数据;读不到的(unreachable)也在,只是不发请求。
|
|
17
|
+
const members = listMembers(coda).map(m => ({ ...m, entry: null, author: null, friends: [], loaded: false, accordProblem: null }));
|
|
18
|
+
const live = members.filter(m => !m.unreachable);
|
|
19
|
+
const multi = members.length > 1;
|
|
20
|
+
const state = { busy: new Set(), notice: new Map(), heroNotice: new Map(), detail: null, removing: null };
|
|
21
|
+
const via = m => ({ machine: m.id });
|
|
22
|
+
const rowKey = (m, node) => `${m.id}\u0000${node}`;
|
|
33
23
|
function say(target, text, kind = 'hint') { target.className = kind; target.textContent = text; }
|
|
34
24
|
|
|
35
|
-
// ----
|
|
36
|
-
|
|
37
|
-
let counter = 0;
|
|
38
|
-
const bridge = createMachineBridge({
|
|
39
|
-
coda,
|
|
40
|
-
getMachine: () => currentMachine,
|
|
41
|
-
getEpoch: () => selectionGen,
|
|
42
|
-
onMessage({ type, rid, ok, value, error }) {
|
|
43
|
-
if (type !== 'reply') return;
|
|
44
|
-
const wait = pendingReplies.get(rid);
|
|
45
|
-
if (!wait) return;
|
|
46
|
-
pendingReplies.delete(rid);
|
|
47
|
-
if (ok) wait.resolve(value); else wait.reject(new Error(error));
|
|
48
|
-
},
|
|
49
|
-
onError(error, msg) {
|
|
50
|
-
const wait = pendingReplies.get(msg.rid);
|
|
51
|
-
if (!wait) return;
|
|
52
|
-
pendingReplies.delete(msg.rid);
|
|
53
|
-
wait.reject(new Error(`发送失败:${error?.message ?? error}`));
|
|
54
|
-
},
|
|
55
|
-
});
|
|
56
|
-
function ask(type, input = {}) {
|
|
57
|
-
const rid = `n${++counter}`, machine = currentMachine;
|
|
58
|
-
return new Promise((resolve, reject) => {
|
|
59
|
-
const timer = setTimeout(() => { pendingReplies.delete(rid); reject(new Error('模块没有回应')); }, 10_000);
|
|
60
|
-
pendingReplies.set(rid, { machine, resolve: v => { clearTimeout(timer); resolve(v); }, reject: e => { clearTimeout(timer); reject(e); } });
|
|
61
|
-
bridge.send({ type, rid, ...input });
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// ---- 读本机 Accord ----
|
|
66
|
-
let reading = 0;
|
|
67
|
-
// profile 与 friends/card 各读各的:一种读不到或没有记录,不挡另一种。
|
|
68
|
-
async function readProfiles() {
|
|
69
|
-
const mine = ++reading;
|
|
70
|
-
const [profiles, cards] = await Promise.allSettled(['profile', CARD_KIND].map(kind => coda.query(LOCAL, 'records', { kind, limit: 200 }, via())));
|
|
71
|
-
if (mine !== reading) return;
|
|
72
|
-
if (profiles.status === 'fulfilled') state.profiles = profiles.value?.records ?? [];
|
|
73
|
-
if (cards.status === 'fulfilled') state.cards = cards.value?.records ?? [];
|
|
74
|
-
const failed = [profiles, cards].find(read => read.status === 'rejected');
|
|
75
|
-
state.problem = failed ? `读不到好友名片:${failed.reason?.message ?? failed.reason}` : null;
|
|
76
|
-
loadOwn();
|
|
77
|
-
draw();
|
|
78
|
-
}
|
|
79
|
-
// 本机已发布的名片(Hero 展示它);编辑弹层另有一份草稿,打开时从它拷贝。
|
|
80
|
-
const blankOwn = () => ({ name: '', emoji: '', color: 'blue', avatar: undefined });
|
|
81
|
-
const own = blankOwn();
|
|
82
|
-
const latestOwn = (records, read) => records.map(read).filter(p => p && p.author === state.author).sort((a, b) => b.version - a.version)[0];
|
|
83
|
-
// 与好友列表同一条合并规则(profile 写了的优先,缺的由 friends/card 补);头像随名片带着,重新发布不会把它丢掉或带回已清除的。
|
|
84
|
-
function loadOwn() {
|
|
85
|
-
const mine = mergeProfileAndCard(latestOwn(state.profiles, readProfileRecord), latestOwn(state.cards, readCardRecord));
|
|
86
|
-
if (mine) Object.assign(own, { name: mine.name, emoji: mine.emoji ?? '', color: mine.color ?? 'blue', avatar: mine.avatarPresent ? mine.avatar : undefined });
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
let watching = null;
|
|
90
|
-
function onChange({ type, entry }) {
|
|
25
|
+
// ---- 读各台的 Accord ----
|
|
26
|
+
function onChange(m, { type, entry }) {
|
|
91
27
|
if (type === 'DELETED' || !entry) return;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
28
|
+
m.entry = entry;
|
|
29
|
+
m.author = entry.spec?.author ?? null;
|
|
30
|
+
m.friends = entry.status?.friends ?? [];
|
|
31
|
+
m.loaded = true;
|
|
96
32
|
draw();
|
|
97
|
-
readProfiles();
|
|
98
33
|
}
|
|
99
|
-
async function start() {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (gen !== selectionGen) return;
|
|
104
|
-
if (!watching) {
|
|
105
|
-
let mine;
|
|
106
|
-
try {
|
|
107
|
-
mine = watching = Promise.resolve(coda.watch(ACCORD, entry => { if (gen === selectionGen) onChange(entry); }, { id: 'local' }, opts));
|
|
108
|
-
await mine;
|
|
109
|
-
} catch (e) { if (watching === mine) watching = null; if (gen === selectionGen) { state.problem = `读不到本机的好友信息:${e.message}`; state.loaded = true; } }
|
|
110
|
-
}
|
|
111
|
-
if (gen === selectionGen) draw();
|
|
34
|
+
async function start(m) {
|
|
35
|
+
try { await coda.watch(ACCORD, event => onChange(m, event), { id: 'local' }, via(m)); }
|
|
36
|
+
catch (e) { m.accordProblem = `读不到本机的好友信息:${e.message}`; }
|
|
37
|
+
draw();
|
|
112
38
|
}
|
|
113
39
|
|
|
114
40
|
// ---- 头像 ----
|
|
115
|
-
// 按 resolveAvatar
|
|
41
|
+
// 按 resolveAvatar 的描述画头像:公钥的几何纹,没有公钥时取首字。
|
|
116
42
|
function avatar(desc, size) {
|
|
117
|
-
const node = h('span', { 'aria-hidden': 'true' });
|
|
118
|
-
|
|
119
|
-
if (d.kind === 'photo' && state.failedPhotos.has(d.url)) return show(d.fallback);
|
|
120
|
-
node.className = `av ${size} k-${d.kind}`;
|
|
121
|
-
if (d.kind === 'photo') node.replaceChildren(h('img', { src: d.url, alt: '', draggable: 'false', onerror: () => { state.failedPhotos.add(d.url); show(d.fallback); } }));
|
|
122
|
-
else if (d.kind === 'identicon') node.replaceChildren(identiconSvg(d.grid));
|
|
123
|
-
else node.textContent = d.kind === 'emoji' ? d.emoji : d.text;
|
|
124
|
-
};
|
|
125
|
-
show(desc);
|
|
43
|
+
const node = h('span', { 'aria-hidden': 'true', class: `av ${size} k-${desc.kind}` });
|
|
44
|
+
if (desc.kind === 'identicon') node.replaceChildren(identiconSvg(desc.grid)); else node.textContent = desc.text;
|
|
126
45
|
return node;
|
|
127
46
|
}
|
|
128
|
-
const
|
|
129
|
-
const rowAvatar = row => resolveAvatar({ avatar: row.avatar, emoji: row.emoji, author: row.author, name: row.title, machine: blobMachine() });
|
|
130
|
-
const cameraIcon = () => svg('svg', { viewBox: '0 0 24 24', width: 11, height: 11, fill: 'none', stroke: 'currentColor', 'stroke-width': 2.4, 'aria-hidden': 'true' },
|
|
131
|
-
svg('path', { d: 'M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z' }), svg('circle', { cx: 12, cy: 13, r: 4 }));
|
|
47
|
+
const rowAvatar = row => resolveAvatar({ author: row.author, name: row.title });
|
|
132
48
|
|
|
133
49
|
// ---- 弹层:宽屏居中模态,窄屏底部抽屉;同一时间只开一个 ----
|
|
134
50
|
let openDialog = null;
|
|
135
|
-
function makeDialog({ title, close: closeLabel, onClose
|
|
51
|
+
function makeDialog({ title, close: closeLabel, onClose }) {
|
|
136
52
|
const heading = h('h3', {}, title), body = h('div', { class: 'dlg-body' });
|
|
137
53
|
const panel = h('div', { class: 'dialog', role: 'dialog', 'aria-modal': 'true' }, h('div', { class: 'grabber', 'aria-hidden': 'true' }),
|
|
138
54
|
h('div', { class: 'dlg-head' }, heading, h('button', { class: 'btn quiet small', type: 'button', onclick: () => closeDialog() }, closeLabel)),
|
|
139
|
-
body
|
|
55
|
+
body);
|
|
140
56
|
const scrim = h('div', { class: 'scrim', onclick: e => { if (e.target === scrim) closeDialog(); } }, panel);
|
|
141
57
|
scrim.hidden = true;
|
|
142
58
|
return { scrim, heading, body, onClose };
|
|
@@ -157,259 +73,52 @@ function closeDialog() {
|
|
|
157
73
|
}
|
|
158
74
|
addEventListener('keydown', e => { if (e.key === 'Escape' && openDialog) closeDialog(); });
|
|
159
75
|
|
|
160
|
-
// ----
|
|
161
|
-
|
|
162
|
-
function drawMachines() {
|
|
163
|
-
const machines = Array.isArray(coda.machines) ? coda.machines : [];
|
|
164
|
-
machineBar.hidden = machines.length < 2;
|
|
165
|
-
fill(machineBar, ...(machines.length < 2 ? [] : machines.map(m => {
|
|
166
|
-
if (m.unreachable) {
|
|
167
|
-
const why = m.unreachable === true ? '暂时连不上' : String(m.unreachable);
|
|
168
|
-
return h('button', { class: 'm-pill', type: 'button', disabled: true, title: `不可达:${why}`, 'aria-label': `${m.name || m.id}(不可达:${why})` },
|
|
169
|
-
h('span', { class: 'm-dot off' }), m.name || m.id, h('small', { class: 'm-why' }, '不可达'));
|
|
170
|
-
}
|
|
171
|
-
return h('button', { class: 'm-pill', type: 'button', 'aria-pressed': String(m.id === currentMachine), onclick: () => switchMachine(m.id) }, h('span', { class: 'm-dot' }), m.name || m.id);
|
|
172
|
-
})));
|
|
173
|
-
}
|
|
174
|
-
function switchMachine(id) {
|
|
175
|
-
if (id === currentMachine) return;
|
|
176
|
-
currentMachine = id;
|
|
177
|
-
selectionGen++;
|
|
178
|
-
editorGen++;
|
|
179
|
-
coda.setState?.({ machine: currentMachine });
|
|
180
|
-
reading++;
|
|
181
|
-
watching?.then(w => w?.dispose?.(), () => {});
|
|
182
|
-
watching = null;
|
|
183
|
-
for (const [rid, wait] of [...pendingReplies]) { pendingReplies.delete(rid); wait.reject(new Error('已切换机器')); }
|
|
184
|
-
closeDialog();
|
|
185
|
-
Object.assign(state, newState());
|
|
186
|
-
Object.assign(own, blankOwn());
|
|
187
|
-
editor.publish.disabled = false; invite.button.disabled = false;
|
|
188
|
-
for (const target of [hero.message, editor.message, invite.message]) say(target, '');
|
|
189
|
-
drawMachines();
|
|
190
|
-
draw();
|
|
191
|
-
start();
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// ---- 我的名片:Hero 与状态条 ----
|
|
195
|
-
const hero = {
|
|
196
|
-
card: h('section', { class: 'hero' }),
|
|
197
|
-
avatar: h('button', { class: 'av-ring', type: 'button', 'aria-label': '更换头像', title: '更换头像', onclick: () => openEditor() }),
|
|
198
|
-
name: h('h2', { class: 'hero-name selectable' }),
|
|
199
|
-
key: h('button', { class: 'fp mono', type: 'button', onclick: copyKey }),
|
|
200
|
-
lead: h('p', { class: 'hint' }),
|
|
201
|
-
message: h('p', { class: 'hint' }),
|
|
202
|
-
friends: h('div', { class: 'tile' }),
|
|
203
|
-
cert: h('div', { class: 'tile' }),
|
|
204
|
-
};
|
|
205
|
-
hero.card.append(
|
|
206
|
-
h('div', { class: 'banner' }, h('div', { class: 'banner-acts' },
|
|
207
|
-
h('button', { class: 'pill', type: 'button', onclick: () => openEditor() }, '编辑名片'),
|
|
208
|
-
h('button', { class: 'pill strong', type: 'button', onclick: () => showDialog(invite.dialog) }, '添加好友'))),
|
|
209
|
-
h('div', { class: 'hero-body' },
|
|
210
|
-
h('div', { class: 'hero-id' }, hero.avatar, h('div', { class: 'hero-names' }, h('div', { class: 'name-line' }, hero.name, h('span', { class: 'tag' }, '我的名片')), hero.key)),
|
|
211
|
-
hero.lead, hero.message,
|
|
212
|
-
h('div', { class: 'bento' }, hero.friends, hero.cert)));
|
|
76
|
+
// ---- 本机身份:每个身份一张 Hero ----
|
|
77
|
+
// 只有一台电脑时,Hero 上带这台的状态条和「添加好友」;几台时这两样都在各自的节里。
|
|
213
78
|
const CERT = {
|
|
214
79
|
active: { dot: 'ok-dot', text: '可发布' },
|
|
215
|
-
needs_cert: { dot: 'warn-dot', text: '需要证书', sub: '需绑定或续期代签证书:前往客户端「设置 ›
|
|
80
|
+
needs_cert: { dot: 'warn-dot', text: '需要证书', sub: '需绑定或续期代签证书:前往客户端「设置 › 身份」处理' },
|
|
216
81
|
no_identity: { dot: 'off-dot', text: '未建身份', sub: '前往客户端「设置 › 身份」创建' },
|
|
217
82
|
};
|
|
218
83
|
const formatTime = ms => new Date(ms).toLocaleString('zh-CN', { month: 'numeric', day: 'numeric', hour: '2-digit', minute: '2-digit' });
|
|
219
|
-
function tile(
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
function mirrorCrop() {
|
|
258
|
-
const ctx = preview.canvas.getContext('2d');
|
|
259
|
-
ctx.clearRect(0, 0, 144, 144);
|
|
260
|
-
ctx.drawImage(cropper.canvas, 0, 0, 144, 144);
|
|
261
|
-
}
|
|
262
|
-
const editor = {
|
|
263
|
-
dialog: null,
|
|
264
|
-
preview: h('div', { class: 'mini-card' }),
|
|
265
|
-
modes: h('div', { class: 'seg', role: 'group', 'aria-label': '头像类型' }),
|
|
266
|
-
file: h('input', { type: 'file', accept: 'image/*', onchange: pickFile }),
|
|
267
|
-
photo: h('div', { class: 'pane' }),
|
|
268
|
-
emoji: h('input', { type: 'text', maxlength: '8', placeholder: '自定义 Emoji(1–8 字)', autocomplete: 'off', oninput: e => { draft.emoji = e.target.value; syncEditor(); } }),
|
|
269
|
-
emojis: h('div', { class: 'emoji-grid' }),
|
|
270
|
-
other: h('p', { class: 'hint' }),
|
|
271
|
-
name: h('input', { type: 'text', maxlength: '40', placeholder: '昵称(1–40 字)', autocomplete: 'off', oninput: e => { draft.name = e.target.value; syncPreview(); } }),
|
|
272
|
-
swatches: h('div', { class: 'swatches', role: 'group', 'aria-label': '主题色' }),
|
|
273
|
-
message: h('p', { class: 'hint' }),
|
|
274
|
-
publish: h('button', { class: 'btn', type: 'button', onclick: publishCard }, '发布名片'),
|
|
275
|
-
};
|
|
276
|
-
editor.file.hidden = true;
|
|
277
|
-
editor.emojiPane = h('div', { class: 'pane' }, editor.emojis, editor.emoji);
|
|
278
|
-
editor.dialog = makeDialog({ title: '编辑名片', close: '取消', onClose: () => { editorGen++; cropper.clear(); },
|
|
279
|
-
foot: [editor.message, h('span', { class: 'spacer' }), editor.publish] });
|
|
280
|
-
editor.dialog.body.append(h('div', { class: 'ed-cols' },
|
|
281
|
-
h('div', { class: 'ed-preview' }, editor.preview, h('p', { class: 'hint center' }, '实时预览 · 好友看到的效果')),
|
|
282
|
-
h('div', { class: 'ed-controls' },
|
|
283
|
-
h('span', { class: 'lbl' }, '头像'), editor.modes, editor.file, editor.photo, editor.emojiPane, editor.other,
|
|
284
|
-
h('label', { class: 'field' }, h('span', { class: 'lbl' }, '昵称'), editor.name),
|
|
285
|
-
h('div', { class: 'field' }, h('span', { class: 'lbl' }, '主题色'), editor.swatches))));
|
|
286
|
-
|
|
287
|
-
const modeOf = card => (card.avatar ? 'photo' : !card.emoji ? 'identicon' : card.emoji === initialChar(card.name, '') ? 'monogram' : 'emoji');
|
|
288
|
-
function openEditor() {
|
|
289
|
-
editorGen++;
|
|
290
|
-
cropper.clear();
|
|
291
|
-
editor.publish.disabled = false;
|
|
292
|
-
Object.assign(draft, { name: own.name, color: own.color, mode: modeOf(own), emoji: own.emoji, avatar: own.avatar ?? null });
|
|
293
|
-
editor.name.value = draft.name;
|
|
294
|
-
editor.emoji.value = draft.mode === 'emoji' ? draft.emoji : '';
|
|
295
|
-
if (draft.mode !== 'emoji') draft.emoji = '';
|
|
296
|
-
say(editor.message, '');
|
|
297
|
-
syncEditor();
|
|
298
|
-
showDialog(editor.dialog);
|
|
299
|
-
}
|
|
300
|
-
function draftAvatar() {
|
|
301
|
-
const author = state.author, name = draft.name;
|
|
302
|
-
if (draft.mode === 'photo') return resolveAvatar({ avatar: draft.avatar, emoji: own.emoji, author, name, machine: blobMachine() });
|
|
303
|
-
if (draft.mode === 'emoji') return resolveAvatar({ emoji: draft.emoji, author, name });
|
|
304
|
-
if (draft.mode === 'monogram') return { kind: 'monogram', text: initialChar(name) };
|
|
305
|
-
return resolveAvatar({ author, name });
|
|
306
|
-
}
|
|
307
|
-
function syncPreview() {
|
|
308
|
-
const fresh = draft.mode === 'photo' && cropper.ready;
|
|
309
|
-
editor.preview.className = `mini-card c-${draft.color}`;
|
|
310
|
-
editor.preview.replaceChildren(h('div', { class: 'banner small' }),
|
|
311
|
-
h('div', { class: 'mini-body' },
|
|
312
|
-
h('span', { class: 'av-ring md' }, fresh ? h('span', { class: 'av md k-photo' }, preview.canvas) : avatar(draftAvatar(), 'md')),
|
|
313
|
-
h('div', { class: 'mini-name truncate' }, draft.name.trim() || '未命名'),
|
|
314
|
-
state.author ? h('div', { class: 'mono secondary' }, `@${shortAuthor(state.author)}`) : null));
|
|
315
|
-
if (fresh) mirrorCrop();
|
|
316
|
-
if (draft.mode === 'monogram') editor.other.textContent = `以昵称首字「${initialChar(draft.name)}」作头像,昵称改了它跟着变。`;
|
|
317
|
-
}
|
|
318
|
-
function syncEditor() {
|
|
319
|
-
editor.modes.replaceChildren(...MODES.map(([mode, label]) => h('button', { type: 'button', 'aria-pressed': String(draft.mode === mode), onclick: () => { draft.mode = mode; syncEditor(); } }, label)));
|
|
320
|
-
editor.photo.hidden = draft.mode !== 'photo';
|
|
321
|
-
editor.emojiPane.hidden = draft.mode !== 'emoji';
|
|
322
|
-
editor.other.hidden = draft.mode !== 'identicon' && draft.mode !== 'monogram';
|
|
323
|
-
editor.other.textContent = draft.mode === 'identicon' ? (state.author ? '由你的公钥确定性生成的 5×5 几何纹,换设备也一样。' : '取得身份后按公钥生成几何纹;现在先以昵称首字代替。') : '';
|
|
324
|
-
const choose = h('button', { class: 'btn quiet small', type: 'button', onclick: () => editor.file.click() }, cropper.ready || draft.avatar ? '换一张' : '选择图片');
|
|
325
|
-
const clear = cropper.ready || draft.avatar ? h('button', { class: 'btn danger small', type: 'button', onclick: clearPhoto }, '清除照片') : null;
|
|
326
|
-
fill(editor.photo, ...(cropper.ready
|
|
327
|
-
? [cropper.box, h('div', { class: 'zoom-row' }, h('span', { class: 'secondary' }, '缩放'), cropper.zoom, cropper.zoomText),
|
|
328
|
-
h('div', { class: 'actions' }, choose, h('button', { class: 'btn quiet small', type: 'button', onclick: () => cropper.reset() }, '重置裁切'), clear),
|
|
329
|
-
h('p', { class: 'hint' }, '拖动图片调整位置;发布时导出 512×512 的正方形头像。')]
|
|
330
|
-
: [h('p', { class: 'hint' }, draft.avatar ? '正在使用已发布的照片。' : '从本机选一张图片,裁剪成正方形头像。'), h('div', { class: 'actions' }, choose, clear)]));
|
|
331
|
-
editor.emojis.replaceChildren(...EMOJIS.map(mark => h('button', { type: 'button', class: 'emoji-opt', 'aria-label': `Emoji ${mark}`, 'aria-pressed': String(draft.emoji === mark),
|
|
332
|
-
onclick: () => { draft.emoji = mark; editor.emoji.value = mark; syncEditor(); } }, mark)));
|
|
333
|
-
editor.swatches.replaceChildren(...COLORS.map(color => h('button', { class: `swatch c-${color}`, type: 'button', 'aria-label': `主题色:${COLOR_NAMES[color]}`, 'aria-pressed': String(draft.color === color),
|
|
334
|
-
onclick: () => { draft.color = color; syncEditor(); } })));
|
|
335
|
-
syncPreview();
|
|
336
|
-
}
|
|
337
|
-
function pickFile(e) {
|
|
338
|
-
const file = e.target.files?.[0];
|
|
339
|
-
e.target.value = '';
|
|
340
|
-
if (!file) return;
|
|
341
|
-
say(editor.message, '');
|
|
342
|
-
cropper.load(file).then(loaded => { if (loaded) { draft.mode = 'photo'; syncEditor(); } }, error => { say(editor.message, error.message, 'error'); syncEditor(); });
|
|
343
|
-
}
|
|
344
|
-
function clearPhoto() {
|
|
345
|
-
cropper.clear();
|
|
346
|
-
draft.avatar = null;
|
|
347
|
-
draft.mode = 'identicon';
|
|
348
|
-
syncEditor();
|
|
349
|
-
}
|
|
350
|
-
// 发布用的名片字段:原名片有图而这次不用照片,就显式写 avatar: null 清除;照片模式没换图就沿用已有指针,不重复上传。
|
|
351
|
-
function draftFields() {
|
|
352
|
-
const fields = { name: draft.name, color: draft.color }, drop = own.avatar ? null : undefined;
|
|
353
|
-
if (draft.mode === 'photo') {
|
|
354
|
-
if (!cropper.ready && !draft.avatar) throw new Error('请先选择一张图片,或换用其他头像类型');
|
|
355
|
-
return { ...fields, emoji: own.emoji, avatar: cropper.ready ? undefined : draft.avatar };
|
|
356
|
-
}
|
|
357
|
-
if (draft.mode === 'emoji') {
|
|
358
|
-
if (!draft.emoji.trim()) throw new Error('请选一个或输入一个 Emoji');
|
|
359
|
-
return { ...fields, emoji: draft.emoji, avatar: drop };
|
|
360
|
-
}
|
|
361
|
-
// 字标不扩协议:把昵称首字写进 emoji,双方都按 emoji 显示。
|
|
362
|
-
return { ...fields, emoji: draft.mode === 'monogram' ? initialChar(draft.name, '') : undefined, avatar: drop };
|
|
363
|
-
}
|
|
364
|
-
async function uploadAvatar(blob, machine) {
|
|
365
|
-
let response;
|
|
366
|
-
try { response = await fetch(blobUploadUrl(machine), { method: 'POST', headers: { 'Content-Type': blob.type }, body: blob }); }
|
|
367
|
-
catch (e) { throw new Error(`头像上传失败(网络错误:${e.message})`); }
|
|
368
|
-
if (!response.ok) throw new Error(UPLOAD_ERRORS[response.status] ?? `头像上传失败(HTTP ${response.status})`);
|
|
369
|
-
const { owner, path, sha256, mime, size, name } = await response.json();
|
|
370
|
-
return normalizeContentPointer({ owner, path, sha256, mime, size, name });
|
|
371
|
-
}
|
|
372
|
-
// 发 profile;绑定了代签证书时 profile 须由客户端根签,这里发不了:改发可代签的社交名片 friends/card,好友一样看得到。
|
|
373
|
-
async function publishBody(body, opts, author, live) {
|
|
374
|
-
try {
|
|
375
|
-
await coda.call(LOCAL, 'publish', { kind: 'profile', id: 'self', body, audience: ['public'] }, opts);
|
|
376
|
-
return '名片已发布,好友会看到它';
|
|
377
|
-
} catch (e) {
|
|
378
|
-
if (e?.code !== 'forbidden') throw e;
|
|
379
|
-
await coda.call(LOCAL, 'publish', { kind: CARD_KIND, id: 'self', body, audience: ['public'] }, opts);
|
|
380
|
-
// 缓存的 state.profiles 可能还没读完或读失败了:现查一次本机作者的根签 profile,查不出来就不承诺。已切机就不必查了。
|
|
381
|
-
if (!live()) return null;
|
|
382
|
-
let rooted = null;
|
|
383
|
-
try { rooted = ((await coda.query(LOCAL, 'records', { kind: 'profile', author, limit: 1 }, opts))?.records ?? []).some(record => readProfileRecord(record)); } catch { /* 按没读出来提示 */ }
|
|
384
|
-
return cardFallbackNotice(rooted);
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
// 上传与发布都绑定入口时的机器与代际:途中切了机,仍在原机器做完,但不往新机器的页面写任何状态。
|
|
388
|
-
async function publishCard() {
|
|
389
|
-
const upload = draft.mode === 'photo' && cropper.ready;
|
|
390
|
-
let body;
|
|
391
|
-
try { body = normalizeCardInput(draftFields()); } catch (e) { return say(editor.message, e.message, 'error'); }
|
|
392
|
-
const gen = selectionGen, session = editorGen, opts = via(), machine = blobMachine(), author = state.author, live = () => gen === selectionGen;
|
|
393
|
-
const current = () => live() && session === editorGen;
|
|
394
|
-
editor.publish.disabled = true;
|
|
395
|
-
try {
|
|
396
|
-
if (upload) {
|
|
397
|
-
say(editor.message, '正在处理图片…', 'progress');
|
|
398
|
-
const blob = await cropper.export();
|
|
399
|
-
if (current()) say(editor.message, '正在上传头像…', 'progress');
|
|
400
|
-
body = { ...body, avatar: await uploadAvatar(blob, machine) };
|
|
401
|
-
}
|
|
402
|
-
if (current()) say(editor.message, '正在发布…', 'progress');
|
|
403
|
-
const notice = await publishBody(body, opts, author, live);
|
|
404
|
-
if (live()) readProfiles();
|
|
405
|
-
if (current()) {
|
|
406
|
-
// 关弹层会让会话代际自增,所以先恢复按钮。
|
|
407
|
-
editor.publish.disabled = false;
|
|
408
|
-
closeDialog();
|
|
409
|
-
say(hero.message, notice, 'ok');
|
|
410
|
-
}
|
|
411
|
-
} catch (e) { if (current()) say(editor.message, `没有发布成功:${e.message}`, 'error'); }
|
|
412
|
-
if (current()) editor.publish.disabled = false;
|
|
84
|
+
function tile(label, value, sub, dot) {
|
|
85
|
+
return h('div', { class: 'tile' }, h('span', { class: 'tile-k' }, label), h('span', { class: 'tile-v' }, dot ? h('span', { class: `dot ${dot}` }) : null, value), sub);
|
|
86
|
+
}
|
|
87
|
+
// 一台电脑的好友数与证书状态。
|
|
88
|
+
function statusTiles(m, rows) {
|
|
89
|
+
if (!m.loaded) return h('div', { class: 'bento' }, tile('好友', '读取中…'), tile('身份', '读取中…'));
|
|
90
|
+
const summary = accordStatusSummary(m.entry, rows), { bound, pending, conflict, total } = summary.friends, cert = CERT[summary.cert];
|
|
91
|
+
const sub = summary.cert === 'active' ? (summary.certUntil ? `证书有效至 ${formatTime(summary.certUntil)}` : '可正常发布记录') : cert.sub;
|
|
92
|
+
return h('div', { class: 'bento' },
|
|
93
|
+
tile('好友', `${bound} 位已互认`, h('span', { class: 'tile-s' },
|
|
94
|
+
conflict ? h('span', { class: 'badge conflict' }, `${conflict} 冲突`) : null,
|
|
95
|
+
pending ? h('span', { class: 'badge pending' }, `${pending} 待互认`) : null,
|
|
96
|
+
conflict || pending ? null : total ? '全部已互认' : '还没有好友')),
|
|
97
|
+
tile('身份', cert.text, h('span', { class: 'tile-s' }, sub), cert.dot));
|
|
98
|
+
}
|
|
99
|
+
function drawHero(id, sections) {
|
|
100
|
+
const { author, signer } = id, ready = Boolean(signer);
|
|
101
|
+
const summary = accordStatusSummary(signer?.entry, []);
|
|
102
|
+
const notice = state.heroNotice.get(author ?? '');
|
|
103
|
+
const settled = members.every(m => m.loaded || m.unreachable || m.accordProblem);
|
|
104
|
+
const name = author ? identityTitle(author) : !settled ? '读取中…' : ready ? '未建身份' : '读不到身份';
|
|
105
|
+
const single = !multi && sections[0];
|
|
106
|
+
return h('section', { class: 'hero' },
|
|
107
|
+
h('div', { class: 'banner' }, h('div', { class: 'banner-acts' },
|
|
108
|
+
single && !single.unreachable ? h('button', { class: 'pill strong', type: 'button', onclick: () => openInvite(single.member) }, '添加好友') : null)),
|
|
109
|
+
h('div', { class: 'hero-body' },
|
|
110
|
+
h('div', { class: 'hero-id' },
|
|
111
|
+
h('span', { class: 'av-ring' }, avatar(resolveAvatar({ author, name }), 'lg')),
|
|
112
|
+
h('div', { class: 'hero-names' }, h('div', { class: 'name-line' }, h('h2', { class: 'hero-name selectable' }, name), h('span', { class: 'tag' }, '我的身份')),
|
|
113
|
+
h('button', { class: 'fp mono', type: 'button', disabled: !author, title: author ? '复制完整公钥' : '', onclick: () => copyKey(author),
|
|
114
|
+
}, author ? `ed25519:${summary.short}… · 复制` : !ready ? (settled ? '读不到公钥' : '读取中…') : summary.cert === 'no_identity' ? '未建身份' : '未取得公钥'))),
|
|
115
|
+
!author && ready && settled ? h('p', { class: 'hint' }, '这台电脑还没有 Accord 身份:前往客户端「设置 › 身份」创建后,好友才能认出你。') : null,
|
|
116
|
+
notice ? h('p', { class: notice.kind }, notice.text) : null,
|
|
117
|
+
single && !single.unreachable ? statusTiles(single.member, single.rows) : null));
|
|
118
|
+
}
|
|
119
|
+
async function copyKey(author) {
|
|
120
|
+
try { await coda.copy(author); state.heroNotice.set(author, { kind: 'ok', text: '公钥已复制' }); } catch (e) { state.heroNotice.set(author, { kind: 'error', text: `复制失败:${e.message}` }); }
|
|
121
|
+
draw();
|
|
413
122
|
}
|
|
414
123
|
|
|
415
124
|
// ---- 添加好友 ----
|
|
@@ -425,6 +134,13 @@ const invite = {
|
|
|
425
134
|
hidden: {},
|
|
426
135
|
};
|
|
427
136
|
invite.dialog = makeDialog({ title: '添加好友', close: '关闭' });
|
|
137
|
+
// 接受邀请和随后的互认都发给打开它的那一节的电脑。
|
|
138
|
+
function openInvite(m) {
|
|
139
|
+
if (invite.target !== m) say(invite.message, '');
|
|
140
|
+
invite.target = m;
|
|
141
|
+
invite.dialog.heading.textContent = multi ? `添加好友 · ${m.name}` : '添加好友';
|
|
142
|
+
showDialog(invite.dialog);
|
|
143
|
+
}
|
|
428
144
|
invite.dialog.body.append(h('div', { class: 'stack' },
|
|
429
145
|
h('p', { class: 'hint' }, '暂需由对方提供 friend 角色入网码,后续将支持客户端一键生成邀请。'),
|
|
430
146
|
invite.paste,
|
|
@@ -464,160 +180,132 @@ async function submitInvite() {
|
|
|
464
180
|
}
|
|
465
181
|
// 入网码是凭据:取出后立刻清空所有输入框,不写进任何存储或日志。
|
|
466
182
|
clearInvite();
|
|
467
|
-
const
|
|
183
|
+
const target = invite.target, opts = via(target), show = (text, kind) => { if (invite.target === target) say(invite.message, text, kind); };
|
|
468
184
|
invite.button.disabled = true;
|
|
469
|
-
|
|
185
|
+
show('正在接受邀请,加入对方的通道…', 'progress');
|
|
470
186
|
const peerNode = input.peerNode;
|
|
471
187
|
let node;
|
|
472
188
|
try {
|
|
473
189
|
({ node } = await coda.call(LOCAL, 'accept', input, opts));
|
|
474
190
|
} catch (e) {
|
|
475
191
|
input = null;
|
|
476
|
-
if (!live()) return;
|
|
477
192
|
invite.button.disabled = false;
|
|
478
|
-
return
|
|
193
|
+
return show(`接受邀请失败:${e.message}。入网码已清空,请重新粘贴邀请再试`, 'error');
|
|
479
194
|
}
|
|
480
195
|
input = null;
|
|
481
|
-
|
|
196
|
+
show('已加入通道,正在与对方互认…', 'progress');
|
|
482
197
|
try {
|
|
483
198
|
await coda.call(LOCAL, 'handshake', { node, ...(peerNode ? { peerNode } : {}) }, opts);
|
|
484
|
-
|
|
485
|
-
} catch (e) {
|
|
486
|
-
|
|
199
|
+
show('好友已添加并完成互认', 'ok');
|
|
200
|
+
} catch (e) { show(`已加入通道,但互认暂未完成(${e.message})。可稍后在好友详情里点「重新互认」重试`, 'error'); }
|
|
201
|
+
invite.button.disabled = false;
|
|
487
202
|
}
|
|
488
203
|
|
|
489
|
-
// ----
|
|
490
|
-
|
|
491
|
-
const
|
|
492
|
-
function rows() { return mergeFriendRows({ friends: state.friends, profiles: state.profiles, cards: state.cards, notes: state.notes }); }
|
|
204
|
+
// ---- 好友详情:重新互认、移除都发给这一行所在的电脑 ----
|
|
205
|
+
// 不同电脑上的 node 可能相同,忙碌与提示按(电脑, node)记。
|
|
206
|
+
const sectionRows = m => friendSections([m])[0].rows;
|
|
493
207
|
|
|
494
|
-
async function handshake(row) {
|
|
495
|
-
const
|
|
496
|
-
state.busy.add(
|
|
208
|
+
async function handshake(m, row) {
|
|
209
|
+
const key = rowKey(m, row.node);
|
|
210
|
+
state.busy.add(key); state.notice.set(key, { kind: 'progress', text: '正在互认…' }); draw();
|
|
497
211
|
let notice;
|
|
498
|
-
try { await coda.call(LOCAL, 'handshake', { node: row.node, ...(row.peerNode ? { peerNode: row.peerNode } : {}) },
|
|
212
|
+
try { await coda.call(LOCAL, 'handshake', { node: row.node, ...(row.peerNode ? { peerNode: row.peerNode } : {}) }, via(m)); notice = { kind: 'ok', text: '互认完成' }; }
|
|
499
213
|
catch (e) { notice = { kind: 'error', text: `互认失败:${e.message}` }; }
|
|
500
|
-
|
|
501
|
-
state.notice.set(row.node, notice); state.busy.delete(row.node); draw();
|
|
214
|
+
state.notice.set(key, notice); state.busy.delete(key); draw();
|
|
502
215
|
}
|
|
503
|
-
async function remove(row) {
|
|
504
|
-
const
|
|
505
|
-
state.busy.add(
|
|
216
|
+
async function remove(m, row) {
|
|
217
|
+
const key = rowKey(m, row.node);
|
|
218
|
+
state.busy.add(key); state.notice.set(key, { kind: 'progress', text: '正在移除…' }); draw();
|
|
506
219
|
let failure = null;
|
|
507
|
-
try { await coda.call(LOCAL, 'remove', { node: row.node },
|
|
220
|
+
try { await coda.call(LOCAL, 'remove', { node: row.node }, via(m)); }
|
|
508
221
|
catch (e) { failure = { kind: 'error', text: `移除失败:${e.message}` }; }
|
|
509
|
-
|
|
510
|
-
state.
|
|
511
|
-
if (failure) state.notice.set(row.node, failure);
|
|
512
|
-
else {
|
|
513
|
-
state.notice.delete(row.node); state.removing = null;
|
|
514
|
-
if (state.detail === row.node) closeDialog();
|
|
515
|
-
}
|
|
516
|
-
draw();
|
|
517
|
-
}
|
|
518
|
-
async function saveNote(row, text) {
|
|
519
|
-
const gen = selectionGen;
|
|
520
|
-
state.busy.add(row.node); state.notice.set(row.node, { kind: 'progress', text: '正在保存备注…' }); draw();
|
|
521
|
-
let saved, failure;
|
|
522
|
-
try { saved = await ask(text.trim() ? 'notes.set' : 'notes.delete', { node: row.node, note: text }); }
|
|
523
|
-
catch (e) { failure = { kind: 'error', text: `备注没存下:${e.message}` }; }
|
|
524
|
-
if (gen !== selectionGen) return;
|
|
525
|
-
state.busy.delete(row.node);
|
|
526
|
-
if (failure) state.notice.set(row.node, failure);
|
|
222
|
+
state.busy.delete(key);
|
|
223
|
+
if (failure) state.notice.set(key, failure);
|
|
527
224
|
else {
|
|
528
|
-
state.
|
|
529
|
-
state.
|
|
530
|
-
if (state.detail === row.node) detail.note.value = rows().find(r => r.node === row.node)?.note ?? '';
|
|
225
|
+
state.notice.delete(key); state.removing = null;
|
|
226
|
+
if (state.detail?.key === key) closeDialog();
|
|
531
227
|
}
|
|
532
228
|
draw();
|
|
533
229
|
}
|
|
534
|
-
|
|
535
230
|
const detail = {
|
|
536
231
|
dialog: makeDialog({ title: '好友', close: '完成', onClose: () => { state.detail = null; state.removing = null; } }),
|
|
537
232
|
card: h('div'),
|
|
538
233
|
info: h('dl', { class: 'facts' }),
|
|
539
|
-
note: h('input', { type: 'text', maxlength: '40', placeholder: '备注名(只存在本机,不会发给好友)', autocomplete: 'off',
|
|
540
|
-
onkeydown: e => { if (e.key === 'Enter') noteAction(row => saveNote(row, detail.note.value)); } }),
|
|
541
|
-
noteButtons: h('div', { class: 'line' }),
|
|
542
234
|
actions: h('div', { class: 'actions split' }),
|
|
543
235
|
notice: h('div'),
|
|
544
236
|
};
|
|
545
|
-
detail.dialog.body.append(h('div', { class: 'stack' }, detail.card, detail.info,
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
function noteAction(run) { const row = detailRow(); if (row && !state.busy.has(row.node)) run(row); }
|
|
550
|
-
function openDetail(row) {
|
|
551
|
-
state.detail = row.node; state.removing = null;
|
|
552
|
-
detail.note.value = row.note;
|
|
237
|
+
detail.dialog.body.append(h('div', { class: 'stack' }, detail.card, detail.info, detail.actions, detail.notice));
|
|
238
|
+
const detailRow = () => state.detail && sectionRows(state.detail.member).find(row => row.node === state.detail.node);
|
|
239
|
+
function openDetail(m, row) {
|
|
240
|
+
state.detail = { member: m, node: row.node, key: rowKey(m, row.node) }; state.removing = null;
|
|
553
241
|
drawDetail();
|
|
554
242
|
showDialog(detail.dialog);
|
|
555
243
|
}
|
|
556
244
|
function fact(label, ...value) { return [h('dt', {}, label), h('dd', { class: 'selectable' }, ...value)]; }
|
|
557
|
-
async function copyFriendKey(row) {
|
|
558
|
-
const
|
|
559
|
-
try { await coda.copy(row.author);
|
|
560
|
-
catch (e) {
|
|
245
|
+
async function copyFriendKey(m, row) {
|
|
246
|
+
const key = rowKey(m, row.node);
|
|
247
|
+
try { await coda.copy(row.author); state.notice.set(key, { kind: 'ok', text: '公钥已复制' }); }
|
|
248
|
+
catch (e) { state.notice.set(key, { kind: 'error', text: `复制失败:${e.message}` }); }
|
|
249
|
+
draw();
|
|
561
250
|
}
|
|
562
251
|
function drawDetail() {
|
|
563
|
-
const row = detailRow();
|
|
252
|
+
const row = detailRow(), { member: m, key } = state.detail;
|
|
564
253
|
if (!row) return;
|
|
565
|
-
const busy = state.busy.has(
|
|
566
|
-
detail.dialog.heading.textContent = row.title;
|
|
567
|
-
detail.card.replaceChildren(h('div', { class:
|
|
254
|
+
const busy = state.busy.has(key), notice = state.notice.get(key);
|
|
255
|
+
detail.dialog.heading.textContent = multi ? `${row.title} · ${m.name}` : row.title;
|
|
256
|
+
detail.card.replaceChildren(h('div', { class: 'mini-card' }, h('div', { class: 'banner small' }),
|
|
568
257
|
h('div', { class: 'mini-body row' }, h('span', { class: 'av-ring lg' }, avatar(rowAvatar(row), 'lg')),
|
|
569
|
-
h('div', { class: 'mini-names' }, h('div', { class: 'mini-name truncate selectable' }, row.title),
|
|
570
|
-
row.note && row.profileName ? h('div', { class: 'secondary truncate selectable' }, `原名 ${row.profileName}`) : null),
|
|
258
|
+
h('div', { class: 'mini-names' }, h('div', { class: 'mini-name truncate selectable' }, row.title)),
|
|
571
259
|
h('span', { class: `badge ${row.status}` }, STATUS_TEXT[row.status]))));
|
|
572
260
|
detail.info.replaceChildren(
|
|
573
261
|
...fact('公钥', row.author ? h('span', { class: 'mono key-full' }, row.author) : h('span', { class: 'secondary' }, '密钥未互认'),
|
|
574
|
-
row.author ? h('button', { class: 'btn quiet small', type: 'button', onclick: () => copyFriendKey(row) }, '复制公钥') : null),
|
|
262
|
+
row.author ? h('button', { class: 'btn quiet small', type: 'button', onclick: () => copyFriendKey(m, row) }, '复制公钥') : null),
|
|
575
263
|
...fact('节点', h('span', { class: 'mono' }, row.peerNode ?? row.node)),
|
|
576
264
|
...fact('连接', STATUS_TEXT[row.status]));
|
|
577
|
-
|
|
578
|
-
h('button', { class: 'btn small', type: 'button', disabled: busy, onclick: () => noteAction(r => saveNote(r, detail.note.value)) }, '保存'),
|
|
579
|
-
row.note ? h('button', { class: 'btn quiet small', type: 'button', disabled: busy, onclick: () => noteAction(r => saveNote(r, '')) }, '清空') : null);
|
|
580
|
-
detail.actions.replaceChildren(...(state.removing === row.node
|
|
265
|
+
detail.actions.replaceChildren(...(state.removing === key
|
|
581
266
|
? [h('span', { class: 'secondary' }, '移除后不再同步这位好友,确定吗?'), h('span', { class: 'spacer' }),
|
|
582
|
-
h('button', { class: 'btn danger small', type: 'button', disabled: busy, onclick: () => remove(row) }, '确认移除'),
|
|
267
|
+
h('button', { class: 'btn danger small', type: 'button', disabled: busy, onclick: () => remove(m, row) }, '确认移除'),
|
|
583
268
|
h('button', { class: 'btn quiet small', type: 'button', onclick: () => { state.removing = null; draw(); } }, '再想想')]
|
|
584
|
-
: [h('button', { class: 'btn quiet small', type: 'button', disabled: busy, onclick: () => handshake(row) }, '重新互认'), h('span', { class: 'spacer' }),
|
|
585
|
-
h('button', { class: 'btn danger small', type: 'button', disabled: busy, onclick: () => { state.removing =
|
|
269
|
+
: [h('button', { class: 'btn quiet small', type: 'button', disabled: busy, onclick: () => handshake(m, row) }, '重新互认'), h('span', { class: 'spacer' }),
|
|
270
|
+
h('button', { class: 'btn danger small', type: 'button', disabled: busy, onclick: () => { state.removing = key; draw(); } }, '移除好友…')]));
|
|
586
271
|
fill(detail.notice,
|
|
587
272
|
row.status === 'conflict' ? h('p', { class: 'warn-box' }, '对方的密钥与之前互认的不一致,可能换了设备或身份。确认是本人后可「重新互认」,否则建议移除。') : null,
|
|
588
273
|
notice ? h('p', { class: notice.kind }, notice.text) : null);
|
|
589
274
|
}
|
|
590
275
|
|
|
591
|
-
function friendTile(row) {
|
|
592
|
-
|
|
593
|
-
return h('li', {}, h('button', { class: `f-tile c-${row.color}`, type: 'button', 'aria-label': `${row.title},${STATUS_TEXT[row.status]}`, onclick: () => openDetail(row) },
|
|
276
|
+
function friendTile(m, row) {
|
|
277
|
+
return h('li', {}, h('button', { class: 'f-tile', type: 'button', 'aria-label': `${row.title},${STATUS_TEXT[row.status]}`, onclick: () => openDetail(m, row) },
|
|
594
278
|
avatar(rowAvatar(row), 'sm'),
|
|
595
|
-
h('span', { class: 'f-info' }, h('span', { class: 'f-name truncate' }, row.title), h('span', { class: 'f-meta truncate' },
|
|
279
|
+
h('span', { class: 'f-info' }, h('span', { class: 'f-name truncate' }, row.title), h('span', { class: 'f-meta truncate' }, row.short ? `@${row.short}` : '密钥未互认')),
|
|
596
280
|
h('span', { class: `badge ${row.status}` }, STATUS_TEXT[row.status])));
|
|
597
281
|
}
|
|
598
282
|
|
|
599
|
-
|
|
283
|
+
// 每台电脑一节;只有一台时没有节标题,也不重复画身份卡上已有的状态条和「添加好友」。
|
|
284
|
+
function drawSection({ member: m, title, unreachable, rows }) {
|
|
285
|
+
const down = !unreachable ? null : m.unreachable ? `${m.name} 读不到:${unreachable}` : `${multi ? `${m.name}:` : ''}${unreachable}`;
|
|
286
|
+
const summary = !unreachable && m.loaded ? accordStatusSummary(m.entry, rows) : null;
|
|
287
|
+
// 几台时状态条只在有需要提示的状态(证书有问题、待互认、冲突)时出;只有一台时状态条在身份卡上。
|
|
288
|
+
const attention = summary && (summary.cert !== 'active' || summary.friends.pending || summary.friends.conflict);
|
|
289
|
+
const empty = !m.loaded ? '正在读取…' : '还没有好友。点上方「添加好友」,粘贴对方发来的邀请即可。';
|
|
290
|
+
return h('section', {},
|
|
291
|
+
h('h2', { class: 'sec' }, title ?? '好友', unreachable ? null : h('span', { class: 'count', hidden: !rows.length }, String(rows.length))),
|
|
292
|
+
down ? h('p', { class: 'error selectable' }, down) : [
|
|
293
|
+
multi && attention ? statusTiles(m, rows) : null,
|
|
294
|
+
multi ? h('div', { class: 'line' }, h('button', { class: 'pill strong', type: 'button', onclick: () => openInvite(m) }, '添加好友')) : null,
|
|
295
|
+
rows.length ? h('ul', { class: 'friends' }, rows.map(row => friendTile(m, row))) : multi && m.loaded ? null : h('p', { class: 'empty' }, empty)]);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const page = h('div', { class: 'page' });
|
|
600
299
|
function draw() {
|
|
601
|
-
const
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
countBadge.textContent = String(shown.length);
|
|
607
|
-
countBadge.hidden = !shown.length;
|
|
608
|
-
list.replaceChildren(shown.length ? h('ul', { class: 'friends' }, shown.map(friendTile))
|
|
609
|
-
: h('p', { class: 'empty' }, state.loaded ? '还没有好友。点上方「添加好友」,粘贴对方发来的邀请即可。' : '正在读取…'));
|
|
610
|
-
if (state.detail !== null) { if (detailRow()) drawDetail(); else closeDialog(); }
|
|
300
|
+
const sections = friendSections(members);
|
|
301
|
+
const ids = identities(live);
|
|
302
|
+
if (!ids.length) ids.push({ author: null, signer: null, members: [] });
|
|
303
|
+
fill(page, ids.map(id => drawHero(id, sections)), sections.map(drawSection));
|
|
304
|
+
if (state.detail) { if (detailRow()) drawDetail(); else closeDialog(); }
|
|
611
305
|
}
|
|
612
306
|
|
|
613
|
-
root.replaceChildren(
|
|
614
|
-
h('section', {}, h('h2', { class: 'sec' }, '好友', countBadge), list)),
|
|
615
|
-
editor.dialog.scrim, detail.dialog.scrim, invite.dialog.scrim);
|
|
616
|
-
drawMachines();
|
|
617
|
-
syncEditor();
|
|
307
|
+
root.replaceChildren(page, detail.dialog.scrim, invite.dialog.scrim);
|
|
618
308
|
draw();
|
|
619
309
|
|
|
620
|
-
coda.on('toolbar', ({ id } = {}) => { if (id === 'refresh') { state.failedPhotos.clear(); draw(); readProfiles(); } });
|
|
621
|
-
coda.toolbar([{ id: 'refresh', label: '刷新', icon: 'arrow.clockwise', kind: 'button' }]).catch(() => {});
|
|
622
310
|
coda.header({ text: '好友' }).catch(() => {});
|
|
623
|
-
start();
|
|
311
|
+
for (const m of live) start(m);
|