koishi-plugin-wordpress-notifier 1.0.0 → 1.1.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.
- package/lib/index.d.ts +11 -0
- package/lib/index.js +18 -9
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { Context, Schema } from 'koishi';
|
|
2
|
+
export declare const inject: string[];
|
|
2
3
|
export declare const name = "wordpress-notifier";
|
|
4
|
+
declare module 'koishi' {
|
|
5
|
+
interface Tables {
|
|
6
|
+
wordpress_posts: WordPressPostRecord;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
3
9
|
export interface Config {
|
|
4
10
|
wordpressUrl: string;
|
|
5
11
|
interval: number;
|
|
@@ -22,5 +28,10 @@ export interface WordPressPost {
|
|
|
22
28
|
categories: number[];
|
|
23
29
|
tags: number[];
|
|
24
30
|
}
|
|
31
|
+
export interface WordPressPostRecord {
|
|
32
|
+
id: number;
|
|
33
|
+
postId: number;
|
|
34
|
+
pushedAt: Date;
|
|
35
|
+
}
|
|
25
36
|
export declare const Config: Schema<Config>;
|
|
26
37
|
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Config = exports.name = void 0;
|
|
3
|
+
exports.Config = exports.name = exports.inject = void 0;
|
|
4
4
|
exports.apply = apply;
|
|
5
5
|
const koishi_1 = require("koishi");
|
|
6
|
+
exports.inject = ['database', 'http'];
|
|
6
7
|
exports.name = 'wordpress-notifier';
|
|
7
8
|
exports.Config = koishi_1.Schema.object({
|
|
8
9
|
wordpressUrl: koishi_1.Schema.string().description('WordPress 网站地址(例如:https://example.com)'),
|
|
@@ -14,7 +15,11 @@ exports.Config = koishi_1.Schema.object({
|
|
|
14
15
|
});
|
|
15
16
|
function apply(ctx, config) {
|
|
16
17
|
ctx.logger.info('WordPress 推送插件已加载');
|
|
17
|
-
|
|
18
|
+
ctx.model.extend('wordpress_posts', {
|
|
19
|
+
id: 'unsigned',
|
|
20
|
+
postId: 'integer',
|
|
21
|
+
pushedAt: 'timestamp'
|
|
22
|
+
});
|
|
18
23
|
async function fetchLatestPosts() {
|
|
19
24
|
try {
|
|
20
25
|
const url = `${config.wordpressUrl}/wp-json/wp/v2/posts?per_page=${config.maxArticles}&orderby=date&order=desc`;
|
|
@@ -28,11 +33,15 @@ function apply(ctx, config) {
|
|
|
28
33
|
return [];
|
|
29
34
|
}
|
|
30
35
|
}
|
|
31
|
-
function isPostPushed(postId) {
|
|
32
|
-
|
|
36
|
+
async function isPostPushed(postId) {
|
|
37
|
+
const record = await ctx.database.get('wordpress_posts', { postId });
|
|
38
|
+
return record.length > 0;
|
|
33
39
|
}
|
|
34
|
-
function markPostAsPushed(postId) {
|
|
35
|
-
|
|
40
|
+
async function markPostAsPushed(postId) {
|
|
41
|
+
await ctx.database.create('wordpress_posts', {
|
|
42
|
+
postId,
|
|
43
|
+
pushedAt: new Date()
|
|
44
|
+
});
|
|
36
45
|
}
|
|
37
46
|
function formatPostMessage(post, mention = false) {
|
|
38
47
|
const title = post.title.rendered.replace(/<[^>]*>/g, '');
|
|
@@ -55,18 +64,18 @@ function apply(ctx, config) {
|
|
|
55
64
|
if (posts.length === 0)
|
|
56
65
|
return;
|
|
57
66
|
for (const post of posts) {
|
|
58
|
-
if (!isPostPushed(post.id)) {
|
|
67
|
+
if (!(await isPostPushed(post.id))) {
|
|
59
68
|
const message = formatPostMessage(post, true);
|
|
60
69
|
for (const target of config.targets) {
|
|
61
70
|
try {
|
|
62
|
-
await ctx.broadcast([
|
|
71
|
+
await ctx.broadcast([target], message);
|
|
63
72
|
ctx.logger.info(`已推送文章到 ${target}: ${post.title.rendered}`);
|
|
64
73
|
}
|
|
65
74
|
catch (error) {
|
|
66
75
|
ctx.logger.error(`推送文章到 ${target} 失败: ${error}`);
|
|
67
76
|
}
|
|
68
77
|
}
|
|
69
|
-
markPostAsPushed(post.id);
|
|
78
|
+
await markPostAsPushed(post.id);
|
|
70
79
|
}
|
|
71
80
|
}
|
|
72
81
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koishi-plugin-wordpress-notifier",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "WordPress 文章自动推送到 QQ",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
],
|
|
35
35
|
"repository": {
|
|
36
36
|
"type": "git",
|
|
37
|
-
"url": "https://github.com/
|
|
37
|
+
"url": "https://github.com/Lexo0522/koishi-plugin-wordpress-notifier.git"
|
|
38
38
|
},
|
|
39
39
|
"homepage": "https://github.com/your-username/koishi-plugin-wordpress-notifier#readme",
|
|
40
40
|
"bugs": {
|