koishi-plugin-wordpress-notifier 2.4.0 → 2.4.2
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/index.js +102 -60
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -28,7 +28,7 @@ function apply(ctx, config) {
|
|
|
28
28
|
}, {
|
|
29
29
|
primary: ['id'],
|
|
30
30
|
autoInc: true,
|
|
31
|
-
unique: ['postId']
|
|
31
|
+
unique: ['postId'] // 合理,因为我们只需要保留每篇文章的最新更新记录
|
|
32
32
|
});
|
|
33
33
|
ctx.model.extend('wordpress_user_registrations', {
|
|
34
34
|
id: 'integer',
|
|
@@ -143,9 +143,26 @@ function apply(ctx, config) {
|
|
|
143
143
|
return records.length > 0 ? records[0] : null;
|
|
144
144
|
}
|
|
145
145
|
async function isGroupPushed(groupId, postId) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
146
|
+
try {
|
|
147
|
+
// 明确查询 groupId + postId 组合是否存在
|
|
148
|
+
const query = { groupId, postId };
|
|
149
|
+
ctx.logger.info(`检查群 ${groupId} 是否已推送文章 ${postId},查询条件:${JSON.stringify(query)}`);
|
|
150
|
+
const records = await ctx.database.get('wordpress_group_pushes', query);
|
|
151
|
+
const result = records.length > 0;
|
|
152
|
+
ctx.logger.info(`检查群 ${groupId} 是否已推送文章 ${postId}:${result ? '是' : '否'}`);
|
|
153
|
+
// 添加调试日志,显示所有相关记录
|
|
154
|
+
if (records.length > 0) {
|
|
155
|
+
ctx.logger.debug(`找到 ${records.length} 条匹配记录:${JSON.stringify(records)}`);
|
|
156
|
+
}
|
|
157
|
+
return result;
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
161
|
+
ctx.logger.error(`检查推送记录失败:${errorMessage}`);
|
|
162
|
+
ctx.logger.error(`错误栈:${error instanceof Error ? error.stack : '无'}`);
|
|
163
|
+
// 发生错误时,默认返回 false,避免阻塞推送流程
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
149
166
|
}
|
|
150
167
|
async function markUserAsPushed(userId) {
|
|
151
168
|
await ctx.database.create('wordpress_user_registrations', {
|
|
@@ -154,33 +171,66 @@ function apply(ctx, config) {
|
|
|
154
171
|
});
|
|
155
172
|
}
|
|
156
173
|
async function updatePostUpdateRecord(postId, modifiedDate) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
174
|
+
try {
|
|
175
|
+
ctx.logger.info(`开始更新文章更新记录,文章 ID: ${postId},修改时间: ${modifiedDate}`);
|
|
176
|
+
const record = await getPostUpdateRecord(postId);
|
|
177
|
+
if (record) {
|
|
178
|
+
ctx.logger.info(`发现现有记录,文章 ID: ${postId},上次修改时间: ${record.lastModified}`);
|
|
179
|
+
// Koishi database API 不支持 update 方法,使用 remove + create 代替
|
|
180
|
+
await ctx.database.remove('wordpress_post_updates', { postId });
|
|
181
|
+
ctx.logger.info(`已删除旧记录,文章 ID: ${postId}`);
|
|
182
|
+
}
|
|
183
|
+
// 创建新记录
|
|
184
|
+
const newRecord = {
|
|
185
|
+
postId,
|
|
186
|
+
lastModified: modifiedDate,
|
|
187
|
+
pushedAt: new Date()
|
|
188
|
+
};
|
|
189
|
+
ctx.logger.info(`准备创建新记录,文章 ID: ${postId},记录内容: ${JSON.stringify(newRecord)}`);
|
|
190
|
+
await ctx.database.create('wordpress_post_updates', newRecord);
|
|
191
|
+
ctx.logger.info(`已成功更新文章更新记录,文章 ID: ${postId}`);
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
195
|
+
ctx.logger.error(`更新文章更新记录失败,文章 ID: ${postId}`);
|
|
196
|
+
ctx.logger.error(`错误信息: ${errorMessage}`);
|
|
197
|
+
ctx.logger.error(`错误栈: ${error instanceof Error ? error.stack : '无'}`);
|
|
198
|
+
throw error;
|
|
199
|
+
}
|
|
167
200
|
}
|
|
168
201
|
async function markGroupAsPushed(groupId, postId, isUpdate) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
202
|
+
try {
|
|
203
|
+
const record = await ctx.database.get('wordpress_group_pushes', { groupId, postId });
|
|
204
|
+
ctx.logger.info(`准备标记群 ${groupId} 已推送文章 ${postId},当前记录数量:${record.length}`);
|
|
205
|
+
if (record.length > 0) {
|
|
206
|
+
// Koishi database API 不支持 update 方法,使用 remove + create 代替
|
|
207
|
+
ctx.logger.info(`已存在记录,更新旧记录`);
|
|
208
|
+
await ctx.database.remove('wordpress_group_pushes', { groupId, postId });
|
|
209
|
+
}
|
|
210
|
+
ctx.logger.info(`创建新的推送记录,参数:groupId=${groupId}, postId=${postId}, isUpdate=${isUpdate}`);
|
|
211
|
+
await ctx.database.create('wordpress_group_pushes', {
|
|
212
|
+
groupId,
|
|
213
|
+
postId,
|
|
214
|
+
pushedAt: new Date(),
|
|
215
|
+
isUpdate
|
|
216
|
+
});
|
|
217
|
+
ctx.logger.info(`已标记群 ${groupId} 已推送文章 ${postId}`);
|
|
218
|
+
}
|
|
219
|
+
catch (error) {
|
|
220
|
+
// 处理唯一约束冲突错误
|
|
221
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
222
|
+
if (errorMessage.includes('UNIQUE constraint failed')) {
|
|
223
|
+
ctx.logger.warn(`推送记录已存在,跳过重复插入:群 ${groupId},文章 ${postId}`);
|
|
224
|
+
ctx.logger.warn(`完整错误信息:${errorMessage}`);
|
|
225
|
+
// 可以选择不抛出错误,将其视为预期情况
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
ctx.logger.error(`标记推送记录失败:${errorMessage}`);
|
|
229
|
+
ctx.logger.error(`错误栈:${error instanceof Error ? error.stack : '无'}`);
|
|
230
|
+
ctx.logger.error(`插入参数:groupId=${groupId}, postId=${postId}, isUpdate=${isUpdate}`);
|
|
231
|
+
throw error;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
184
234
|
}
|
|
185
235
|
function formatPostMessage(post, mention = false, isUpdate = false) {
|
|
186
236
|
// 彻底过滤 HTML 标签和非法字符,只保留安全文本
|
|
@@ -299,7 +349,9 @@ function apply(ctx, config) {
|
|
|
299
349
|
ctx.logger.error('没有可用的 Bot 实例');
|
|
300
350
|
return;
|
|
301
351
|
}
|
|
302
|
-
|
|
352
|
+
// 修复 Bot 标识 undefined 问题
|
|
353
|
+
const botId = bot.selfId || 'unknown';
|
|
354
|
+
ctx.logger.info(`使用 bot ${bot.platform}:${botId} 进行推送`);
|
|
303
355
|
// 推送新文章
|
|
304
356
|
if (config.enableAutoPush) {
|
|
305
357
|
const posts = await fetchLatestPosts();
|
|
@@ -348,14 +400,9 @@ function apply(ctx, config) {
|
|
|
348
400
|
if (!updateRecord || postModifiedDate > new Date(updateRecord.lastModified)) {
|
|
349
401
|
for (const target of config.targets) {
|
|
350
402
|
try {
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
ctx.logger.error(`无效的目标 ${target},必须是数字类型`);
|
|
355
|
-
continue;
|
|
356
|
-
}
|
|
357
|
-
// 保持字符串类型,但确保内容是有效的数字格式
|
|
358
|
-
const stringTarget = numericTarget.toString();
|
|
403
|
+
ctx.logger.info(`正在处理目标: ${target}`);
|
|
404
|
+
// 直接使用原始目标字符串,与新文章推送逻辑保持一致
|
|
405
|
+
const stringTarget = target;
|
|
359
406
|
// 检查该群是否已推送过此文章
|
|
360
407
|
if (await isGroupPushed(stringTarget, post.id)) {
|
|
361
408
|
const segments = formatPostMessage(post, true, true);
|
|
@@ -385,14 +432,9 @@ function apply(ctx, config) {
|
|
|
385
432
|
if (!(await isUserPushed(user.id))) {
|
|
386
433
|
for (const target of config.targets) {
|
|
387
434
|
try {
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
ctx.logger.error(`无效的目标 ${target},必须是数字类型`);
|
|
392
|
-
continue;
|
|
393
|
-
}
|
|
394
|
-
// 保持字符串类型,但确保内容是有效的数字格式
|
|
395
|
-
const stringTarget = numericTarget.toString();
|
|
435
|
+
ctx.logger.info(`正在处理目标: ${target}`);
|
|
436
|
+
// 直接使用原始目标字符串,与新文章推送逻辑保持一致
|
|
437
|
+
const stringTarget = target;
|
|
396
438
|
const segments = formatUserMessage(user, true);
|
|
397
439
|
ctx.logger.info(`准备推送新用户到目标: ${stringTarget}`);
|
|
398
440
|
await bot.sendMessage(stringTarget, segments);
|
|
@@ -632,20 +674,20 @@ ${targetText}
|
|
|
632
674
|
ctx.command('wordpress', 'WordPress 推送插件菜单')
|
|
633
675
|
.action(() => {
|
|
634
676
|
ctx.logger.info('命令 wordpress 被调用');
|
|
635
|
-
return `📚 WordPress 推送插件菜单:
|
|
636
|
-
|
|
637
|
-
🔹 /wordpress.status - 查看插件状态
|
|
638
|
-
🔹 /wordpress.latest - 查看最新文章
|
|
639
|
-
🔹 /wordpress.list - 查看文章列表
|
|
640
|
-
🔹 /wordpress.push - 手动推送最新文章
|
|
641
|
-
🔹 /wordpress.set-url <url> - 修改 WordPress 站点地址
|
|
642
|
-
🔹 /wordpress.pushed - 查看已推送文章列表
|
|
643
|
-
🔹 /wordpress.clean [days] - 清理旧推送记录
|
|
644
|
-
🔹 /wordpress.toggle - 切换自动推送开关(仅超级管理员)
|
|
645
|
-
🔹 /wordpress.toggle-update - 切换文章更新推送开关(仅超级管理员)
|
|
646
|
-
🔹 /wordpress.toggle-user - 切换新用户注册推送开关(仅超级管理员)
|
|
647
|
-
🔹 /wordpress.mention - 切换 @全体成员 开关(仅超级管理员)
|
|
648
|
-
|
|
677
|
+
return `📚 WordPress 推送插件菜单:
|
|
678
|
+
|
|
679
|
+
🔹 /wordpress.status - 查看插件状态
|
|
680
|
+
🔹 /wordpress.latest - 查看最新文章
|
|
681
|
+
🔹 /wordpress.list - 查看文章列表
|
|
682
|
+
🔹 /wordpress.push - 手动推送最新文章
|
|
683
|
+
🔹 /wordpress.set-url <url> - 修改 WordPress 站点地址
|
|
684
|
+
🔹 /wordpress.pushed - 查看已推送文章列表
|
|
685
|
+
🔹 /wordpress.clean [days] - 清理旧推送记录
|
|
686
|
+
🔹 /wordpress.toggle - 切换自动推送开关(仅超级管理员)
|
|
687
|
+
🔹 /wordpress.toggle-update - 切换文章更新推送开关(仅超级管理员)
|
|
688
|
+
🔹 /wordpress.toggle-user - 切换新用户注册推送开关(仅超级管理员)
|
|
689
|
+
🔹 /wordpress.mention - 切换 @全体成员 开关(仅超级管理员)
|
|
690
|
+
|
|
649
691
|
💡 提示:所有命令都需要加 / 前缀`;
|
|
650
692
|
});
|
|
651
693
|
ctx.on('ready', async () => {
|