koishi-plugin-best-cave 2.4.0 → 2.4.2
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/Utils.d.ts +82 -0
- package/lib/index.js +8 -6
- package/package.json +1 -1
package/lib/Utils.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Context, h, Logger, Session } from 'koishi';
|
|
2
|
+
import { CaveObject, Config, StoredElement } from './index';
|
|
3
|
+
import { FileManager } from './FileManager';
|
|
4
|
+
import { HashManager, CaveHashObject } from './HashManager';
|
|
5
|
+
import { PendManager } from './PendManager';
|
|
6
|
+
/**
|
|
7
|
+
* @description 构建一条用于发送的完整回声洞消息,处理不同存储后端的资源链接。
|
|
8
|
+
* @param cave 回声洞对象。
|
|
9
|
+
* @param config 插件配置。
|
|
10
|
+
* @param fileManager 文件管理器实例。
|
|
11
|
+
* @param logger 日志记录器实例。
|
|
12
|
+
* @param platform 目标平台名称 (e.g., 'onebot')。
|
|
13
|
+
* @param prefix 可选的消息前缀 (e.g., '已删除', '待审核')。
|
|
14
|
+
* @returns 包含多条消息的数组,每条消息是一个 (string | h)[] 数组。
|
|
15
|
+
*/
|
|
16
|
+
export declare function buildCaveMessage(cave: CaveObject, config: Config, fileManager: FileManager, logger: Logger, platform?: string, prefix?: string): Promise<(string | h)[][]>;
|
|
17
|
+
/**
|
|
18
|
+
* @description 清理数据库中标记为 'delete' 状态的回声洞及其关联文件和哈希。
|
|
19
|
+
* @param ctx Koishi 上下文。
|
|
20
|
+
* @param fileManager 文件管理器实例。
|
|
21
|
+
* @param logger 日志记录器实例。
|
|
22
|
+
* @param reusableIds 可复用 ID 的内存缓存。
|
|
23
|
+
*/
|
|
24
|
+
export declare function cleanupPendingDeletions(ctx: Context, fileManager: FileManager, logger: Logger, reusableIds: Set<number>): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* @description 根据配置和会话,生成数据库查询的范围条件。
|
|
27
|
+
* @param session 当前会话。
|
|
28
|
+
* @param config 插件配置。
|
|
29
|
+
* @param includeStatus 是否包含 status: 'active' 条件,默认为 true。
|
|
30
|
+
* @returns 数据库查询条件对象。
|
|
31
|
+
*/
|
|
32
|
+
export declare function getScopeQuery(session: Session, config: Config, includeStatus?: boolean): object;
|
|
33
|
+
/**
|
|
34
|
+
* @description 获取下一个可用的回声洞 ID,采用“回收ID > 扫描空缺 > 最大ID+1”策略。
|
|
35
|
+
* @param ctx Koishi 上下文。
|
|
36
|
+
* @param reusableIds 可复用 ID 的内存缓存。
|
|
37
|
+
* @returns 可用的新 ID。
|
|
38
|
+
*/
|
|
39
|
+
export declare function getNextCaveId(ctx: Context, reusableIds: Set<number>): Promise<number>;
|
|
40
|
+
/**
|
|
41
|
+
* @description 检查用户是否处于指令冷却中。
|
|
42
|
+
* @returns 若在冷却中则提示字符串,否则 null。
|
|
43
|
+
*/
|
|
44
|
+
export declare function checkCooldown(session: Session, config: Config, lastUsed: Map<string, number>): string | null;
|
|
45
|
+
/**
|
|
46
|
+
* @description 更新指定频道的指令使用时间戳。
|
|
47
|
+
*/
|
|
48
|
+
export declare function updateCooldownTimestamp(session: Session, config: Config, lastUsed: Map<string, number>): void;
|
|
49
|
+
/**
|
|
50
|
+
* @description 解析消息元素,分离出文本和待下载的媒体文件。
|
|
51
|
+
* @param sourceElements 原始的 Koishi 消息元素数组。
|
|
52
|
+
* @param newId 这条回声洞的新 ID。
|
|
53
|
+
* @param session 触发操作的会话。
|
|
54
|
+
* @param config 插件配置。
|
|
55
|
+
* @param logger 日志实例。
|
|
56
|
+
* @returns 包含数据库元素和待保存媒体列表的对象。
|
|
57
|
+
*/
|
|
58
|
+
export declare function processMessageElements(sourceElements: h[], newId: number, session: Session, config: Config, logger: Logger): Promise<{
|
|
59
|
+
finalElementsForDb: StoredElement[];
|
|
60
|
+
mediaToSave: {
|
|
61
|
+
sourceUrl: string;
|
|
62
|
+
fileName: string;
|
|
63
|
+
}[];
|
|
64
|
+
}>;
|
|
65
|
+
/**
|
|
66
|
+
* @description 异步处理文件上传、查重和状态更新的后台任务。
|
|
67
|
+
* @param ctx - Koishi 上下文。
|
|
68
|
+
* @param config - 插件配置。
|
|
69
|
+
* @param fileManager - FileManager 实例,用于保存文件。
|
|
70
|
+
* @param logger - 日志记录器实例。
|
|
71
|
+
* @param reviewManager - ReviewManager 实例,用于提交审核。
|
|
72
|
+
* @param cave - 刚刚在数据库中创建的 `preload` 状态的回声洞对象。
|
|
73
|
+
* @param mediaToSave - 需要下载和处理的媒体文件列表。
|
|
74
|
+
* @param reusableIds - 可复用 ID 的内存缓存。
|
|
75
|
+
* @param session - 触发此操作的用户会话,用于发送反馈。
|
|
76
|
+
* @param hashManager - HashManager 实例,如果启用则用于哈希计算和比较。
|
|
77
|
+
* @param textHashesToStore - 已预先计算好的、待存入数据库的文本哈希对象数组。
|
|
78
|
+
*/
|
|
79
|
+
export declare function handleFileUploads(ctx: Context, config: Config, fileManager: FileManager, logger: Logger, reviewManager: PendManager, cave: CaveObject, mediaToToSave: {
|
|
80
|
+
sourceUrl: string;
|
|
81
|
+
fileName: string;
|
|
82
|
+
}[], reusableIds: Set<number>, session: Session, hashManager: HashManager, textHashesToStore: Omit<CaveHashObject, 'cave'>[]): Promise<void>;
|
package/lib/index.js
CHANGED
|
@@ -377,7 +377,7 @@ function getScopeQuery(session, config, includeStatus = true) {
|
|
|
377
377
|
return config.perChannel && session.channelId ? { ...baseQuery, channelId: session.channelId } : baseQuery;
|
|
378
378
|
}
|
|
379
379
|
__name(getScopeQuery, "getScopeQuery");
|
|
380
|
-
async function getNextCaveId(ctx,
|
|
380
|
+
async function getNextCaveId(ctx, reusableIds) {
|
|
381
381
|
for (const id of reusableIds) {
|
|
382
382
|
if (id > 0) {
|
|
383
383
|
reusableIds.delete(id);
|
|
@@ -386,12 +386,12 @@ async function getNextCaveId(ctx, query = {}, reusableIds) {
|
|
|
386
386
|
}
|
|
387
387
|
if (reusableIds.has(0)) {
|
|
388
388
|
reusableIds.delete(0);
|
|
389
|
-
const [lastCave] = await ctx.database.get("cave",
|
|
389
|
+
const [lastCave] = await ctx.database.get("cave", {}, { sort: { id: "desc" }, limit: 1 });
|
|
390
390
|
const newId2 = (lastCave?.id || 0) + 1;
|
|
391
391
|
reusableIds.add(0);
|
|
392
392
|
return newId2;
|
|
393
393
|
}
|
|
394
|
-
const allCaveIds = (await ctx.database.get("cave",
|
|
394
|
+
const allCaveIds = (await ctx.database.get("cave", {}, { fields: ["id"] })).map((c) => c.id);
|
|
395
395
|
const existingIds = new Set(allCaveIds);
|
|
396
396
|
let newId = 1;
|
|
397
397
|
while (existingIds.has(newId)) newId++;
|
|
@@ -506,7 +506,8 @@ async function handleFileUploads(ctx, config, fileManager, logger2, reviewManage
|
|
|
506
506
|
}
|
|
507
507
|
}
|
|
508
508
|
await Promise.all(downloadedMedia.map((item) => fileManager.saveFile(item.fileName, item.buffer)));
|
|
509
|
-
const
|
|
509
|
+
const needsReview = config.enablePend && session.channelId !== config.adminChannel?.split(":")[1];
|
|
510
|
+
const finalStatus = needsReview ? "pending" : "active";
|
|
510
511
|
await ctx.database.upsert("cave", [{ id: cave.id, status: finalStatus }]);
|
|
511
512
|
if (hashManager) {
|
|
512
513
|
const allHashesToInsert = [...textHashesToStore, ...imageHashesToStore].map((h4) => ({ ...h4, cave: cave.id }));
|
|
@@ -1054,7 +1055,7 @@ function apply(ctx, config) {
|
|
|
1054
1055
|
if (!reply) return "等待操作超时";
|
|
1055
1056
|
sourceElements = import_koishi3.h.parse(reply);
|
|
1056
1057
|
}
|
|
1057
|
-
const newId = await getNextCaveId(ctx,
|
|
1058
|
+
const newId = await getNextCaveId(ctx, reusableIds);
|
|
1058
1059
|
const { finalElementsForDb, mediaToSave } = await processMessageElements(sourceElements, newId, session, config, logger);
|
|
1059
1060
|
if (finalElementsForDb.length === 0) return "无可添加内容";
|
|
1060
1061
|
const textHashesToStore = [];
|
|
@@ -1074,7 +1075,8 @@ function apply(ctx, config) {
|
|
|
1074
1075
|
}
|
|
1075
1076
|
const userName = (config.enableName ? await profileManager.getNickname(session.userId) : null) || session.username;
|
|
1076
1077
|
const hasMedia = mediaToSave.length > 0;
|
|
1077
|
-
const
|
|
1078
|
+
const needsReview = config.enablePend && session.channelId !== config.adminChannel?.split(":")[1];
|
|
1079
|
+
const initialStatus = hasMedia ? "preload" : needsReview ? "pending" : "active";
|
|
1078
1080
|
const newCave = await ctx.database.create("cave", {
|
|
1079
1081
|
id: newId,
|
|
1080
1082
|
elements: finalElementsForDb,
|