dsh-fishpal 0.9.12 → 0.9.13
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/CHANGELOG.md +47 -0
- package/_verify-bottle-route.mjs +59 -0
- package/_verify-bottle-ui-data.mjs +49 -0
- package/_verify-favorite-fix.mjs +44 -0
- package/_verify-global-panel.mjs +49 -0
- package/_verify-history-api.mjs +121 -0
- package/_verify-history-safety.mjs +88 -0
- package/_verify-history-two-halls.mjs +84 -0
- package/_verify-history-ui.mjs +151 -0
- package/_verify-presence.mjs +142 -0
- package/lib/bottle-ack.js +64 -11
- package/lib/bottle-history.js +416 -0
- package/lib/daily.js +102 -4
- package/lib/index.js +417 -17
- package/lib/localserver.js +116 -25
- package/lib/presence.js +240 -0
- package/lib/protocol.js +6 -1
- package/lib/room.js +48 -5
- package/lib/transport.js +74 -1
- package/lib/ui.js +620 -120
- package/package.json +10 -1
- package/tests/run.mjs +13 -0
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 漂流瓶历史记录(2026-11-30 岸的三段话,合起来是这个文件的全部需求):
|
|
3
|
+
*
|
|
4
|
+
* ① 「收到的瓶子肯定是要有历史记录的。回信也是。」
|
|
5
|
+
* ② 「这个历史记录也可以全部删,也可以一条一条删。」
|
|
6
|
+
* ③ 「这两个收藏馆可以住在一起。毕竟一个瓶子你收藏了,然后还可以点开
|
|
7
|
+
* 你回的什么信。如果没回就没有呗。」
|
|
8
|
+
*
|
|
9
|
+
* ★ 所以结构是**一条 = 一个瓶子 + 它的回信**,不是「瓶子一条、回信一条」分开存。
|
|
10
|
+
* 分开存的话,点开一只瓶子还得再去另一张表里配它 —— 而配对键(bottleId)
|
|
11
|
+
* 恰恰是两边都有的,那就没理由分成两张表。
|
|
12
|
+
*
|
|
13
|
+
* ★ 跟其它几个瓶子文件的分工,别混:
|
|
14
|
+
* - `bottle-replies.json` 回信草稿的状态机(draft / approved / dropped),管「还没发 / 发了 / 丢了」
|
|
15
|
+
* - `bottle-seen.json` 去重标记(只记 id,不存正文)
|
|
16
|
+
* - `bottle-history.json` **这个文件**:长期账本。收瓶时建条目,回信时往条目上挂,
|
|
17
|
+
* 可删、可收藏、收藏的不会被「全删」带走
|
|
18
|
+
*
|
|
19
|
+
* ★ 收藏的语义(岸说「防止被误删」):
|
|
20
|
+
* `favorited: true` 的条目,「全部删除」**不会**动它 —— 那是防手滑的锚。
|
|
21
|
+
* 要删它必须一条一条删。全删接口留了 `keepFavorites: false` 的口子给脚本用,
|
|
22
|
+
* 界面上不给。
|
|
23
|
+
*
|
|
24
|
+
* ★ 容量:MAX_ITEMS = 2000。超了从最老的**非收藏**条目开始丢。
|
|
25
|
+
* 不设上限的话,收瓶有一天会把文件撑到几十兆,读一次卡一次。
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import fs from 'node:fs';
|
|
29
|
+
import path from 'node:path';
|
|
30
|
+
|
|
31
|
+
const FILE = 'bottle-history.json';
|
|
32
|
+
const MAX_ITEMS = 2000;
|
|
33
|
+
|
|
34
|
+
function historyFile(dataDir) {
|
|
35
|
+
return path.join(dataDir, FILE);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function blank() {
|
|
39
|
+
return { schemaVersion: 2, items: [] };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 读整个账本。文件不存在 → 空账本(正常,第一次用);**文件坏了 → 先留证据**。
|
|
44
|
+
*
|
|
45
|
+
* ★ 2026-12-01 探针抓到的真 bug:以前坏了就直接退回空账本,下一次写就把
|
|
46
|
+
* 那份坏文件**覆盖掉了** —— 老数据彻底没救。实测:
|
|
47
|
+
* 放两只老瓶子 → 把文件截断成半个 JSON → 再写一只新的 →
|
|
48
|
+
* 磁盘上只剩那一只新的,两只老的连影子都没有。
|
|
49
|
+
* 现在坏了先改名成 `.corrupt-<时间戳>` 留档(只留最近 3 份),
|
|
50
|
+
* 至少还有机会手工捞回来。**备份是覆盖不是跳过** —— 用时间戳取名,
|
|
51
|
+
* 每次坏都能留一份,靠 MAX_CORRUPT 数量控制而不是靠文件已存在。
|
|
52
|
+
*/
|
|
53
|
+
const MAX_CORRUPT = 3;
|
|
54
|
+
|
|
55
|
+
function quarantineCorrupt(dataDir) {
|
|
56
|
+
const f = historyFile(dataDir);
|
|
57
|
+
try {
|
|
58
|
+
if (!fs.existsSync(f)) return null;
|
|
59
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
60
|
+
const dest = `${f}.corrupt-${stamp}`;
|
|
61
|
+
fs.renameSync(f, dest);
|
|
62
|
+
// 只留最近 MAX_CORRUPT 份 —— 攒多了会把目录塞满
|
|
63
|
+
try {
|
|
64
|
+
const olds = fs
|
|
65
|
+
.readdirSync(dataDir)
|
|
66
|
+
.filter((n) => n.startsWith(FILE + '.corrupt-'))
|
|
67
|
+
.sort();
|
|
68
|
+
for (const n of olds.slice(0, Math.max(0, olds.length - MAX_CORRUPT))) {
|
|
69
|
+
try {
|
|
70
|
+
fs.unlinkSync(path.join(dataDir, n));
|
|
71
|
+
} catch {
|
|
72
|
+
/* 清不掉就算了,留档不该影响主流程 */
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
} catch {
|
|
76
|
+
/* 列目录失败也不影响「已经把坏文件挪走」这件事 */
|
|
77
|
+
}
|
|
78
|
+
return dest;
|
|
79
|
+
} catch {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function loadHistory(dataDir) {
|
|
85
|
+
try {
|
|
86
|
+
const raw = fs.readFileSync(historyFile(dataDir), 'utf8');
|
|
87
|
+
const j = JSON.parse(raw);
|
|
88
|
+
if (j && Array.isArray(j.items)) return j;
|
|
89
|
+
// ★ 结构不对(items 不是数组)也算坏:比如被别的程序写成 {}
|
|
90
|
+
quarantineCorrupt(dataDir);
|
|
91
|
+
} catch (e) {
|
|
92
|
+
// ENOENT = 还没建过,正常;其它(截断 / 编码坏)都是真坏
|
|
93
|
+
if (e && e.code !== 'ENOENT') quarantineCorrupt(dataDir);
|
|
94
|
+
}
|
|
95
|
+
return blank();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function saveHistory(dataDir, h) {
|
|
99
|
+
const f = historyFile(dataDir);
|
|
100
|
+
const tmp = `${f}.tmp`;
|
|
101
|
+
fs.writeFileSync(tmp, JSON.stringify(h, null, 2), 'utf8');
|
|
102
|
+
// ★ 2026-12-01 实测:全量跑测试时这条 rename 偶发失败一次(单独跑从不失败)。
|
|
103
|
+
// Windows 上 `renameSync` 覆盖已存在的文件,如果目标正被杀毒 / 索引器
|
|
104
|
+
// 短暂打开,会抛 EPERM / EBUSY —— 不是磁盘坏了,是它正在被看一眼。
|
|
105
|
+
// 不重试的话,收藏 / 删一条 / 全删都会偶发「报没办成,其实下一次就好了」。
|
|
106
|
+
// 重试窗口很短(几次几毫秒),不会把真错误拖成卡死。
|
|
107
|
+
let lastErr = null;
|
|
108
|
+
for (let i = 0; i < 8; i++) {
|
|
109
|
+
try {
|
|
110
|
+
fs.renameSync(tmp, f);
|
|
111
|
+
return true;
|
|
112
|
+
} catch (e) {
|
|
113
|
+
lastErr = e;
|
|
114
|
+
const code = e && e.code;
|
|
115
|
+
if (code !== 'EPERM' && code !== 'EBUSY' && code !== 'EACCES') throw e;
|
|
116
|
+
const until = Date.now() + 5;
|
|
117
|
+
while (Date.now() < until) {
|
|
118
|
+
/* 重试窗口里短暂让出一拍 */
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
throw lastErr;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** 条目的稳定 id —— 一个瓶子只有一条。 */
|
|
126
|
+
export function itemId(bottleId) {
|
|
127
|
+
return String(bottleId || '').trim();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 正文存多少。
|
|
132
|
+
* ★ 2026-12-01 探针抓到的:以前一个字节都不截。**发瓶子的那边限 500 字,
|
|
133
|
+
* 收瓶子的这边没限** —— 对面伪造一个 20 万字的 text,一条就写出 200 KB。
|
|
134
|
+
* 2000 条上限一填满,文件能到 400 MB,读一次卡一次。收端必须自己兜。
|
|
135
|
+
* 2000 字 = 发送上限的 4 倍,正常瓶子永远不会被截到。
|
|
136
|
+
*/
|
|
137
|
+
const MAX_TEXT = 2000;
|
|
138
|
+
function clipText(v) {
|
|
139
|
+
const s = String(v ?? '');
|
|
140
|
+
return s.length > MAX_TEXT ? s.slice(0, MAX_TEXT) : s;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** 时间戳兜底:`Number(at) || Date.now()` 会把「真的是 0」变成「现在」。 */
|
|
144
|
+
function atOf(v, fallback) {
|
|
145
|
+
const n = Number(v);
|
|
146
|
+
return Number.isFinite(n) && n > 0 ? n : fallback;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 方向:`in` = 捡到的,`out` = 自己扔出去的。
|
|
151
|
+
*
|
|
152
|
+
* ★ 2026-12-01 岸:「丢过的瓶子,也是要放在一个专门存瓶子的地方的。」
|
|
153
|
+
* 「就是有两个展览馆,一个是收的瓶子,一个是丢的瓶子。」
|
|
154
|
+
*
|
|
155
|
+
* 所以一个账本、两个馆 —— 不是两个文件。理由跟当初「瓶子 + 回信合成一条」
|
|
156
|
+
* 一样:一只瓶子只有一行,去掉两个文件的同步问题。
|
|
157
|
+
*
|
|
158
|
+
* ★ 老条目没有方向字段时**一律当 `in`** —— 在这个字段存在之前,
|
|
159
|
+
* 账本里只可能记过收进来的(发出去那条路当时根本没写账)。
|
|
160
|
+
* 这是迁移,不是猜测。
|
|
161
|
+
*/
|
|
162
|
+
export function dirOf(x) {
|
|
163
|
+
return x && x.direction === 'out' ? 'out' : 'in';
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** 方向过滤:不传 = 全都;传 'in'/'out' = 只要那一边。 */
|
|
167
|
+
function matchDir(x, direction) {
|
|
168
|
+
if (direction !== 'in' && direction !== 'out') return true;
|
|
169
|
+
return dirOf(x) === direction;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 记 / 更新一条账目(收的、发的都走这一个)。
|
|
174
|
+
*
|
|
175
|
+
* ★ 幂等:同一个 bottleId 再来一次只更新正文和时间,不产生第二条。
|
|
176
|
+
* **不动 favorited,也不动 reply** —— 重复收到不能把收藏或回信冲掉。
|
|
177
|
+
* ★ `direction` 只在**新建**时写死:已经存在的条目不会因为一次调用被改方向
|
|
178
|
+
* (比如一只发出去的瓶子被谁捡回来自己收到,那是另一只,id 也不同)。
|
|
179
|
+
*/
|
|
180
|
+
function upsertItem(
|
|
181
|
+
dataDir,
|
|
182
|
+
{ bottleId, text = '', nick = '', peerFp = '', at = Date.now(), direction = 'in' } = {}
|
|
183
|
+
) {
|
|
184
|
+
const bid = itemId(bottleId);
|
|
185
|
+
if (!bid) return { ok: false, error: '没有 bottleId' };
|
|
186
|
+
const h = loadHistory(dataDir);
|
|
187
|
+
const hit = h.items.find((x) => x && x.id === bid);
|
|
188
|
+
const t = atOf(at, Date.now());
|
|
189
|
+
let created = false;
|
|
190
|
+
if (hit) {
|
|
191
|
+
// ★★ 2026-12-01 抓到的真 bug(岸:「如果收到了回信,也是附在那个瓶子那边」)★★
|
|
192
|
+
// 回信的 `bottle_id` 跟我丢出去那只**是同一个 id**
|
|
193
|
+
// (bottle.js:1005 里 `bottle_id` 和 `reply_to_bottle_id` 填的是同一个 key)。
|
|
194
|
+
// 所以「有人回了我丢的瓶子」在账本里会命中我自己那条 out 记录。
|
|
195
|
+
// 以前走的是普通更新分支 —— 结果**我丢出去的正文被对方的回信覆盖了**,
|
|
196
|
+
// 署名还被我改成了对方。实测:
|
|
197
|
+
// 发:"我丢出去的那只瓶子" → 收回信后 → "别人回我的话",nick=陌生人
|
|
198
|
+
// 这不是显示问题,是**账本被写坏了**:我写的东西没了。
|
|
199
|
+
// ★ 现在的规矩:out 的记录,收到 in 的回信时**只挂 gotReply,一个字段都不动**。
|
|
200
|
+
const hitDir = dirOf(hit);
|
|
201
|
+
const cameDir = direction === 'out' ? 'out' : 'in';
|
|
202
|
+
if (hitDir === 'out' && cameDir === 'in') {
|
|
203
|
+
hit.gotReply = {
|
|
204
|
+
text: clipText(text),
|
|
205
|
+
at: t,
|
|
206
|
+
nick: String(nick || '').slice(0, 80),
|
|
207
|
+
peerFp: String(peerFp || ''),
|
|
208
|
+
};
|
|
209
|
+
hit.lastAt = t;
|
|
210
|
+
// ★ 不动 text / nick / direction / count —— 那些是「我发的那只」的事实
|
|
211
|
+
} else {
|
|
212
|
+
hit.text = clipText(text ?? hit.text ?? '');
|
|
213
|
+
if (nick) hit.nick = String(nick).slice(0, 80);
|
|
214
|
+
if (peerFp) hit.peerFp = String(peerFp);
|
|
215
|
+
hit.lastAt = t;
|
|
216
|
+
hit.count = (Number(hit.count) || 1) + 1;
|
|
217
|
+
}
|
|
218
|
+
} else {
|
|
219
|
+
created = true;
|
|
220
|
+
h.items.push({
|
|
221
|
+
id: bid,
|
|
222
|
+
bottleId: bid,
|
|
223
|
+
direction: direction === 'out' ? 'out' : 'in',
|
|
224
|
+
text: clipText(text),
|
|
225
|
+
nick: String(nick || '').slice(0, 80),
|
|
226
|
+
peerFp: String(peerFp || ''),
|
|
227
|
+
reply: null, // ← 还没回。岸原话:「如果没回就没有呗」
|
|
228
|
+
favorited: false,
|
|
229
|
+
firstAt: t,
|
|
230
|
+
lastAt: t,
|
|
231
|
+
count: 1,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
if (h.items.length > MAX_ITEMS) {
|
|
235
|
+
const over = h.items.length - MAX_ITEMS;
|
|
236
|
+
const victims = h.items
|
|
237
|
+
.filter((x) => x && !x.favorited)
|
|
238
|
+
.sort((a, b) => (Number(a.lastAt) || 0) - (Number(b.lastAt) || 0))
|
|
239
|
+
.slice(0, over);
|
|
240
|
+
const drop = new Set(victims.map((x) => x.id));
|
|
241
|
+
h.items = h.items.filter((x) => !drop.has(x.id));
|
|
242
|
+
// ★ 2026-12-01 探针抓到的:收藏塞满 2000 条时,新进来的那条(默认没收藏)
|
|
243
|
+
// 会被**自己**顶掉 —— 而函数照样返回 ok,等于告诉调用方「记上了」,
|
|
244
|
+
// 其实磁盘上根本没有它。**这正是「报成功但什么都没发生」。**
|
|
245
|
+
// 现在如实报失败,并且把裁剪后的状态存下去(别留一个超限文件)。
|
|
246
|
+
if (drop.has(bid)) {
|
|
247
|
+
try {
|
|
248
|
+
saveHistory(dataDir, h);
|
|
249
|
+
} catch {
|
|
250
|
+
/* 存不下就算了,反正这条本来就没记上 */
|
|
251
|
+
}
|
|
252
|
+
return {
|
|
253
|
+
ok: false,
|
|
254
|
+
error: `历史满了(上限 ${MAX_ITEMS} 条,收藏的不会自动清),这一条没记上`,
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (!saveHistory(dataDir, h)) return { ok: false, error: '历史存不下(磁盘或权限)' };
|
|
259
|
+
return { ok: true, id: bid, created, direction };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** 记一只「收到的瓶子」。 */
|
|
263
|
+
export function recordBottle(dataDir, opts = {}) {
|
|
264
|
+
return upsertItem(dataDir, { ...opts, direction: 'in' });
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* 记一只「我丢出去的瓶子」。
|
|
269
|
+
*
|
|
270
|
+
* ★ 挂在 publish **成功之后**,跟回信同一个规矩:没真发出去的不该进「丢过的」。
|
|
271
|
+
* ★ 循环广播会把同一只发好几遍 —— 这里靠 bottleId 幂等,**一只瓶子只有一条**,
|
|
272
|
+
* 只把 count 涨上去(就是「这场广播总共发了几遍」)。
|
|
273
|
+
*/
|
|
274
|
+
export function recordSent(dataDir, { bottleId, text = '', at = Date.now() } = {}) {
|
|
275
|
+
return upsertItem(dataDir, { bottleId, text, at, direction: 'out' });
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* 往某只瓶子挂上「我回的信」。
|
|
280
|
+
* ★ 没有对应条目时**自动建一条**(正文可能已经不在隔离区了,但回信还在)——
|
|
281
|
+
* 账本不能因为缺了前半截就把回信丢了。
|
|
282
|
+
* ★ 回信只挂一次;再挂是覆盖(改过的回信以最后一次为准)。
|
|
283
|
+
*/
|
|
284
|
+
export function attachReply(dataDir, { bottleId, text = '', at = Date.now(), status = 'approved' } = {}) {
|
|
285
|
+
const bid = itemId(bottleId);
|
|
286
|
+
if (!bid) return { ok: false, error: '没有 bottleId' };
|
|
287
|
+
const body = clipText(String(text ?? '').trim());
|
|
288
|
+
if (!body) return { ok: false, error: '回信正文是空的' };
|
|
289
|
+
const h = loadHistory(dataDir);
|
|
290
|
+
const t = atOf(at, Date.now());
|
|
291
|
+
let hit = h.items.find((x) => x && x.id === bid);
|
|
292
|
+
let created = false;
|
|
293
|
+
if (!hit) {
|
|
294
|
+
created = true;
|
|
295
|
+
hit = {
|
|
296
|
+
id: bid,
|
|
297
|
+
bottleId: bid,
|
|
298
|
+
text: '',
|
|
299
|
+
nick: '',
|
|
300
|
+
peerFp: '',
|
|
301
|
+
reply: null,
|
|
302
|
+
favorited: false,
|
|
303
|
+
firstAt: t,
|
|
304
|
+
lastAt: t,
|
|
305
|
+
count: 1,
|
|
306
|
+
};
|
|
307
|
+
h.items.push(hit);
|
|
308
|
+
}
|
|
309
|
+
hit.reply = { text: body, at: t, status: String(status || 'approved') };
|
|
310
|
+
hit.lastAt = t;
|
|
311
|
+
// ★ 自建条目也得守上限,理由跟 recordBottle 里那段一样(收藏塞满时不能骗 ok)。
|
|
312
|
+
if (h.items.length > MAX_ITEMS) {
|
|
313
|
+
const over = h.items.length - MAX_ITEMS;
|
|
314
|
+
const victims = h.items
|
|
315
|
+
.filter((x) => x && !x.favorited && x.id !== bid)
|
|
316
|
+
.sort((a, b) => (Number(a.lastAt) || 0) - (Number(b.lastAt) || 0))
|
|
317
|
+
.slice(0, over);
|
|
318
|
+
const drop = new Set(victims.map((x) => x.id));
|
|
319
|
+
h.items = h.items.filter((x) => !drop.has(x.id));
|
|
320
|
+
if (created && h.items.length > MAX_ITEMS) {
|
|
321
|
+
// 只剩它自己还得裁 —— 说明满的全是收藏,这条也留不下
|
|
322
|
+
try {
|
|
323
|
+
saveHistory(dataDir, h);
|
|
324
|
+
} catch {
|
|
325
|
+
/* 存不下就算了 */
|
|
326
|
+
}
|
|
327
|
+
return { ok: false, error: `历史满了(上限 ${MAX_ITEMS} 条,收藏的不会自动清),这封回信没记上` };
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (!saveHistory(dataDir, h)) return { ok: false, error: '存不下' };
|
|
331
|
+
return { ok: true, id: bid };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* 列表:新的在前。
|
|
336
|
+
* - `favoritedOnly` 只看收藏
|
|
337
|
+
* - `direction` 传 'in' / 'out' 只看那一个馆;不传 = 两个馆合在一起(按时间排)
|
|
338
|
+
*/
|
|
339
|
+
export function listHistory(dataDir, { favoritedOnly = false, direction = '' } = {}) {
|
|
340
|
+
return loadHistory(dataDir)
|
|
341
|
+
.items.filter((x) => x && typeof x.id === 'string')
|
|
342
|
+
.filter((x) => matchDir(x, direction))
|
|
343
|
+
.filter((x) => !favoritedOnly || x.favorited === true)
|
|
344
|
+
.slice()
|
|
345
|
+
.sort((a, b) => (Number(b.lastAt) || 0) - (Number(a.lastAt) || 0));
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** 收藏 / 取消收藏。收藏的条目全删时保留。 */
|
|
349
|
+
export function setFavorite(dataDir, id, on = true) {
|
|
350
|
+
const h = loadHistory(dataDir);
|
|
351
|
+
const hit = h.items.find((x) => x && x.id === String(id || ''));
|
|
352
|
+
if (!hit) return { ok: false, error: '没有这一条历史' };
|
|
353
|
+
hit.favorited = Boolean(on);
|
|
354
|
+
hit.favAt = hit.favorited ? Date.now() : 0;
|
|
355
|
+
if (!saveHistory(dataDir, h)) return { ok: false, error: '存不下' };
|
|
356
|
+
return { ok: true, id: hit.id, favorited: hit.favorited };
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** 删一条。**收藏的也删** —— 用户在这一条上按了删除,就是明确要删它。 */
|
|
360
|
+
export function deleteItem(dataDir, id) {
|
|
361
|
+
const h = loadHistory(dataDir);
|
|
362
|
+
const before = h.items.length;
|
|
363
|
+
h.items = h.items.filter((x) => x && x.id !== String(id || ''));
|
|
364
|
+
if (h.items.length === before) return { ok: false, error: '没有这一条历史' };
|
|
365
|
+
if (!saveHistory(dataDir, h)) return { ok: false, error: '存不下' };
|
|
366
|
+
return { ok: true, removed: before - h.items.length };
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* 全删。
|
|
371
|
+
* ★ 默认 `keepFavorites: true` —— 收藏的是防误删的锚,不该被一次手滑带走。
|
|
372
|
+
* 返回里把「还剩几条(都是收藏的)」说清楚,界面上要显示。
|
|
373
|
+
* ★ `direction` 传了 = **只清那一个馆**。清「丢过的」不能顺手把「收到的」也清了 ——
|
|
374
|
+
* 两个馆在界面上是两个按钮,语义必须分开。
|
|
375
|
+
*/
|
|
376
|
+
export function clearHistory(dataDir, { keepFavorites = true, direction = '' } = {}) {
|
|
377
|
+
const h = loadHistory(dataDir);
|
|
378
|
+
const before = h.items.length;
|
|
379
|
+
h.items = h.items.filter((x) => {
|
|
380
|
+
if (!x) return false;
|
|
381
|
+
if (!matchDir(x, direction)) return true; // 不在这次要清的范围内
|
|
382
|
+
if (keepFavorites && x.favorited === true) return true;
|
|
383
|
+
return false;
|
|
384
|
+
});
|
|
385
|
+
const after = h.items.length;
|
|
386
|
+
if (!saveHistory(dataDir, h)) return { ok: false, error: '存不下' };
|
|
387
|
+
return { ok: true, removed: before - after, kept: after, keptFavorites: keepFavorites, direction };
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* 统计,给界面显示条数用。
|
|
392
|
+
* ★ 分方向给 —— 两个馆各显示各的条数;`total` 等老字段保留为**合计**,
|
|
393
|
+
* 免得已经写好的界面 / 测试一次性全挂。
|
|
394
|
+
*/
|
|
395
|
+
export function stats(dataDir) {
|
|
396
|
+
const items = loadHistory(dataDir).items.filter((x) => x && typeof x.id === 'string');
|
|
397
|
+
const replyOk = (x) => Boolean(x.reply && String(x.reply.text || '').trim());
|
|
398
|
+
const count = (list) => ({
|
|
399
|
+
total: list.length,
|
|
400
|
+
replied: list.filter(replyOk).length,
|
|
401
|
+
unreplied: list.filter((x) => !replyOk(x)).length,
|
|
402
|
+
favorited: list.filter((x) => x.favorited === true).length,
|
|
403
|
+
});
|
|
404
|
+
const ins = items.filter((x) => dirOf(x) === 'in');
|
|
405
|
+
const outs = items.filter((x) => dirOf(x) === 'out');
|
|
406
|
+
return {
|
|
407
|
+
// 老字段:全部合计
|
|
408
|
+
total: items.length,
|
|
409
|
+
replied: items.filter(replyOk).length,
|
|
410
|
+
unreplied: items.filter((x) => !replyOk(x)).length,
|
|
411
|
+
favorited: items.filter((x) => x.favorited === true).length,
|
|
412
|
+
// 新字段:两个馆分开
|
|
413
|
+
in: count(ins),
|
|
414
|
+
out: count(outs),
|
|
415
|
+
};
|
|
416
|
+
}
|
package/lib/daily.js
CHANGED
|
@@ -10,7 +10,88 @@
|
|
|
10
10
|
* ── 一条硬规矩
|
|
11
11
|
* 日报是**统计 + 摘句**,不是鱼的日记。它只搬运已经发生的事,
|
|
12
12
|
* 一个字都不替鱼发挥。想写心得是鱼的事(它有 diary 工具),别混进来。
|
|
13
|
+
*
|
|
14
|
+
* ── 2026-12-01 补(岸:「就是那个鱼对于今天的社交给用户发一段话呀」)
|
|
15
|
+
* 统计底下要有一段**鱼自己写的**。上面那条规矩不变:统计仍然不算、
|
|
16
|
+
* 不发挥;鱼写的那段单独存、单独显示,两块不混。
|
|
17
|
+
* 存成 `daily-notes.json`,按日期一个键。
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import fs from 'node:fs';
|
|
21
|
+
import path from 'node:path';
|
|
22
|
+
|
|
23
|
+
function notesFile(dataDir) {
|
|
24
|
+
return path.join(String(dataDir || '.'), 'daily-notes.json');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 全部笔记 `{ 'YYYY-MM-DD': { text, at } }`;读不出来就当空 */
|
|
28
|
+
export function loadNotes(dataDir) {
|
|
29
|
+
try {
|
|
30
|
+
const raw = fs.readFileSync(notesFile(dataDir), 'utf8');
|
|
31
|
+
const p = JSON.parse(raw);
|
|
32
|
+
if (!p || typeof p !== 'object' || Array.isArray(p)) return {};
|
|
33
|
+
return p;
|
|
34
|
+
} catch {
|
|
35
|
+
return {};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 存某一天的鱼话。
|
|
41
|
+
* ★ 同一天再写就是**覆盖**(鱼后来想改口,不该留两条互相打脸)。
|
|
42
|
+
* ★ 先写 `.tmp` 再 rename —— 写一半断电时不会留下半个 JSON。
|
|
13
43
|
*/
|
|
44
|
+
export function saveNote(dataDir, day, text) {
|
|
45
|
+
const key = String(day || '').trim();
|
|
46
|
+
const body = String(text ?? '').trim();
|
|
47
|
+
if (!key) return { ok: false, error: '缺日期' };
|
|
48
|
+
if (!body) return { ok: false, error: '是空的' };
|
|
49
|
+
const all = loadNotes(dataDir);
|
|
50
|
+
all[key] = { text: body, at: Date.now() };
|
|
51
|
+
try {
|
|
52
|
+
const tmp = notesFile(dataDir) + '.tmp';
|
|
53
|
+
fs.writeFileSync(tmp, JSON.stringify(all, null, 2), { encoding: 'utf8' });
|
|
54
|
+
fs.renameSync(tmp, notesFile(dataDir));
|
|
55
|
+
return { ok: true, day: key };
|
|
56
|
+
} catch (e) {
|
|
57
|
+
return { ok: false, error: String((e && e.message) || e) };
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** 某一天鱼写的那段;没有就是 null */
|
|
62
|
+
export function noteOf(dataDir, day) {
|
|
63
|
+
const n = loadNotes(dataDir)[String(day || '')];
|
|
64
|
+
return n && typeof n.text === 'string' && n.text ? n : null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 「哪天已经提醒过鱼写了」—— 存成一个保留键,跟笔记本身放同一个文件。
|
|
69
|
+
* ★ 为什么必须持久:定时器在内存里记「今天投过」不够。电脑一重启,
|
|
70
|
+
* 内存清空,启动补漏就会把**昨天**再补一次 —— 一天两条提醒,很烦。
|
|
71
|
+
* ★ 保留键用 `__prompted`(两个下划线开头),日期键是 YYYY-MM-DD,
|
|
72
|
+
* 不会撞;`noteOf` 按日期取,也永远取不到它。
|
|
73
|
+
*/
|
|
74
|
+
export function promptedDay(dataDir) {
|
|
75
|
+
const n = loadNotes(dataDir);
|
|
76
|
+
const v = n && n.__prompted;
|
|
77
|
+
return typeof v === 'string' ? v : '';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function markPrompted(dataDir, day) {
|
|
81
|
+
const key = String(day || '').trim();
|
|
82
|
+
if (!key) return { ok: false, error: '缺日期' };
|
|
83
|
+
const all = loadNotes(dataDir);
|
|
84
|
+
all.__prompted = key;
|
|
85
|
+
try {
|
|
86
|
+
const tmp = notesFile(dataDir) + '.tmp';
|
|
87
|
+
fs.writeFileSync(tmp, JSON.stringify(all, null, 2), { encoding: 'utf8' });
|
|
88
|
+
fs.renameSync(tmp, notesFile(dataDir));
|
|
89
|
+
return { ok: true, day: key };
|
|
90
|
+
} catch (e) {
|
|
91
|
+
return { ok: false, error: String((e && e.message) || e) };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
14
95
|
|
|
15
96
|
/** 一天按本机时区切(不是 UTC)—— 否则晚上 8 点的话会被算到「明天」 */
|
|
16
97
|
export function dayKey(d = new Date()) {
|
|
@@ -36,6 +117,23 @@ function hhmm(ts) {
|
|
|
36
117
|
return `${p(d.getHours())}:${p(d.getMinutes())}`;
|
|
37
118
|
}
|
|
38
119
|
|
|
120
|
+
/**
|
|
121
|
+
* 把记录的时间戳读成毫秒数。
|
|
122
|
+
*
|
|
123
|
+
* ★ 2026-12-01 修(岸:「日报为什么还是今日收发零 / 日报还是空的」):
|
|
124
|
+
* mailbox 里的 `at` 是 **ISO 字符串**("2026-11-30T11:25:59.103Z"),
|
|
125
|
+
* 以前这里写的是 `Number(r.at || r.atMs || 0)` —— 对字符串一律得 NaN,
|
|
126
|
+
* 于是 `NaN >= 今天0点` 永远是 false,**今天的每一条都被过滤掉**。
|
|
127
|
+
* 结果就是日报永远「今天没有来消息」,收 0 发 0。
|
|
128
|
+
* 这是「看起来在跑、其实一条都没进来」的假绿,不是数据没来。
|
|
129
|
+
*/
|
|
130
|
+
function atOf(r) {
|
|
131
|
+
const v = r?.at ?? r?.atMs ?? 0;
|
|
132
|
+
if (typeof v === 'number') return Number.isFinite(v) ? v : 0;
|
|
133
|
+
const t = Date.parse(String(v ?? ''));
|
|
134
|
+
return Number.isFinite(t) ? t : 0;
|
|
135
|
+
}
|
|
136
|
+
|
|
39
137
|
/**
|
|
40
138
|
* 把今天的 mailbox 压成一份日报。
|
|
41
139
|
*
|
|
@@ -48,7 +146,7 @@ function hhmm(ts) {
|
|
|
48
146
|
export function buildDaily({ rows = [], cfg = null, now = new Date() } = {}) {
|
|
49
147
|
const day = dayKey(now);
|
|
50
148
|
const since = dayStart(now);
|
|
51
|
-
const today = (rows || []).filter((r) =>
|
|
149
|
+
const today = (rows || []).filter((r) => atOf(r) >= since);
|
|
52
150
|
|
|
53
151
|
const people = new Map(); // fp -> { name, in, out, last, lastAt }
|
|
54
152
|
const nameOf = (fp) => {
|
|
@@ -61,7 +159,7 @@ export function buildDaily({ rows = [], cfg = null, now = new Date() } = {}) {
|
|
|
61
159
|
const e = people.get(fp) ?? { fp, name: nameOf(fp), in: 0, out: 0, last: '', lastAt: 0 };
|
|
62
160
|
if (r?.dir === 'out') e.out += 1;
|
|
63
161
|
else e.in += 1;
|
|
64
|
-
const at =
|
|
162
|
+
const at = atOf(r);
|
|
65
163
|
if (at >= e.lastAt && r?.text) {
|
|
66
164
|
e.last = String(r.text);
|
|
67
165
|
e.lastAt = at;
|
|
@@ -89,8 +187,8 @@ export function buildDaily({ rows = [], cfg = null, now = new Date() } = {}) {
|
|
|
89
187
|
// 今天最先和最后一条 —— 用时间戳,不自己编
|
|
90
188
|
const first = today[0];
|
|
91
189
|
const last = today[today.length - 1];
|
|
92
|
-
const fa =
|
|
93
|
-
const la =
|
|
190
|
+
const fa = atOf(first);
|
|
191
|
+
const la = atOf(last);
|
|
94
192
|
if (fa && la) lines.push(`---`, `今天最早 ${hhmm(fa)},最晚 ${hhmm(la)}。`);
|
|
95
193
|
}
|
|
96
194
|
|