koishi-plugin-best-cave 1.5.3 → 1.5.5

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.
@@ -0,0 +1,43 @@
1
+ import { Context } from 'koishi';
2
+ import { Config } from '../index';
3
+ import { IdManager } from './IdManager';
4
+ export interface CaveObject {
5
+ cave_id: number;
6
+ elements: Element[];
7
+ contributor_number: string;
8
+ contributor_name: string;
9
+ }
10
+ interface BaseElement {
11
+ type: 'text' | 'img' | 'video';
12
+ index: number;
13
+ }
14
+ interface TextElement extends BaseElement {
15
+ type: 'text';
16
+ content: string;
17
+ }
18
+ interface MediaElement extends BaseElement {
19
+ type: 'img' | 'video';
20
+ file?: string;
21
+ fileName?: string;
22
+ fileSize?: string;
23
+ filePath?: string;
24
+ }
25
+ type Element = TextElement | MediaElement;
26
+ export interface PendingCave extends CaveObject {
27
+ }
28
+ export declare class AuditManager {
29
+ private ctx;
30
+ private config;
31
+ private caveDir;
32
+ private idManager;
33
+ private logger;
34
+ constructor(ctx: Context, config: Config, caveDir: string, idManager: IdManager);
35
+ processAudit(pendingData: PendingCave[], isApprove: boolean, caveFilePath: string, resourceDir: string, pendingFilePath: string, session: any, targetId?: number): Promise<string>;
36
+ private handleSingleAudit;
37
+ private handleBatchAudit;
38
+ sendAuditMessage(cave: PendingCave, content: string, session: any): Promise<void>;
39
+ private deleteMediaFiles;
40
+ private cleanElementsForSave;
41
+ private sendMessage;
42
+ }
43
+ export {};
@@ -0,0 +1,80 @@
1
+ import { Buffer } from 'buffer';
2
+ /**
3
+ * 图片哈希计算工具类
4
+ * 使用 DCT(离散余弦变换)方法计算图片的感知哈希值,可用于图片相似度比较
5
+ */
6
+ export declare class ContentHasher {
7
+ /**
8
+ * 计算图片的感知哈希值
9
+ * @param imageBuffer - 图片的二进制数据
10
+ * @returns 返回64位的十六进制哈希字符串
11
+ * @throws 当图片处理失败时可能抛出错误
12
+ */
13
+ static calculateHash(imageBuffer: Buffer): Promise<string>;
14
+ /**
15
+ * 将二进制字符串转换为十六进制
16
+ * @param binary - 二进制字符串
17
+ * @returns 十六进制字符串
18
+ * @private
19
+ */
20
+ private static binaryToHex;
21
+ /**
22
+ * 将十六进制字符串转换为二进制
23
+ * @param hex - 十六进制字符串
24
+ * @returns 二进制字符串
25
+ * @private
26
+ */
27
+ private static hexToBinary;
28
+ /**
29
+ * 计算图像的DCT(离散余弦变换)
30
+ * @param data - 图像数据
31
+ * @param size - 图像尺寸
32
+ * @returns DCT变换后的矩阵
33
+ * @private
34
+ */
35
+ private static computeDCT;
36
+ /**
37
+ * 获取DCT系数
38
+ * @param index - 索引值
39
+ * @param size - 矩阵大小
40
+ * @returns DCT系数
41
+ * @private
42
+ */
43
+ private static getDCTCoefficient;
44
+ /**
45
+ * 计算数组的中位数
46
+ * @param arr - 输入数组
47
+ * @returns 中位数
48
+ * @private
49
+ */
50
+ private static calculateMedian;
51
+ /**
52
+ * 从DCT矩阵中提取特征值
53
+ * @param matrix - DCT矩阵
54
+ * @param size - 矩阵大小
55
+ * @returns 特征值数组
56
+ * @private
57
+ */
58
+ private static extractFeatures;
59
+ /**
60
+ * 计算两个哈希值之间的汉明距离
61
+ * @param hash1 - 第一个哈希值
62
+ * @param hash2 - 第二个哈希值
63
+ * @returns 汉明距离
64
+ * @throws 当两个哈希值长度不等时抛出错误
65
+ */
66
+ static calculateDistance(hash1: string, hash2: string): number;
67
+ /**
68
+ * 计算两个图片哈希值的相似度
69
+ * @param hash1 - 第一个哈希值
70
+ * @param hash2 - 第二个哈希值
71
+ * @returns 返回0-1之间的相似度值,1表示完全相同,0表示完全不同
72
+ */
73
+ static calculateSimilarity(hash1: string, hash2: string): number;
74
+ /**
75
+ * 计算文本的哈希值
76
+ * @param text - 输入文本
77
+ * @returns 文本的哈希值(36进制字符串)
78
+ */
79
+ static calculateTextHash(text: string): string;
80
+ }
@@ -0,0 +1,63 @@
1
+ export declare class FileHandler {
2
+ private static locks;
3
+ private static readonly RETRY_COUNT;
4
+ private static readonly RETRY_DELAY;
5
+ private static readonly CONCURRENCY_LIMIT;
6
+ /**
7
+ * 并发控制
8
+ * @param operation 要执行的操作
9
+ * @param limit 并发限制
10
+ * @returns 操作结果
11
+ */
12
+ private static withConcurrencyLimit;
13
+ /**
14
+ * 文件操作包装器
15
+ * @param filePath 文件路径
16
+ * @param operation 要执行的操作
17
+ * @returns 操作结果
18
+ */
19
+ private static withFileOp;
20
+ /**
21
+ * 事务处理
22
+ * @param operations 要执行的操作数组
23
+ * @returns 操作结果数组
24
+ */
25
+ static withTransaction<T>(operations: Array<{
26
+ filePath: string;
27
+ operation: () => Promise<T>;
28
+ rollback?: () => Promise<void>;
29
+ }>): Promise<T[]>;
30
+ /**
31
+ * 读取 JSON 数据
32
+ * @param filePath 文件路径
33
+ * @returns JSON 数据
34
+ */
35
+ static readJsonData<T>(filePath: string): Promise<T[]>;
36
+ /**
37
+ * 写入 JSON 数据
38
+ * @param filePath 文件路径
39
+ * @param data 要写入的数据
40
+ */
41
+ static writeJsonData<T>(filePath: string, data: T[]): Promise<void>;
42
+ /**
43
+ * 确保目录存在
44
+ * @param dir 目录路径
45
+ */
46
+ static ensureDirectory(dir: string): Promise<void>;
47
+ /**
48
+ * 确保 JSON 文件存在
49
+ * @param filePath 文件路径
50
+ */
51
+ static ensureJsonFile(filePath: string): Promise<void>;
52
+ /**
53
+ * 保存媒体文件
54
+ * @param filePath 文件路径
55
+ * @param data 文件数据
56
+ */
57
+ static saveMediaFile(filePath: string, data: Buffer | string): Promise<void>;
58
+ /**
59
+ * 删除媒体文件
60
+ * @param filePath 文件路径
61
+ */
62
+ static deleteMediaFile(filePath: string): Promise<void>;
63
+ }
@@ -0,0 +1,108 @@
1
+ /**
2
+ * 哈希存储状态类型
3
+ */
4
+ interface HashStorageStatus {
5
+ lastUpdated: string;
6
+ entries: Array<{
7
+ caveId: number;
8
+ imageHashes: string[];
9
+ textHashes: string[];
10
+ }>;
11
+ }
12
+ /**
13
+ * 图片哈希值存储管理类
14
+ * 负责管理和维护回声洞图片的哈希值
15
+ */
16
+ export declare class HashManager {
17
+ private readonly caveDir;
18
+ private static readonly HASH_FILE;
19
+ private static readonly CAVE_FILE;
20
+ private static readonly BATCH_SIZE;
21
+ private imageHashes;
22
+ private textHashes;
23
+ private initialized;
24
+ /**
25
+ * 初始化HashManager实例
26
+ * @param caveDir 回声洞数据目录路径
27
+ */
28
+ constructor(caveDir: string);
29
+ private get filePath();
30
+ private get resourceDir();
31
+ private get caveFilePath();
32
+ /**
33
+ * 初始化哈希存储
34
+ * 读取现有哈希数据或重新构建哈希值
35
+ * @throws 初始化失败时抛出错误
36
+ */
37
+ initialize(): Promise<void>;
38
+ /**
39
+ * 获取当前哈希存储状态
40
+ * @returns 包含最后更新时间和所有条目的状态对象
41
+ */
42
+ getStatus(): Promise<HashStorageStatus>;
43
+ /**
44
+ * 更新指定回声洞的图片哈希值
45
+ * @param caveId 回声洞ID
46
+ * @param content 图片buffer数组
47
+ */
48
+ updateCaveContent(caveId: number, content: {
49
+ images?: Buffer[];
50
+ texts?: string[];
51
+ }): Promise<void>;
52
+ /**
53
+ * 更新所有回声洞的哈希值
54
+ * @param isInitialBuild 是否为初始构建
55
+ */
56
+ updateAllCaves(isInitialBuild?: boolean): Promise<void>;
57
+ /**
58
+ * 查找重复的图片
59
+ * @param content 待查找的图片buffer数组
60
+ * @param thresholds 相似度阈值
61
+ * @returns 匹配结果数组,包含索引、回声洞ID和相似度
62
+ */
63
+ findDuplicates(content: {
64
+ images?: Buffer[];
65
+ texts?: string[];
66
+ }, thresholds: {
67
+ image: number;
68
+ text: number;
69
+ }): Promise<Array<{
70
+ type: 'image' | 'text';
71
+ index: number;
72
+ caveId: number;
73
+ similarity: number;
74
+ } | null>>;
75
+ private findTextDuplicates;
76
+ private calculateTextSimilarity;
77
+ private findImageDuplicates;
78
+ /**
79
+ * 加载回声洞数据
80
+ * @returns 回声洞数据数组
81
+ * @private
82
+ */
83
+ private loadCaveData;
84
+ /**
85
+ * 保存哈希数据到文件
86
+ * @private
87
+ */
88
+ private saveContentHashes;
89
+ /**
90
+ * 构建初始哈希数据
91
+ * @private
92
+ */
93
+ private buildInitialHashes;
94
+ /**
95
+ * 更新缺失的哈希值
96
+ * @private
97
+ */
98
+ private updateMissingHashes;
99
+ /**
100
+ * 批量处理数组项
101
+ * @param items 待处理项数组
102
+ * @param processor 处理函数
103
+ * @param batchSize 批处理大小
104
+ * @private
105
+ */
106
+ private processBatch;
107
+ }
108
+ export {};
@@ -0,0 +1,69 @@
1
+ /**
2
+ * ID管理器类
3
+ * 负责管理回声洞ID的分配、删除和统计信息
4
+ */
5
+ export declare class IdManager {
6
+ private deletedIds;
7
+ private maxId;
8
+ private initialized;
9
+ private readonly statusFilePath;
10
+ private stats;
11
+ private usedIds;
12
+ /**
13
+ * 初始化ID管理器
14
+ * @param baseDir - 基础目录路径
15
+ */
16
+ constructor(baseDir: string);
17
+ /**
18
+ * 初始化ID管理系统
19
+ * @param caveFilePath - 正式回声洞数据文件路径
20
+ * @param pendingFilePath - 待处理回声洞数据文件路径
21
+ * @throws 当初始化失败时抛出错误
22
+ */
23
+ initialize(caveFilePath: string, pendingFilePath: string): Promise<void>;
24
+ /**
25
+ * 处理ID冲突
26
+ * @param conflicts - ID冲突映射表
27
+ * @param caveFilePath - 正式回声洞数据文件路径
28
+ * @param pendingFilePath - 待处理回声洞数据文件路径
29
+ * @param caveData - 正式回声洞数据
30
+ * @param pendingData - 待处理回声洞数据
31
+ * @private
32
+ */
33
+ private handleConflicts;
34
+ /**
35
+ * 获取下一个可用的ID
36
+ * @returns 下一个可用的ID
37
+ * @throws 当ID管理器未初始化时抛出错误
38
+ */
39
+ getNextId(): number;
40
+ /**
41
+ * 标记ID为已删除状态
42
+ * @param id - 要标记为删除的ID
43
+ * @throws 当ID管理器未初始化时抛出错误
44
+ */
45
+ markDeleted(id: number): Promise<void>;
46
+ /**
47
+ * 添加贡献统计
48
+ * @param contributorNumber - 贡献者编号
49
+ * @param caveId - 回声洞ID
50
+ */
51
+ addStat(contributorNumber: string, caveId: number): Promise<void>;
52
+ /**
53
+ * 移除贡献统计
54
+ * @param contributorNumber - 贡献者编号
55
+ * @param caveId - 回声洞ID
56
+ */
57
+ removeStat(contributorNumber: string, caveId: number): Promise<void>;
58
+ /**
59
+ * 获取所有贡献统计信息
60
+ * @returns 贡献者编号到回声洞ID列表的映射
61
+ */
62
+ getStats(): Record<string, number[]>;
63
+ /**
64
+ * 保存当前状态到文件
65
+ * @private
66
+ * @throws 当保存失败时抛出错误
67
+ */
68
+ private saveStatus;
69
+ }
@@ -0,0 +1,42 @@
1
+ import { Context } from 'koishi';
2
+ export interface BaseElement {
3
+ type: 'text' | 'img' | 'video';
4
+ index: number;
5
+ }
6
+ export interface TextElement extends BaseElement {
7
+ type: 'text';
8
+ content: string;
9
+ }
10
+ export interface MediaElement extends BaseElement {
11
+ type: 'img' | 'video';
12
+ file?: string;
13
+ fileName?: string;
14
+ fileSize?: string;
15
+ filePath?: string;
16
+ }
17
+ export type Element = TextElement | MediaElement;
18
+ export declare function extractMediaContent(originalContent: string, config: {
19
+ imageMaxSize: number;
20
+ videoMaxSize: number;
21
+ }, session: any): Promise<{
22
+ imageUrls: string[];
23
+ imageElements: Array<{
24
+ type: 'img';
25
+ index: number;
26
+ fileName?: string;
27
+ fileSize?: string;
28
+ }>;
29
+ videoUrls: string[];
30
+ videoElements: Array<{
31
+ type: 'video';
32
+ index: number;
33
+ fileName?: string;
34
+ fileSize?: string;
35
+ }>;
36
+ textParts: Element[];
37
+ }>;
38
+ export declare function saveMedia(urls: string[], fileNames: (string | undefined)[], resourceDir: string, caveId: number, mediaType: 'img' | 'video', config: {
39
+ enableImageDuplicate: boolean;
40
+ imageDuplicateThreshold: number;
41
+ textDuplicateThreshold: number;
42
+ }, ctx: Context, session: any, buffers?: Buffer[]): Promise<string[]>;
@@ -0,0 +1,52 @@
1
+ import { Context } from 'koishi';
2
+ export interface BaseElement {
3
+ type: 'text' | 'img' | 'video';
4
+ index: number;
5
+ }
6
+ interface CaveObject {
7
+ cave_id: number;
8
+ elements: Element[];
9
+ contributor_number: string;
10
+ contributor_name: string;
11
+ }
12
+ export interface TextElement extends BaseElement {
13
+ type: 'text';
14
+ content: string;
15
+ }
16
+ export interface MediaElement extends BaseElement {
17
+ type: 'img' | 'video';
18
+ file?: string;
19
+ fileName?: string;
20
+ fileSize?: string;
21
+ filePath?: string;
22
+ }
23
+ export type Element = TextElement | MediaElement;
24
+ export declare function buildMessage(cave: CaveObject, resourceDir: string, session?: any): Promise<string>;
25
+ export declare function sendMessage(session: any, key: string, params?: any[], isTemp?: boolean, timeout?: number): Promise<string>;
26
+ export declare function processMediaFile(filePath: string, type: 'image' | 'video'): Promise<string | null>;
27
+ export declare function extractMediaContent(originalContent: string, config: {
28
+ imageMaxSize: number;
29
+ videoMaxSize: number;
30
+ }, session: any): Promise<{
31
+ imageUrls: string[];
32
+ imageElements: Array<{
33
+ type: 'img';
34
+ index: number;
35
+ fileName?: string;
36
+ fileSize?: string;
37
+ }>;
38
+ videoUrls: string[];
39
+ videoElements: Array<{
40
+ type: 'video';
41
+ index: number;
42
+ fileName?: string;
43
+ fileSize?: string;
44
+ }>;
45
+ textParts: Element[];
46
+ }>;
47
+ export declare function saveMedia(urls: string[], fileNames: (string | undefined)[], resourceDir: string, caveId: number, mediaType: 'img' | 'video', config: {
48
+ enableImageDuplicate: boolean;
49
+ imageDuplicateThreshold: number;
50
+ textDuplicateThreshold: number;
51
+ }, ctx: Context, session: any, buffers?: Buffer[]): Promise<string[]>;
52
+ export {};
@@ -0,0 +1,7 @@
1
+ import { Config } from '../index';
2
+ import { IdManager } from './IdManager';
3
+ import { HashManager } from './HashManager';
4
+ export declare function processList(session: any, config: Config, idManager: IdManager, userId?: string, pageNum?: number): Promise<string>;
5
+ export declare function processView(caveFilePath: string, resourceDir: string, session: any, options: any, content: string[]): Promise<string>;
6
+ export declare function processRandom(caveFilePath: string, resourceDir: string, session: any): Promise<string | void>;
7
+ export declare function processDelete(caveFilePath: string, resourceDir: string, pendingFilePath: string, session: any, config: Config, options: any, content: string[], idManager: IdManager, HashManager: HashManager): Promise<string>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-best-cave",
3
3
  "description": "最好的 cave 插件,可开关的审核系统,可引用添加,支持图文混合内容,可查阅投稿列表,完美复刻你的 .cave 体验!",
4
- "version": "1.5.3",
4
+ "version": "1.5.5",
5
5
  "contributors": [
6
6
  "Yis_Rime <yis_rime@outlook.com>"
7
7
  ],