koishi-plugin-kkk 3.7.2 → 3.9.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.
@@ -14,13 +14,17 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.cleanOldDynamicCache = exports.bootstrapDatabases = exports.statisticsDBInstance = exports.bilibiliDBInstance = exports.douyinDBInstance = exports.statisticsDB = exports.bilibiliDB = exports.douyinDB = exports.initAllDatabases = exports.getStatisticsDB = exports.getBilibiliDB = exports.getDouyinDB = void 0;
17
+ exports.cleanOldDynamicCache = exports.bootstrapDatabases = exports.statisticsDBInstance = exports.bilibiliDBInstance = exports.douyinDBInstance = exports.statisticsDB = exports.bilibiliDB = exports.douyinDB = exports.initAllDatabases = exports.getStatisticsDB = exports.getBilibiliDB = exports.getDouyinDB = exports.setDatabaseContext = exports.extendModels = exports.TABLE = void 0;
18
18
  const bilibili_1 = require("./bilibili");
19
19
  const douyin_1 = require("./douyin");
20
+ const model_1 = require("./model");
20
21
  const statistics_1 = require("./statistics");
21
22
  __exportStar(require("./bilibili"), exports);
22
23
  __exportStar(require("./douyin"), exports);
23
24
  __exportStar(require("./statistics"), exports);
25
+ var model_2 = require("./model");
26
+ Object.defineProperty(exports, "TABLE", { enumerable: true, get: function () { return model_2.TABLE; } });
27
+ Object.defineProperty(exports, "extendModels", { enumerable: true, get: function () { return model_2.extendModels; } });
24
28
  /** 抖音数据库实例 */
25
29
  let douyinDB = null;
26
30
  let douyinInitializing = false;
@@ -30,6 +34,26 @@ let bilibiliInitializing = false;
30
34
  /** 统计数据库实例 */
31
35
  let statisticsDB = null;
32
36
  let statisticsInitializing = false;
37
+ /**
38
+ * 数据库用的是 **Koishi 原生数据库服务**(`ctx.database`),
39
+ * 插件自己不再开 sqlite 文件。这里存的就是插件启动时传进来的那个 ctx。
40
+ */
41
+ let dbCtx = null;
42
+ /**
43
+ * 绑定数据库上下文并注册表结构。
44
+ * 插件 apply 阶段调用一次;之后所有数据库实例都从这个 ctx 上取 `ctx.database`。
45
+ */
46
+ const setDatabaseContext = (ctx) => {
47
+ dbCtx = ctx;
48
+ (0, model_1.extendModels)(ctx);
49
+ };
50
+ exports.setDatabaseContext = setDatabaseContext;
51
+ /** 取数据库上下文;还没绑定就抛一个说得清楚的错 */
52
+ const requireCtx = (name) => {
53
+ if (!dbCtx)
54
+ throw new Error('[' + name + '] 数据库尚未初始化,请先等待插件启动完成');
55
+ return dbCtx;
56
+ };
33
57
  /**
34
58
  * 获取或初始化 DouyinDB 实例(单例模式)
35
59
  * @returns DouyinDB实例
@@ -44,7 +68,7 @@ const getDouyinDB = async () => {
44
68
  }
45
69
  douyinInitializing = true;
46
70
  try {
47
- douyinDB = await new douyin_1.DouyinDBBase().init();
71
+ douyinDB = await new douyin_1.DouyinDBBase(requireCtx('DouyinDB')).init();
48
72
  return douyinDB;
49
73
  }
50
74
  finally {
@@ -66,7 +90,7 @@ const getBilibiliDB = async () => {
66
90
  }
67
91
  bilibiliInitializing = true;
68
92
  try {
69
- bilibiliDB = await new bilibili_1.BilibiliDBBase().init();
93
+ bilibiliDB = await new bilibili_1.BilibiliDBBase(requireCtx('BilibiliDB')).init();
70
94
  return bilibiliDB;
71
95
  }
72
96
  finally {
@@ -88,7 +112,7 @@ const getStatisticsDB = async () => {
88
112
  }
89
113
  statisticsInitializing = true;
90
114
  try {
91
- statisticsDB = await new statistics_1.StatisticsDBBase().init();
115
+ statisticsDB = await new statistics_1.StatisticsDBBase(requireCtx('StatisticsDB')).init();
92
116
  return statisticsDB;
93
117
  }
94
118
  finally {
@@ -138,7 +162,8 @@ exports.douyinDBInstance = douyinDB;
138
162
  exports.bilibiliDBInstance = bilibiliDB;
139
163
  exports.statisticsDBInstance = statisticsDB;
140
164
  /** 初始化并填充所有数据库实例(插件启动时调用一次) */
