koishi-plugin-wordpress-notifier 2.5.1 → 2.5.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.
Files changed (2) hide show
  1. package/lib/index.js +40 -23
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -21,23 +21,25 @@ exports.Config = koishi_1.Schema.object({
21
21
  function apply(ctx, config) {
22
22
  ctx.logger.info('WordPress 推送插件已加载');
23
23
  // 修复 MySQL 自增主键问题,使用正确的模型配置
24
+ // 关键修复:将 primary 从数组 ['id'] 改为字符串 'id'
25
+ // 当 primary 为字符串且等于当前字段名时,Koishi 会自动为 MySQL 添加 AUTO_INCREMENT 属性
24
26
  ctx.model.extend('wordpress_post_updates', {
25
27
  id: 'integer',
26
28
  postId: 'integer',
27
29
  lastModified: 'timestamp',
28
30
  pushedAt: 'timestamp'
29
31
  }, {
30
- primary: ['id'],
31
- autoInc: true, // 这是正确的自增配置
32
- unique: ['postId'] // 合理,因为我们只需要保留每篇文章的最新更新记录
32
+ primary: 'id',
33
+ autoInc: true,
34
+ unique: ['postId']
33
35
  });
34
36
  ctx.model.extend('wordpress_user_registrations', {
35
37
  id: 'integer',
36
38
  userId: 'integer',
37
39
  pushedAt: 'timestamp'
38
40
  }, {
39
- primary: ['id'],
40
- autoInc: true, // 这是正确的自增配置
41
+ primary: 'id',
42
+ autoInc: true,
41
43
  unique: ['userId']
42
44
  });
43
45
  ctx.model.extend('wordpress_group_pushes', {
@@ -47,8 +49,8 @@ function apply(ctx, config) {
47
49
  pushedAt: 'timestamp',
48
50
  isUpdate: 'boolean'
49
51
  }, {
50
- primary: ['id'],
51
- autoInc: true, // 这是正确的自增配置
52
+ primary: 'id',
53
+ autoInc: true,
52
54
  unique: ['groupId', 'postId']
53
55
  });
54
56
  // 确保所有插入操作都不手动指定 id 字段,让数据库自动生成
@@ -258,16 +260,16 @@ function apply(ctx, config) {
258
260
  async function markGroupAsPushed(groupId, postId, isUpdate) {
259
261
  try {
260
262
  ctx.logger.info(`开始标记群 ${groupId} 已推送文章 ${postId}`);
261
- // 1. 先检查是否已存在记录
263
+ // 1. 获取当前表中的所有记录,检查是否有相同 groupId 的不同 postId 记录
264
+ const allGroupRecords = await ctx.database.get('wordpress_group_pushes', { groupId });
265
+ ctx.logger.info(`群 ${groupId} 已有 ${allGroupRecords.length} 条推送记录`);
266
+ if (allGroupRecords.length > 0) {
267
+ ctx.logger.info(`已有记录示例:${JSON.stringify(allGroupRecords.slice(0, 2))}`);
268
+ }
269
+ // 2. 检查是否已存在该 groupId + postId 的组合
262
270
  const existingRecords = await ctx.database.get('wordpress_group_pushes', { groupId, postId });
263
271
  ctx.logger.info(`检查结果:群 ${groupId} 已推送文章 ${postId}:${existingRecords.length > 0 ? '是' : '否'}`);
264
- // 2. 执行原子操作(remove + create)
265
- if (existingRecords.length > 0) {
266
- ctx.logger.info(`发现现有记录,准备更新,文章 ID: ${postId}`);
267
- await ctx.database.remove('wordpress_group_pushes', { groupId, postId });
268
- ctx.logger.info(`已删除旧记录,文章 ID: ${postId}`);
269
- }
270
- // 3. 创建新记录
272
+ // 3. 创建新记录,不删除旧记录(除非是同篇文章的更新)
271
273
  const newRecord = {
272
274
  groupId,
273
275
  postId,
@@ -281,6 +283,9 @@ function apply(ctx, config) {
281
283
  const verification = await ctx.database.get('wordpress_group_pushes', { groupId, postId });
282
284
  if (verification.length > 0) {
283
285
  ctx.logger.info(`记录写入验证成功,群 ${groupId},文章 ${postId}`);
286
+ // 验证后再次检查该群的所有记录
287
+ const updatedAllGroupRecords = await ctx.database.get('wordpress_group_pushes', { groupId });
288
+ ctx.logger.info(`群 ${groupId} 现在共有 ${updatedAllGroupRecords.length} 条推送记录`);
284
289
  }
285
290
  else {
286
291
  ctx.logger.error(`记录写入验证失败,群 ${groupId},文章 ${postId}`);
@@ -289,17 +294,29 @@ function apply(ctx, config) {
289
294
  catch (error) {
290
295
  // 处理唯一约束冲突错误
291
296
  const errorMessage = error instanceof Error ? error.message : String(error);
297
+ ctx.logger.error(`标记推送记录失败:${errorMessage}`);
298
+ ctx.logger.error(`错误栈:${error instanceof Error ? error.stack : '无'}`);
299
+ ctx.logger.error(`插入参数:groupId=${groupId}, postId=${postId}, isUpdate=${isUpdate}`);
300
+ // 详细记录错误信息,便于诊断
292
301
  if (errorMessage.includes('UNIQUE constraint failed')) {
293
- ctx.logger.warn(`推送记录已存在,跳过重复插入:群 ${groupId},文章 ${postId}`);
294
- ctx.logger.warn(`完整错误信息:${errorMessage}`);
295
- // 可以选择不抛出错误,将其视为预期情况
302
+ ctx.logger.error(`唯一约束冲突,可能的原因:`);
303
+ ctx.logger.error(`1. 数据库表的 unique 约束设置错误,可能只针对 groupId 而不是 groupId+postId 组合`);
304
+ ctx.logger.error(`2. 同一篇文章被多次推送`);
305
+ ctx.logger.error(`3. 数据库表结构与模型定义不符`);
306
+ // 尝试获取表结构信息(如果可能)
307
+ try {
308
+ const allRecords = await ctx.database.get('wordpress_group_pushes', {});
309
+ ctx.logger.error(`当前表中共有 ${allRecords.length} 条记录`);
310
+ if (allRecords.length > 0) {
311
+ ctx.logger.error(`前 3 条记录:${JSON.stringify(allRecords.slice(0, 3))}`);
312
+ }
313
+ }
314
+ catch (e) {
315
+ ctx.logger.error(`获取表记录失败:${e}`);
316
+ }
296
317
  }
297
318
  else {
298
- ctx.logger.error(`标记推送记录失败:${errorMessage}`);
299
- ctx.logger.error(`错误栈:${error instanceof Error ? error.stack : '无'}`);
300
- ctx.logger.error(`插入参数:groupId=${groupId}, postId=${postId}, isUpdate=${isUpdate}`);
301
- // 非约束冲突错误,不抛出,确保插件继续运行
302
- ctx.logger.error(`非致命错误,插件将继续运行`);
319
+ ctx.logger.error(`非约束冲突错误,插件将继续运行`);
303
320
  }
304
321
  }
305
322
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-wordpress-notifier",
3
- "version": "2.5.1",
3
+ "version": "2.5.3",
4
4
  "description": "WordPress 文章自动推送到 QQ",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",