oh-my-dws 0.0.2 → 0.0.3
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 +17 -5
- package/bin/oh-my-dws.js +2 -2
- package/docs//344/275/277/347/224/250/350/257/264/346/230/216.md +104 -5
- package/package.json +1 -1
- package/src/actions.js +590 -63
- package/src/ai.js +300 -0
- package/src/archive.js +17 -8
- package/src/auth.js +33 -1
- package/src/avatars.js +143 -0
- package/src/contacts.js +563 -0
- package/src/layout.js +63 -0
- package/src/listen.js +289 -0
- package/src/media.js +12 -1
- package/src/rev.js +23 -0
- package/src/server.js +385 -7
- package/src/settings.js +51 -0
- package/src/sync.js +213 -0
- package/web/index.html +2136 -74
package/src/listen.js
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 实时监听(免额度):`dws event +listen-im` 走钉钉 Stream 长连接,
|
|
5
|
+
* 事件里自带 conversation_id / message_id / content / sender / create_time,
|
|
6
|
+
* 所以新消息直接写进归档,**不需要再调消息接口**,也不占 API 配额。
|
|
7
|
+
*
|
|
8
|
+
* 群聊和单聊是两个 EventKey,各起一个子进程;stdout 是 NDJSON,stderr 的
|
|
9
|
+
* `[event] ready` 表示订阅就绪。进程异常退出会退避重连。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const { spawn } = require('node:child_process');
|
|
13
|
+
const fs = require('node:fs');
|
|
14
|
+
const path = require('node:path');
|
|
15
|
+
|
|
16
|
+
const P = require('./paths');
|
|
17
|
+
const { ensureDir, fmtMinute, fmtSecond } = require('./util');
|
|
18
|
+
const { shortNames, archiveKind, conversationId } = require('./archive');
|
|
19
|
+
const A = require('./actions');
|
|
20
|
+
|
|
21
|
+
const LOG_FILE = path.join(P.DATA_DIR, '_运行', 'listen.log');
|
|
22
|
+
|
|
23
|
+
const KINDS = [
|
|
24
|
+
{ kind: 'all-group', label: '全部群消息' },
|
|
25
|
+
{ kind: 'all-direct', label: '全部单聊' },
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
const state = {
|
|
29
|
+
enabled: true,
|
|
30
|
+
children: new Map(), // kind -> { proc, ready, startedAt, restarts, lastError }
|
|
31
|
+
ready: false,
|
|
32
|
+
events: 0,
|
|
33
|
+
appended: 0,
|
|
34
|
+
duplicates: 0,
|
|
35
|
+
ignored: 0,
|
|
36
|
+
lastEventAt: '',
|
|
37
|
+
lastError: '',
|
|
38
|
+
pending: new Map(), // short -> { items: [], timer }
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// ---------------------------------------------------------------- 事件 → 归档
|
|
42
|
+
|
|
43
|
+
/** 把事件里的时间规范成归档用的 `YYYY-MM-DD HH:mm:ss`(东八区) */
|
|
44
|
+
function normTime(v) {
|
|
45
|
+
if (v === undefined || v === null || v === '') return fmtSecond(new Date());
|
|
46
|
+
if (typeof v === 'number' || /^\d{10,}$/.test(String(v))) {
|
|
47
|
+
const n = Number(v);
|
|
48
|
+
return fmtSecond(new Date(n < 1e12 ? n * 1000 : n));
|
|
49
|
+
}
|
|
50
|
+
const s = String(v);
|
|
51
|
+
// 已经是 "YYYY-MM-DD HH:mm[:ss]"
|
|
52
|
+
if (/^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}/.test(s)) return s.replace('T', ' ').slice(0, 19);
|
|
53
|
+
const d = new Date(s);
|
|
54
|
+
return Number.isNaN(d.getTime()) ? fmtMinute(new Date()) : fmtSecond(d);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function cidToShort() {
|
|
58
|
+
const map = new Map();
|
|
59
|
+
for (const short of shortNames()) {
|
|
60
|
+
if (archiveKind(short) === 'mentions') continue;
|
|
61
|
+
const cid = conversationId(short);
|
|
62
|
+
if (cid) map.set(cid, short);
|
|
63
|
+
}
|
|
64
|
+
return map;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 同一会话的消息攒一下再落盘,避免每条消息都重写 Markdown/索引 */
|
|
68
|
+
function enqueue(short, msg) {
|
|
69
|
+
let slot = state.pending.get(short);
|
|
70
|
+
if (!slot) {
|
|
71
|
+
slot = { items: [], timer: null };
|
|
72
|
+
slot.timer = setTimeout(() => flush(short), 1200);
|
|
73
|
+
if (slot.timer.unref) slot.timer.unref();
|
|
74
|
+
state.pending.set(short, slot);
|
|
75
|
+
}
|
|
76
|
+
slot.items.push(msg);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function flush(short) {
|
|
80
|
+
const slot = state.pending.get(short);
|
|
81
|
+
state.pending.delete(short);
|
|
82
|
+
if (!slot || !slot.items.length) return;
|
|
83
|
+
for (const msg of slot.items) {
|
|
84
|
+
try {
|
|
85
|
+
const r = A.appendMessage(short, msg);
|
|
86
|
+
if (r.added) state.appended += 1;
|
|
87
|
+
else if (r.duplicate) state.duplicates += 1;
|
|
88
|
+
else state.ignored += 1;
|
|
89
|
+
} catch (e) {
|
|
90
|
+
state.lastError = `写入 ${short} 失败:${e.message}`;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function handleEvent(raw) {
|
|
96
|
+
state.events += 1;
|
|
97
|
+
state.lastEventAt = fmtSecond(new Date());
|
|
98
|
+
let ev;
|
|
99
|
+
try {
|
|
100
|
+
ev = JSON.parse(raw);
|
|
101
|
+
} catch {
|
|
102
|
+
state.ignored += 1;
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// 撤回事件:标记归档里那条消息
|
|
107
|
+
const typeStr = String((ev && ev.type) || (ev && ev.data && ev.data.type) || '');
|
|
108
|
+
if (typeStr.includes('recall') || String(raw).includes('recall_time')) {
|
|
109
|
+
const d0 = ev && ev.data && typeof ev.data === 'object' ? ev.data : ev;
|
|
110
|
+
const mid0 = String(d0.message_id || d0.messageId || '').trim();
|
|
111
|
+
if (mid0) {
|
|
112
|
+
try {
|
|
113
|
+
A.markRecalledByMessageId(mid0, String(d0.recaller || ''));
|
|
114
|
+
state.appended += 1;
|
|
115
|
+
} catch (e) {
|
|
116
|
+
state.lastError = `标记撤回失败:${e.message}`;
|
|
117
|
+
}
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
// 有的封装会带 envelope,扁平字段也可能在 data 里
|
|
122
|
+
const d = ev && ev.data && typeof ev.data === 'object' ? ev.data : ev;
|
|
123
|
+
const cid = String(d.conversation_id || d.conversationId || '').trim();
|
|
124
|
+
const mid = String(d.message_id || d.messageId || '').trim();
|
|
125
|
+
if (!cid || !mid) {
|
|
126
|
+
state.ignored += 1;
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const short = cidToShort().get(cid);
|
|
130
|
+
if (!short) {
|
|
131
|
+
state.ignored += 1; // 没归档的会话直接忽略,不浪费写盘
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const msg = {
|
|
135
|
+
conversationId: cid,
|
|
136
|
+
messageId: mid,
|
|
137
|
+
sender: String(d.sender || ''),
|
|
138
|
+
senderId: String(d.sender_open_dingtalk_id || d.senderId || ''),
|
|
139
|
+
text: String(d.content || d.text || ''),
|
|
140
|
+
createTime: normTime(d.create_time || d.createTime || d.event_time),
|
|
141
|
+
};
|
|
142
|
+
if (!msg.text && !msg.sender) {
|
|
143
|
+
state.ignored += 1;
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
enqueue(short, msg);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ---------------------------------------------------------------- 子进程管理
|
|
150
|
+
|
|
151
|
+
function logLine(line) {
|
|
152
|
+
try {
|
|
153
|
+
ensureDir(path.dirname(LOG_FILE));
|
|
154
|
+
fs.appendFileSync(LOG_FILE, `${fmtSecond(new Date())} ${line}\n`, 'utf8');
|
|
155
|
+
} catch {}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function spawnOne({ kind, label }, attempt = 0) {
|
|
159
|
+
const args = [
|
|
160
|
+
'event', '+listen-im',
|
|
161
|
+
'--kind', kind,
|
|
162
|
+
'--events', 'message',
|
|
163
|
+
'--duration', '0',
|
|
164
|
+
'--format', 'ndjson',
|
|
165
|
+
];
|
|
166
|
+
const entry = { kind, label, proc: null, ready: false, startedAt: fmtSecond(new Date()), restarts: attempt, lastError: '' };
|
|
167
|
+
let proc;
|
|
168
|
+
try {
|
|
169
|
+
proc = spawn('dws', args, { stdio: ['ignore', 'pipe', 'pipe'] });
|
|
170
|
+
} catch (e) {
|
|
171
|
+
entry.lastError = e.message;
|
|
172
|
+
state.lastError = `启动监听失败:${e.message}`;
|
|
173
|
+
return entry;
|
|
174
|
+
}
|
|
175
|
+
entry.proc = proc;
|
|
176
|
+
state.children.set(kind, entry);
|
|
177
|
+
logLine(`[spawn] ${kind} pid=${proc.pid} args=${args.join(' ')}`);
|
|
178
|
+
|
|
179
|
+
let buf = '';
|
|
180
|
+
proc.stdout.on('data', (chunk) => {
|
|
181
|
+
buf += chunk.toString('utf8');
|
|
182
|
+
let idx;
|
|
183
|
+
while ((idx = buf.indexOf('\n')) >= 0) {
|
|
184
|
+
const line = buf.slice(0, idx).trim();
|
|
185
|
+
buf = buf.slice(idx + 1);
|
|
186
|
+
if (line) handleEvent(line);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
proc.stderr.on('data', (chunk) => {
|
|
191
|
+
const text = chunk.toString('utf8');
|
|
192
|
+
for (const line of text.split('\n')) {
|
|
193
|
+
const t = line.trim();
|
|
194
|
+
if (!t) continue;
|
|
195
|
+
if (t.includes('[event] ready')) entry.ready = true;
|
|
196
|
+
if (t.includes('exited') || t.includes('error')) entry.lastError = t.slice(0, 200);
|
|
197
|
+
logLine(`[${kind}] ${t.slice(0, 300)}`);
|
|
198
|
+
}
|
|
199
|
+
state.ready = [...state.children.values()].some((c) => c.ready);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
proc.on('exit', (code, signal) => {
|
|
203
|
+
entry.ready = false;
|
|
204
|
+
logLine(`[exit] ${kind} code=${code} signal=${signal || ''}`);
|
|
205
|
+
state.children.delete(kind);
|
|
206
|
+
state.ready = [...state.children.values()].some((c) => c.ready);
|
|
207
|
+
if (!state.enabled) return;
|
|
208
|
+
// 退避重连:2s, 5s, 15s, 30s, 60s…
|
|
209
|
+
const wait = [2000, 5000, 15000, 30000, 60000][Math.min(attempt, 4)];
|
|
210
|
+
const timer = setTimeout(() => {
|
|
211
|
+
if (!state.enabled) return;
|
|
212
|
+
spawnOne({ kind, label }, attempt + 1);
|
|
213
|
+
}, wait);
|
|
214
|
+
if (timer.unref) timer.unref();
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
return entry;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** 清掉历史遗留的孤儿监听进程(父进程已不存在的那种) */
|
|
221
|
+
function reapStale() {
|
|
222
|
+
try {
|
|
223
|
+
const { spawnSync } = require('node:child_process');
|
|
224
|
+
const r = spawnSync('pgrep', ['-f', 'dws event \\+listen-im --kind all-'], { encoding: 'utf8' });
|
|
225
|
+
const pids = String(r.stdout || '')
|
|
226
|
+
.split('\n')
|
|
227
|
+
.map((x) => Number(x.trim()))
|
|
228
|
+
.filter((x) => Number.isInteger(x) && x > 0 && x !== process.pid);
|
|
229
|
+
let killed = 0;
|
|
230
|
+
for (const pid of pids) {
|
|
231
|
+
try {
|
|
232
|
+
const ppid = String(
|
|
233
|
+
spawnSync('ps', ['-o', 'ppid=', '-p', String(pid)], { encoding: 'utf8' }).stdout || ''
|
|
234
|
+
).trim();
|
|
235
|
+
if (Number(ppid) === 1) {
|
|
236
|
+
process.kill(pid, 'SIGTERM');
|
|
237
|
+
killed += 1;
|
|
238
|
+
}
|
|
239
|
+
} catch {}
|
|
240
|
+
}
|
|
241
|
+
if (killed) logLine(`[reap] 清理孤儿监听进程 ${killed} 个`);
|
|
242
|
+
} catch {}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function start() {
|
|
246
|
+
if (state.enabled && state.children.size) return status();
|
|
247
|
+
reapStale();
|
|
248
|
+
state.enabled = true;
|
|
249
|
+
for (const k of KINDS) {
|
|
250
|
+
if (!state.children.has(k.kind)) spawnOne(k);
|
|
251
|
+
}
|
|
252
|
+
return status();
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function stop() {
|
|
256
|
+
state.enabled = false;
|
|
257
|
+
for (const [, entry] of state.children) {
|
|
258
|
+
try {
|
|
259
|
+
entry.proc.kill('SIGTERM');
|
|
260
|
+
} catch {}
|
|
261
|
+
}
|
|
262
|
+
state.children.clear();
|
|
263
|
+
state.ready = false;
|
|
264
|
+
return status();
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function status() {
|
|
268
|
+
return {
|
|
269
|
+
enabled: state.enabled,
|
|
270
|
+
ready: state.ready,
|
|
271
|
+
events: state.events,
|
|
272
|
+
appended: state.appended,
|
|
273
|
+
duplicates: state.duplicates,
|
|
274
|
+
ignored: state.ignored,
|
|
275
|
+
lastEventAt: state.lastEventAt,
|
|
276
|
+
lastError: state.lastError,
|
|
277
|
+
kinds: [...state.children.values()].map((c) => ({
|
|
278
|
+
kind: c.kind,
|
|
279
|
+
label: c.label,
|
|
280
|
+
pid: c.proc && c.proc.pid,
|
|
281
|
+
ready: c.ready,
|
|
282
|
+
startedAt: c.startedAt,
|
|
283
|
+
restarts: c.restarts,
|
|
284
|
+
lastError: c.lastError,
|
|
285
|
+
})),
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
module.exports = { start, stop, status, handleEvent, LOG_FILE };
|
package/src/media.js
CHANGED
|
@@ -83,8 +83,19 @@ function fetchMedia(short, messageId, rid) {
|
|
|
83
83
|
return dst;
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
+
/** 自己发出去的图片/文件:存在 _媒体/已发/,用文件名找回来 */
|
|
87
|
+
const SENT_DIR = path.join(P.MEDIA_DIR, '已发');
|
|
88
|
+
|
|
89
|
+
function sentFileByName(name) {
|
|
90
|
+
const base = path.basename(String(name || ''));
|
|
91
|
+
if (!base || base.startsWith('_')) return '';
|
|
92
|
+
const file = path.join(SENT_DIR, base);
|
|
93
|
+
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) return '';
|
|
94
|
+
return file;
|
|
95
|
+
}
|
|
96
|
+
|
|
86
97
|
function contentTypeOf(file) {
|
|
87
98
|
return CONTENT_TYPES[path.extname(file).toLowerCase()] || 'application/octet-stream';
|
|
88
99
|
}
|
|
89
100
|
|
|
90
|
-
module.exports = { fetchMedia, contentTypeOf, safeRid };
|
|
101
|
+
module.exports = { fetchMedia, contentTypeOf, safeRid, sentFileByName, SENT_DIR };
|
package/src/rev.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 数据版本号:归档有任何写入(拉取 / 事件落库 / 删除 / 纳管 / 汇总)就 +1。
|
|
5
|
+
* 前端轮询 /api/ping 只比这个数字,比每次拉全量群列表便宜得多。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
let rev = 0;
|
|
9
|
+
let at = '';
|
|
10
|
+
|
|
11
|
+
function bump(reason = '') {
|
|
12
|
+
rev += 1;
|
|
13
|
+
at = new Date().toISOString();
|
|
14
|
+
if (reason) lastReason = reason;
|
|
15
|
+
return rev;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let lastReason = '';
|
|
19
|
+
|
|
20
|
+
const get = () => rev;
|
|
21
|
+
const info = () => ({ rev, at, reason: lastReason });
|
|
22
|
+
|
|
23
|
+
module.exports = { bump, get, info };
|