fix: group notify db cache

This commit is contained in:
linyuchen 2024-03-10 22:44:33 +08:00
parent 192736c8be
commit eeadaa12e9
9 changed files with 64 additions and 33 deletions

View File

@ -1,10 +1,10 @@
{ {
"manifest_version": 4, "manifest_version": 4,
"type": "extension", "type": "extension",
"name": "LLOneBot v3.13.10", "name": "LLOneBot v3.13.11",
"slug": "LLOneBot", "slug": "LLOneBot",
"description": "LiteLoaderQQNT的OneBotApi", "description": "LiteLoaderQQNT的OneBotApi",
"version": "3.13.10", "version": "3.13.11",
"thumbnail": "./icon.png", "thumbnail": "./icon.png",
"authors": [ "authors": [
{ {

17
scripts/test/test_db.ts Normal file
View File

@ -0,0 +1,17 @@
import {Level} from "level"
const db = new Level(process.env["level_db_path"], {valueEncoding: 'json'});
async function getGroupNotify() {
let keys = await db.keys().all();
let result = []
for (const key of keys) {
// console.log(key)
if (key.startsWith("group_notify_")) {
result.push(key)
}
}
return result
}
getGroupNotify().then(console.log)

View File

@ -21,7 +21,6 @@ export const selfInfo: SelfInfo = {
} }
export let groups: Group[] = [] export let groups: Group[] = []
export let friends: Friend[] = [] export let friends: Friend[] = []
export let groupNotifies: Map<string, GroupNotify> = new Map<string, GroupNotify>()
export let friendRequests: Map<number, FriendRequest> = new Map<number, FriendRequest>() export let friendRequests: Map<number, FriendRequest> = new Map<number, FriendRequest>()
export const llonebotError: LLOneBotError = { export const llonebotError: LLOneBotError = {
ffmpegError: '', ffmpegError: '',

View File

@ -1,17 +1,18 @@
import {Level} from "level"; import {Level} from "level";
import {RawMessage} from "../ntqqapi/types"; import {type GroupNotify, RawMessage} from "../ntqqapi/types";
import {DATA_DIR, log} from "./utils"; import {DATA_DIR, log} from "./utils";
import {selfInfo} from "./data"; import {selfInfo} from "./data";
import {FileCache} from "./types"; import {FileCache} from "./types";
class DBUtil { class DBUtil {
private readonly DB_KEY_PREFIX_MSG_ID = "msg_id_"; public readonly DB_KEY_PREFIX_MSG_ID = "msg_id_";
private readonly DB_KEY_PREFIX_MSG_SHORT_ID = "msg_short_id_"; public readonly DB_KEY_PREFIX_MSG_SHORT_ID = "msg_short_id_";
private readonly DB_KEY_PREFIX_MSG_SEQ_ID = "msg_seq_id_"; public readonly DB_KEY_PREFIX_MSG_SEQ_ID = "msg_seq_id_";
private readonly DB_KEY_PREFIX_FILE = "file_"; public readonly DB_KEY_PREFIX_FILE = "file_";
private db: Level; public readonly DB_KEY_PREFIX_GROUP_NOTIFY = "group_notify_";
public cache: Record<string, RawMessage | string | FileCache> = {} // <msg_id_ | msg_short_id_ | msg_seq_id_><id>: RawMessage public db: Level;
public cache: Record<string, RawMessage | string | FileCache | GroupNotify> = {} // <msg_id_ | msg_short_id_ | msg_seq_id_><id>: RawMessage
private currentShortId: number; private currentShortId: number;
/* /*
@ -203,6 +204,29 @@ class DBUtil {
} }
} }
async addGroupNotify(notify: GroupNotify) {
const key = this.DB_KEY_PREFIX_GROUP_NOTIFY + notify.seq;
let existNotify = this.cache[key] as GroupNotify
if (existNotify){
return
}
this.cache[key] = notify;
this.db.put(key, JSON.stringify(notify)).then();
}
async getGroupNotify(seq: string): Promise<GroupNotify | undefined> {
const key = this.DB_KEY_PREFIX_GROUP_NOTIFY + seq;
if (this.cache[key]) {
return this.cache[key] as GroupNotify
}
try {
let data = await this.db.get(key);
return JSON.parse(data);
} catch (e) {
}
}
} }
export const dbUtil = new DBUtil(); export const dbUtil = new DBUtil();

View File

@ -16,7 +16,6 @@ import {
friendRequests, getFriend, friendRequests, getFriend,
getGroup, getGroup,
getGroupMember, getGroupMember,
groupNotifies,
llonebotError, refreshGroupMembers, llonebotError, refreshGroupMembers,
selfInfo selfInfo
} from "../common/data"; } from "../common/data";
@ -251,19 +250,17 @@ function onLoad() {
for (const notify of notifies) { for (const notify of notifies) {
try { try {
notify.time = Date.now(); notify.time = Date.now();
const notifyTime = parseInt(notify.seq) / 1000 // const notifyTime = parseInt(notify.seq) / 1000
// log(`加群通知时间${notifyTime}`, `LLOneBot启动时间${startTime}`); // log(`加群通知时间${notifyTime}`, `LLOneBot启动时间${startTime}`);
// if (notifyTime < startTime) { // if (notifyTime < startTime) {
// continue; // continue;
// } // }
let existNotify = groupNotifies[notify.seq]; let existNotify = await dbUtil.getGroupNotify(notify.seq);
if (existNotify) { if (existNotify) {
if (Date.now() - existNotify.time < 3000) { continue
continue
}
} }
log("收到群通知", notify); log("收到群通知", notify);
groupNotifies[notify.seq] = notify; await dbUtil.addGroupNotify(notify);
// let member2: GroupMember; // let member2: GroupMember;
// if (notify.user2.uid) { // if (notify.user2.uid) {
// member2 = await getGroupMember(notify.group.groupCode, null, notify.user2.uid); // member2 = await getGroupMember(notify.group.groupCode, null, notify.user2.uid);

View File

@ -21,7 +21,7 @@ import {
CacheFileList, CacheFileListItem, CacheFileType, CacheFileList, CacheFileListItem, CacheFileType,
} from "./types"; } from "./types";
import * as fs from "fs"; import * as fs from "fs";
import {friendRequests, groupNotifies, selfInfo, uidMaps} from "../common/data"; import {friendRequests, selfInfo, uidMaps} from "../common/data";
import {v4 as uuidv4} from "uuid" import {v4 as uuidv4} from "uuid"
import path from "path"; import path from "path";
import {dbUtil} from "../common/db"; import {dbUtil} from "../common/db";
@ -590,11 +590,11 @@ export class NTQQApi {
} }
static async handleGroupRequest(seq: string, operateType: GroupRequestOperateTypes, reason?: string) { static async handleGroupRequest(seq: string, operateType: GroupRequestOperateTypes, reason?: string) {
const notify: GroupNotify = groupNotifies[seq]; const notify: GroupNotify = await dbUtil.getGroupNotify(seq)
if (!notify) { if (!notify) {
throw `${seq}对应的加群通知不存在` throw `${seq}对应的加群通知不存在`
} }
delete groupNotifies[seq]; // delete groupNotifies[seq];
return await callNTQQApi<GeneralCallResult>({ return await callNTQQApi<GeneralCallResult>({
methodName: NTQQApiMethod.HANDLE_GROUP_REQUEST, methodName: NTQQApiMethod.HANDLE_GROUP_REQUEST,
args: [ args: [

View File

@ -366,7 +366,7 @@ export interface GroupNotifies {
export interface GroupNotify { export interface GroupNotify {
time: number; // 自己添加的字段,时间戳,毫秒, 用于判断收到短时间内收到重复的notify time: number; // 自己添加的字段,时间戳,毫秒, 用于判断收到短时间内收到重复的notify
seq: string, // 转成数字再除以1000应该就是时间戳 seq: string, // 唯一标识符,转成数字再除以1000应该就是时间戳
type: GroupNotifyTypes, type: GroupNotifyTypes,
status: 0, // 未知 status: 0, // 未知
group: { groupCode: string, groupName: string }, group: { groupCode: string, groupName: string },

View File

@ -1,6 +1,5 @@
import BaseAction from "./BaseAction"; import BaseAction from "./BaseAction";
import {groupNotifies} from "../../common/data"; import {GroupRequestOperateTypes} from "../../ntqqapi/types";
import {GroupNotify, GroupRequestOperateTypes} from "../../ntqqapi/types";
import {NTQQApi} from "../../ntqqapi/ntcall"; import {NTQQApi} from "../../ntqqapi/ntcall";
import {ActionName} from "./types"; import {ActionName} from "./types";
@ -17,15 +16,10 @@ export default class SetGroupAddRequest extends BaseAction<Payload, null> {
protected async _handle(payload: Payload): Promise<null> { protected async _handle(payload: Payload): Promise<null> {
const seq = payload.flag.toString(); const seq = payload.flag.toString();
const notify: GroupNotify = groupNotifies[seq] await NTQQApi.handleGroupRequest(seq,
try { payload.approve ? GroupRequestOperateTypes.approve : GroupRequestOperateTypes.reject,
await NTQQApi.handleGroupRequest(seq, payload.reason
payload.approve ? GroupRequestOperateTypes.approve : GroupRequestOperateTypes.reject, )
payload.reason
)
} catch (e) {
throw e
}
return null return null
} }
} }

View File

@ -1 +1 @@
export const version = "3.13.10" export const version = "3.13.11"