koishi-plugin-wordpress-notifier 2.5.3 → 2.5.4
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 +61 -1
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -62,9 +62,69 @@ function apply(ctx, config) {
|
|
|
62
62
|
ctx.logger.info('数据库表配置:');
|
|
63
63
|
ctx.logger.info('wordpress_post_updates: id 字段设置为 autoInc: true');
|
|
64
64
|
ctx.logger.info('wordpress_user_registrations: id 字段设置为 autoInc: true');
|
|
65
|
-
ctx.logger.info('wordpress_group_pushes: id 字段设置为 autoInc: true');
|
|
65
|
+
ctx.logger.info('wordpress_group_pushes: id 字段设置为 autoInc: true,唯一键为 groupId+postId');
|
|
66
66
|
ctx.logger.info('插件将确保所有插入操作不手动指定 id 字段,让数据库自动生成');
|
|
67
|
+
// 检查并修复数据库表结构问题
|
|
68
|
+
await checkAndFixTableStructure();
|
|
67
69
|
});
|
|
70
|
+
// 检查并修复数据库表结构的函数
|
|
71
|
+
async function checkAndFixTableStructure() {
|
|
72
|
+
try {
|
|
73
|
+
ctx.logger.info('开始检查数据库表结构...');
|
|
74
|
+
// 获取表中的所有记录,检查是否存在相同 groupId 不同 postId 的记录
|
|
75
|
+
const allRecords = await ctx.database.get('wordpress_group_pushes', {});
|
|
76
|
+
ctx.logger.info(`当前 wordpress_group_pushes 表共有 ${allRecords.length} 条记录`);
|
|
77
|
+
// 检查是否存在相同 groupId 不同 postId 的记录
|
|
78
|
+
const groupPostMap = new Map();
|
|
79
|
+
let hasDuplicateGroupId = false;
|
|
80
|
+
for (const record of allRecords) {
|
|
81
|
+
const groupId = record.groupId;
|
|
82
|
+
const postId = record.postId;
|
|
83
|
+
if (!groupPostMap.has(groupId)) {
|
|
84
|
+
groupPostMap.set(groupId, new Set());
|
|
85
|
+
}
|
|
86
|
+
const postIds = groupPostMap.get(groupId);
|
|
87
|
+
if (postIds.has(postId)) {
|
|
88
|
+
ctx.logger.warn(`发现重复记录:groupId=${groupId}, postId=${postId}`);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
postIds.add(postId);
|
|
92
|
+
}
|
|
93
|
+
// 如果一个群有多个不同的 postId,说明表结构正确
|
|
94
|
+
if (postIds.size > 1) {
|
|
95
|
+
hasDuplicateGroupId = true;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (hasDuplicateGroupId) {
|
|
99
|
+
ctx.logger.info('检测到同一群有多个不同文章记录,表结构正确');
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
ctx.logger.warn('检测到每个群只有一个文章记录,可能存在表结构问题');
|
|
103
|
+
ctx.logger.warn('建议检查 wordpress_group_pushes 表的唯一键设置,应设置为 groupId+postId 的联合唯一键');
|
|
104
|
+
ctx.logger.warn('当前表结构可能导致一个群只能推送一篇文章');
|
|
105
|
+
}
|
|
106
|
+
// 尝试通过重新创建表来修复结构(使用 Koishi 内置的表重建机制)
|
|
107
|
+
ctx.logger.info('尝试通过 Koishi 模型扩展机制修复表结构...');
|
|
108
|
+
// 重新扩展模型,确保唯一键设置正确
|
|
109
|
+
ctx.model.extend('wordpress_group_pushes', {
|
|
110
|
+
id: 'integer',
|
|
111
|
+
groupId: 'string',
|
|
112
|
+
postId: 'integer',
|
|
113
|
+
pushedAt: 'timestamp',
|
|
114
|
+
isUpdate: 'boolean'
|
|
115
|
+
}, {
|
|
116
|
+
primary: 'id',
|
|
117
|
+
autoInc: true,
|
|
118
|
+
unique: ['groupId', 'postId']
|
|
119
|
+
});
|
|
120
|
+
ctx.logger.info('表结构检查和修复完成');
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
124
|
+
ctx.logger.error(`检查表结构失败:${errorMessage}`);
|
|
125
|
+
ctx.logger.error(`错误栈:${error instanceof Error ? error.stack : '无'}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
68
128
|
async function fetchLatestPosts() {
|
|
69
129
|
try {
|
|
70
130
|
const url = `${config.wordpressUrl}/wp-json/wp/v2/posts?per_page=${config.maxArticles}&orderby=date&order=desc`;
|