@coda-console/friends 0.3.1 → 0.5.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 +14 -3
- package/lib/avatar.mjs +86 -0
- package/lib/friends.mjs +13 -0
- package/lib/outbox.mjs +1 -12
- package/lib/view-members.mjs +50 -0
- package/package.json +1 -1
- package/view/app.mjs +463 -243
- package/view/cropper.mjs +106 -0
- package/view/dom.mjs +22 -0
- package/view/style.css +187 -47
- package/lib/view-machine.mjs +0 -42
package/README.md
CHANGED
|
@@ -2,11 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
管理本机的 Accord 身份、个人名片与熟人好友通道(`coda/accord@1`)。
|
|
4
4
|
|
|
5
|
-
-
|
|
6
|
-
-
|
|
7
|
-
-
|
|
5
|
+
- **我的名片**:名片卡显示昵称、头像、主题色与本机公钥短码(点按复制),下方状态条给出已互认好友数(有待互认、冲突时提示)与身份、证书状态。
|
|
6
|
+
- **编辑名片**:头像可选照片、几何纹(按公钥生成)、Emoji 或字标(昵称首字,写进 `emoji`)。照片从本机选图,在弹层里拖动、缩放裁成 512×512,优先编 WebP(不支持时用 JPEG),上传到 `coda-view://blob/content` 后把内容指针写进名片的 `avatar`;只改昵称、颜色不重新上传,换成非照片头像时写 `avatar: null` 清除。名片以公开的 `profile` / `self` 记录发布;本机绑定了代签证书、`profile` 被拒(`forbidden`)时改发可代签的社交名片 `friends/card` / `self`。
|
|
7
|
+
- **添加好友**:点名片上的「添加好友」,粘贴对方给的邀请(JSON、`coda-friend://` 链接或多行 `key=value`),接受后自动尝试互认。暂需由对方提供 friend 角色入网码。
|
|
8
|
+
- **好友列表**:实时跟随好友表变化,宽屏双列;显示好友的照片头像(读不出时退回 Emoji、几何纹或首字,点「刷新」重试)。点好友打开详情:完整公钥、节点名、本机备注、重新互认、二次确认后移除。
|
|
8
9
|
- **社交网关 `friends/social@1`**:业务无关,别的插件(在 `optionalUses` 里声明)经它读好友与好友分享的记录、向好友发布与撤回记录、发私信与动态、实时跟随私信与动态,不必各自处理没有身份、证书过期和重试。
|
|
9
10
|
|
|
11
|
+
## 几台电脑
|
|
12
|
+
|
|
13
|
+
页面不自己选电脑:App 的全局视图里有哪几台,页面就读哪几台,没有机器标签。
|
|
14
|
+
|
|
15
|
+
- **我的名片按身份显示**:按各台 Accord 的 `spec.author` 分组,同一身份只显示一张名片,视图里有不同身份的电脑时每个身份一张。同一身份的几台读到的名片不一样(同步有先后)时取记录版本高的那份,不逐字段比对。改名片只发给这个身份里视图顺序第一台能签名(证书可用)的电脑,页面只确认这一台写入成功,头像也上传到这一台;其他电脑要等同步后才显示新名片(同步见下面的「推荐用法」)。不会向几台都发,两台同时改的冲突不处理。
|
|
16
|
+
- **好友按电脑分节**:每台一节,顺序同视图,节标题是电脑名;节里是这台自己的好友通道(好友关系按机器上的通道登记,在 A 上加的好友 B 上不会自动出现),状态条(好友数、证书状态、待互认与冲突)和「添加好友」也在这一节。接受邀请与随后的互认、好友详情里的备注、重新互认、移除,都发给这一节、这一行所在的电脑。没有好友的电脑也占一节;同一个好友在两台上都加了,就在两节里各出现一次,不合并。读不到的电脑(`unreachable`)节里写明原因,不出「添加好友」。
|
|
17
|
+
- **只有一台时**页面不出现节标题,状态条与「添加好友」仍在名片上。
|
|
18
|
+
- **推荐用法**:好友只在一台电脑上加。好友关系登记在那台电脑上,别的电脑不会自动出现这个好友。邀请里的入网码只能用一次,不必在每台上重复加。
|
|
19
|
+
- **几台电脑绑定同一个身份、又在同一个空间里时**,其他电脑发布的记录由 Coda 在这几台之间同步,好友从加了他的那台读取(见 Coda 仓库 `docs/peers.md`)。这是 Coda 基座的行为,本插件不另外保证,也不检查同步是否已经完成。
|
|
20
|
+
|
|
10
21
|
## 社交网关
|
|
11
22
|
|
|
12
23
|
签名写入与记录查询全部透传 `coda/accord@1`,网关自己没有第二份目录库。本机网关记录(`friends/social@1`,key `local`)的 `status` 给出作者、证书状态(`no_identity` / `needs_cert` / `active`)、好友数、待发数与暂停的 kind。
|
package/lib/avatar.mjs
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// 好友页头像与 Accord 状态的纯逻辑:头像地址、回退链、确定性几何纹、身份与好友统计。
|
|
2
|
+
// 页面(WKWebView)与 Node 单测都直接加载它,所以只用标准 JS 和 ./friends.mjs,不拼 HTML。
|
|
3
|
+
|
|
4
|
+
import { certStateOf, readContentPointer, shortAuthor } from './friends.mjs';
|
|
5
|
+
|
|
6
|
+
/** 头像图片的大小上限,与客户端和后端的 5 MiB 一致。 */
|
|
7
|
+
export const BLOB_MAX_BYTES = 5 * 1024 * 1024;
|
|
8
|
+
|
|
9
|
+
const CONTROL_CHARS = /[\p{Cc}\u2028\u2029]/gu;
|
|
10
|
+
const AUTHOR = /^ed25519:([0-9a-f]{64})$/;
|
|
11
|
+
const MAX_EMOJI = 8;
|
|
12
|
+
|
|
13
|
+
const clean = text => typeof text === 'string' ? text.replace(CONTROL_CHARS, '').trim() : '';
|
|
14
|
+
const machineQuery = machine => typeof machine === 'string' && machine.trim() ? `?coda-machine=${encodeURIComponent(machine.trim())}` : '';
|
|
15
|
+
|
|
16
|
+
/** 头像指针的读取地址:指针不合法(或为空)给 null;多机空间里带上 machine。 */
|
|
17
|
+
export function blobReadUrl(pointer, machine) {
|
|
18
|
+
const p = readContentPointer(pointer);
|
|
19
|
+
return p ? `coda-view://blob/${p.owner}/content/${p.sha256}${machineQuery(machine)}` : null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** 上传头像的地址;多机空间里带上 machine。 */
|
|
23
|
+
export const blobUploadUrl = machine => `coda-view://blob/content${machineQuery(machine)}`;
|
|
24
|
+
|
|
25
|
+
/** 一串字节的确定性混合(FNV-1a 作种,mulberry32 取数),给几何纹用。 */
|
|
26
|
+
function* stream(bytes) {
|
|
27
|
+
let seed = 0x811c9dc5;
|
|
28
|
+
for (const byte of bytes) seed = Math.imul(seed ^ byte, 0x01000193) >>> 0;
|
|
29
|
+
for (;;) {
|
|
30
|
+
seed = (seed + 0x6d2b79f5) >>> 0;
|
|
31
|
+
let t = seed;
|
|
32
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
33
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
34
|
+
yield (t ^ (t >>> 14)) >>> 0;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 5×5 水平轴对称的几何纹(`grid[r][c] === grid[r][4 - c]`):同一个 author 永远同一张,
|
|
40
|
+
* 不会全空也不会全满。合法的 `ed25519:<64 位十六进制>` 取公钥字节推导,其余非空串取其字符码兜底。
|
|
41
|
+
*/
|
|
42
|
+
export function identiconPattern(author) {
|
|
43
|
+
const text = String(author ?? '');
|
|
44
|
+
const hex = AUTHOR.exec(text)?.[1];
|
|
45
|
+
const bytes = hex ? Array.from({ length: 32 }, (_, i) => parseInt(hex.slice(i * 2, i * 2 + 2), 16)) : [...text].map(ch => ch.codePointAt(0));
|
|
46
|
+
for (const draw of stream(bytes)) {
|
|
47
|
+
const grid = Array.from({ length: 5 }, (_, r) => {
|
|
48
|
+
const half = [0, 1, 2].map(c => ((draw >>> (r * 3 + c)) & 1) === 1);
|
|
49
|
+
return [half[0], half[1], half[2], half[1], half[0]];
|
|
50
|
+
});
|
|
51
|
+
const active = grid.flat().filter(Boolean).length;
|
|
52
|
+
if (active >= 8 && active <= 18) return grid;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** 昵称等文本的首字:清掉控制字符与首尾空白后取第一个 Unicode 字符,ASCII 字母转大写,空则给 fallback。 */
|
|
57
|
+
export function initialChar(text, fallback = '友') {
|
|
58
|
+
const first = [...clean(text)][0];
|
|
59
|
+
return first ? (/^[a-z]$/.test(first) ? first.toUpperCase() : first) : fallback;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 决定头像怎么展示,只返回纯数据:
|
|
64
|
+
* 有可读的图片指针且没失败 → photo;否则有 emoji(1–8 字)→ emoji;否则有合法 author(且 prefer 不是 monogram)→ identicon;否则 monogram。
|
|
65
|
+
* photo 带 `fallback`:图片加载失败时直接换用的描述。
|
|
66
|
+
*/
|
|
67
|
+
export function resolveAvatar({ avatar, emoji, author, name, fallback = '友', machine, photoFailed = false, prefer = 'auto' } = {}) {
|
|
68
|
+
const url = photoFailed ? null : blobReadUrl(avatar, machine);
|
|
69
|
+
if (url) return { kind: 'photo', url, fallback: resolveAvatar({ avatar, emoji, author, name, fallback, machine, photoFailed: true, prefer }) };
|
|
70
|
+
const mark = clean(emoji);
|
|
71
|
+
if (mark && [...mark].length <= MAX_EMOJI) return { kind: 'emoji', emoji: mark };
|
|
72
|
+
if (prefer !== 'monogram' && AUTHOR.test(author ?? '')) return { kind: 'identicon', grid: identiconPattern(author), author };
|
|
73
|
+
return { kind: 'monogram', text: initialChar(name, fallback) };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 本机 Accord 身份与证书状态(判定见 friends.mjs 的 certStateOf),加上好友统计。
|
|
78
|
+
*/
|
|
79
|
+
export function accordStatusSummary(entry, rows = []) {
|
|
80
|
+
const certificate = entry?.status?.certificate;
|
|
81
|
+
const author = entry?.spec?.author ?? null;
|
|
82
|
+
const until = [certificate?.until, certificate?.expiresAt].find(value => Number.isFinite(value) && value > 0);
|
|
83
|
+
const count = status => rows.filter(row => row.status === status).length;
|
|
84
|
+
return { cert: certStateOf(entry), author, short: shortAuthor(author), certUntil: until ?? null,
|
|
85
|
+
friends: { total: rows.length, bound: count('bound'), pending: count('pending'), conflict: count('conflict') } };
|
|
86
|
+
}
|
package/lib/friends.mjs
CHANGED
|
@@ -217,6 +217,19 @@ export function shortAuthor(author) {
|
|
|
217
217
|
return match ? match[1].slice(0, 8) : '';
|
|
218
218
|
}
|
|
219
219
|
|
|
220
|
+
/**
|
|
221
|
+
* 本机 Accord 记录(`coda/accord@1` 的 local)的证书状态:状态里写了 `certificate` 的按它判:null 没有身份,
|
|
222
|
+
* `active` 为 true 才是可发布,其余要换证书;旧宿主不写这个键,有作者就当可发布(发布遇 invalid_state 再落到需要证书)。
|
|
223
|
+
*/
|
|
224
|
+
export function certStateOf(accord) {
|
|
225
|
+
const status = accord?.status;
|
|
226
|
+
if (status && typeof status === 'object' && Object.hasOwn(status, 'certificate')) {
|
|
227
|
+
if (status.certificate === null) return 'no_identity';
|
|
228
|
+
return status.certificate?.active === true ? 'active' : 'needs_cert';
|
|
229
|
+
}
|
|
230
|
+
return accord?.spec?.author ? 'active' : 'no_identity';
|
|
231
|
+
}
|
|
232
|
+
|
|
220
233
|
// ---- 好友列表 ----
|
|
221
234
|
|
|
222
235
|
const RANK = { conflict: 0, pending: 1, bound: 2 };
|
package/lib/outbox.mjs
CHANGED
|
@@ -47,18 +47,7 @@ export function checkPublish({ kind, id, body, audience = ['public'] } = {}) {
|
|
|
47
47
|
return { kind, id, body: JSON.parse(text), audience: [...audience] };
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
* 本机 Accord 记录(`coda/accord@1` 的 local)的证书状态:状态里写了 `certificate` 的按它判:null 没有身份,
|
|
52
|
-
* `active` 为 true 才是可发布,其余要换证书;旧宿主不写这个键,有作者就当可发布(发布遇 invalid_state 再落到需要证书)。
|
|
53
|
-
*/
|
|
54
|
-
export function certStateOf(accord) {
|
|
55
|
-
const status = accord?.status;
|
|
56
|
-
if (status && typeof status === 'object' && Object.hasOwn(status, 'certificate')) {
|
|
57
|
-
if (status.certificate === null) return 'no_identity';
|
|
58
|
-
return status.certificate?.active === true ? 'active' : 'needs_cert';
|
|
59
|
-
}
|
|
60
|
-
return accord?.spec?.author ? 'active' : 'no_identity';
|
|
61
|
-
}
|
|
50
|
+
export { certStateOf } from './friends.mjs';
|
|
62
51
|
|
|
63
52
|
const canonical = value => {
|
|
64
53
|
if (Array.isArray(value)) return value.map(canonical);
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// 好友页按「视图里的每台电脑」读数据的纯逻辑:成员列表、按身份合并名片、按电脑分节。不碰 Coda 的 Node API;页面和单测都加载它。
|
|
2
|
+
// 页面不自己选机器:App 的全局视图里有哪几台,页面就读哪几台。
|
|
3
|
+
|
|
4
|
+
import { certStateOf, mergeFriendRows, mergeProfileAndCard, readCardRecord, readProfileRecord } from './friends.mjs';
|
|
5
|
+
|
|
6
|
+
/** 视图里的成员(去重、保持 coda.machines 的顺序);`unreachable` 是读不到的原因,可达的为 null。 */
|
|
7
|
+
export function listMembers(coda) {
|
|
8
|
+
const list = Array.isArray(coda?.machines) ? coda.machines : [coda?.machine];
|
|
9
|
+
const seen = new Set();
|
|
10
|
+
return list.filter(m => typeof m?.id === 'string' && m.id && !seen.has(m.id) && seen.add(m.id))
|
|
11
|
+
.map(m => ({ id: m.id, name: m.name || m.id, unreachable: m.unreachable ? (m.unreachable === true ? '暂时连不上' : String(m.unreachable)) : null }));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// 一个身份的几台电脑里,某种名片记录(profile 或 friends/card)版本最高的那条,记下是哪台读到的;版本相同取视图里靠前的。
|
|
15
|
+
const newest = (members, records, read, author) => members
|
|
16
|
+
.flatMap(m => m[records].map(read).filter(card => card && card.author === author).map(card => ({ ...card, from: m.id })))
|
|
17
|
+
.reduce((best, one) => (best && best.version >= one.version ? best : one), null);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 按 `author` 把读到了的成员分组(没有身份的电脑归在 author 为 null 一组;Accord 没读到的成员不参与),组的顺序是成员第一次出现的顺序。
|
|
21
|
+
* 每组一张名片:几台读到的不一样(同步有先后)时,profile 与 friends/card 各取版本最高的一条,再按 mergeProfileAndCard 合成;
|
|
22
|
+
* `from` 是提供头像那条记录的电脑(头像没写就是提供名字的那条)。不逐字段比对。
|
|
23
|
+
* `signer` 是改名片时发往的那一台:组里第一台能签名(证书可用)的,没有就是组里第一台。
|
|
24
|
+
*/
|
|
25
|
+
export function identities(members) {
|
|
26
|
+
const groups = new Map();
|
|
27
|
+
for (const member of members.filter(m => m.loaded)) {
|
|
28
|
+
if (!groups.has(member.author)) groups.set(member.author, []);
|
|
29
|
+
groups.get(member.author).push(member);
|
|
30
|
+
}
|
|
31
|
+
return [...groups].map(([author, group]) => {
|
|
32
|
+
const profile = newest(group, 'profiles', readProfileRecord, author), card = newest(group, 'cards', readCardRecord, author);
|
|
33
|
+
const merged = mergeProfileAndCard(profile, card);
|
|
34
|
+
const source = [profile, card].find(one => one?.avatarPresent) ?? profile ?? card;
|
|
35
|
+
return { author, members: group, card: merged && { ...merged, from: source.from }, signer: group.find(m => certStateOf(m.entry) === 'active') ?? group[0] };
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 好友按电脑分节:每个成员一节,顺序同视图;节里是这台自己的好友通道(行的内容见 mergeFriendRows),不同电脑上的同一个好友各出现一次。
|
|
41
|
+
* 只有一个成员时 `title` 为 null(不出节标题);读不到的成员(宿主标了 `unreachable`,或 Accord 读失败 `accordProblem`)`unreachable` 写明原因,没有行。
|
|
42
|
+
*/
|
|
43
|
+
export function friendSections(members) {
|
|
44
|
+
return members.map(member => ({
|
|
45
|
+
member,
|
|
46
|
+
title: members.length > 1 ? member.name : null,
|
|
47
|
+
unreachable: member.unreachable || member.accordProblem || null,
|
|
48
|
+
rows: member.unreachable || member.accordProblem ? [] : mergeFriendRows({ friends: member.friends, profiles: member.profiles, cards: member.cards, notes: member.notes }),
|
|
49
|
+
}));
|
|
50
|
+
}
|