alemonjs 2.1.0-alpha.27 → 2.1.0-alpha.28
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/lib/cbp/testone.js +53 -8
- package/package.json +1 -1
package/lib/cbp/testone.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as flattedJSON from 'flatted';
|
|
2
2
|
import { onProcessor } from '../app/event-processor.js';
|
|
3
3
|
import { join, dirname } from 'path';
|
|
4
|
-
import { existsSync, mkdirSync, watch, readFileSync } from 'fs';
|
|
4
|
+
import { existsSync, mkdirSync, watch, readFileSync, writeFile } from 'fs';
|
|
5
5
|
import _ from 'lodash';
|
|
6
6
|
import { readFile } from 'fs/promises';
|
|
7
7
|
import { actionResolves, actionTimeouts, apiResolves, apiTimeouts } from './config.js';
|
|
@@ -53,7 +53,7 @@ const connectionTestOne = (ws, _request) => {
|
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
catch (error) {
|
|
56
|
-
|
|
56
|
+
logger.error(`监听文件失败: ${filePath}`, error);
|
|
57
57
|
}
|
|
58
58
|
};
|
|
59
59
|
// 监听整个目录,捕获新文件创建
|
|
@@ -90,7 +90,7 @@ const connectionTestOne = (ws, _request) => {
|
|
|
90
90
|
}));
|
|
91
91
|
})
|
|
92
92
|
.catch(error => {
|
|
93
|
-
|
|
93
|
+
logger.error('读取 commands.json 失败:', error);
|
|
94
94
|
});
|
|
95
95
|
}, 1000);
|
|
96
96
|
const usersPath = join(testonePath, 'users.json');
|
|
@@ -106,7 +106,7 @@ const connectionTestOne = (ws, _request) => {
|
|
|
106
106
|
}));
|
|
107
107
|
})
|
|
108
108
|
.catch(error => {
|
|
109
|
-
|
|
109
|
+
logger.error('读取 users.json 失败:', error);
|
|
110
110
|
});
|
|
111
111
|
}, 1000);
|
|
112
112
|
const channelsPath = join(testonePath, 'channels.json');
|
|
@@ -122,7 +122,7 @@ const connectionTestOne = (ws, _request) => {
|
|
|
122
122
|
}));
|
|
123
123
|
})
|
|
124
124
|
.catch(error => {
|
|
125
|
-
|
|
125
|
+
logger.error('读取 channels.json 失败:', error);
|
|
126
126
|
});
|
|
127
127
|
}, 1000);
|
|
128
128
|
// 初始化时监听已存在的文件
|
|
@@ -166,9 +166,39 @@ const connectionTestOne = (ws, _request) => {
|
|
|
166
166
|
}));
|
|
167
167
|
}
|
|
168
168
|
catch (error) {
|
|
169
|
-
|
|
169
|
+
logger.error('初始化数据失败:', error);
|
|
170
170
|
}
|
|
171
171
|
};
|
|
172
|
+
const onDeleteMessage = (type, CreateAt) => {
|
|
173
|
+
const messagePath = type === 'private' ? privateMessagePath : publicMessagePath;
|
|
174
|
+
if (existsSync(messagePath)) {
|
|
175
|
+
try {
|
|
176
|
+
const messages = JSON.parse(readFileSync(messagePath, 'utf-8'));
|
|
177
|
+
const updatedMessages = messages.filter((msg) => msg.CreateAt !== CreateAt);
|
|
178
|
+
console.log('updatedMessages', updatedMessages);
|
|
179
|
+
writeFile(messagePath, JSON.stringify(updatedMessages, null, 2), (error) => {
|
|
180
|
+
if (error) {
|
|
181
|
+
logger.error(`写入 ${type} 消息失败:`, error);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
logger.error(`读取 ${type} 消息失败:`, error);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
const onSaveMessage = (type, message) => {
|
|
191
|
+
const messagePath = type === 'private' ? privateMessagePath : publicMessagePath;
|
|
192
|
+
const messages = existsSync(messagePath)
|
|
193
|
+
? JSON.parse(readFileSync(messagePath, 'utf-8'))
|
|
194
|
+
: [];
|
|
195
|
+
messages.push(message);
|
|
196
|
+
writeFile(messagePath, JSON.stringify(messages, null, 2), (error) => {
|
|
197
|
+
if (error) {
|
|
198
|
+
logger.error(`写入 ${type} 消息失败:`, error);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
};
|
|
172
202
|
// 处理消息事件
|
|
173
203
|
global.testoneClient.on('message', (message) => {
|
|
174
204
|
try {
|
|
@@ -178,6 +208,7 @@ const connectionTestOne = (ws, _request) => {
|
|
|
178
208
|
if (parsedMessage.name) {
|
|
179
209
|
// 如果有 name,说明是一个事件请求。要进行处理
|
|
180
210
|
onProcessor(parsedMessage.name, parsedMessage, parsedMessage.value);
|
|
211
|
+
// 消息写入
|
|
181
212
|
}
|
|
182
213
|
else if (parsedMessage?.actionId) {
|
|
183
214
|
// 如果有 actionId
|
|
@@ -233,14 +264,28 @@ const connectionTestOne = (ws, _request) => {
|
|
|
233
264
|
else if (parsedMessage.type === 'channels') {
|
|
234
265
|
onChannels();
|
|
235
266
|
}
|
|
267
|
+
else if (parsedMessage.type === 'private.message.delete') {
|
|
268
|
+
const CreateAt = parsedMessage.payload.CreateAt;
|
|
269
|
+
onDeleteMessage('private', CreateAt);
|
|
270
|
+
}
|
|
271
|
+
else if (parsedMessage.type === 'public.message.delete') {
|
|
272
|
+
const CreateAt = parsedMessage.payload.CreateAt;
|
|
273
|
+
onDeleteMessage('public', CreateAt);
|
|
274
|
+
}
|
|
275
|
+
else if (parsedMessage.type === 'private.message.save') {
|
|
276
|
+
onSaveMessage('private', parsedMessage.payload);
|
|
277
|
+
}
|
|
278
|
+
else if (parsedMessage.type === 'public.message.save') {
|
|
279
|
+
onSaveMessage('public', parsedMessage.payload);
|
|
280
|
+
}
|
|
236
281
|
}
|
|
237
282
|
catch (error) {
|
|
238
|
-
|
|
283
|
+
logger.error('客户端解析消息失败:', error);
|
|
239
284
|
}
|
|
240
285
|
});
|
|
241
286
|
// 处理关闭事件
|
|
242
287
|
global.testoneClient.on('close', () => {
|
|
243
|
-
|
|
288
|
+
logger.info('WebSocket connection closed');
|
|
244
289
|
// 清理所有文件监听器
|
|
245
290
|
fileWatchers.forEach(watcher => watcher.close());
|
|
246
291
|
fileWatchers.clear();
|