koishi-plugin-wordpress-notifier 2.4.2 → 2.5.0

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 +52 -14
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -135,12 +135,36 @@ function apply(ctx, config) {
135
135
  }
136
136
  }
137
137
  async function isUserPushed(userId) {
138
- const record = await ctx.database.get('wordpress_user_registrations', { userId });
139
- return record.length > 0;
138
+ try {
139
+ ctx.logger.info(`检查用户是否已推送,用户 ID: ${userId}`);
140
+ const record = await ctx.database.get('wordpress_user_registrations', { userId });
141
+ const result = record.length > 0;
142
+ ctx.logger.info(`检查结果:用户 ${userId} 已推送:${result ? '是' : '否'}`);
143
+ return result;
144
+ }
145
+ catch (error) {
146
+ const errorMessage = error instanceof Error ? error.message : String(error);
147
+ ctx.logger.error(`检查用户推送记录失败:${errorMessage}`);
148
+ ctx.logger.error(`错误栈:${error instanceof Error ? error.stack : '无'}`);
149
+ // 发生错误时,默认返回 false,避免阻塞推送流程
150
+ return false;
151
+ }
140
152
  }
141
153
  async function getPostUpdateRecord(postId) {
142
- const records = await ctx.database.get('wordpress_post_updates', { postId });
143
- return records.length > 0 ? records[0] : null;
154
+ try {
155
+ ctx.logger.info(`获取文章更新记录,文章 ID: ${postId}`);
156
+ const records = await ctx.database.get('wordpress_post_updates', { postId });
157
+ const result = records.length > 0 ? records[0] : null;
158
+ ctx.logger.info(`获取结果:文章 ${postId} 更新记录:${result ? '找到' : '未找到'}`);
159
+ return result;
160
+ }
161
+ catch (error) {
162
+ const errorMessage = error instanceof Error ? error.message : String(error);
163
+ ctx.logger.error(`获取文章更新记录失败:${errorMessage}`);
164
+ ctx.logger.error(`错误栈:${error instanceof Error ? error.stack : '无'}`);
165
+ // 发生错误时,返回 null,避免阻塞推送流程
166
+ return null;
167
+ }
144
168
  }
145
169
  async function isGroupPushed(groupId, postId) {
146
170
  try {
@@ -200,21 +224,34 @@ function apply(ctx, config) {
200
224
  }
201
225
  async function markGroupAsPushed(groupId, postId, isUpdate) {
202
226
  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(`已存在记录,更新旧记录`);
227
+ ctx.logger.info(`开始标记群 ${groupId} 已推送文章 ${postId}`);
228
+ // 1. 先检查是否已存在记录
229
+ const existingRecords = await ctx.database.get('wordpress_group_pushes', { groupId, postId });
230
+ ctx.logger.info(`检查结果:群 ${groupId} 已推送文章 ${postId}:${existingRecords.length > 0 ? '是' : '否'}`);
231
+ // 2. 执行原子操作(remove + create)
232
+ if (existingRecords.length > 0) {
233
+ ctx.logger.info(`发现现有记录,准备更新,文章 ID: ${postId}`);
208
234
  await ctx.database.remove('wordpress_group_pushes', { groupId, postId });
235
+ ctx.logger.info(`已删除旧记录,文章 ID: ${postId}`);
209
236
  }
210
- ctx.logger.info(`创建新的推送记录,参数:groupId=${groupId}, postId=${postId}, isUpdate=${isUpdate}`);
211
- await ctx.database.create('wordpress_group_pushes', {
237
+ // 3. 创建新记录
238
+ const newRecord = {
212
239
  groupId,
213
240
  postId,
214
241
  pushedAt: new Date(),
215
242
  isUpdate
216
- });
217
- ctx.logger.info(`已标记群 ${groupId} 已推送文章 ${postId}`);
243
+ };
244
+ ctx.logger.info(`准备创建新记录:${JSON.stringify(newRecord)}`);
245
+ await ctx.database.create('wordpress_group_pushes', newRecord);
246
+ ctx.logger.info(`已创建新记录,群 ${groupId},文章 ${postId}`);
247
+ // 4. 写入后验证,确保记录真正存入数据库
248
+ const verification = await ctx.database.get('wordpress_group_pushes', { groupId, postId });
249
+ if (verification.length > 0) {
250
+ ctx.logger.info(`记录写入验证成功,群 ${groupId},文章 ${postId}`);
251
+ }
252
+ else {
253
+ ctx.logger.error(`记录写入验证失败,群 ${groupId},文章 ${postId}`);
254
+ }
218
255
  }
219
256
  catch (error) {
220
257
  // 处理唯一约束冲突错误
@@ -228,7 +265,8 @@ function apply(ctx, config) {
228
265
  ctx.logger.error(`标记推送记录失败:${errorMessage}`);
229
266
  ctx.logger.error(`错误栈:${error instanceof Error ? error.stack : '无'}`);
230
267
  ctx.logger.error(`插入参数:groupId=${groupId}, postId=${postId}, isUpdate=${isUpdate}`);
231
- throw error;
268
+ // 非约束冲突错误,不抛出,确保插件继续运行
269
+ ctx.logger.error(`非致命错误,插件将继续运行`);
232
270
  }
233
271
  }
234
272
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-wordpress-notifier",
3
- "version": "2.4.2",
3
+ "version": "2.5.0",
4
4
  "description": "WordPress 文章自动推送到 QQ",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",