@coda-console/friends 0.11.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 +11 -6
- package/lib/friends.mjs +29 -6
- package/lib/gateway.mjs +3 -2
- 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 +131 -72
- package/view/style.css +4 -0
package/README.md
CHANGED
|
@@ -5,15 +5,20 @@
|
|
|
5
5
|
- **本机身份**:身份卡显示本机的展示名、确定性几何纹头像、公钥短码(点按复制),下方状态条给出已互认好友数(有待互认、冲突时提示)与身份、证书状态。没有建身份时显示「未建身份」并提示前往客户端「设置 › 身份」创建。
|
|
6
6
|
- **公钥派生的昵称与头像**:本机和好友都没有可编辑的昵称、照片、Emoji 或主题色,统一由 Accord 公钥推出。展示名是 `昵称 #尾号`:昵称按 coda#3040 的算法取公钥 256 位,从最高位起每 7 位一组(至多 36 组),`0–77` 为字母、`78–87` 为数字、`88–127` 为结束符(已有不足 2 位时跳过,够 2 位就结束,读完仍不足 2 位则没有昵称);尾号是公钥按大端整数转成十进制(补零到 78 位)后的末 4 位,同昵称且末 4 位相撞的公钥从右往左逐位加位直到区分(如 `xs #04084`、`xs #14084`)。撞号一起比的是当前页面(跨电脑时是视图里所有电脑)上的全部本人公钥与好友公钥;网关 `friends` 操作的 `title` 则由本机身份与全部好友一起比。解不出昵称时退回公钥短码(`ed25519:` 之后前 8 位),还没有公钥的待互认好友用节点名。头像是由公钥确定性生成的 5×5 几何纹,灰色,换设备也一样;没有合法公钥时取名字首字。这些算法不在插件里:页面用 WebView 标准库 `coda-view://lib/v1/accord.js`,Node 侧(网关、代理记录)用 `@coda-console/protocol/accord`,两者是同一份实现,测试用协议包的测试向量对齐。
|
|
7
7
|
- **添加好友**:点身份卡上的「添加好友」,粘贴对方给的邀请(JSON、`coda-friend://` 链接或多行 `key=value`),接受后自动尝试互认。邀请也可以在同一个弹层里生成,见下文「生成与接受邀请」。
|
|
8
|
+
- **同身份通道合并**:同一台电脑上,指向同一个完整公钥(`author`)的多条通道(如 `node:` + `link:`、两条 `link:`,或对方多台设备共用个人根身份)合并成一张好友卡片:状态里有冲突就算冲突(不隐藏密钥冲突),否则有已互认的算已互认,其余为待互认;多条连接时副标题写「N 条连接」,已互认的好友名下还有待互认通道时再注明「(M 待互认)」。没有公钥的待互认通道不与任何条目合并。详情里公钥与「共享」归整个好友,「连接」一节逐条列出各通道(通道键、机器、范围有差别时一并写出),每条各自有状态、重新互认和二次确认的移除,移除一条后弹层留在其余连接上;只有一条连接时详情保持原样。好友数、状态条与网关 `friends/social@1` 的 `friendCount`、`friends` 都按合并后的好友统计。
|
|
8
9
|
- **好友列表**:实时跟随好友表变化,宽屏双列。点好友打开详情:昵称、几何纹头像、完整公钥(可复制)、节点名、连接状态、「共享」(见下文「把远程执行授给好友」)、重新互认、二次确认后移除。
|
|
9
10
|
- **社交网关 `friends/social@1`**:业务无关,别的插件(在 `optionalUses` 里声明)经它读好友与好友分享的记录、向好友发布与撤回记录,不必各自处理没有身份、证书过期和重试。
|
|
10
11
|
|
|
11
12
|
## 几台电脑
|
|
12
13
|
|
|
13
|
-
页面不自己选电脑:App
|
|
14
|
+
页面不自己选电脑:App 的全局视图里有哪几台,页面就显示哪几台,没有机器标签。
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
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 上不会自动出现),状态条(好友数、证书状态、待互认与冲突)和「添加好友」也在这一节。接受邀请与随后的互认、好友详情里的重新互认、移除,都发给这一节、这一行所在的电脑。没有好友的电脑也占一节;同一个好友在两台上都加了,就在两节里各出现一次,不跨电脑合并(合并只在同一台电脑内按完整公钥进行)。离线的电脑照样出节,显示它最后写下的身份与好友,并标「离线」;没有这条记录的电脑(没装好友插件、旧版)不出节、也不报错。
|
|
17
22
|
- **只有一台时**页面不出现节标题,状态条与「添加好友」仍在身份卡上。
|
|
18
23
|
- **推荐用法**:好友只在一台电脑上加。好友关系登记在那台电脑上,别的电脑不会自动出现这个好友。邀请里的入网码只能用一次,不必在每台上重复加。
|
|
19
24
|
- **要共享别的电脑**:不必在那台上再加一次好友。加了好友的这台作网关,能把同一空间里别的电脑的远程执行逐台授给好友(见下一节「经网关授出别的机器」)。
|
|
@@ -184,7 +189,7 @@ coda self grants.restrict '{"grant":"<授权号>","restrict":{"dirs":[…]}}'
|
|
|
184
189
|
|
|
185
190
|
| 操作 | 作用 |
|
|
186
191
|
| --- | --- |
|
|
187
|
-
| `friends`(读) | 互认的好友(`includePending` 带上待互认与冲突的),只来自 Accord 的好友表:`{ selfAuthor, certState, friends }`,每行的 `title`
|
|
192
|
+
| `friends`(读) | 互认的好友(`includePending` 带上待互认与冲突的),只来自 Accord 的好友表:`{ selfAuthor, certState, friends }`,每行的 `title` 是公钥派生的展示名,不读名片记录;指向同一公钥的多条通道合并成一行,顶层 `node`、`peerNode`、`scope` 取代表通道,`channels` 列出各条通道 |
|
|
188
193
|
| `records`(读) | 按 `kind`、`author`、`id`、`cursor` 读好友分享的记录,不读 `binding` 与 `friends/rpc-` 前缀 |
|
|
189
194
|
| `publish` | 发布 `{ kind, id, body, audience? }`;同作者同内容不重发,换作者重发 |
|
|
190
195
|
| `retract` | 撤回一条,或省略 `id` 撤回本机在这个 `kind` 下的全部(含队列里还没发出去的) |
|
|
@@ -202,9 +207,9 @@ coda self grants.restrict '{"grant":"<授权号>","restrict":{"dirs":[…]}}'
|
|
|
202
207
|
## 权限与数据边界
|
|
203
208
|
|
|
204
209
|
- `friends/social@1` 的权限组:`read` 管 `friends`、`records`;`publish` 管 `publish`、`retract`、`outbox`。
|
|
205
|
-
- `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`(转给本机的远程执行,没装就没有这部分)。
|
|
206
211
|
- 保留种类 `friends/rpc-grant`、`friends/rpc-call`、`friends/rpc-answer`:只有本插件的内部入口能签发与读取,通用网关的发布、撤回、`records`、`outbox` 都不给出。
|
|
207
212
|
- 入网码(`authKey`)是凭据:只在提交时用一次,提交后立即清空输入框,不写入任何文件、日志或页面状态。
|
|
208
213
|
- 节点名等外来数据只当文本渲染(`textContent`),不当指令或 HTML。
|
|
209
|
-
-
|
|
214
|
+
- 好友页读目录里的 `friends/machine@1`,Accord 的写(接受、互认、移除)在页面交互时发起;模块在 `activate` 时监听本机 Accord 记录(证书、好友表),用来刷新网关记录、写 `friends/machine@1`、证书恢复后冲洗待发队列。
|
|
210
215
|
- 待发的 `body` 只在本机 `outbox.json`,不经别处。
|
package/lib/friends.mjs
CHANGED
|
@@ -118,13 +118,36 @@ export function certStateOf(accord) {
|
|
|
118
118
|
const RANK = { conflict: 0, pending: 1, bound: 2 };
|
|
119
119
|
export const statusOf = friend => friend.conflict ? 'conflict' : friend.bound ? 'bound' : 'pending';
|
|
120
120
|
|
|
121
|
+
const compare = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
122
|
+
const channelOf = friend => ({ node: friend.node, mode: typeof friend.mode === 'string' && friend.mode ? friend.mode : null, peerNode: friend.peerNode ?? null, scope: friend.scope,
|
|
123
|
+
author: friend.author ?? null, machine: typeof friend.machine === 'string' && friend.machine ? friend.machine : null,
|
|
124
|
+
certified: typeof friend.certified === 'boolean' ? friend.certified : null, status: statusOf(friend), updatedAt: friend.updatedAt ?? 0 });
|
|
125
|
+
const certifiedOf = channels => {
|
|
126
|
+
const list = channels.some(c => c.status === 'bound') ? channels.filter(c => c.status === 'bound') : channels;
|
|
127
|
+
return list.some(c => c.certified === true) ? true : list.some(c => c.certified === false) ? false : null;
|
|
128
|
+
};
|
|
129
|
+
|
|
121
130
|
/**
|
|
122
|
-
* 好友表(`status.friends
|
|
123
|
-
*
|
|
124
|
-
*
|
|
131
|
+
* 好友表(`status.friends`)合成的行:同一台电脑上指向同一完整公钥(author)的通道归并成一个好友,通道逐条放在 `channels`(状态、更新时间新的在前);
|
|
132
|
+
* 没有公钥的待互认通道不与任何条目归并,各自成行。好友状态:任一通道密钥冲突即冲突(不隐藏冲突),否则任一已互认即已互认,其余待互认;
|
|
133
|
+
* 顶层 `node`、`peerNode`、`scope` 取代表通道(第一条状态等于好友状态的通道),`updatedAt` 取各通道最大值。
|
|
134
|
+
* 标题是公钥派生的 `昵称 #尾号`(`identityTitles`:同昵称且尾号相撞时向左加位,无昵称的公钥是短码),没有公钥的待互认项用节点名。
|
|
135
|
+
* 撞号要和本人、别的电脑上的好友一起比,所以 `titles`(author → 标题,`identityTitles` 的结果)由调用方算好给出。
|
|
136
|
+
* 好友行按状态(冲突、待互认、已互认)再按更新时间新的在前排序,同样的输入排出同样的顺序。
|
|
125
137
|
*/
|
|
126
138
|
export function mergeFriendRows(friends = [], { titles, shortAuthor }) {
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
139
|
+
const groups = new Map();
|
|
140
|
+
for (const friend of friends) {
|
|
141
|
+
const channel = channelOf(friend), key = channel.author ? `a:${channel.author}` : `n:${channel.node}`;
|
|
142
|
+
if (!groups.has(key)) groups.set(key, []);
|
|
143
|
+
groups.get(key).push(channel);
|
|
144
|
+
}
|
|
145
|
+
const rows = [...groups.values()].map(channels => {
|
|
146
|
+
channels.sort((a, b) => RANK[a.status] - RANK[b.status] || b.updatedAt - a.updatedAt || compare(a.node, b.node));
|
|
147
|
+
const status = channels.some(c => c.status === 'conflict') ? 'conflict' : channels.some(c => c.status === 'bound') ? 'bound' : 'pending';
|
|
148
|
+
const primary = channels.find(c => c.status === status) ?? channels[0], { author } = primary;
|
|
149
|
+
return { node: primary.node, author, short: shortAuthor(author), status, peerNode: primary.peerNode, scope: primary.scope, certified: certifiedOf(channels),
|
|
150
|
+
updatedAt: Math.max(...channels.map(c => c.updatedAt)), title: titles.get(author) ?? primary.node, channels };
|
|
151
|
+
});
|
|
152
|
+
return rows.sort((a, b) => RANK[a.status] - RANK[b.status] || b.updatedAt - a.updatedAt || compare(a.author ?? '', b.author ?? '') || compare(a.node, b.node));
|
|
130
153
|
}
|
package/lib/gateway.mjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// 自己不建第二份目录库,只在 outbox.json 里留还没发出去的 body(同步成功即释放)。所有会写盘、会外发的动作排成一条队,
|
|
3
3
|
// 发布、撤回、证书变化引起的冲洗互不交叉。
|
|
4
4
|
import { identityTitles, shortAuthor } from '@coda-console/protocol/accord';
|
|
5
|
-
import { mergeFriendRows
|
|
5
|
+
import { mergeFriendRows } from './friends.mjs';
|
|
6
6
|
import { MAX_BODY_BYTES, canSkip, canonicalHash, certStateOf, checkId, checkKind, checkPublish, entryKey, holdState, keyOf, retractKey, invalid, isPending, markRejected, markSynced,
|
|
7
7
|
nextSeq, queuePublish, queueRetract, readOutbox, withState, writeOutbox } from './outbox.mjs';
|
|
8
8
|
import { readPolicy, setKindPaused } from './policy.mjs';
|
|
@@ -154,8 +154,9 @@ export function createGateway(coda, dir) {
|
|
|
154
154
|
const outbox = dir ? await readOutbox(dir) : {}, { pausedKinds } = dir ? await readPolicy(dir) : { pausedKinds: [] };
|
|
155
155
|
const until = accord?.status?.certificate?.until;
|
|
156
156
|
const friends = Array.isArray(accord?.status?.friends) ? accord.status.friends : [];
|
|
157
|
+
const rows = mergeFriendRows(friends, { titles: new Map(), shortAuthor });
|
|
157
158
|
const status = { author, certState, ...(Number.isSafeInteger(until) && until >= 0 ? { certUntil: until } : {}),
|
|
158
|
-
friendCount:
|
|
159
|
+
friendCount: rows.filter(row => row.status === 'bound').length, pendingCount: Object.values(outbox).filter(isPending).length, pausedKinds };
|
|
159
160
|
if (!stopped) await coda.put({ kind: SOCIAL, key: 'local' }, { spec: { version: 1 }, status });
|
|
160
161
|
} catch { /* 状态记录下一次动作会再写 */ }
|
|
161
162
|
}
|
|
@@ -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,21 +1,27 @@
|
|
|
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 字符串。
|
|
10
|
+
import { shortAuthor } from 'coda-view://lib/v1/accord.js';
|
|
8
11
|
import { accordStatusSummary, resolveAvatar } from '../lib/avatar.mjs';
|
|
9
12
|
import { checked, parseInvite } from '../lib/friends.mjs';
|
|
10
|
-
import { friendSections, identities, listMembers, memberTitles } from '../lib/view-members.mjs';
|
|
13
|
+
import { applyEntry, applyOffline, friendSections, identities, listMembers, memberOf, memberTitles } from '../lib/view-members.mjs';
|
|
11
14
|
import { fill, h, identiconSvg, svg } from './dom.mjs';
|
|
12
15
|
|
|
13
16
|
const ACCORD = 'coda/accord@1', LOCAL = { kind: ACCORD, id: 'local' };
|
|
17
|
+
const MACHINE_RECORD = 'friends/machine@1', MACHINE = 'coda/machine@1';
|
|
14
18
|
const coda = window.coda;
|
|
15
19
|
const root = document.getElementById('root');
|
|
16
20
|
const STATUS_TEXT = { bound: '已互认', pending: '待互认', conflict: '密钥冲突' };
|
|
17
21
|
const MACHINE_KIND = 'remote-exec/machine@1';
|
|
18
22
|
const RECOVER_MS = 3000;
|
|
23
|
+
const OFFLINE_HINT = '这台电脑离线,显示的是它最后一次写下的身份与好友;添加、互认、移除等操作要等它回来才能做。';
|
|
24
|
+
const OFFLINE_POLL_MS = 30_000;
|
|
19
25
|
// 结果不明的撤销:连续读到这么多遍有效列表、授权仍可用,就不再自动重发。
|
|
20
26
|
const REVOKE_RETRIES = 3;
|
|
21
27
|
// 结果不明的授予:连续读到这么多遍有效列表、里面都没有这条授权,就回滚这一行,允许重试(持有方对同一目标的授予幂等)。
|
|
@@ -23,11 +29,11 @@ const GRANT_CONFIRMS = 3;
|
|
|
23
29
|
// 仅限目录的授予、改范围要先到目标机探测(持有方最长等 30 秒):页面多等一会儿再算没有回应。
|
|
24
30
|
const PROBE_WAIT_MS = 45_000;
|
|
25
31
|
|
|
26
|
-
//
|
|
32
|
+
// 视图里的每台电脑(成员在页面生命周期内不变)各存一份数据;没有 friends/machine@1 记录的 `loaded` 为 false,不出节。
|
|
27
33
|
// 节点的名字、忙碌、错误、已收回按(电脑, 控制服务器节点 ID)记在 m.fn 里,不与好友行的 state.busy / state.notice 混用(那两个按 Accord 的 node 记)。
|
|
28
|
-
const members = listMembers(coda).map(m => ({ ...m, entry: null, author: null, friends: [], loaded: false,
|
|
29
|
-
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 } }));
|
|
30
35
|
const multi = members.length > 1;
|
|
36
|
+
let firstRead = false, loadError = null; // firstRead:第一次读完(成功或失败)才画,之后记录变了才重画
|
|
31
37
|
const state = { busy: new Set(), notice: new Map(), heroNotice: new Map(), detail: null, removing: null, menu: null };
|
|
32
38
|
let netBusy = false, netView = null; // 同一页同一时间只有一个好友节点操作在途;netView 是开着的抽屉里读到的内容
|
|
33
39
|
const via = m => ({ machine: m.id });
|
|
@@ -59,19 +65,30 @@ addEventListener('message', event => {
|
|
|
59
65
|
if (wait.late) { pendingReplies.delete(rid); wait.late(ok ? { value } : { failure }); } else if (ok) wait.resolve(value); else wait.reject(failure);
|
|
60
66
|
});
|
|
61
67
|
|
|
62
|
-
// ----
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
m
|
|
66
|
-
m
|
|
67
|
-
m
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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);
|
|
74
|
+
for (const one of gone) {
|
|
75
|
+
if (one.m !== m || m.friends.some(f => f.node === one.node)) continue;
|
|
76
|
+
gone.delete(one); state.busy.delete(one.key); state.notice.delete(one.key);
|
|
77
|
+
}
|
|
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;
|
|
74
90
|
draw();
|
|
91
|
+
setInterval(async () => { if (await readOffline()) draw(); }, OFFLINE_POLL_MS);
|
|
75
92
|
}
|
|
76
93
|
|
|
77
94
|
// ---- 头像 ----
|
|
@@ -130,7 +147,6 @@ function tile(label, value, sub, dot) {
|
|
|
130
147
|
}
|
|
131
148
|
// 一台电脑的好友数与证书状态。
|
|
132
149
|
function statusTiles(m, rows) {
|
|
133
|
-
if (!m.loaded) return h('div', { class: 'bento' }, tile('好友', '读取中…'), tile('身份', '读取中…'));
|
|
134
150
|
const summary = accordStatusSummary(m.entry, rows), { bound, pending, conflict, total } = summary.friends, cert = CERT[summary.cert];
|
|
135
151
|
const sub = summary.cert === 'active' ? (summary.certUntil ? `证书有效至 ${formatTime(summary.certUntil)}` : '可正常发布记录') : cert.sub;
|
|
136
152
|
return h('div', { class: 'bento' },
|
|
@@ -144,21 +160,22 @@ function drawHero(id, sections, titles) {
|
|
|
144
160
|
const { author, signer } = id, ready = Boolean(signer);
|
|
145
161
|
const summary = accordStatusSummary(signer?.entry, []);
|
|
146
162
|
const notice = state.heroNotice.get(author ?? '');
|
|
147
|
-
const
|
|
148
|
-
const name = author ? titles.get(author) : !settled ? '读取中…' : ready ? '未建身份' : '读不到身份';
|
|
163
|
+
const name = author ? titles.get(author) : ready ? '未建身份' : '读不到身份';
|
|
149
164
|
const single = !multi && sections[0];
|
|
150
165
|
return h('section', { class: 'hero' },
|
|
151
166
|
h('div', { class: 'banner' }, h('div', { class: 'banner-acts' },
|
|
152
|
-
single
|
|
167
|
+
single ? h('button', { class: 'pill strong', type: 'button', onclick: () => openInvite(single.member) }, '添加好友') : null)),
|
|
153
168
|
h('div', { class: 'hero-body' },
|
|
154
169
|
h('div', { class: 'hero-id' },
|
|
155
170
|
h('span', { class: 'av-ring' }, avatar(resolveAvatar({ author, name }), 'lg')),
|
|
156
171
|
h('div', { class: 'hero-names' }, h('div', { class: 'name-line' }, h('h2', { class: 'hero-name selectable' }, name), h('span', { class: 'tag' }, '我的身份')),
|
|
157
172
|
h('button', { class: 'fp mono', type: 'button', disabled: !author, title: author ? '复制完整公钥' : '', onclick: () => copyKey(author),
|
|
158
|
-
}, author ? `ed25519:${summary.short}… · 复制` : !ready ?
|
|
159
|
-
!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,
|
|
160
177
|
notice ? h('p', { class: notice.kind }, notice.text) : null,
|
|
161
|
-
single
|
|
178
|
+
single ? statusTiles(single.member, single.rows) : null));
|
|
162
179
|
}
|
|
163
180
|
async function copyKey(author) {
|
|
164
181
|
try { await coda.copy(author); state.heroNotice.set(author, { kind: 'ok', text: '公钥已复制' }); } catch (e) { state.heroNotice.set(author, { kind: 'error', text: `复制失败:${e.message}` }); }
|
|
@@ -318,25 +335,32 @@ async function submitInvite() {
|
|
|
318
335
|
// 不同电脑上的 node 可能相同,忙碌与提示按(电脑, node)记。
|
|
319
336
|
let drawn = []; // 最近一次 draw 的分节,详情按它找当前行
|
|
320
337
|
|
|
321
|
-
async function handshake(m,
|
|
322
|
-
const key = rowKey(m,
|
|
338
|
+
async function handshake(m, ch) {
|
|
339
|
+
const key = rowKey(m, ch.node);
|
|
323
340
|
state.busy.add(key); state.notice.set(key, { kind: 'progress', text: '正在互认…' }); draw();
|
|
324
341
|
let notice;
|
|
325
|
-
try { await coda.call(LOCAL, 'handshake', { node:
|
|
342
|
+
try { await coda.call(LOCAL, 'handshake', { node: ch.node, ...(ch.peerNode ? { peerNode: ch.peerNode } : {}) }, via(m)); notice = { kind: 'ok', text: '互认完成' }; }
|
|
326
343
|
catch (e) { notice = { kind: 'error', text: `互认失败:${e.message}` }; }
|
|
327
344
|
state.notice.set(key, notice); state.busy.delete(key); draw();
|
|
328
345
|
}
|
|
329
|
-
|
|
330
|
-
|
|
346
|
+
// 移除已成功、却还在等 Accord 推送确认这条通道消失的连接。
|
|
347
|
+
const gone = new Set();
|
|
348
|
+
async function remove(m, ch) {
|
|
349
|
+
const key = rowKey(m, ch.node);
|
|
331
350
|
state.busy.add(key); state.notice.set(key, { kind: 'progress', text: '正在移除…' }); draw();
|
|
332
351
|
let failure = null;
|
|
333
|
-
try { await coda.call(LOCAL, 'remove', { node:
|
|
352
|
+
try { await coda.call(LOCAL, 'remove', { node: ch.node }, via(m)); }
|
|
334
353
|
catch (e) { failure = { kind: 'error', text: `移除失败:${e.message}` }; }
|
|
335
|
-
state.busy.delete(key);
|
|
336
|
-
if (failure) state.notice.set(key, failure);
|
|
354
|
+
if (failure) { state.busy.delete(key); state.notice.set(key, failure); }
|
|
337
355
|
else {
|
|
338
|
-
state.
|
|
339
|
-
|
|
356
|
+
if (state.removing === key) state.removing = null;
|
|
357
|
+
// 只剩这一条连接就没有好友可看了;多条时留着弹层,等 Accord 推送变化后画出剩下的连接(最后一条没了,重画时弹层自己关)。
|
|
358
|
+
const open = detailRow();
|
|
359
|
+
const last = open && state.detail.member === m && open.channels.length === 1 && open.channels[0].node === ch.node;
|
|
360
|
+
// 推送还没把这条通道拿掉时,它保持不可操作,免得对已移除的通道再发请求;推送到了(onChange)再清理。
|
|
361
|
+
if (!last && m.friends.some(f => f.node === ch.node)) gone.add({ key, m, node: ch.node });
|
|
362
|
+
else { state.busy.delete(key); state.notice.delete(key); }
|
|
363
|
+
if (last) closeDialog();
|
|
340
364
|
}
|
|
341
365
|
draw();
|
|
342
366
|
}
|
|
@@ -344,30 +368,60 @@ const detail = {
|
|
|
344
368
|
dialog: makeDialog({ title: '好友', close: '完成', onClose: () => { state.detail = null; state.removing = null; state.menu = null; closePicker(); } }),
|
|
345
369
|
card: h('div'),
|
|
346
370
|
info: h('dl', { class: 'facts' }),
|
|
371
|
+
conns: h('section', { class: 'conns' }),
|
|
347
372
|
share: h('section', { class: 'share' }),
|
|
348
373
|
actions: h('div', { class: 'actions split' }),
|
|
349
374
|
notice: h('div'),
|
|
350
375
|
};
|
|
351
|
-
detail.dialog.body.append(h('div', { class: 'stack' }, detail.card, detail.info, detail.share, detail.actions, detail.notice));
|
|
352
|
-
|
|
376
|
+
detail.dialog.body.append(h('div', { class: 'stack' }, detail.card, detail.info, detail.conns, detail.share, detail.actions, detail.notice));
|
|
377
|
+
// 详情按公钥跟着好友走;还没有公钥的待互认通道按节点找,互认成功拿到公钥后改按公钥跟,并读出共享。
|
|
378
|
+
function detailRow() {
|
|
379
|
+
const d = state.detail;
|
|
380
|
+
if (!d) return undefined;
|
|
381
|
+
const rows = drawn.find(section => section.member === d.member).rows;
|
|
382
|
+
if (d.author) return rows.find(row => row.author === d.author);
|
|
383
|
+
const found = rows.find(row => row.channels.some(c => c.node === d.node));
|
|
384
|
+
if (found?.author) { d.author = found.author; loadShare(d.member, found); }
|
|
385
|
+
return found;
|
|
386
|
+
}
|
|
353
387
|
function openDetail(m, row) {
|
|
354
|
-
state.detail = { member: m,
|
|
388
|
+
state.detail = { member: m, author: row.author, node: row.node }; state.removing = null; state.menu = null;
|
|
355
389
|
shareOf(m).opsOpen = false;
|
|
356
390
|
drawDetail();
|
|
357
391
|
showDialog(detail.dialog);
|
|
358
392
|
loadShare(m, row);
|
|
359
393
|
}
|
|
360
394
|
function fact(label, ...value) { return [h('dt', {}, label), h('dd', { class: 'selectable' }, ...value)]; }
|
|
395
|
+
const copiedKey = (m, row) => rowKey(m, `copy:${row.author}`);
|
|
361
396
|
async function copyFriendKey(m, row) {
|
|
362
|
-
const key =
|
|
397
|
+
const key = copiedKey(m, row);
|
|
363
398
|
try { await coda.copy(row.author); state.notice.set(key, { kind: 'ok', text: '公钥已复制' }); }
|
|
364
399
|
catch (e) { state.notice.set(key, { kind: 'error', text: `复制失败:${e.message}` }); }
|
|
365
400
|
draw();
|
|
366
401
|
}
|
|
402
|
+
// 多条连接时,逐条列出:通道键、机器、范围只在条与条之间有差别时才写出来,否则分不清。
|
|
403
|
+
function connRow(m, row, ch) {
|
|
404
|
+
const key = rowKey(m, ch.node), busy = state.busy.has(key), notice = state.notice.get(key);
|
|
405
|
+
const machines = new Set(row.channels.map(c => c.machine).filter(Boolean)), scopes = new Set(row.channels.map(c => c.scope));
|
|
406
|
+
const sub = [ch.peerNode && ch.node !== ch.peerNode ? ch.node : null, machines.size > 1 && ch.machine ? `机器 @${shortAuthor(ch.machine)}` : null, scopes.size > 1 ? ch.scope : null].filter(Boolean);
|
|
407
|
+
return h('li', { class: 'g-row conn', 'aria-busy': busy ? 'true' : null },
|
|
408
|
+
h('div', { class: 'g-info' }, h('span', { class: 'g-name mono conn-name' }, ch.peerNode ?? ch.node), sub.length ? h('span', { class: 'g-sub conn-sub' }, sub.join(' · ')) : null),
|
|
409
|
+
h('span', { class: `badge ${ch.status}` }, STATUS_TEXT[ch.status]),
|
|
410
|
+
h('div', { class: 'conn-body' },
|
|
411
|
+
ch.status === 'conflict' ? h('p', { class: 'warn-box' }, '对方的密钥与之前互认的不一致,可能换了设备或身份。确认是本人后可「重新互认」,否则建议移除。') : null,
|
|
412
|
+
h('div', { class: 'actions' }, ...(state.removing === key
|
|
413
|
+
? [h('span', { class: 'secondary' }, '移除这条连接后不再经它同步,确定吗?'), h('span', { class: 'spacer' }),
|
|
414
|
+
h('button', { class: 'btn danger small', type: 'button', disabled: busy, onclick: () => remove(m, ch) }, '确认移除'),
|
|
415
|
+
h('button', { class: 'btn quiet small', type: 'button', onclick: () => { state.removing = null; draw(); } }, '再想想')]
|
|
416
|
+
: [h('button', { class: 'btn quiet small', type: 'button', disabled: busy, onclick: () => handshake(m, ch) }, '重新互认'), h('span', { class: 'spacer' }),
|
|
417
|
+
h('button', { class: 'btn danger small', type: 'button', disabled: busy, onclick: () => { state.removing = key; draw(); } }, '移除…')])),
|
|
418
|
+
notice ? h('p', { class: notice.kind }, notice.text) : null));
|
|
419
|
+
}
|
|
367
420
|
function drawDetail() {
|
|
368
|
-
const row = detailRow(), { member: m
|
|
421
|
+
const row = detailRow(), { member: m } = state.detail;
|
|
369
422
|
if (!row) return;
|
|
370
|
-
const
|
|
423
|
+
const many = row.channels.length > 1, [ch] = row.channels, key = rowKey(m, ch.node);
|
|
424
|
+
const busy = state.busy.has(key), notice = state.notice.get(key), copied = row.author ? state.notice.get(copiedKey(m, row)) : null;
|
|
371
425
|
detail.dialog.heading.textContent = multi ? `${row.title} · ${m.name}` : row.title;
|
|
372
426
|
detail.card.replaceChildren(h('div', { class: 'mini-card' }, h('div', { class: 'banner small' }),
|
|
373
427
|
h('div', { class: 'mini-body row' }, h('span', { class: 'av-ring lg' }, avatar(rowAvatar(row), 'lg')),
|
|
@@ -375,19 +429,23 @@ function drawDetail() {
|
|
|
375
429
|
h('span', { class: `badge ${row.status}` }, STATUS_TEXT[row.status]))));
|
|
376
430
|
detail.info.replaceChildren(
|
|
377
431
|
...fact('公钥', row.author ? h('span', { class: 'mono key-full' }, row.author) : h('span', { class: 'secondary' }, '密钥未互认'),
|
|
378
|
-
row.author ? h('button', { class: 'btn quiet small', type: 'button', onclick: () => copyFriendKey(m, row) }, '复制公钥') : null
|
|
379
|
-
|
|
380
|
-
...fact('连接', STATUS_TEXT[row.status]));
|
|
381
|
-
detail.
|
|
432
|
+
row.author ? h('button', { class: 'btn quiet small', type: 'button', onclick: () => copyFriendKey(m, row) }, '复制公钥') : null,
|
|
433
|
+
copied ? h('span', { class: copied.kind }, copied.text) : null),
|
|
434
|
+
...(many ? [] : [...fact('节点', h('span', { class: 'mono' }, row.peerNode ?? row.node)), ...fact('连接', STATUS_TEXT[row.status])]));
|
|
435
|
+
detail.conns.hidden = !many;
|
|
436
|
+
detail.conns.replaceChildren(...(many ? [h('h4', { class: 'sub-head' }, '连接', h('span', { class: 'count' }, String(row.channels.length))),
|
|
437
|
+
h('ul', { class: 'glist' }, row.channels.map(c => connRow(m, row, c)))] : []));
|
|
438
|
+
detail.actions.hidden = many;
|
|
439
|
+
detail.actions.replaceChildren(...(many ? [] : state.removing === key
|
|
382
440
|
? [h('span', { class: 'secondary' }, '移除后不再同步这位好友,确定吗?'), h('span', { class: 'spacer' }),
|
|
383
|
-
h('button', { class: 'btn danger small', type: 'button', disabled: busy, onclick: () => remove(m,
|
|
441
|
+
h('button', { class: 'btn danger small', type: 'button', disabled: busy, onclick: () => remove(m, ch) }, '确认移除'),
|
|
384
442
|
h('button', { class: 'btn quiet small', type: 'button', onclick: () => { state.removing = null; draw(); } }, '再想想')]
|
|
385
|
-
: [h('button', { class: 'btn quiet small', type: 'button', disabled: busy, onclick: () => handshake(m,
|
|
443
|
+
: [h('button', { class: 'btn quiet small', type: 'button', disabled: busy, onclick: () => handshake(m, ch) }, '重新互认'), h('span', { class: 'spacer' }),
|
|
386
444
|
h('button', { class: 'btn danger small', type: 'button', disabled: busy, onclick: () => { state.removing = key; draw(); } }, '移除好友…')]));
|
|
387
445
|
drawShare(m, row);
|
|
388
|
-
fill(detail.notice,
|
|
446
|
+
fill(detail.notice, ...(many ? [] : [
|
|
389
447
|
row.status === 'conflict' ? h('p', { class: 'warn-box' }, '对方的密钥与之前互认的不一致,可能换了设备或身份。确认是本人后可「重新互认」,否则建议移除。') : null,
|
|
390
|
-
notice ? h('p', { class: notice.kind }, notice.text) : null);
|
|
448
|
+
notice ? h('p', { class: notice.kind }, notice.text) : null]));
|
|
391
449
|
}
|
|
392
450
|
|
|
393
451
|
// ---- 共享:授给这位好友的远程执行、好友授给本机的、最近的操作 ----
|
|
@@ -406,7 +464,6 @@ const PROXY_STATUS = {
|
|
|
406
464
|
// 授出的范围不让选,由对方的身份决定:个人身份授给他名下的机器,机器身份只授给那一台;基座还没给 certified 时不写。
|
|
407
465
|
const scopeNote = certified => (certified === true ? '授给这个人名下的机器' : certified === false ? '只授给对方那一台机器' : null);
|
|
408
466
|
const shareOf = m => (m.share ??= { grants: null, problem: null, corrupt: null, clock: 0, writes: 0, touched: 0, reread: false, pending: [], revoking: new Map(), theirs: new Map(), ops: new Map(), targets: null, targetsProblem: null, opsOpen: false, scopeOpen: new Set(), rescoping: new Map(), timer: null });
|
|
409
|
-
const certifiedOf = (m, row) => m.friends.find(friend => friend.node === row.node)?.certified;
|
|
410
467
|
const mergeGrant = (sh, made) => { const all = sh.grants ?? []; sh.grants = all.some(g => g.grant === made.grant) ? all.map(g => (g.grant === made.grant ? made : g)) : [...all, made]; };
|
|
411
468
|
// 已撤的不显示;撤销中的(名下的调用还在取消)显示。
|
|
412
469
|
// 能改范围的:还在用的授权(已撤销、已失效、目标不在了的不行)。
|
|
@@ -559,7 +616,7 @@ function shareRow({ key, name, sub, subKind, scope, badge, busy, menu }) {
|
|
|
559
616
|
onclick: e => { e.stopPropagation?.(); item.run(); } }, item.label, item.note ? h('span', { class: 'menu-note' }, item.note) : null))) : null);
|
|
560
617
|
}
|
|
561
618
|
function drawShare(m, row) {
|
|
562
|
-
const sh = shareOf(m), scope = scopeNote(
|
|
619
|
+
const sh = shareOf(m), scope = scopeNote(row.certified);
|
|
563
620
|
const mine = [
|
|
564
621
|
...(sh.grants ?? []).filter(g => g.friend === row.author && visibleGrant(g)).map(g => {
|
|
565
622
|
const local = sh.revoking.get(g.grant), re = sh.rescoping.get(g.grant), waiting = re?.unknown, failed = local?.error ?? re?.error;
|
|
@@ -576,7 +633,7 @@ function drawShare(m, row) {
|
|
|
576
633
|
const their = sh.theirs.get(row.author);
|
|
577
634
|
const theirRows = (their?.rows ?? []).map(proxy => shareRow({ key: `t:${proxy.key}`, name: proxy.name, scope: scopeView(sh, `t:${proxy.key}`, proxy.restrict), badge: PROXY_STATUS[proxy.status] ?? ['不可用', 'pending'] }));
|
|
578
635
|
const ops = sh.ops.get(row.author);
|
|
579
|
-
const canGrant = row.status === 'bound' && Boolean(row.author) && sh.grants !== null && !sh.corrupt;
|
|
636
|
+
const canGrant = row.channels.some(c => c.status === 'bound') && Boolean(row.author) && sh.grants !== null && !sh.corrupt;
|
|
580
637
|
fill(detail.share,
|
|
581
638
|
h('h4', {}, `共享 · ${m.name}`),
|
|
582
639
|
h('div', {}, h('div', { class: 'sub-head' }, '我授给 TA', h('span', { class: 'spacer' }),
|
|
@@ -632,7 +689,7 @@ function closePicker() {
|
|
|
632
689
|
function cancelPicker() { if (!picker.ctx?.busy) closePicker(); }
|
|
633
690
|
function drawPicker() {
|
|
634
691
|
if (picker.ctx?.stage !== 'list') return;
|
|
635
|
-
const { m, row } = picker.ctx, sh = shareOf(m), scope = scopeNote(
|
|
692
|
+
const { m, row } = picker.ctx, sh = shareOf(m), scope = scopeNote(row.certified);
|
|
636
693
|
const taken = new Set([...(sh.grants ?? []).filter(g => g.friend === row.author && visibleGrant(g)).map(g => g.target.id), ...sh.pending.filter(p => p.friend === row.author && p.state !== 'denied').map(p => p.target.id)]);
|
|
637
694
|
fill(picker.list, sh.targetsProblem ? h('li', { class: 'error g-empty' }, sh.targetsProblem) : sh.targets === null ? emptyRow('正在读取…')
|
|
638
695
|
: sh.targets.length ? sh.targets.map(target => {
|
|
@@ -777,7 +834,7 @@ function dirRow(c, dir, i) {
|
|
|
777
834
|
h('button', { class: 'btn quiet small', type: 'button', 'aria-label': `删除第 ${i + 1} 行`, disabled: c.busy, onclick: () => { c.dirs.splice(c.dirs.indexOf(dir), 1); c.problem = null; drawForm(); } }, '删除'));
|
|
778
835
|
}
|
|
779
836
|
function drawForm() {
|
|
780
|
-
const c = picker.ctx, { m, row, target } = c, modify = Boolean(c.grant), dirsMode = c.mode === 'dirs', scope = scopeNote(
|
|
837
|
+
const c = picker.ctx, { m, row, target } = c, modify = Boolean(c.grant), dirsMode = c.mode === 'dirs', scope = scopeNote(row.certified);
|
|
781
838
|
const option = (mode, label, note, off) => h('label', { class: `opt${c.mode === mode ? ' on' : ''}${off ? ' off' : ''}` },
|
|
782
839
|
h('input', { type: 'radio', name: 'scope', checked: c.mode === mode, disabled: off || c.busy, onchange: () => { c.mode = mode; c.problem = null; drawForm(); } }),
|
|
783
840
|
h('span', { class: 'g-info' }, h('span', { class: 'g-name' }, label), h('span', { class: 'g-sub' }, note)));
|
|
@@ -930,34 +987,37 @@ function removalBlock(m) {
|
|
|
930
987
|
}
|
|
931
988
|
|
|
932
989
|
function friendTile(m, row) {
|
|
933
|
-
|
|
990
|
+
const count = row.channels.length, pending = row.channels.filter(c => c.status === 'pending').length;
|
|
991
|
+
const meta = row.short ? `@${row.short}` : '密钥未互认';
|
|
992
|
+
return h('li', {}, h('button', { class: 'f-tile', type: 'button', 'aria-label': `${row.title},${STATUS_TEXT[row.status]}${count > 1 ? `,${count} 条连接` : ''}`, onclick: () => openDetail(m, row) },
|
|
934
993
|
avatar(rowAvatar(row), 'sm'),
|
|
935
|
-
h('span', { class: 'f-info' }, h('span', { class: 'f-name truncate' }, row.title),
|
|
994
|
+
h('span', { class: 'f-info' }, h('span', { class: 'f-name truncate' }, row.title),
|
|
995
|
+
h('span', { class: 'f-meta truncate' }, count > 1 ? `${meta} · ${count} 条连接${row.status === 'bound' && pending ? `(${pending} 待互认)` : ''}` : meta)),
|
|
936
996
|
h('span', { class: `badge ${row.status}` }, STATUS_TEXT[row.status])));
|
|
937
997
|
}
|
|
938
998
|
|
|
939
999
|
// 每台电脑一节;只有一台时没有节标题,也不重复画身份卡上已有的状态条和「添加好友」。
|
|
940
|
-
function drawSection({ member: m, title,
|
|
941
|
-
const
|
|
942
|
-
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);
|
|
943
1002
|
// 几台时状态条只在有需要提示的状态(证书有问题、待互认、冲突)时出;只有一台时状态条在身份卡上。
|
|
944
|
-
const attention = summary
|
|
945
|
-
const empty = !m.loaded ? '正在读取…' : '还没有好友。点上方「添加好友」,粘贴对方发来的邀请即可。';
|
|
1003
|
+
const attention = summary.cert !== 'active' || summary.friends.pending || summary.friends.conflict;
|
|
946
1004
|
return h('section', {},
|
|
947
|
-
h('h2', { class: 'sec' }, title ?? '好友',
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
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));
|
|
954
1013
|
}
|
|
955
1014
|
|
|
956
1015
|
const page = h('div', { class: 'page' });
|
|
957
1016
|
function draw() {
|
|
1017
|
+
if (!firstRead) return;
|
|
958
1018
|
const titles = memberTitles(members), sections = friendSections(members, titles);
|
|
959
1019
|
drawn = sections;
|
|
960
|
-
const ids = identities(
|
|
1020
|
+
const ids = identities(members);
|
|
961
1021
|
if (!ids.length) ids.push({ author: null, signer: null, members: [] });
|
|
962
1022
|
fill(page, ids.map(id => drawHero(id, sections, titles)), sections.map(drawSection));
|
|
963
1023
|
if (state.detail) { if (detailRow()) drawDetail(); else closeDialog(); }
|
|
@@ -967,7 +1027,6 @@ function draw() {
|
|
|
967
1027
|
|
|
968
1028
|
picker.dialog.scrim.hidden = true;
|
|
969
1029
|
root.replaceChildren(page, detail.dialog.scrim, invite.dialog.scrim, picker.dialog.scrim, net.dialog.scrim);
|
|
970
|
-
draw();
|
|
971
1030
|
|
|
972
1031
|
coda.header({ text: '好友' }).catch(() => {});
|
|
973
|
-
|
|
1032
|
+
start();
|
package/view/style.css
CHANGED
|
@@ -165,6 +165,10 @@ details[open] > summary { margin-bottom: 10px; }
|
|
|
165
165
|
.g-name { font-weight: 650; }
|
|
166
166
|
.g-sub { font-size: .85em; color: var(--fg3); }
|
|
167
167
|
.g-sub.error { color: var(--red); }
|
|
168
|
+
.g-row.conn { flex-wrap: wrap; align-items: flex-start; }
|
|
169
|
+
.conn-name, .conn-sub { overflow-wrap: anywhere; }
|
|
170
|
+
.conn-body { flex: 1 1 100%; min-width: 0; display: flex; flex-direction: column; gap: 8px; }
|
|
171
|
+
.conn-body .warn-box { margin: 0; }
|
|
168
172
|
.badge.busy { background: color-mix(in srgb, var(--accent) 15%, transparent); color: var(--accent); }
|
|
169
173
|
.badge.busy::before { content: ""; display: inline-block; width: 6px; height: 6px; margin-right: 5px; border-radius: 50%; background: currentColor; animation: pulse 1s ease-in-out infinite; }
|
|
170
174
|
@keyframes pulse { 50% { opacity: .25; } }
|