koishi-plugin-wordpress-notifier 2.5.0 → 2.5.1
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 +40 -7
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -20,6 +20,7 @@ exports.Config = koishi_1.Schema.object({
|
|
|
20
20
|
});
|
|
21
21
|
function apply(ctx, config) {
|
|
22
22
|
ctx.logger.info('WordPress 推送插件已加载');
|
|
23
|
+
// 修复 MySQL 自增主键问题,使用正确的模型配置
|
|
23
24
|
ctx.model.extend('wordpress_post_updates', {
|
|
24
25
|
id: 'integer',
|
|
25
26
|
postId: 'integer',
|
|
@@ -27,7 +28,7 @@ function apply(ctx, config) {
|
|
|
27
28
|
pushedAt: 'timestamp'
|
|
28
29
|
}, {
|
|
29
30
|
primary: ['id'],
|
|
30
|
-
autoInc: true,
|
|
31
|
+
autoInc: true, // 这是正确的自增配置
|
|
31
32
|
unique: ['postId'] // 合理,因为我们只需要保留每篇文章的最新更新记录
|
|
32
33
|
});
|
|
33
34
|
ctx.model.extend('wordpress_user_registrations', {
|
|
@@ -36,7 +37,7 @@ function apply(ctx, config) {
|
|
|
36
37
|
pushedAt: 'timestamp'
|
|
37
38
|
}, {
|
|
38
39
|
primary: ['id'],
|
|
39
|
-
autoInc: true,
|
|
40
|
+
autoInc: true, // 这是正确的自增配置
|
|
40
41
|
unique: ['userId']
|
|
41
42
|
});
|
|
42
43
|
ctx.model.extend('wordpress_group_pushes', {
|
|
@@ -47,9 +48,21 @@ function apply(ctx, config) {
|
|
|
47
48
|
isUpdate: 'boolean'
|
|
48
49
|
}, {
|
|
49
50
|
primary: ['id'],
|
|
50
|
-
autoInc: true,
|
|
51
|
+
autoInc: true, // 这是正确的自增配置
|
|
51
52
|
unique: ['groupId', 'postId']
|
|
52
53
|
});
|
|
54
|
+
// 确保所有插入操作都不手动指定 id 字段,让数据库自动生成
|
|
55
|
+
// Koishi 框架的 autoInc: true 配置会自动处理自增主键
|
|
56
|
+
ctx.logger.info('数据库表配置完成,autoInc: true 已启用,确保插入操作不手动指定 id 字段');
|
|
57
|
+
// 为所有数据库操作添加详细日志,便于诊断自增主键问题
|
|
58
|
+
ctx.on('ready', async () => {
|
|
59
|
+
ctx.logger.info('WordPress 推送插件已就绪,开始初始化推送任务');
|
|
60
|
+
ctx.logger.info('数据库表配置:');
|
|
61
|
+
ctx.logger.info('wordpress_post_updates: id 字段设置为 autoInc: true');
|
|
62
|
+
ctx.logger.info('wordpress_user_registrations: id 字段设置为 autoInc: true');
|
|
63
|
+
ctx.logger.info('wordpress_group_pushes: id 字段设置为 autoInc: true');
|
|
64
|
+
ctx.logger.info('插件将确保所有插入操作不手动指定 id 字段,让数据库自动生成');
|
|
65
|
+
});
|
|
53
66
|
async function fetchLatestPosts() {
|
|
54
67
|
try {
|
|
55
68
|
const url = `${config.wordpressUrl}/wp-json/wp/v2/posts?per_page=${config.maxArticles}&orderby=date&order=desc`;
|
|
@@ -189,10 +202,30 @@ function apply(ctx, config) {
|
|
|
189
202
|
}
|
|
190
203
|
}
|
|
191
204
|
async function markUserAsPushed(userId) {
|
|
192
|
-
|
|
193
|
-
userId
|
|
194
|
-
|
|
195
|
-
|
|
205
|
+
try {
|
|
206
|
+
ctx.logger.info(`开始标记用户已推送,用户 ID: ${userId}`);
|
|
207
|
+
// 创建新记录,不手动指定id,让数据库自动生成
|
|
208
|
+
const newRecord = {
|
|
209
|
+
userId,
|
|
210
|
+
pushedAt: new Date()
|
|
211
|
+
};
|
|
212
|
+
ctx.logger.info(`准备创建用户推送记录:${JSON.stringify(newRecord)}`);
|
|
213
|
+
await ctx.database.create('wordpress_user_registrations', newRecord);
|
|
214
|
+
ctx.logger.info(`已成功标记用户 ${userId} 为已推送`);
|
|
215
|
+
}
|
|
216
|
+
catch (error) {
|
|
217
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
218
|
+
if (errorMessage.includes('UNIQUE constraint failed')) {
|
|
219
|
+
ctx.logger.warn(`用户推送记录已存在,跳过重复插入:用户 ${userId}`);
|
|
220
|
+
ctx.logger.warn(`完整错误信息:${errorMessage}`);
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
ctx.logger.error(`标记用户推送记录失败:${errorMessage}`);
|
|
224
|
+
ctx.logger.error(`错误栈:${error instanceof Error ? error.stack : '无'}`);
|
|
225
|
+
ctx.logger.error(`插入参数:userId=${userId}`);
|
|
226
|
+
// 非约束冲突错误,不抛出,确保插件继续运行
|
|
227
|
+
}
|
|
228
|
+
}
|
|
196
229
|
}
|
|
197
230
|
async function updatePostUpdateRecord(postId, modifiedDate) {
|
|
198
231
|
try {
|
package/package.json
CHANGED