@coda-console/friends 0.10.1 → 0.12.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 CHANGED
@@ -3,8 +3,9 @@
3
3
  管理本机的 Accord 身份与熟人好友通道(`coda/accord@1`),并提供业务无关的好友网关(`friends/social@1`)。
4
4
 
5
5
  - **本机身份**:身份卡显示本机的展示名、确定性几何纹头像、公钥短码(点按复制),下方状态条给出已互认好友数(有待互认、冲突时提示)与身份、证书状态。没有建身份时显示「未建身份」并提示前往客户端「设置 › 身份」创建。
6
- - **公钥派生的昵称与头像**:本机和好友都没有可编辑的昵称、照片、Emoji 或主题色,统一由 Accord 公钥推出。展示名是 `昵称 #尾号`:昵称按 coda#3040 的算法取公钥 256 位,从最高位起每 7 位一组(至多 36 组),`0–77` 为字母、`78–87` 为数字、`88–127` 为结束符(已有不足 2 位时跳过,够 2 位就结束,读完仍不足 2 位则没有昵称);尾号是公钥按大端整数转成十进制(补零到 78 位)后的末 4 位。解不出昵称时退回公钥短码(`ed25519:` 之后前 8 位),还没有公钥的待互认好友用节点名。头像是由公钥确定性生成的 5×5 几何纹,灰色,换设备也一样;没有合法公钥时取名字首字。算法在 `lib/nickname.mjs`,浏览器与 Node 通用。
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
 
@@ -13,7 +14,7 @@
13
14
  页面不自己选电脑:App 的全局视图里有哪几台,页面就读哪几台,没有机器标签。
14
15
 
15
16
  - **本机身份按公钥显示**:按各台 Accord 的 `spec.author` 分组,同一身份只显示一张身份卡,视图里有不同身份的电脑时每个身份一张。
16
- - **好友按电脑分节**:每台一节,顺序同视图,节标题是电脑名;节里是这台自己的好友通道(好友关系按机器上的通道登记,在 A 上加的好友 B 上不会自动出现),状态条(好友数、证书状态、待互认与冲突)和「添加好友」也在这一节。接受邀请与随后的互认、好友详情里的重新互认、移除,都发给这一节、这一行所在的电脑。没有好友的电脑也占一节;同一个好友在两台上都加了,就在两节里各出现一次,不合并。读不到的电脑(`unreachable`)节里写明原因,不出「添加好友」。
17
+ - **好友按电脑分节**:每台一节,顺序同视图,节标题是电脑名;节里是这台自己的好友通道(好友关系按机器上的通道登记,在 A 上加的好友 B 上不会自动出现),状态条(好友数、证书状态、待互认与冲突)和「添加好友」也在这一节。接受邀请与随后的互认、好友详情里的重新互认、移除,都发给这一节、这一行所在的电脑。没有好友的电脑也占一节;同一个好友在两台上都加了,就在两节里各出现一次,不跨电脑合并(合并只在同一台电脑内按完整公钥进行)。读不到的电脑(`unreachable`)节里写明原因,不出「添加好友」。
17
18
  - **只有一台时**页面不出现节标题,状态条与「添加好友」仍在身份卡上。
18
19
  - **推荐用法**:好友只在一台电脑上加。好友关系登记在那台电脑上,别的电脑不会自动出现这个好友。邀请里的入网码只能用一次,不必在每台上重复加。
19
20
  - **要共享别的电脑**:不必在那台上再加一次好友。加了好友的这台作网关,能把同一空间里别的电脑的远程执行逐台授给好友(见下一节「经网关授出别的机器」)。
@@ -116,11 +117,11 @@ coda self grants.restrict '{"grant":"<授权号>","restrict":{"dirs":[…]}}'
116
117
 
117
118
  ### 代理记录的名字
118
119
 
