@coda-console/friends 0.12.0 → 0.13.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 +9 -5
- package/lib/machine-record.mjs +67 -0
- package/lib/view-members.mjs +31 -10
- package/main.mjs +8 -1
- package/package.json +53 -3
- package/view/app.mjs +52 -41
package/README.md
CHANGED
|
@@ -11,10 +11,14 @@
|
|
|
11
11
|
|
|
12
12
|
## 几台电脑
|
|
13
13
|
|
|
14
|
-
页面不自己选电脑:App
|
|
14
|
+
页面不自己选电脑:App 的全局视图里有哪几台,页面就显示哪几台,没有机器标签。
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
**数据从哪里来**:好友页是目录聚合视图(清单 `directoryViews`),不再逐台去读 `coda/accord@1`(它是 Coda 的本机种类,不进目录)。每台的模块把本机 Accord 记录里页面要用的部分写成一条目录记录 `friends/machine@1`(key `local`):`spec.author`,`status.friends`、`status.certificate`、`status.networkRemovals`;跟着网关已有的 Accord 订阅写,启动时写一次,内容没变不重写。私钥、入网码(`authKey`)、一次性口令与证书签名(`sig`)本来就不在 Accord 给的记录里,这条记录再按字段挑一遍,一概不带。页面只用一个不带 `machine` 的 `watch` 读这种记录,先读齐一次再画(不再逐台显示「读取中…」),按条目的 `owner` 对到视图里的电脑,之后记录变了才更新,已展开的详情与打开的弹层保持原样。哪几台离线看 `coda/machine@1` 的 `offline`(打开时读一次,之后每 30 秒读一次,读不到就都当在线)。
|
|
17
|
+
|
|
18
|
+
**写操作照旧发给那一台**:添加好友、接受、互认、移除、好友节点都带 `{ machine }` 发给那台,目录视图会在那台按需开实例;那台离线时操作失败,页面写出原因。「共享」一节经 `postMessage(…, { machine })` 问那台的模块(Coda 文档:目录聚合视图里页面显式指定某台成员时才在那台按需建实例,页面问后端的一切包括消息都在这个实例下)。操作之后页面以目录里的记录为准。
|
|
19
|
+
|
|
20
|
+
- **本机身份按公钥显示**:按各台记录里的 `spec.author` 分组,同一身份只显示一张身份卡,视图里有不同身份的电脑时每个身份一张。
|
|
21
|
+
- **好友按电脑分节**:每台一节,顺序同视图,节标题是电脑名;节里是这台自己的好友通道(好友关系按机器上的通道登记,在 A 上加的好友 B 上不会自动出现),状态条(好友数、证书状态、待互认与冲突)和「添加好友」也在这一节。接受邀请与随后的互认、好友详情里的重新互认、移除,都发给这一节、这一行所在的电脑。没有好友的电脑也占一节;同一个好友在两台上都加了,就在两节里各出现一次,不跨电脑合并(合并只在同一台电脑内按完整公钥进行)。离线的电脑照样出节,显示它最后写下的身份与好友,并标「离线」;没有这条记录的电脑(没装好友插件、旧版)不出节、也不报错。
|
|
18
22
|
- **只有一台时**页面不出现节标题,状态条与「添加好友」仍在身份卡上。
|
|
19
23
|
- **推荐用法**:好友只在一台电脑上加。好友关系登记在那台电脑上,别的电脑不会自动出现这个好友。邀请里的入网码只能用一次,不必在每台上重复加。
|
|
20
24
|
- **要共享别的电脑**:不必在那台上再加一次好友。加了好友的这台作网关,能把同一空间里别的电脑的远程执行逐台授给好友(见下一节「经网关授出别的机器」)。
|
|
@@ -203,9 +207,9 @@ coda self grants.restrict '{"grant":"<授权号>","restrict":{"dirs":[…]}}'
|
|
|
203
207
|
## 权限与数据边界
|
|
204
208
|
|
|
205
209
|
- `friends/social@1` 的权限组:`read` 管 `friends`、`records`;`publish` 管 `publish`、`retract`、`outbox`。
|
|
206
|
-
- `uses`:`coda/accord@1` 的 `read`(读好友表)、`publish`(网关代别的插件发布)、`manage`(接受邀请、互认、移除);`coda/machine@1` 的 `read
|
|
210
|
+
- `uses`:`coda/accord@1` 的 `read`(读好友表)、`publish`(网关代别的插件发布)、`manage`(接受邀请、互认、移除);`coda/machine@1` 的 `read`(别台的目标读不到时,看那台还在不在空间里;好友页据此标出离线的电脑);`friends/machine@1` 的 `read`(好友页读各台的身份与好友摘要)。`optionalUses`:`remote-exec/machine@1` 的 `use`(转给本机的远程执行,没装就没有这部分)。
|
|
207
211
|
- 保留种类 `friends/rpc-grant`、`friends/rpc-call`、`friends/rpc-answer`:只有本插件的内部入口能签发与读取,通用网关的发布、撤回、`records`、`outbox` 都不给出。
|
|
208
212
|
- 入网码(`authKey`)是凭据:只在提交时用一次,提交后立即清空输入框,不写入任何文件、日志或页面状态。
|
|
209
213
|
- 节点名等外来数据只当文本渲染(`textContent`),不当指令或 HTML。
|
|
210
|
-
-
|
|
214
|
+
- 好友页读目录里的 `friends/machine@1`,Accord 的写(接受、互认、移除)在页面交互时发起;模块在 `activate` 时监听本机 Accord 记录(证书、好友表),用来刷新网关记录、写 `friends/machine@1`、证书恢复后冲洗待发队列。
|
|
211
215
|
- 待发的 `body` 只在本机 `outbox.json`,不经别处。
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// 好友页读的目录记录 friends/machine@1:把本机 Accord 记录(coda/accord@1 的 local)里页面要用的身份与好友摘要写成一条(key `local`),
|
|
2
|
+
// 好友页是目录聚合视图,按条目的 owner 对到各台,别台离线时照样读得到它最后写下的。Accord 不给私钥、authKey 与证书的 sig,这里再按字段挑一遍,
|
|
3
|
+
// 记录里只有下面列出的。纯逻辑(machineRecord)与写记录的 createMachineRecord 都在这里,单测直接用。
|
|
4
|
+
|
|
5
|
+
export const MACHINE_RECORD_KIND = 'friends/machine@1';
|
|
6
|
+
const FRIEND_FIELDS = ['node', 'mode', 'scope', 'author', 'bound', 'machine', 'certified', 'createdAt', 'updatedAt', 'linkId', 'peerNode', 'peerPort', 'conflict'];
|
|
7
|
+
const CERTIFICATE_FIELDS = ['key', 'root', 'active', 'from', 'until'];
|
|
8
|
+
const REMOVAL_FIELDS = ['node', 'since'];
|
|
9
|
+
|
|
10
|
+
const pick = (object, keys) => Object.fromEntries(keys.filter(key => object[key] !== undefined).map(key => [key, object[key]]));
|
|
11
|
+
const listOf = (value, keys) => (Array.isArray(value) ? value : []).filter(item => item && typeof item === 'object').map(item => pick(item, keys));
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Accord 记录的条目 → 这台的记录 `{ spec, status }`:`spec.author`,`status.friends`、`status.certificate`(Accord 状态里有这个键才有;没绑个人身份是 null)、
|
|
15
|
+
* `status.networkRemovals`。形状同 Accord 条目,页面用同一套 certStateOf、accordStatusSummary 读它。
|
|
16
|
+
*/
|
|
17
|
+
export function machineRecord(accord) {
|
|
18
|
+
const status = accord?.status ?? {};
|
|
19
|
+
return {
|
|
20
|
+
spec: { author: accord?.spec?.author ?? null },
|
|
21
|
+
status: {
|
|
22
|
+
friends: listOf(status.friends, FRIEND_FIELDS),
|
|
23
|
+
...(Object.hasOwn(status, 'certificate') ? { certificate: status.certificate === null ? null : pick(status.certificate ?? {}, CERTIFICATE_FIELDS) } : {}),
|
|
24
|
+
networkRemovals: listOf(status.networkRemovals, REMOVAL_FIELDS),
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const RETRY_MS = 30_000;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 跟着 Accord 的变化写这条记录:`onAccord({ type, entry })` 接网关转来的事件(启动时的第一次也是它),内容和上次写成的一样就不重写。
|
|
33
|
+
* 只记最新一份还没写成的:写失败(写一行警告)后隔 `RETRY_MS` 重写这一份,直到写成或 `stop`;期间来了新变化就换成新的,同一时间只有一个定时器。
|
|
34
|
+
* 没有 Accord 记录(没读到、被删)就不写。写按顺序做,`stop` 之后不再写。
|
|
35
|
+
*/
|
|
36
|
+
export function createMachineRecord(coda) {
|
|
37
|
+
let latest = null, written = null, chain = Promise.resolve(), stopped = false, timer = null;
|
|
38
|
+
const flush = () => {
|
|
39
|
+
chain = chain.then(async () => {
|
|
40
|
+
if (stopped || !latest || latest.text === written) return;
|
|
41
|
+
const { record, text } = latest;
|
|
42
|
+
try {
|
|
43
|
+
await coda.put({ kind: MACHINE_RECORD_KIND, key: 'local' }, record);
|
|
44
|
+
written = text;
|
|
45
|
+
} catch (e) {
|
|
46
|
+
console.warn(`好友页的目录记录写不进去,${RETRY_MS / 1000} 秒后重试:${e?.message ?? e}`);
|
|
47
|
+
timer ??= setTimeout(() => { timer = null; flush(); }, RETRY_MS);
|
|
48
|
+
timer.unref?.();
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
onAccord({ type, entry } = {}) {
|
|
54
|
+
if (type === 'DELETED' || !entry) return;
|
|
55
|
+
const record = machineRecord(entry);
|
|
56
|
+
latest = { record, text: JSON.stringify(record) };
|
|
57
|
+
clearTimeout(timer);
|
|
58
|
+
timer = null;
|
|
59
|
+
flush();
|
|
60
|
+
},
|
|
61
|
+
stop() {
|
|
62
|
+
stopped = true;
|
|
63
|
+
clearTimeout(timer);
|
|
64
|
+
timer = null;
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
package/lib/view-members.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
//
|
|
2
|
-
// 页面不自己选机器:App
|
|
1
|
+
// 好友页按「视图里的每台电脑」显示数据的纯逻辑:成员列表、目录记录按 owner 对到成员、离线标记、按身份归组、按电脑分节。不碰 Coda 的 Node API;页面和单测都加载它。
|
|
2
|
+
// 页面不自己选机器:App 的全局视图里有哪几台,页面就显示哪几台;数据是目录里的 friends/machine@1,没有这条记录的成员(没装插件、旧版)不出节。
|
|
3
3
|
|
|
4
4
|
import { identityTitles, shortAuthor } from 'coda-view://lib/v1/accord.js';
|
|
5
5
|
import { certStateOf, mergeFriendRows } from './friends.mjs';
|
|
@@ -8,16 +8,37 @@ import { certStateOf, mergeFriendRows } from './friends.mjs';
|
|
|
8
8
|
const authorsOf = members => members.flatMap(m => [m.author, ...m.friends.map(friend => friend.author)]);
|
|
9
9
|
export const memberTitles = members => identityTitles(authorsOf(members));
|
|
10
10
|
|
|
11
|
-
/** 视图里的成员(去重、保持 coda.machines
|
|
11
|
+
/** 视图里的成员(去重、保持 coda.machines 的顺序)。`offline` 由 applyOffline 按机器记录填,读到它的记录之前不算离线。 */
|
|
12
12
|
export function listMembers(coda) {
|
|
13
13
|
const list = Array.isArray(coda?.machines) ? coda.machines : [coda?.machine];
|
|
14
14
|
const seen = new Set();
|
|
15
15
|
return list.filter(m => typeof m?.id === 'string' && m.id && !seen.has(m.id) && seen.add(m.id))
|
|
16
|
-
.map(m => ({ id: m.id, name: m.name || m.id,
|
|
16
|
+
.map(m => ({ id: m.id, name: m.name || m.id, offline: false }));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** 目录条目的 `owner`(机器 ID,不分大小写)对到的成员;不在视图里的机器没有。 */
|
|
20
|
+
export const memberOf = (members, owner) => typeof owner === 'string' ? members.find(m => m.id.toLowerCase() === owner.toLowerCase()) : undefined;
|
|
21
|
+
|
|
22
|
+
/** 把一条 friends/machine@1 条目放进它的成员(成员读到了);`entry` 为 null 是这条记录没了。 */
|
|
23
|
+
export function applyEntry(member, entry) {
|
|
24
|
+
member.entry = entry;
|
|
25
|
+
member.author = entry?.spec?.author ?? null;
|
|
26
|
+
member.friends = entry?.status?.friends ?? [];
|
|
27
|
+
member.loaded = Boolean(entry);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 按 `coda/machine@1` 的条目(`status.offline`)标各成员是否离线;返回有没有变化。没有机器记录的成员不算离线。 */
|
|
31
|
+
export function applyOffline(members, machines) {
|
|
32
|
+
let changed = false;
|
|
33
|
+
for (const member of members) {
|
|
34
|
+
const offline = machines.some(entry => entry?.id?.toLowerCase() === member.id.toLowerCase() && entry.status?.offline === true);
|
|
35
|
+
if (offline !== member.offline) { member.offline = offline; changed = true; }
|
|
36
|
+
}
|
|
37
|
+
return changed;
|
|
17
38
|
}
|
|
18
39
|
|
|
19
40
|
/**
|
|
20
|
-
* 按 `author` 把读到了的成员分组(没有身份的电脑归在 author 为 null
|
|
41
|
+
* 按 `author` 把读到了的成员分组(没有身份的电脑归在 author 为 null 一组;没有记录的成员不参与),组的顺序是成员第一次出现的顺序。
|
|
21
42
|
* `signer` 是代表这个身份证书状态的那一台:组里第一台能签名(证书可用)的,没有就是组里第一台。
|
|
22
43
|
*/
|
|
23
44
|
export function identities(members) {
|
|
@@ -30,14 +51,14 @@ export function identities(members) {
|
|
|
30
51
|
}
|
|
31
52
|
|
|
32
53
|
/**
|
|
33
|
-
*
|
|
34
|
-
* 只有一个成员时 `title` 为 null
|
|
54
|
+
* 好友按电脑分节:读到记录的成员一节,顺序同视图;没有记录的成员不出节。节里是这台自己的好友通道(行的内容见 mergeFriendRows,标题用 `titles`,默认按页面上所有身份一起算一次),不同电脑上的同一个好友各出现一次。
|
|
55
|
+
* 只有一个成员时 `title` 为 null(不出节标题);离线的成员照样出节,`offline` 为 true,内容是它最后写下的。
|
|
35
56
|
*/
|
|
36
57
|
export function friendSections(members, titles = memberTitles(members)) {
|
|
37
|
-
return members.map(member => ({
|
|
58
|
+
return members.filter(member => member.loaded).map(member => ({
|
|
38
59
|
member,
|
|
39
60
|
title: members.length > 1 ? member.name : null,
|
|
40
|
-
|
|
41
|
-
rows:
|
|
61
|
+
offline: member.offline,
|
|
62
|
+
rows: mergeFriendRows(member.friends, { titles, shortAuthor }),
|
|
42
63
|
}));
|
|
43
64
|
}
|
package/main.mjs
CHANGED
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
// - 签名网关的调用方(lib/rpc-caller.mjs):把好友授给本机的记录登记成代理记录,接住对它的 exec 转给好友签名执行。调用方出错不影响社交网关与持有方。
|
|
8
8
|
// 好友授给本机的代理记录用 `coda self proxies.list` 读;授予时可选的 remote-exec 记录(本机的与同空间别的机器的)用 `coda self grants.targets` 读。
|
|
9
9
|
// 持有方、调用方的这几样(shared)好友页也经视图消息调用。
|
|
10
|
+
// - 好友页是目录聚合视图,读的是目录里的 friends/machine@1:模块跟着网关已有的 Accord 订阅,把本机的身份与好友摘要写成一条记录(lib/machine-record.mjs)。
|
|
10
11
|
import { createGateway } from './lib/gateway.mjs';
|
|
12
|
+
import { createMachineRecord } from './lib/machine-record.mjs';
|
|
11
13
|
import { createCaller } from './lib/rpc-caller.mjs';
|
|
12
14
|
import { sandboxHint } from './lib/rpc-format.mjs';
|
|
13
15
|
import { createHolder } from './lib/rpc-holder.mjs';
|
|
@@ -15,7 +17,7 @@ import { createRpcStore } from './lib/rpc-store.mjs';
|
|
|
15
17
|
|
|
16
18
|
const HOLDER_ACTIONS = ['grants.list', 'grants.grant', 'grants.restrict', 'grants.revoke', 'ledger.list'];
|
|
17
19
|
const TARGET_KIND = 'remote-exec/machine@1';
|
|
18
|
-
let gateway = null, holder = null, caller = null, callerStart = null, callerDown = null;
|
|
20
|
+
let gateway = null, machineRecord = null, holder = null, caller = null, callerStart = null, callerDown = null;
|
|
19
21
|
const handles = [];
|
|
20
22
|
|
|
21
23
|
export async function activate(coda) {
|
|
@@ -49,6 +51,9 @@ export async function activate(coda) {
|
|
|
49
51
|
});
|
|
50
52
|
gateway = createGateway(coda, dir);
|
|
51
53
|
for (const [name, fn] of Object.entries(gateway.policy)) handles.push(coda.self.handle(name, input => fn(input ?? {})));
|
|
54
|
+
// 先挂上再 start:网关订阅 Accord 时给的第一次(ADDED)就是启动时写的那一次。
|
|
55
|
+
machineRecord = createMachineRecord(coda);
|
|
56
|
+
handles.push(gateway.onAccord(event => machineRecord.onAccord(event)));
|
|
52
57
|
await gateway.start();
|
|
53
58
|
if (dir) {
|
|
54
59
|
const store = createRpcStore(dir), rpc = { store, signRpc: gateway.signRpc, retractRpc: gateway.retractRpc, onAccord: gateway.onAccord, pages: gateway.pages };
|
|
@@ -76,5 +81,7 @@ export function deactivate() {
|
|
|
76
81
|
holder = null;
|
|
77
82
|
gateway?.stop();
|
|
78
83
|
gateway = null;
|
|
84
|
+
machineRecord?.stop();
|
|
85
|
+
machineRecord = null;
|
|
79
86
|
for (const handle of handles.splice(0)) handle?.dispose?.();
|
|
80
87
|
}
|
package/package.json
CHANGED
|
@@ -4,19 +4,22 @@
|
|
|
4
4
|
"displayName": "好友",
|
|
5
5
|
"description": "管理 Accord 身份与熟人好友通道,并提供业务无关的好友网关",
|
|
6
6
|
"icon": { "symbol": "person.2.fill", "color": "blue" },
|
|
7
|
-
"version": "0.
|
|
7
|
+
"version": "0.13.0",
|
|
8
8
|
"categories": [
|
|
9
9
|
"Skills 与工具"
|
|
10
10
|
],
|
|
11
11
|
"type": "module",
|
|
12
12
|
"engines": {
|
|
13
|
-
"coda":
|
|
13
|
+
"coda": 22
|
|
14
14
|
},
|
|
15
15
|
"main": "main.mjs",
|
|
16
16
|
"activationEvents": [
|
|
17
17
|
"onStartupFinished"
|
|
18
18
|
],
|
|
19
19
|
"uses": {
|
|
20
|
+
"friends/machine@1": [
|
|
21
|
+
"read"
|
|
22
|
+
],
|
|
20
23
|
"coda/accord@1": [
|
|
21
24
|
"read",
|
|
22
25
|
"publish",
|
|
@@ -207,10 +210,57 @@
|
|
|
207
210
|
"read": { "title": "查看好友列表、读取好友分享的记录", "read": true, "operations": ["friends", "records"] },
|
|
208
211
|
"publish": { "title": "通过好友网关发布、撤回记录、查看待发队列", "operations": ["publish", "retract", "outbox"] }
|
|
209
212
|
}
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
"name": "friends/machine@1",
|
|
216
|
+
"title": "各台机器的好友身份摘要",
|
|
217
|
+
"description": "每台机器的本机 Accord 身份与好友摘要(作者、好友通道、证书状态、待回收的网络节点):好友页是目录聚合视图,读它们就能一次看全各台,离线的机器也显示它最后写下的;不含私钥、入网码与证书签名。",
|
|
218
|
+
"spec": {
|
|
219
|
+
"type": "object",
|
|
220
|
+
"properties": { "author": { "type": ["string", "null"] } },
|
|
221
|
+
"required": ["author"],
|
|
222
|
+
"additionalProperties": false
|
|
223
|
+
},
|
|
224
|
+
"status": {
|
|
225
|
+
"type": "object",
|
|
226
|
+
"properties": {
|
|
227
|
+
"friends": { "type": "array", "items": { "type": "object" } },
|
|
228
|
+
"certificate": {
|
|
229
|
+
"type": ["object", "null"],
|
|
230
|
+
"properties": {
|
|
231
|
+
"key": { "type": "string" },
|
|
232
|
+
"root": { "type": "string" },
|
|
233
|
+
"active": { "type": "boolean" },
|
|
234
|
+
"from": { "type": "integer", "minimum": 0 },
|
|
235
|
+
"until": { "type": "integer", "minimum": 0 }
|
|
236
|
+
},
|
|
237
|
+
"additionalProperties": false
|
|
238
|
+
},
|
|
239
|
+
"networkRemovals": {
|
|
240
|
+
"type": "array",
|
|
241
|
+
"items": {
|
|
242
|
+
"type": "object",
|
|
243
|
+
"properties": { "node": { "type": "string" }, "since": { "type": "integer", "minimum": 0 } },
|
|
244
|
+
"required": ["node", "since"],
|
|
245
|
+
"additionalProperties": false
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
},
|
|
249
|
+
"required": ["friends", "networkRemovals"],
|
|
250
|
+
"additionalProperties": false
|
|
251
|
+
},
|
|
252
|
+
"operations": {},
|
|
253
|
+
"permissions": {
|
|
254
|
+
"read": { "title": "读各台机器的好友身份与好友列表摘要", "read": true }
|
|
255
|
+
}
|
|
210
256
|
}
|
|
211
257
|
],
|
|
212
258
|
"protocolImplementations": [
|
|
213
|
-
"friends/social@1"
|
|
259
|
+
"friends/social@1",
|
|
260
|
+
"friends/machine@1"
|
|
261
|
+
],
|
|
262
|
+
"directoryViews": [
|
|
263
|
+
"friends"
|
|
214
264
|
],
|
|
215
265
|
"optionalProtocolImplementations": [
|
|
216
266
|
"remote-exec/machine@1"
|
package/view/app.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
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),没有可编辑的名片;
|
|
@@ -8,15 +10,18 @@
|
|
|
8
10
|
import { shortAuthor } from 'coda-view://lib/v1/accord.js';
|
|
9
11
|
import { accordStatusSummary, resolveAvatar } from '../lib/avatar.mjs';
|
|
10
12
|
import { checked, parseInvite } from '../lib/friends.mjs';
|
|
11
|
-
import { friendSections, identities, listMembers, memberTitles } from '../lib/view-members.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';
|
|
13
15
|
|
|
14
16
|
const ACCORD = 'coda/accord@1', LOCAL = { kind: ACCORD, id: 'local' };
|
|
17
|
+
const MACHINE_RECORD = 'friends/machine@1', MACHINE = 'coda/machine@1';
|
|
15
18
|
const coda = window.coda;
|
|
16
19
|
const root = document.getElementById('root');
|
|
17
20
|
const STATUS_TEXT = { bound: '已互认', pending: '待互认', conflict: '密钥冲突' };
|
|
18
21
|
const MACHINE_KIND = 'remote-exec/machine@1';
|
|
19
22
|
const RECOVER_MS = 3000;
|
|
23
|
+
const OFFLINE_HINT = '这台电脑离线,显示的是它最后一次写下的身份与好友;添加、互认、移除等操作要等它回来才能做。';
|
|
24
|
+
const OFFLINE_POLL_MS = 30_000;
|
|
20
25
|
// 结果不明的撤销:连续读到这么多遍有效列表、授权仍可用,就不再自动重发。
|
|
21
26
|
const REVOKE_RETRIES = 3;
|
|
22
27
|
// 结果不明的授予:连续读到这么多遍有效列表、里面都没有这条授权,就回滚这一行,允许重试(持有方对同一目标的授予幂等)。
|
|
@@ -24,11 +29,11 @@ const GRANT_CONFIRMS = 3;
|
|
|
24
29
|
// 仅限目录的授予、改范围要先到目标机探测(持有方最长等 30 秒):页面多等一会儿再算没有回应。
|
|
25
30
|
const PROBE_WAIT_MS = 45_000;
|
|
26
31
|
|
|
27
|
-
//
|
|
32
|
+
// 视图里的每台电脑(成员在页面生命周期内不变)各存一份数据;没有 friends/machine@1 记录的 `loaded` 为 false,不出节。
|
|
28
33
|
// 节点的名字、忙碌、错误、已收回按(电脑, 控制服务器节点 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);
|
|
34
|
+
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
35
|
const multi = members.length > 1;
|
|
36
|
+
let firstRead = false, loadError = null; // firstRead:第一次读完(成功或失败)才画,之后记录变了才重画
|
|
32
37
|
const state = { busy: new Set(), notice: new Map(), heroNotice: new Map(), detail: null, removing: null, menu: null };
|
|
33
38
|
let netBusy = false, netView = null; // 同一页同一时间只有一个好友节点操作在途;netView 是开着的抽屉里读到的内容
|
|
34
39
|
const via = m => ({ machine: m.id });
|
|
@@ -60,23 +65,30 @@ addEventListener('message', event => {
|
|
|
60
65
|
if (wait.late) { pendingReplies.delete(rid); wait.late(ok ? { value } : { failure }); } else if (ok) wait.resolve(value); else wait.reject(failure);
|
|
61
66
|
});
|
|
62
67
|
|
|
63
|
-
// ----
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
m
|
|
67
|
-
m
|
|
68
|
-
m
|
|
68
|
+
// ---- 读目录里各台的 friends/machine@1 ----
|
|
69
|
+
// 条目没了(DELETED)当这台没有记录。记录里的好友表一变,已成功移除而等推送确认的通道就可以清理了。
|
|
70
|
+
function onChange({ type, entry }) {
|
|
71
|
+
const m = memberOf(members, entry?.owner);
|
|
72
|
+
if (!m) return;
|
|
73
|
+
applyEntry(m, type === 'DELETED' ? null : entry);
|
|
69
74
|
for (const one of gone) {
|
|
70
75
|
if (one.m !== m || m.friends.some(f => f.node === one.node)) continue;
|
|
71
76
|
gone.delete(one); state.busy.delete(one.key); state.notice.delete(one.key);
|
|
72
77
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
async function
|
|
77
|
-
try {
|
|
78
|
-
|
|
78
|
+
if (firstRead) draw();
|
|
79
|
+
}
|
|
80
|
+
// 哪几台离线:只有 coda/machine@1 的 offline 说得出,它到点不推送,所以打开时读一次,之后隔一会儿再读;读不到就都当在线。
|
|
81
|
+
async function readOffline() {
|
|
82
|
+
try { return applyOffline(members, await coda.list(MACHINE)); } catch { return false; }
|
|
83
|
+
}
|
|
84
|
+
async function start() {
|
|
85
|
+
await Promise.all([
|
|
86
|
+
Promise.resolve(coda.watch(MACHINE_RECORD, onChange)).catch(e => { loadError = `读不到好友信息:${e.message}`; }),
|
|
87
|
+
readOffline(),
|
|
88
|
+
]);
|
|
89
|
+
firstRead = true;
|
|
79
90
|
draw();
|
|
91
|
+
setInterval(async () => { if (await readOffline()) draw(); }, OFFLINE_POLL_MS);
|
|
80
92
|
}
|
|
81
93
|
|
|
82
94
|
// ---- 头像 ----
|
|
@@ -135,7 +147,6 @@ function tile(label, value, sub, dot) {
|
|
|
135
147
|
}
|
|
136
148
|
// 一台电脑的好友数与证书状态。
|
|
137
149
|
function statusTiles(m, rows) {
|
|
138
|
-
if (!m.loaded) return h('div', { class: 'bento' }, tile('好友', '读取中…'), tile('身份', '读取中…'));
|
|
139
150
|
const summary = accordStatusSummary(m.entry, rows), { bound, pending, conflict, total } = summary.friends, cert = CERT[summary.cert];
|
|
140
151
|
const sub = summary.cert === 'active' ? (summary.certUntil ? `证书有效至 ${formatTime(summary.certUntil)}` : '可正常发布记录') : cert.sub;
|
|
141
152
|
return h('div', { class: 'bento' },
|
|
@@ -149,21 +160,22 @@ function drawHero(id, sections, titles) {
|
|
|
149
160
|
const { author, signer } = id, ready = Boolean(signer);
|
|
150
161
|
const summary = accordStatusSummary(signer?.entry, []);
|
|
151
162
|
const notice = state.heroNotice.get(author ?? '');
|
|
152
|
-
const
|
|
153
|
-
const name = author ? titles.get(author) : !settled ? '读取中…' : ready ? '未建身份' : '读不到身份';
|
|
163
|
+
const name = author ? titles.get(author) : ready ? '未建身份' : '读不到身份';
|
|
154
164
|
const single = !multi && sections[0];
|
|
155
165
|
return h('section', { class: 'hero' },
|
|
156
166
|
h('div', { class: 'banner' }, h('div', { class: 'banner-acts' },
|
|
157
|
-
single
|
|
167
|
+
single ? h('button', { class: 'pill strong', type: 'button', onclick: () => openInvite(single.member) }, '添加好友') : null)),
|
|
158
168
|
h('div', { class: 'hero-body' },
|
|
159
169
|
h('div', { class: 'hero-id' },
|
|
160
170
|
h('span', { class: 'av-ring' }, avatar(resolveAvatar({ author, name }), 'lg')),
|
|
161
171
|
h('div', { class: 'hero-names' }, h('div', { class: 'name-line' }, h('h2', { class: 'hero-name selectable' }, name), h('span', { class: 'tag' }, '我的身份')),
|
|
162
172
|
h('button', { class: 'fp mono', type: 'button', disabled: !author, title: author ? '复制完整公钥' : '', onclick: () => copyKey(author),
|
|
163
|
-
}, author ? `ed25519:${summary.short}… · 复制` : !ready ?
|
|
164
|
-
!author && ready
|
|
173
|
+
}, author ? `ed25519:${summary.short}… · 复制` : !ready ? '读不到公钥' : summary.cert === 'no_identity' ? '未建身份' : '未取得公钥'))),
|
|
174
|
+
!author && ready ? h('p', { class: 'hint' }, '这台电脑还没有 Accord 身份:前往客户端「设置 › 身份」创建后,好友才能认出你。') : null,
|
|
175
|
+
loadError ? h('p', { class: 'error selectable' }, loadError) : null,
|
|
176
|
+
single?.offline ? h('p', { class: 'hint' }, OFFLINE_HINT) : null,
|
|
165
177
|
notice ? h('p', { class: notice.kind }, notice.text) : null,
|
|
166
|
-
single
|
|
178
|
+
single ? statusTiles(single.member, single.rows) : null));
|
|
167
179
|
}
|
|
168
180
|
async function copyKey(author) {
|
|
169
181
|
try { await coda.copy(author); state.heroNotice.set(author, { kind: 'ok', text: '公钥已复制' }); } catch (e) { state.heroNotice.set(author, { kind: 'error', text: `复制失败:${e.message}` }); }
|
|
@@ -985,27 +997,27 @@ function friendTile(m, row) {
|
|
|
985
997
|
}
|
|
986
998
|
|
|
987
999
|
// 每台电脑一节;只有一台时没有节标题,也不重复画身份卡上已有的状态条和「添加好友」。
|
|
988
|
-
function drawSection({ member: m, title,
|
|
989
|
-
const
|
|
990
|
-
const summary = !unreachable && m.loaded ? accordStatusSummary(m.entry, rows) : null;
|
|
1000
|
+
function drawSection({ member: m, title, offline, rows }) {
|
|
1001
|
+
const summary = accordStatusSummary(m.entry, rows);
|
|
991
1002
|
// 几台时状态条只在有需要提示的状态(证书有问题、待互认、冲突)时出;只有一台时状态条在身份卡上。
|
|
992
|
-
const attention = summary
|
|
993
|
-
const empty = !m.loaded ? '正在读取…' : '还没有好友。点上方「添加好友」,粘贴对方发来的邀请即可。';
|
|
1003
|
+
const attention = summary.cert !== 'active' || summary.friends.pending || summary.friends.conflict;
|
|
994
1004
|
return h('section', {},
|
|
995
|
-
h('h2', { class: 'sec' }, title ?? '好友',
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1005
|
+
h('h2', { class: 'sec' }, title ?? '好友', h('span', { class: 'count', hidden: !rows.length }, String(rows.length)),
|
|
1006
|
+
offline && multi ? h('span', { class: 'badge pending' }, '离线') : null,
|
|
1007
|
+
canNet() ? [h('span', { class: 'spacer' }), h('button', { class: 'kebab', type: 'button', title: '网络节点', 'aria-label': '网络节点', disabled: netBusy, onclick: () => openNet(m) }, netIcon())] : null),
|
|
1008
|
+
multi && offline ? h('p', { class: 'hint' }, OFFLINE_HINT) : null,
|
|
1009
|
+
multi && attention ? statusTiles(m, rows) : null,
|
|
1010
|
+
multi ? h('div', { class: 'line' }, h('button', { class: 'pill strong', type: 'button', onclick: () => openInvite(m) }, '添加好友')) : null,
|
|
1011
|
+
rows.length ? h('ul', { class: 'friends' }, rows.map(row => friendTile(m, row))) : multi ? null : h('p', { class: 'empty' }, '还没有好友。点上方「添加好友」,粘贴对方发来的邀请即可。'),
|
|
1012
|
+
removalBlock(m));
|
|
1002
1013
|
}
|
|
1003
1014
|
|
|
1004
1015
|
const page = h('div', { class: 'page' });
|
|
1005
1016
|
function draw() {
|
|
1017
|
+
if (!firstRead) return;
|
|
1006
1018
|
const titles = memberTitles(members), sections = friendSections(members, titles);
|
|
1007
1019
|
drawn = sections;
|
|
1008
|
-
const ids = identities(
|
|
1020
|
+
const ids = identities(members);
|
|
1009
1021
|
if (!ids.length) ids.push({ author: null, signer: null, members: [] });
|
|
1010
1022
|
fill(page, ids.map(id => drawHero(id, sections, titles)), sections.map(drawSection));
|
|
1011
1023
|
if (state.detail) { if (detailRow()) drawDetail(); else closeDialog(); }
|
|
@@ -1015,7 +1027,6 @@ function draw() {
|
|
|
1015
1027
|
|
|
1016
1028
|
picker.dialog.scrim.hidden = true;
|
|
1017
1029
|
root.replaceChildren(page, detail.dialog.scrim, invite.dialog.scrim, picker.dialog.scrim, net.dialog.scrim);
|
|
1018
|
-
draw();
|
|
1019
1030
|
|
|
1020
1031
|
coda.header({ text: '好友' }).catch(() => {});
|
|
1021
|
-
|
|
1032
|
+
start();
|