141
- const bootstrapDatabases = async () => {
165
+ const bootstrapDatabases = async (ctx) => {
166
+ (0, exports.setDatabaseContext)(ctx);
142
167
  const result = await (0, exports.initAllDatabases)();
143
168
  douyinDBReal = result.douyinDB;
144
169
  bilibiliDBReal = result.bilibiliDB;
@@ -0,0 +1,200 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extendModels = exports.TABLE = void 0;
4
+ /** 表名常量表 */
5
+ exports.TABLE = {
6
+ douyinBot: 'kkk.douyin.bot',
7
+ douyinGroup: 'kkk.douyin.group',
8
+ douyinUser: 'kkk.douyin.user',
9
+ douyinSubscription: 'kkk.douyin.subscription',
10
+ douyinAwemeCache: 'kkk.douyin.awemeCache',
11
+ douyinFilterWord: 'kkk.douyin.filterWord',
12
+ douyinFilterTag: 'kkk.douyin.filterTag',
13
+ douyinListSnapshot: 'kkk.douyin.listSnapshot',
14
+ bilibiliBot: 'kkk.bilibili.bot',
15
+ bilibiliGroup: 'kkk.bilibili.group',
16
+ bilibiliUser: 'kkk.bilibili.user',
17
+ bilibiliSubscription: 'kkk.bilibili.subscription',
18
+ bilibiliDynamicCache: 'kkk.bilibili.dynamicCache',
19
+ bilibiliFilterWord: 'kkk.bilibili.filterWord',
20
+ bilibiliFilterTag: 'kkk.bilibili.filterTag',
21
+ statsParse: 'kkk.statistics.parse',
22
+ statsHistory: 'kkk.statistics.history',
23
+ statsGlobal: 'kkk.statistics.global',
24
+ statsGroupHistory: 'kkk.statistics.groupHistory',
25
+ statsHour: 'kkk.statistics.hour',
26
+ statsWorkType: 'kkk.statistics.workType',
27
+ statsMetric: 'kkk.statistics.metric',
28
+ };
29
+ /* ------------------------------ 建表 ------------------------------ */
30
+ /** 进程内只建一次(插件热重载时 apply 会再跑一遍,重复 extend 没意义) */
31
+ let modelsReady = false;
32
+ /**
33
+ * 注册插件用到的所有表。
34
+ * 在数据库实例初始化之前调用一次即可;实际建表由 Koishi 的数据库服务负责。
35
+ */
36
+ const extendModels = (ctx) => {
37
+ if (modelsReady)
38
+ return;
39
+ modelsReady = true;
40
+ // ---------- 抖音 ----------
41
+ ctx.model.extend(exports.TABLE.douyinBot, {
42
+ id: 'string',
43
+ createdAt: 'string',
44
+ updatedAt: 'string',
45
+ }, { primary: 'id' });
46
+ ctx.model.extend(exports.TABLE.douyinGroup, {
47
+ id: 'string',
48
+ botId: 'string',
49
+ createdAt: 'string',
50
+ updatedAt: 'string',
51
+ }, { primary: ['id', 'botId'] });
52
+ ctx.model.extend(exports.TABLE.douyinUser, {
53
+ sec_uid: 'string',
54
+ short_id: 'string',
55
+ remark: 'string',
56
+ living: 'boolean',
57
+ filterMode: 'string',
58
+ createdAt: 'string',
59
+ updatedAt: 'string',
60
+ }, { primary: 'sec_uid' });
61
+ ctx.model.extend(exports.TABLE.douyinSubscription, {
62
+ groupId: 'string',
63
+ sec_uid: 'string',
64
+ createdAt: 'string',
65
+ updatedAt: 'string',
66
+ }, { primary: ['groupId', 'sec_uid'] });
67
+ ctx.model.extend(exports.TABLE.douyinAwemeCache, {
68
+ id: 'unsigned',
69
+ aweme_id: 'string',
70
+ sec_uid: 'string',
71
+ groupId: 'string',
72
+ pushType: 'string',
73
+ createdAt: 'string',
74
+ updatedAt: 'string',
75
+ }, { autoInc: true, unique: [['aweme_id', 'sec_uid', 'groupId', 'pushType']] });
76
+ ctx.model.extend(exports.TABLE.douyinFilterWord, {
77
+ id: 'unsigned',
78
+ sec_uid: 'string',
79
+ word: 'string',
80
+ createdAt: 'string',
81
+ updatedAt: 'string',
82
+ }, { autoInc: true, unique: [['sec_uid', 'word']] });
83
+ ctx.model.extend(exports.TABLE.douyinFilterTag, {
84
+ id: 'unsigned',
85
+ sec_uid: 'string',
86
+ tag: 'string',
87
+ createdAt: 'string',
88
+ updatedAt: 'string',
89
+ }, { autoInc: true, unique: [['sec_uid', 'tag']] });
90
+ ctx.model.extend(exports.TABLE.douyinListSnapshot, {
91
+ id: 'unsigned',
92
+ sec_uid: 'string',
93
+ pushType: 'string',
94
+ aweme_id: 'string',
95
+ createdAt: 'string',
96
+ updatedAt: 'string',
97
+ }, { autoInc: true, unique: [['sec_uid', 'pushType', 'aweme_id']] });
98
+ // ---------- B站 ----------
99
+ ctx.model.extend(exports.TABLE.bilibiliBot, {
100
+ id: 'string',
101
+ createdAt: 'string',
102
+ updatedAt: 'string',
103
+ }, { primary: 'id' });
104
+ ctx.model.extend(exports.TABLE.bilibiliGroup, {
105
+ id: 'string',
106
+ botId: 'string',
107
+ createdAt: 'string',
108
+ updatedAt: 'string',
109
+ }, { primary: ['id', 'botId'] });
110
+ ctx.model.extend(exports.TABLE.bilibiliUser, {
111
+ host_mid: 'unsigned',
112
+ remark: 'string',
113
+ filterMode: 'string',
114
+ createdAt: 'string',
115
+ updatedAt: 'string',
116
+ }, { primary: 'host_mid' });
117
+ ctx.model.extend(exports.TABLE.bilibiliSubscription, {
118
+ groupId: 'string',
119
+ host_mid: 'unsigned',
120
+ createdAt: 'string',
121
+ updatedAt: 'string',
122
+ }, { primary: ['groupId', 'host_mid'] });
123
+ ctx.model.extend(exports.TABLE.bilibiliDynamicCache, {
124
+ id: 'unsigned',
125
+ dynamic_id: 'string',
126
+ host_mid: 'unsigned',
127
+ groupId: 'string',
128
+ dynamic_type: 'string',
129
+ createdAt: 'string',
130
+ updatedAt: 'string',
131
+ }, { autoInc: true, unique: [['dynamic_id', 'host_mid', 'groupId']] });
132
+ ctx.model.extend(exports.TABLE.bilibiliFilterWord, {
133
+ id: 'unsigned',
134
+ host_mid: 'unsigned',
135
+ word: 'string',
136
+ createdAt: 'string',
137
+ updatedAt: 'string',
138
+ }, { autoInc: true, unique: [['host_mid', 'word']] });
139
+ ctx.model.extend(exports.TABLE.bilibiliFilterTag, {
140
+ id: 'unsigned',
141
+ host_mid: 'unsigned',
142
+ tag: 'string',
143
+ createdAt: 'string',
144
+ updatedAt: 'string',
145
+ }, { autoInc: true, unique: [['host_mid', 'tag']] });
146
+ // ---------- 统计 ----------
147
+ ctx.model.extend(exports.TABLE.statsParse, {
148
+ id: 'unsigned',
149
+ groupId: 'string',
150
+ userId: 'string',
151
+ platform: 'string',
152
+ parseCount: 'unsigned',
153
+ createdAt: 'string',
154
+ updatedAt: 'string',
155
+ }, { autoInc: true, unique: [['groupId', 'userId', 'platform']] });
156
+ ctx.model.extend(exports.TABLE.statsHistory, {
157
+ id: 'unsigned',
158
+ date: 'string',
159
+ totalParses: 'unsigned',
160
+ douyin: 'unsigned',
161
+ bilibili: 'unsigned',
162
+ kuaishou: 'unsigned',
163
+ xiaohongshu: 'unsigned',
164
+ createdAt: 'string',
165
+ }, { autoInc: true, unique: [['date']] });
166
+ ctx.model.extend(exports.TABLE.statsGlobal, {
167
+ key: 'string',
168
+ value: 'unsigned',
169
+ updatedAt: 'string',
170
+ }, { primary: 'key' });
171
+ ctx.model.extend(exports.TABLE.statsGroupHistory, {
172
+ groupId: 'string',
173
+ date: 'string',
174
+ platform: 'string',
175
+ parseCount: 'unsigned',
176
+ updatedAt: 'string',
177
+ }, { primary: ['groupId', 'date', 'platform'] });
178
+ ctx.model.extend(exports.TABLE.statsHour, {
179
+ groupId: 'string',
180
+ hour: 'unsigned',
181
+ platform: 'string',
182
+ parseCount: 'unsigned',
183
+ updatedAt: 'string',
184
+ }, { primary: ['groupId', 'hour', 'platform'] });
185
+ ctx.model.extend(exports.TABLE.statsWorkType, {
186
+ groupId: 'string',
187
+ platform: 'string',
188
+ workType: 'string',
189
+ parseCount: 'unsigned',
190
+ updatedAt: 'string',
191
+ }, { primary: ['groupId', 'platform', 'workType'] });
192
+ ctx.model.extend(exports.TABLE.statsMetric, {
193
+ groupId: 'string',
194
+ metric: 'string',
195
+ bucket: 'string',
196
+ parseCount: 'unsigned',
197
+ updatedAt: 'string',
198
+ }, { primary: ['groupId', 'metric', 'bucket'] });
199
+ };
200
+ exports.extendModels = extendModels;