119
- 名字固定为 `<机器名>@<公钥昵称>#<尾号>`(coda#3040),只由好友的公钥算出,与好友页上的展示名同一套算法(`lib/nickname.mjs`)。
120
+ 名字固定为 `<机器名>@<公钥昵称>#<尾号>`(coda#3040),只由好友的公钥算出,与好友页上的展示名同一套算法(协议包 `@coda-console/protocol/accord`)。
120
121
 
121
122
  - **公钥**:好友表里这位好友存下的作者:绑了个人身份的是个人钥匙,没绑的是机器钥匙;证书过期时仍是存下的那把,名字不跟着变。
122
123
  - **昵称**:与基座的 `AccordNickname` 同一套:32 字节公钥从最高位起每 7 位一组,至多 36 组;0–77 是字母(对 26 取余),78–87 是数字,88–127 是结束(不足 2 位时跳过)。解不出昵称的用公钥前 8 位十六进制。
123
- - **尾号**:完整尾号是公钥按大端无符号整数写成的十进制,补零到 78 位,记在代理记录的 `tail` 标签里。名字里默认写最后 4 位;本机同一 `<机器名>@<昵称>` 的代理记录最后 4 位也相同时,这几条都写到能互相区分的最短位数。
124
+ - **尾号**:完整尾号是公钥按大端无符号整数写成的十进制,补零到 78 位,记在代理记录的 `tail` 标签里。名字里默认写最后 4 位;本机同一 `<机器名>@<昵称>` 的代理记录最后 4 位也相同时,这几条都写到能互相区分的最短位数(协议包的 `disambiguateTails`)。
124
125
  - **会话里的 `target`**:可以写 `机器名@昵称`(不撞名时)或 `机器名@昵称#尾号`(尾号从最后一位往前数的任意位数),这要求基座含 coda#3040;更早的基座只认完整名字或记录地址。
125
126
  - **机器名**:是持有方自己报的,要能放进标题:含控制字符(C0、DEL、C1)、换行、双向控制符,或首尾有空白的,调用方不登记这条授权(不替换、不裁剪:改完的名字可能与别的记录相同);授予时被授出的那台机器的名字不合这条,授予当场失败。`@`、`#`、中间的空格、非 ASCII 文字照旧可以。
126
127
  - **名字的范围**:名字只在当下在场的记录里判唯一。另一位好友可以专门挖一把昵称相同、末 4 位也相同的钥匙(昵称越短越容易),再授出一台同名机器:两条都在时会被要求带更长的尾号;前一条不在时,后一条就是唯一匹配。基座(含 coda#3040)的保护是:会话开场表里列出过的名字,在这个会话里始终指开场时那条记录,那条没了就报不可用。脚本、Skill、定时任务这类跨会话的引用写记录地址(`coda://…/<UUID>`),地址不会换主人。
@@ -184,7 +185,7 @@ coda self grants.restrict '{"grant":"<授权号>","restrict":{"dirs":[…]}}'
184
185
 
185
186
  | 操作 | 作用 |
186
187
  | --- | --- |
187
- | `friends`(读) | 互认的好友(`includePending` 带上待互认与冲突的),只来自 Accord 的好友表:`{ selfAuthor, certState, friends }`,每行的 `title` 是公钥派生的展示名,不读名片记录 |
188
+ | `friends`(读) | 互认的好友(`includePending` 带上待互认与冲突的),只来自 Accord 的好友表:`{ selfAuthor, certState, friends }`,每行的 `title` 是公钥派生的展示名,不读名片记录;指向同一公钥的多条通道合并成一行,顶层 `node`、`peerNode`、`scope` 取代表通道,`channels` 列出各条通道 |
188
189
  | `records`(读) | 按 `kind`、`author`、`id`、`cursor` 读好友分享的记录,不读 `binding` 与 `friends/rpc-` 前缀 |
189
190
  | `publish` | 发布 `{ kind, id, body, audience? }`;同作者同内容不重发,换作者重发 |
190
191
  | `retract` | 撤回一条,或省略 `id` 撤回本机在这个 `kind` 下的全部(含队列里还没发出去的) |
package/lib/avatar.mjs CHANGED
@@ -1,44 +1,13 @@
1
1
  // 好友页头像与 Accord 状态的纯逻辑:确定性几何纹、无公钥时的首字兜底、身份与好友统计。
2
- // 页面(WKWebView)与 Node 单测都直接加载它,所以只用标准 JS 和 ./friends.mjs,不拼 HTML。
2
+ // 页面(WKWebView)直接加载它,Node 单测经测试加载钩子把 coda-view:// 映射到协议包,所以只用标准 JS 和 ./friends.mjs,不拼 HTML。
3
3
 
4
- import { certStateOf, shortAuthor } from './friends.mjs';
4
+ import { identiconPattern, isAuthor, shortAuthor } from 'coda-view://lib/v1/accord.js';
5
+ import { certStateOf } from './friends.mjs';
5
6
 
6
7
  const CONTROL_CHARS = /[\p{Cc}\u2028\u2029]/gu;
7
- const AUTHOR = /^ed25519:([0-9a-f]{64})$/;
8
8
 
9
9
  const clean = text => typeof text === 'string' ? text.replace(CONTROL_CHARS, '').trim() : '';
10
10
 
11
- /** 一串字节的确定性混合(FNV-1a 作种,mulberry32 取数),给几何纹用。 */
12
- function* stream(bytes) {
13
- let seed = 0x811c9dc5;
14
- for (const byte of bytes) seed = Math.imul(seed ^ byte, 0x01000193) >>> 0;
15
- for (;;) {
16
- seed = (seed + 0x6d2b79f5) >>> 0;
17
- let t = seed;
18
- t = Math.imul(t ^ (t >>> 15), t | 1);
19
- t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
20
- yield (t ^ (t >>> 14)) >>> 0;
21
- }
22
- }
23
-
24
- /**
25
- * 5×5 水平轴对称的几何纹(`grid[r][c] === grid[r][4 - c]`):同一个 author 永远同一张,
26
- * 不会全空也不会全满。合法的 `ed25519:<64 位十六进制>` 取公钥字节推导,其余非空串取其字符码兜底。
27
- */
28
- export function identiconPattern(author) {
29
- const text = String(author ?? '');
30
- const hex = AUTHOR.exec(text)?.[1];
31
- const bytes = hex ? Array.from({ length: 32 }, (_, i) => parseInt(hex.slice(i * 2, i * 2 + 2), 16)) : [...text].map(ch => ch.codePointAt(0));
32
- for (const draw of stream(bytes)) {
33
- const grid = Array.from({ length: 5 }, (_, r) => {
34
- const half = [0, 1, 2].map(c => ((draw >>> (r * 3 + c)) & 1) === 1);
35
- return [half[0], half[1], half[2], half[1], half[0]];
36
- });
37
- const active = grid.flat().filter(Boolean).length;
38
- if (active >= 8 && active <= 18) return grid;
39
- }
40
- }
41
-
42
11
  /** 昵称等文本的首字:清掉控制字符与首尾空白后取第一个 Unicode 字符,ASCII 字母转大写,空则给 fallback。 */
43
12
  export function initialChar(text, fallback = '友') {
44
13
  const first = [...clean(text)][0];
@@ -47,7 +16,7 @@ export function initialChar(text, fallback = '友') {
47
16
 
48
17
  /** 决定头像怎么展示,只返回纯数据:合法的 author 画它的几何纹(identicon),否则取 name 的首字(monogram)。 */
49
18
  export function resolveAvatar({ author, name, fallback = '友' } = {}) {
50
- if (AUTHOR.test(author ?? '')) return { kind: 'identicon', grid: identiconPattern(author), author };
19
+ if (isAuthor(author)) return { kind: 'identicon', grid: identiconPattern(author), author };
51
20
  return { kind: 'monogram', text: initialChar(name, fallback) };
52
21
  }
53
22
 
package/lib/friends.mjs CHANGED
@@ -1,8 +1,5 @@
1
1
  // 好友页的纯逻辑:解析邀请、证书状态、把好友表合成列表行。不碰 Coda,也不碰磁盘。
2
- // 页面也加载它,所以只用浏览器和 Node 都有的 API。
3
- import { identityTitle, shortAuthor } from './nickname.mjs';
4
-
5
- export { shortAuthor };
2
+ // 页面也加载它,所以只用浏览器和 Node 都有的 API;公钥的展示名与短码由调用方传入(Node 用 @coda-console/protocol/accord,页面用 coda-view://lib/v1/accord.js)。
6
3
 
7
4
  // 邀请各字段的上限,与 coda/accord@1 的 accept 输入一致。
8
5
  const LIMITS = { control: 500, authKey: 500, linkId: 64, scope: 520, hostname: 100, peerNode: 100, bindingToken: 200 };
@@ -121,12 +118,36 @@ export function certStateOf(accord) {
121
118
  const RANK = { conflict: 0, pending: 1, bound: 2 };
122
119
  export const statusOf = friend => friend.conflict ? 'conflict' : friend.bound ? 'bound' : 'pending';
123
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
+
124
130
  /**
125
- * 好友表(`status.friends`)合成的行:标题是公钥派生的昵称(见 nickname.mjs),没有公钥的待互认项用节点名;
126
- * 按状态(冲突、待互认、已互认)再按更新时间新的在前排序,同样的输入排出同样的顺序。
131
+ * 好友表(`status.friends`)合成的行:同一台电脑上指向同一完整公钥(author)的通道归并成一个好友,通道逐条放在 `channels`(状态、更新时间新的在前);
132
+ * 没有公钥的待互认通道不与任何条目归并,各自成行。好友状态:任一通道密钥冲突即冲突(不隐藏冲突),否则任一已互认即已互认,其余待互认;
133
+ * 顶层 `node`、`peerNode`、`scope` 取代表通道(第一条状态等于好友状态的通道),`updatedAt` 取各通道最大值。
134
+ * 标题是公钥派生的 `昵称 #尾号`(`identityTitles`:同昵称且尾号相撞时向左加位,无昵称的公钥是短码),没有公钥的待互认项用节点名。
135
+ * 撞号要和本人、别的电脑上的好友一起比,所以 `titles`(author → 标题,`identityTitles` 的结果)由调用方算好给出。
136
+ * 好友行按状态(冲突、待互认、已互认)再按更新时间新的在前排序,同样的输入排出同样的顺序。
127
137
  */
128
- export function mergeFriendRows(friends = []) {
129
- const rows = friends.map(friend => ({ node: friend.node, author: friend.author ?? null, short: shortAuthor(friend.author), status: statusOf(friend),
130
- peerNode: friend.peerNode ?? null, scope: friend.scope, updatedAt: friend.updatedAt ?? 0, title: identityTitle(friend.author, friend.node) }));
131
- return rows.sort((a, b) => RANK[a.status] - RANK[b.status] || b.updatedAt - a.updatedAt || (a.node < b.node ? -1 : a.node > b.node ? 1 : 0));
138
+ export function mergeFriendRows(friends = [], { titles, shortAuthor }) {
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));
132
153
  }
package/lib/gateway.mjs CHANGED
@@ -1,7 +1,8 @@
1
1
  // 抽象社交网关:实现 friends/social@1。业务无关——只认 kind、id、body;签名写入与记录查询全部透传 coda/accord@1,
2
2
  // 自己不建第二份目录库,只在 outbox.json 里留还没发出去的 body(同步成功即释放)。所有会写盘、会外发的动作排成一条队,
3
3
  // 发布、撤回、证书变化引起的冲洗互不交叉。
4
- import { mergeFriendRows, statusOf } from './friends.mjs';
4
+ import { identityTitles, shortAuthor } from '@coda-console/protocol/accord';
5
+ import { mergeFriendRows } from './friends.mjs';
5
6
  import { MAX_BODY_BYTES, canSkip, canonicalHash, certStateOf, checkId, checkKind, checkPublish, entryKey, holdState, keyOf, retractKey, invalid, isPending, markRejected, markSynced,
6
7
  nextSeq, queuePublish, queueRetract, readOutbox, withState, writeOutbox } from './outbox.mjs';
7
8
  import { readPolicy, setKindPaused } from './policy.mjs';
@@ -153,8 +154,9 @@ export function createGateway(coda, dir) {
153
154
  const outbox = dir ? await readOutbox(dir) : {}, { pausedKinds } = dir ? await readPolicy(dir) : { pausedKinds: [] };
154
155
  const until = accord?.status?.certificate?.until;
155
156
  const friends = Array.isArray(accord?.status?.friends) ? accord.status.friends : [];
157
+ const rows = mergeFriendRows(friends, { titles: new Map(), shortAuthor });
156
158
  const status = { author, certState, ...(Number.isSafeInteger(until) && until >= 0 ? { certUntil: until } : {}),
157
- friendCount: friends.filter(friend => statusOf(friend) === 'bound').length, pendingCount: Object.values(outbox).filter(isPending).length, pausedKinds };
159
+ friendCount: rows.filter(row => row.status === 'bound').length, pendingCount: Object.values(outbox).filter(isPending).length, pausedKinds };
158
160
  if (!stopped) await coda.put({ kind: SOCIAL, key: 'local' }, { spec: { version: 1 }, status });
159
161
  } catch { /* 状态记录下一次动作会再写 */ }
160
162
  }
@@ -165,7 +167,7 @@ export function createGateway(coda, dir) {
165
167
  async function friends({ includePending = false } = {}) {
166
168
  const { accord, author, certState } = await accordState();
167
169
  const list = Array.isArray(accord?.status?.friends) ? accord.status.friends : [];
168
- return { selfAuthor: author, certState, friends: mergeFriendRows(list).filter(row => includePending || row.status === 'bound') };
170
+ return { selfAuthor: author, certState, friends: mergeFriendRows(list, { titles: identityTitles([author, ...list.map(friend => friend.author)]), shortAuthor }).filter(row => includePending || row.status === 'bound') };
169
171
  }
170
172
 
171
173
  function records({ kind, author, id, limit, cursor, includeAudience }) {
@@ -6,7 +6,7 @@
6
6
  // (`machine`;不看是否已绑定:持有方个人证书过期时这一行是未绑定、机器钥匙仍在,授出的照常可用),受众与正文的 grantee 恰好是本机此刻的作者,正文的 holder 就是作者(verifyGrant)。
7
7
  // - 代理记录:`friend:<持有方机器钥匙前 16 位>:<授权号>`,spec 取授权记录的、名字改成 `<机器名>@<公钥昵称>#<尾号>`(coda#3040),
8
8
  // labels 记持有方机器钥匙、授权号与完整尾号(`tail`)。公钥是好友表这一行存下的作者(证书过期时仍是这把),没有时用机器钥匙;
9
- // 解不出昵称的用公钥前 8 位十六进制。尾号默认最后 4 位,本机去重后留下的同一主干的代理记录最后几位相同时写到能区分的最短位数。
9
+ // 解不出昵称的用公钥前 8 位十六进制。尾号默认最后 4 位,本机去重后留下的同一主干的代理记录最后几位相同时写到能区分的最短位数(disambiguateTails)。
10
10
  // 机器名含控制字符、换行、双向控制符或首尾有空白的授权不登记(verifyGrant 拒收)。
11
11
  // 同一授权在空间里只留一条:候选是与持有方有这样一条通道的机器,机器 ID 最小的那台留下,其余删掉自己这条(没有通道的不留);
12
12
  // 可用性在留下的那台上单独算,不参与选主。授权撤回(目录里这一号已不在,或只剩撤回后的空正文)就删掉;
@@ -27,8 +27,8 @@
27
27
  // - 本机作者变了:旧作者下的代理记录停用,正在等的请求不再处理,要等重新授予。
28
28
  // - 签名只经网关的内部入口(signRpc、retractRpc)。
29
29
  import { isDeepStrictEqual } from 'node:util';
30
- import { nicknameOf, shortAuthor, tailOf } from './nickname.mjs';
31
- import { KIND_ANSWER, KIND_CALL, KIND_GRANT, MAX_WINDOW_MS, PROTOCOL, answerId, callRecord, cancelRecord, isAuthor, rebuildExecInput, requestId,
30
+ import { disambiguateTails, isAuthor, nicknameOf, shortAuthor, tailOf } from '@coda-console/protocol/accord';
31
+ import { KIND_ANSWER, KIND_CALL, KIND_GRANT, MAX_WINDOW_MS, PROTOCOL, answerId, callRecord, cancelRecord, rebuildExecInput, requestId,
32
32
  signerOf, verifyAnswer, verifyGrant } from './rpc-format.mjs';
33
33
  import { KEEP_AFTER_EXPIRY_MS } from './rpc-store.mjs';
34
34
 
@@ -132,16 +132,8 @@ export function createCaller(coda, { store, signRpc, retractRpc, onAccord, pages
132
132
  }
133
133
  // 尾号默认写最后 4 位;本机去重后留下的同一主干、尾号不同的代理记录最后几位也相同时,写到能区分的最短位数。
134
134
  const kept = [...known].filter(([key]) => contests.get(key).keep).map(([, grant]) => grant);
135
- for (const grant of kept) {
136
- let digits = 4;
137
- for (const other of kept) {
138
- if (other.stem !== grant.stem || other.tail === grant.tail) continue;
139
- let same = 0;
140
- while (other.tail[77 - same] === grant.tail[77 - same]) same++;
141
- digits = Math.max(digits, same + 1);
142
- }
143
- grant.spec = { ...grant.spec, name: `${grant.stem}#${grant.tail.slice(-digits)}` };
144
- }
135
+ const tails = disambiguateTails(kept, { minDigits: 4 });
136
+ kept.forEach((grant, i) => { grant.spec = { ...grant.spec, name: `${grant.stem}#${tails[i]}` }; });
145
137
  for (const [key, grant] of known) {
146
138
  if (stopped) return;
147
139
  await register(key, grant, me, mine.get(key), contests.get(key));
@@ -150,8 +142,8 @@ export function createCaller(coda, { store, signRpc, retractRpc, onAccord, pages
150
142
 
151
143
  // 代理记录名的主干 `<机器名>@<公钥昵称>` 与完整尾号:公钥是好友表这一行存下的作者,没有时用机器钥匙。
152
144
  function stemOf(machineName, friend) {
153
- const author = isAuthor(friend.author) ? friend.author : friend.machine, key = Buffer.from(author.slice('ed25519:'.length), 'hex');
154
- return { stem: `${machineName}@${nicknameOf(key) || shortAuthor(author)}`, tail: tailOf(key) };
145
+ const author = isAuthor(friend.author) ? friend.author : friend.machine;
146
+ return { stem: `${machineName}@${nicknameOf(author) || shortAuthor(author)}`, tail: tailOf(author) };
155
147
  }
156
148
 
157
149
  async function retracted({ holder, grant }) {
@@ -4,6 +4,7 @@
4
4
  // 授权的范围(整台机器,或仅限目录的 `restrict`):整台机器的授权正文是 v1,与加范围之前逐字节一样;仅限目录的授权正文是 v2,多一项 `restrict`。
5
5
  // v1 校验严格拒绝未知字段与 v2,所以旧版好友看不到仅限目录的授权(见 README「新旧版本互通」);范围只在网关与目标机强制,好友一侧的只用来展示。
6
6
  import { createHash } from 'node:crypto';
7
+ import { isAuthor } from '@coda-console/protocol/accord';
7
8
 
8
9
  export const KIND_GRANT = 'friends/rpc-grant', KIND_CALL = 'friends/rpc-call', KIND_ANSWER = 'friends/rpc-answer';
9
10
  export const RPC_PREFIX = 'friends/rpc-';
@@ -20,7 +21,6 @@ export const MAX_WINDOW_MS = 11 * MINUTE, FUTURE_SKEW_MS = 2 * MINUTE, CLOCK_BAC
20
21
  // 最大的那条请求也一定发得出取消版本。
21
22
  export const MAX_CALL_BYTES = 56 * 1024, MAX_ANSWER_BYTES = 60 * 1024;
22
23
 
23
- const AUTHOR = /^ed25519:[0-9a-f]{64}$/;
24
24
  const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
25
25
  const REQUEST = /^c[0-9a-f]{32}$/, ANSWER = /^a[0-9a-f]{32}$/, DIGEST = /^[0-9a-f]{64}$/;
26
26
  const STATES = ['running', 'completed', 'failed', 'canceled'];
@@ -31,7 +31,7 @@ const isPlain = value => value !== null && typeof value === 'object' && !Array.i
31
31
  const isTime = value => Number.isSafeInteger(value) && value >= 0;
32
32
  const isText = value => typeof value === 'string';
33
33
 
34
- export const isAuthor = value => isText(value) && AUTHOR.test(value);
34
+ export { isAuthor };
35
35
 
36
36
  // ---- 授权的范围 ----
37
37
 
@@ -1,8 +1,13 @@
1
1
  // 好友页按「视图里的每台电脑」读数据的纯逻辑:成员列表、按身份归组、按电脑分节。不碰 Coda 的 Node API;页面和单测都加载它。
2
2
  // 页面不自己选机器:App 的全局视图里有哪几台,页面就读哪几台。
3
3
 
4
+ import { identityTitles, shortAuthor } from 'coda-view://lib/v1/accord.js';
4
5
  import { certStateOf, mergeFriendRows } from './friends.mjs';
5
6
 
7
+ /** 页面上全部成员的本人 author 与全部好友的 author 一起算的展示名(author → 标题):跨电脑的同昵称同尾号撞号时向左加位。 */
8
+ const authorsOf = members => members.flatMap(m => [m.author, ...m.friends.map(friend => friend.author)]);
9
+ export const memberTitles = members => identityTitles(authorsOf(members));
10
+
6
11
  /** 视图里的成员(去重、保持 coda.machines 的顺序);`unreachable` 是读不到的原因,可达的为 null。 */
7
12
  export function listMembers(coda) {
8
13
  const list = Array.isArray(coda?.machines) ? coda.machines : [coda?.machine];
@@ -25,14 +30,14 @@ export function identities(members) {
25
30
  }
26
31
 
27
32
  /**
28
- * 好友按电脑分节:每个成员一节,顺序同视图;节里是这台自己的好友通道(行的内容见 mergeFriendRows),不同电脑上的同一个好友各出现一次。
33
+ * 好友按电脑分节:每个成员一节,顺序同视图;节里是这台自己的好友通道(行的内容见 mergeFriendRows,标题用 `titles`,默认按页面上所有身份一起算一次),不同电脑上的同一个好友各出现一次。
29
34
  * 只有一个成员时 `title` 为 null(不出节标题);读不到的成员(宿主标了 `unreachable`,或 Accord 读失败 `accordProblem`)`unreachable` 写明原因,没有行。
30
35
  */
31
- export function friendSections(members) {
36
+ export function friendSections(members, titles = memberTitles(members)) {
32
37
  return members.map(member => ({
33
38
  member,
34
39
  title: members.length > 1 ? member.name : null,
35
40
  unreachable: member.unreachable || member.accordProblem || null,
36
- rows: member.unreachable || member.accordProblem ? [] : mergeFriendRows(member.friends),
41
+ rows: member.unreachable || member.accordProblem ? [] : mergeFriendRows(member.friends, { titles, shortAuthor }),
37
42
  }));
38
43
  }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "displayName": "好友",
5
5
  "description": "管理 Accord 身份与熟人好友通道,并提供业务无关的好友网关",
6
6
  "icon": { "symbol": "person.2.fill", "color": "blue" },
7
- "version": "0.10.1",
7
+ "version": "0.12.0",
8
8
  "categories": [
9
9
  "Skills 与工具"
10
10
  ],
@@ -234,5 +234,8 @@
234
234
  "main.mjs",
235
235
  "lib",
236
236
  "view"
237
- ]
237
+ ],
238
+ "dependencies": {
239
+ "@coda-console/protocol": "6.29.0"
240
+ }
238
241
  }
package/view/app.mjs CHANGED
@@ -3,12 +3,12 @@
3
3
  // Accord 的读写(coda/accord@1)都在这里按交互发起;「共享」(授给好友的、好友授给本机的远程执行、最近的操作)只经模块读写各台本机。
4
4
  // 网络节点(friendNodes / removeFriendNode,试用):分节标题行的入口打开抽屉,列出入了网却没完成互认的残留节点,删除由客户端弹确认框;
5
5
  // 移除好友后留在网络里的节点(status.networkRemovals)显示在分节里,用户点「重试」才读、才删,页面不会自己调用。
6
- // 昵称、头像都由 Accord 公钥推出(nickname.mjs、avatar.mjs),没有可编辑的名片;
6
+ // 昵称、头像都由 Accord 公钥推出(WebView 标准库 coda-view://lib/v1/accord.js、avatar.mjs),没有可编辑的名片;
7
7
  // 节点名等外来数据一律用 textContent / DOM API 渲染,从不拼 HTML 字符串。
8
+ import { shortAuthor } from 'coda-view://lib/v1/accord.js';
8
9
  import { accordStatusSummary, resolveAvatar } from '../lib/avatar.mjs';
9
10
  import { checked, parseInvite } from '../lib/friends.mjs';
10
- import { identityTitle } from '../lib/nickname.mjs';
11
- import { friendSections, identities, listMembers } from '../lib/view-members.mjs';
11
+ import { friendSections, identities, listMembers, memberTitles } from '../lib/view-members.mjs';
12
12
  import { fill, h, identiconSvg, svg } from './dom.mjs';
13
13
 
14
14
  const ACCORD = 'coda/accord@1', LOCAL = { kind: ACCORD, id: 'local' };
@@ -66,6 +66,10 @@ function onChange(m, { type, entry }) {
66
66
  m.entry = entry;
67
67
  m.author = entry.spec?.author ?? null;
68
68
  m.friends = entry.status?.friends ?? [];
69
+ for (const one of gone) {
70
+ if (one.m !== m || m.friends.some(f => f.node === one.node)) continue;
71
+ gone.delete(one); state.busy.delete(one.key); state.notice.delete(one.key);
72
+ }
69
73
  m.loaded = true;
70
74
  draw();
71
75
  }
@@ -141,12 +145,12 @@ function statusTiles(m, rows) {
141
145
  conflict || pending ? null : total ? '全部已互认' : '还没有好友')),
142
146
  tile('身份', cert.text, h('span', { class: 'tile-s' }, sub), cert.dot));
143
147
  }
144
- function drawHero(id, sections) {
148
+ function drawHero(id, sections, titles) {
145
149
  const { author, signer } = id, ready = Boolean(signer);
146
150
  const summary = accordStatusSummary(signer?.entry, []);
147
151
  const notice = state.heroNotice.get(author ?? '');
148
152
  const settled = members.every(m => m.loaded || m.unreachable || m.accordProblem);
149
- const name = author ? identityTitle(author) : !settled ? '读取中…' : ready ? '未建身份' : '读不到身份';
153
+ const name = author ? titles.get(author) : !settled ? '读取中…' : ready ? '未建身份' : '读不到身份';
150
154
  const single = !multi && sections[0];
151
155
  return h('section', { class: 'hero' },
152
156
  h('div', { class: 'banner' }, h('div', { class: 'banner-acts' },
@@ -317,27 +321,34 @@ async function submitInvite() {
317
321
 
318
322
  // ---- 好友详情:重新互认、移除都发给这一行所在的电脑 ----
319
323
  // 不同电脑上的 node 可能相同,忙碌与提示按(电脑, node)记。
320
- const sectionRows = m => friendSections([m])[0].rows;
324
+ let drawn = []; // 最近一次 draw 的分节,详情按它找当前行
321
325
 
322
- async function handshake(m, row) {
323
- const key = rowKey(m, row.node);
326
+ async function handshake(m, ch) {
327
+ const key = rowKey(m, ch.node);
324
328
  state.busy.add(key); state.notice.set(key, { kind: 'progress', text: '正在互认…' }); draw();
325
329
  let notice;
326
- try { await coda.call(LOCAL, 'handshake', { node: row.node, ...(row.peerNode ? { peerNode: row.peerNode } : {}) }, via(m)); notice = { kind: 'ok', text: '互认完成' }; }
330
+ try { await coda.call(LOCAL, 'handshake', { node: ch.node, ...(ch.peerNode ? { peerNode: ch.peerNode } : {}) }, via(m)); notice = { kind: 'ok', text: '互认完成' }; }
327
331
  catch (e) { notice = { kind: 'error', text: `互认失败:${e.message}` }; }
328
332
  state.notice.set(key, notice); state.busy.delete(key); draw();
329
333
  }
330
- async function remove(m, row) {
331
- const key = rowKey(m, row.node);
334
+ // 移除已成功、却还在等 Accord 推送确认这条通道消失的连接。
335
+ const gone = new Set();
336
+ async function remove(m, ch) {
337
+ const key = rowKey(m, ch.node);
332
338
  state.busy.add(key); state.notice.set(key, { kind: 'progress', text: '正在移除…' }); draw();
333
339
  let failure = null;
334
- try { await coda.call(LOCAL, 'remove', { node: row.node }, via(m)); }
340
+ try { await coda.call(LOCAL, 'remove', { node: ch.node }, via(m)); }
335
341
  catch (e) { failure = { kind: 'error', text: `移除失败:${e.message}` }; }
336
- state.busy.delete(key);
337
- if (failure) state.notice.set(key, failure);
342
+ if (failure) { state.busy.delete(key); state.notice.set(key, failure); }
338
343
  else {
339
- state.notice.delete(key); state.removing = null;
340
- if (state.detail?.key === key) closeDialog();
344
+ if (state.removing === key) state.removing = null;
345
+ // 只剩这一条连接就没有好友可看了;多条时留着弹层,等 Accord 推送变化后画出剩下的连接(最后一条没了,重画时弹层自己关)。
346
+ const open = detailRow();
347
+ const last = open && state.detail.member === m && open.channels.length === 1 && open.channels[0].node === ch.node;
348
+ // 推送还没把这条通道拿掉时,它保持不可操作,免得对已移除的通道再发请求;推送到了(onChange)再清理。
349
+ if (!last && m.friends.some(f => f.node === ch.node)) gone.add({ key, m, node: ch.node });
350
+ else { state.busy.delete(key); state.notice.delete(key); }
351
+ if (last) closeDialog();
341
352
  }
342
353
  draw();
343
354
  }
@@ -345,30 +356,60 @@ const detail = {
345
356
  dialog: makeDialog({ title: '好友', close: '完成', onClose: () => { state.detail = null; state.removing = null; state.menu = null; closePicker(); } }),
346
357
  card: h('div'),
347
358
  info: h('dl', { class: 'facts' }),
359
+ conns: h('section', { class: 'conns' }),
348
360
  share: h('section', { class: 'share' }),
349
361
  actions: h('div', { class: 'actions split' }),
350
362
  notice: h('div'),
351
363
  };
352
- detail.dialog.body.append(h('div', { class: 'stack' }, detail.card, detail.info, detail.share, detail.actions, detail.notice));
353
- const detailRow = () => state.detail && sectionRows(state.detail.member).find(row => row.node === state.detail.node);
364
+ detail.dialog.body.append(h('div', { class: 'stack' }, detail.card, detail.info, detail.conns, detail.share, detail.actions, detail.notice));
365
+ // 详情按公钥跟着好友走;还没有公钥的待互认通道按节点找,互认成功拿到公钥后改按公钥跟,并读出共享。
366
+ function detailRow() {
367
+ const d = state.detail;
368
+ if (!d) return undefined;
369
+ const rows = drawn.find(section => section.member === d.member).rows;
370
+ if (d.author) return rows.find(row => row.author === d.author);
371
+ const found = rows.find(row => row.channels.some(c => c.node === d.node));
372
+ if (found?.author) { d.author = found.author; loadShare(d.member, found); }
373
+ return found;
374
+ }
354
375
  function openDetail(m, row) {
355
- state.detail = { member: m, node: row.node, key: rowKey(m, row.node) }; state.removing = null; state.menu = null;
376
+ state.detail = { member: m, author: row.author, node: row.node }; state.removing = null; state.menu = null;
356
377
  shareOf(m).opsOpen = false;
357
378
  drawDetail();
358
379
  showDialog(detail.dialog);
359
380
  loadShare(m, row);
360
381
  }
361
382
  function fact(label, ...value) { return [h('dt', {}, label), h('dd', { class: 'selectable' }, ...value)]; }
383
+ const copiedKey = (m, row) => rowKey(m, `copy:${row.author}`);
362
384
  async function copyFriendKey(m, row) {
363
- const key = rowKey(m, row.node);
385
+ const key = copiedKey(m, row);
364
386
  try { await coda.copy(row.author); state.notice.set(key, { kind: 'ok', text: '公钥已复制' }); }
365
387
  catch (e) { state.notice.set(key, { kind: 'error', text: `复制失败:${e.message}` }); }
366
388
  draw();
367
389
  }
390
+ // 多条连接时,逐条列出:通道键、机器、范围只在条与条之间有差别时才写出来,否则分不清。
391
+ function connRow(m, row, ch) {
392
+ const key = rowKey(m, ch.node), busy = state.busy.has(key), notice = state.notice.get(key);
393
+ const machines = new Set(row.channels.map(c => c.machine).filter(Boolean)), scopes = new Set(row.channels.map(c => c.scope));
394
+ 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);
395
+ return h('li', { class: 'g-row conn', 'aria-busy': busy ? 'true' : null },
396
+ 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),
397
+ h('span', { class: `badge ${ch.status}` }, STATUS_TEXT[ch.status]),
398
+ h('div', { class: 'conn-body' },
399
+ ch.status === 'conflict' ? h('p', { class: 'warn-box' }, '对方的密钥与之前互认的不一致,可能换了设备或身份。确认是本人后可「重新互认」,否则建议移除。') : null,
400
+ h('div', { class: 'actions' }, ...(state.removing === key
401
+ ? [h('span', { class: 'secondary' }, '移除这条连接后不再经它同步,确定吗?'), h('span', { class: 'spacer' }),
402
+ h('button', { class: 'btn danger small', type: 'button', disabled: busy, onclick: () => remove(m, ch) }, '确认移除'),
403
+ h('button', { class: 'btn quiet small', type: 'button', onclick: () => { state.removing = null; draw(); } }, '再想想')]
404
+ : [h('button', { class: 'btn quiet small', type: 'button', disabled: busy, onclick: () => handshake(m, ch) }, '重新互认'), h('span', { class: 'spacer' }),
405
+ h('button', { class: 'btn danger small', type: 'button', disabled: busy, onclick: () => { state.removing = key; draw(); } }, '移除…')])),
406
+ notice ? h('p', { class: notice.kind }, notice.text) : null));
407
+ }
368
408
  function drawDetail() {
369
- const row = detailRow(), { member: m, key } = state.detail;
409
+ const row = detailRow(), { member: m } = state.detail;
370
410
  if (!row) return;
371
- const busy = state.busy.has(key), notice = state.notice.get(key);
411
+ const many = row.channels.length > 1, [ch] = row.channels, key = rowKey(m, ch.node);
412
+ const busy = state.busy.has(key), notice = state.notice.get(key), copied = row.author ? state.notice.get(copiedKey(m, row)) : null;
372
413
  detail.dialog.heading.textContent = multi ? `${row.title} · ${m.name}` : row.title;
373
414
  detail.card.replaceChildren(h('div', { class: 'mini-card' }, h('div', { class: 'banner small' }),
374
415
  h('div', { class: 'mini-body row' }, h('span', { class: 'av-ring lg' }, avatar(rowAvatar(row), 'lg')),
@@ -376,19 +417,23 @@ function drawDetail() {
376
417
  h('span', { class: `badge ${row.status}` }, STATUS_TEXT[row.status]))));
377
418
  detail.info.replaceChildren(
378
419
  ...fact('公钥', row.author ? h('span', { class: 'mono key-full' }, row.author) : h('span', { class: 'secondary' }, '密钥未互认'),
379
- row.author ? h('button', { class: 'btn quiet small', type: 'button', onclick: () => copyFriendKey(m, row) }, '复制公钥') : null),
380
- ...fact('节点', h('span', { class: 'mono' }, row.peerNode ?? row.node)),
381
- ...fact('连接', STATUS_TEXT[row.status]));
382
- detail.actions.replaceChildren(...(state.removing === key
420
+ row.author ? h('button', { class: 'btn quiet small', type: 'button', onclick: () => copyFriendKey(m, row) }, '复制公钥') : null,
421
+ copied ? h('span', { class: copied.kind }, copied.text) : null),
422
+ ...(many ? [] : [...fact('节点', h('span', { class: 'mono' }, row.peerNode ?? row.node)), ...fact('连接', STATUS_TEXT[row.status])]));
423
+ detail.conns.hidden = !many;
424
+ detail.conns.replaceChildren(...(many ? [h('h4', { class: 'sub-head' }, '连接', h('span', { class: 'count' }, String(row.channels.length))),
425
+ h('ul', { class: 'glist' }, row.channels.map(c => connRow(m, row, c)))] : []));
426
+ detail.actions.hidden = many;
427
+ detail.actions.replaceChildren(...(many ? [] : state.removing === key
383
428
  ? [h('span', { class: 'secondary' }, '移除后不再同步这位好友,确定吗?'), h('span', { class: 'spacer' }),
384
- h('button', { class: 'btn danger small', type: 'button', disabled: busy, onclick: () => remove(m, row) }, '确认移除'),
429
+ h('button', { class: 'btn danger small', type: 'button', disabled: busy, onclick: () => remove(m, ch) }, '确认移除'),
385
430
  h('button', { class: 'btn quiet small', type: 'button', onclick: () => { state.removing = null; draw(); } }, '再想想')]
386
- : [h('button', { class: 'btn quiet small', type: 'button', disabled: busy, onclick: () => handshake(m, row) }, '重新互认'), h('span', { class: 'spacer' }),
431
+ : [h('button', { class: 'btn quiet small', type: 'button', disabled: busy, onclick: () => handshake(m, ch) }, '重新互认'), h('span', { class: 'spacer' }),
387
432
  h('button', { class: 'btn danger small', type: 'button', disabled: busy, onclick: () => { state.removing = key; draw(); } }, '移除好友…')]));
388
433
  drawShare(m, row);
389
- fill(detail.notice,
434
+ fill(detail.notice, ...(many ? [] : [
390
435
  row.status === 'conflict' ? h('p', { class: 'warn-box' }, '对方的密钥与之前互认的不一致,可能换了设备或身份。确认是本人后可「重新互认」,否则建议移除。') : null,
391
- notice ? h('p', { class: notice.kind }, notice.text) : null);
436
+ notice ? h('p', { class: notice.kind }, notice.text) : null]));
392
437
  }
393
438
 
394
439
  // ---- 共享:授给这位好友的远程执行、好友授给本机的、最近的操作 ----
@@ -407,7 +452,6 @@ const PROXY_STATUS = {
407
452
  // 授出的范围不让选,由对方的身份决定:个人身份授给他名下的机器,机器身份只授给那一台;基座还没给 certified 时不写。
408
453
  const scopeNote = certified => (certified === true ? '授给这个人名下的机器' : certified === false ? '只授给对方那一台机器' : null);
409
454
  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 });
410
- const certifiedOf = (m, row) => m.friends.find(friend => friend.node === row.node)?.certified;
411
455
  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]; };
412
456
  // 已撤的不显示;撤销中的(名下的调用还在取消)显示。
413
457
  // 能改范围的:还在用的授权(已撤销、已失效、目标不在了的不行)。
@@ -560,7 +604,7 @@ function shareRow({ key, name, sub, subKind, scope, badge, busy, menu }) {
560
604
  onclick: e => { e.stopPropagation?.(); item.run(); } }, item.label, item.note ? h('span', { class: 'menu-note' }, item.note) : null))) : null);
561
605
  }
562
606
  function drawShare(m, row) {
563
- const sh = shareOf(m), scope = scopeNote(certifiedOf(m, row));
607
+ const sh = shareOf(m), scope = scopeNote(row.certified);
564
608
  const mine = [
565
609
  ...(sh.grants ?? []).filter(g => g.friend === row.author && visibleGrant(g)).map(g => {
566
610
  const local = sh.revoking.get(g.grant), re = sh.rescoping.get(g.grant), waiting = re?.unknown, failed = local?.error ?? re?.error;
@@ -577,7 +621,7 @@ function drawShare(m, row) {
577
621
  const their = sh.theirs.get(row.author);
578
622
  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'] }));
579
623
  const ops = sh.ops.get(row.author);
580
- const canGrant = row.status === 'bound' && Boolean(row.author) && sh.grants !== null && !sh.corrupt;
624
+ const canGrant = row.channels.some(c => c.status === 'bound') && Boolean(row.author) && sh.grants !== null && !sh.corrupt;
581
625
  fill(detail.share,
582
626
  h('h4', {}, `共享 · ${m.name}`),
583
627
  h('div', {}, h('div', { class: 'sub-head' }, '我授给 TA', h('span', { class: 'spacer' }),
@@ -633,7 +677,7 @@ function closePicker() {
633
677
  function cancelPicker() { if (!picker.ctx?.busy) closePicker(); }
634
678
  function drawPicker() {
635
679
  if (picker.ctx?.stage !== 'list') return;
636
- const { m, row } = picker.ctx, sh = shareOf(m), scope = scopeNote(certifiedOf(m, row));
680
+ const { m, row } = picker.ctx, sh = shareOf(m), scope = scopeNote(row.certified);
637
681
  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)]);
638
682
  fill(picker.list, sh.targetsProblem ? h('li', { class: 'error g-empty' }, sh.targetsProblem) : sh.targets === null ? emptyRow('正在读取…')
639
683
  : sh.targets.length ? sh.targets.map(target => {
@@ -778,7 +822,7 @@ function dirRow(c, dir, i) {
778
822
  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(); } }, '删除'));
779
823
  }
780
824
  function drawForm() {
781
- const c = picker.ctx, { m, row, target } = c, modify = Boolean(c.grant), dirsMode = c.mode === 'dirs', scope = scopeNote(certifiedOf(m, row));
825
+ const c = picker.ctx, { m, row, target } = c, modify = Boolean(c.grant), dirsMode = c.mode === 'dirs', scope = scopeNote(row.certified);
782
826
  const option = (mode, label, note, off) => h('label', { class: `opt${c.mode === mode ? ' on' : ''}${off ? ' off' : ''}` },
783
827
  h('input', { type: 'radio', name: 'scope', checked: c.mode === mode, disabled: off || c.busy, onchange: () => { c.mode = mode; c.problem = null; drawForm(); } }),
784
828
  h('span', { class: 'g-info' }, h('span', { class: 'g-name' }, label), h('span', { class: 'g-sub' }, note)));
@@ -931,9 +975,12 @@ function removalBlock(m) {
931
975
  }
932
976
 
933
977
  function friendTile(m, row) {
934
- return h('li', {}, h('button', { class: 'f-tile', type: 'button', 'aria-label': `${row.title},${STATUS_TEXT[row.status]}`, onclick: () => openDetail(m, row) },
978
+ const count = row.channels.length, pending = row.channels.filter(c => c.status === 'pending').length;
979
+ const meta = row.short ? `@${row.short}` : '密钥未互认';
980
+ 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) },
935
981
  avatar(rowAvatar(row), 'sm'),
936
- h('span', { class: 'f-info' }, h('span', { class: 'f-name truncate' }, row.title), h('span', { class: 'f-meta truncate' }, row.short ? `@${row.short}` : '密钥未互认')),
982
+ h('span', { class: 'f-info' }, h('span', { class: 'f-name truncate' }, row.title),
983
+ h('span', { class: 'f-meta truncate' }, count > 1 ? `${meta} · ${count} 条连接${row.status === 'bound' && pending ? `(${pending} 待互认)` : ''}` : meta)),
937
984
  h('span', { class: `badge ${row.status}` }, STATUS_TEXT[row.status])));
938
985
  }
939
986
 
@@ -956,10 +1003,11 @@ function drawSection({ member: m, title, unreachable, rows }) {
956
1003
 
957
1004
  const page = h('div', { class: 'page' });
958
1005
  function draw() {
959
- const sections = friendSections(members);
1006
+ const titles = memberTitles(members), sections = friendSections(members, titles);
1007
+ drawn = sections;
960
1008
  const ids = identities(live);
961
1009
  if (!ids.length) ids.push({ author: null, signer: null, members: [] });
962
- fill(page, ids.map(id => drawHero(id, sections)), sections.map(drawSection));
1010
+ fill(page, ids.map(id => drawHero(id, sections, titles)), sections.map(drawSection));
963
1011
  if (state.detail) { if (detailRow()) drawDetail(); else closeDialog(); }
964
1012
  drawPicker();
965
1013
  if (netView) drawNet();
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; } }
package/lib/nickname.mjs DELETED
@@ -1,43 +0,0 @@
1
- // 由 Accord 公钥派生展示昵称(算法见 coda#3040):浏览器与 Node 通用,只用纯 JS 与 Uint8Array,不碰 Buffer 或 node:*。
2
-
3
- const AUTHOR = /^ed25519:([0-9a-f]{64})$/;
4
- const GROUPS = 36;
5
-
6
- /** 密钥的短码:`ed25519:` 之后的前 8 位(没有就空串)。 */
7
- export function shortAuthor(author) {
8
- const match = AUTHOR.exec(author ?? '');
9
- return match ? match[1].slice(0, 8) : '';
10
- }
11
-
12
- /**
13
- * 32 字节公钥的昵称:256 位从第 0 字节最高位起每 7 位一组,至多 36 组。0–77 是字母(v % 26),78–87 是数字,
14
- * 88–127 是结束符:已有不足 2 位时跳过,够 2 位就结束。读完仍不足 2 位给空串。
15
- */
16
- export function nicknameOf(key) {
17
- let name = '';
18
- for (let group = 0; group < GROUPS; group++) {
19
- let v = 0;
20
- for (let bit = group * 7; bit < group * 7 + 7; bit++) v = (v << 1) | ((key[bit >> 3] >> (7 - (bit & 7))) & 1);
21
- if (v < 78) name += String.fromCharCode(97 + (v % 26));
22
- else if (v < 88) name += String(v - 78);
23
- else if (name.length >= 2) return name;
24
- }
25
- return name.length >= 2 ? name : '';
26
- }
27
-
28
- /** 32 字节公钥按大端无符号整数转成十进制,左侧补零到 78 位。 */
29
- export const tailOf = key => BigInt(`0x${[...key].map(byte => byte.toString(16).padStart(2, '0')).join('')}`).toString().padStart(78, '0');
30
-
31
- /**
32
- * 好友与本机身份的展示名:合法公钥且解得出昵称给 `昵称 #尾号`(尾号是 tail 的末 4 位);合法公钥解不出昵称给公钥短码;
33
- * 没有合法公钥(如待互认的好友)给 fallbackNode。
34
- */
35
- export function identityTitle(author, fallbackNode = '') {
36
- const hex = AUTHOR.exec(author ?? '')?.[1];
37
- if (hex) {
38
- const key = Uint8Array.from({ length: 32 }, (_, i) => parseInt(hex.slice(i * 2, i * 2 + 2), 16));
39
- const name = nicknameOf(key);
40
- return name ? `${name} #${tailOf(key).slice(-4)}` : shortAuthor(author);
41
- }
42
- return fallbackNode;
43
- }