mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
refactor: GroupInfo
This commit is contained in:
@@ -55,7 +55,6 @@ class LRU<T> {
|
||||
this.removeNode(node);
|
||||
this.onFuncs.forEach((func) => func(node));
|
||||
this.currentSize--;
|
||||
logDebug("removeLRUNode", "After", this.currentSize);
|
||||
}
|
||||
|
||||
public on(func: (node: cacheNode<T>) => void) {
|
||||
@@ -63,13 +62,6 @@ class LRU<T> {
|
||||
}
|
||||
|
||||
private removeExpired() {
|
||||
console.log("remove expired LRU node", !!this.tail);
|
||||
//console.log(`now current`, this.currentSize);
|
||||
//const rCurrent = Object.values(this.cache)
|
||||
// .map((group) => Object.values(group))
|
||||
// .flat().length;
|
||||
//console.log(`realiy current`, rCurrent);
|
||||
|
||||
const now = Date.now();
|
||||
let current = this.tail;
|
||||
const nodesToRemove: cacheNode<T>[] = [];
|
||||
@@ -94,24 +86,13 @@ class LRU<T> {
|
||||
this.tail = newTail;
|
||||
}
|
||||
|
||||
// 删除收集到的节点
|
||||
// console.log(nodesToRemove)
|
||||
nodesToRemove.forEach((node) => {
|
||||
// console.log("node is null", node === null);
|
||||
node.prev = node.next = null;
|
||||
delete this.cache[node.groupId][node.userId];
|
||||
|
||||
this.currentSize--;
|
||||
this.onFuncs.forEach((func) => func(node));
|
||||
});
|
||||
|
||||
console.log("after remove expired current", this.currentSize);
|
||||
// console.log(
|
||||
// "after remove expired realiy current",
|
||||
// Object.values(this.cache)
|
||||
// .map((group) => Object.values(group))
|
||||
// .flat().length
|
||||
// );
|
||||
}
|
||||
|
||||
private addNode(node: cacheNode<T>) {
|
||||
@@ -134,41 +115,27 @@ class LRU<T> {
|
||||
this.removeNode(node);
|
||||
this.addNode(node);
|
||||
node.prev = null;
|
||||
|
||||
logDebug("moveToHead", node.groupId, node.userId, node.value);
|
||||
}
|
||||
|
||||
public set(groupId: group_id, userId: user_id, value: T) {
|
||||
logDebug("set", groupId, userId, value, this.currentSize);
|
||||
|
||||
if (!this.cache[groupId]) {
|
||||
logDebug("set", "create group", groupId);
|
||||
this.cache[groupId] = Object.create(null);
|
||||
}
|
||||
|
||||
const groupObject = this.cache[groupId];
|
||||
|
||||
if (groupObject[userId]) {
|
||||
logDebug("update", groupId, userId, value);
|
||||
const node = groupObject[userId];
|
||||
node.value = value;
|
||||
node.timestamp = Date.now();
|
||||
this.moveToHead(node);
|
||||
} else {
|
||||
logDebug("add", groupId, userId, value);
|
||||
const node = new cacheNode(groupId, userId, value);
|
||||
groupObject[userId] = node;
|
||||
this.currentSize++;
|
||||
this.addNode(node);
|
||||
if (this.currentSize > this.maxSize) {
|
||||
const tail = this.tail!;
|
||||
logDebug(
|
||||
"remove expired LRU node",
|
||||
tail.groupId,
|
||||
tail.userId,
|
||||
tail.value,
|
||||
this.currentSize
|
||||
);
|
||||
this.removeLRUNode(tail);
|
||||
}
|
||||
}
|
@@ -1,183 +0,0 @@
|
||||
import sqlite3 from "sqlite3";
|
||||
import { logError, logDebug } from "@/common/utils/log";
|
||||
import { selfInfo } from "@/core/data";
|
||||
import { ob11Config } from "@/onebot11/config";
|
||||
import LRU from "../../onebot11/action/group/LRUCache";
|
||||
import path from "path";
|
||||
|
||||
const dbPath = path.join(
|
||||
ob11Config.getConfigDir(),
|
||||
`lastSendAndJoinRember_${selfInfo.uin}.db`
|
||||
);
|
||||
const remberDb = new sqlite3.Database(dbPath);
|
||||
|
||||
// 初始化全部的群到内存中
|
||||
const groupIds: number[] = [];
|
||||
remberDb.serialize(() => {
|
||||
const sql = `SELECT * FROM sqlite_master WHERE type='table'`;
|
||||
remberDb.all(sql, [], (err, rows: { name: string }[]) => {
|
||||
if (err) return logError(err);
|
||||
rows.forEach((row) => groupIds.push(parseInt(row.name)));
|
||||
logDebug(`已加载 ${groupIds.length} 个群`);
|
||||
console.log(groupIds);
|
||||
});
|
||||
});
|
||||
|
||||
const createTableSQL = (groupId: number) =>
|
||||
`CREATE TABLE IF NOT EXISTS "${groupId}" (
|
||||
user_id INTEGER,
|
||||
last_sent_time INTEGER,
|
||||
join_time INTEGER,
|
||||
PRIMARY KEY (user_id)
|
||||
);`;
|
||||
|
||||
async function createTableIfNotExist(groupId: number) {
|
||||
|
||||
|
||||
logDebug("检测数据表存在", groupId);
|
||||
if (groupIds.includes(groupId)) {
|
||||
logDebug("数据表已存在", groupId);
|
||||
return;
|
||||
}
|
||||
|
||||
logDebug("创建数据表", groupId);
|
||||
return new Promise((resolve, reject) => {
|
||||
const sql = createTableSQL(groupId);
|
||||
remberDb.all(sql, (err) => {
|
||||
if (err) {
|
||||
logError("数据表创建失败", err);
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
groupIds.push(groupId);
|
||||
logDebug("数据表创建成功", groupId);
|
||||
resolve(true);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 入群记录
|
||||
export async function insertJoinTime(
|
||||
groupId: number,
|
||||
userId: number,
|
||||
time: number
|
||||
) {
|
||||
logDebug("插入入群时间", userId, groupId);
|
||||
await createTableIfNotExist(groupId);
|
||||
remberDb.all(
|
||||
`INSERT OR REPLACE INTO "${groupId}" (user_id, last_sent_time, join_time) VALUES (?,?,?)`,
|
||||
[userId, time, time],
|
||||
(err) => {
|
||||
if (err)
|
||||
logError(err),
|
||||
Promise.reject(),
|
||||
console.log("插入入群时间失败", userId, groupId);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// 发言记录
|
||||
const LURCache = new LRU<number>();
|
||||
const LastSentCache = new (class {
|
||||
private cache: { gid: number; uid: number }[] = [];
|
||||
private maxSize: number;
|
||||
|
||||
constructor(maxSize: number = 5000) {
|
||||
this.maxSize = maxSize;
|
||||
}
|
||||
|
||||
get(gid: number, uid: number): boolean {
|
||||
const exists = this.cache.some(
|
||||
(entry) => entry.gid === gid && entry.uid === uid
|
||||
);
|
||||
if (!exists) {
|
||||
this.cache.push({ gid, uid });
|
||||
if (this.cache.length > this.maxSize) {
|
||||
this.cache.shift();
|
||||
}
|
||||
}
|
||||
|
||||
return exists;
|
||||
}
|
||||
})();
|
||||
|
||||
LURCache.on(async (node) => {
|
||||
const { value: time, groupId, userId } = node;
|
||||
|
||||
logDebug("插入发言时间", userId, groupId);
|
||||
await createTableIfNotExist(groupId);
|
||||
|
||||
const method = await getDataSetMethod(groupId, userId);
|
||||
logDebug("插入发言时间方法判断", userId, groupId, method);
|
||||
|
||||
const sql =
|
||||
method == "update"
|
||||
? `UPDATE "${groupId}" SET last_sent_time = ? WHERE user_id = ?`
|
||||
: `INSERT INTO "${groupId}" (last_sent_time, user_id) VALUES (?, ?)`;
|
||||
|
||||
remberDb.all(sql, [time, userId], (err) => {
|
||||
if (err) {
|
||||
return logError("插入/更新发言时间失败", userId, groupId);
|
||||
}
|
||||
logDebug("插入/更新发言时间成功", userId, groupId);
|
||||
});
|
||||
});
|
||||
|
||||
async function getDataSetMethod(groupId: number, userId: number) {
|
||||
// 缓存记录
|
||||
if (LastSentCache.get(groupId, userId)) {
|
||||
logDebug("缓存命中", userId, groupId);
|
||||
return "update";
|
||||
}
|
||||
|
||||
// 数据库判断
|
||||
return new Promise<"insert" | "update">((resolve, reject) => {
|
||||
remberDb.all(
|
||||
`SELECT * FROM "${groupId}" WHERE user_id = ?`,
|
||||
[userId],
|
||||
(err, rows) => {
|
||||
if (err) {
|
||||
logError("查询发言时间存在失败", userId, groupId, err);
|
||||
return logError("插入发言时间失败", userId, groupId, err);
|
||||
}
|
||||
|
||||
if (rows.length === 0) {
|
||||
logDebug("查询发言时间不存在", userId, groupId);
|
||||
return resolve("insert");
|
||||
}
|
||||
|
||||
logDebug("查询发言时间存在", userId, groupId);
|
||||
resolve("update");
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
interface IRember {
|
||||
last_sent_time: number;
|
||||
join_time: number;
|
||||
user_id: number;
|
||||
}
|
||||
export async function getLastSentTimeAndJoinTime(
|
||||
groupId: number
|
||||
): Promise<IRember[]> {
|
||||
logDebug("读取发言时间", groupId);
|
||||
return new Promise<IRember[]>((resolve, reject) => {
|
||||
remberDb.all(`SELECT * FROM "${groupId}" `, (err, rows: IRember[]) => {
|
||||
if (err) {
|
||||
logError("查询发言时间失败", groupId);
|
||||
return resolve([]);
|
||||
}
|
||||
logDebug("查询发言时间成功", groupId, rows);
|
||||
resolve(rows);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function insertLastSentTime(
|
||||
groupId: number,
|
||||
userId: number,
|
||||
time: number
|
||||
) {
|
||||
LURCache.set(groupId, userId,time)
|
||||
}
|
@@ -7,8 +7,8 @@ import { napCatCore, NTQQGroupApi, NTQQUserApi, SignMiniApp } from '@/core';
|
||||
import { WebApi } from '@/core/apis/webapi';
|
||||
import { logDebug } from '@/common/utils/log';
|
||||
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
|
||||
import { getLastSentTimeAndJoinTime } from "../../../common/utils/LastSendAndJoinRemberLRU"
|
||||
import { ob11Config } from '@/onebot11/config';
|
||||
import { dbUtil } from '@/core/utils/db';
|
||||
|
||||
const SchemaData = {
|
||||
type: 'object',
|
||||
@@ -63,7 +63,7 @@ class GetGroupMemberList extends BaseAction<Payload, OB11GroupMember[]> {
|
||||
}
|
||||
}
|
||||
} else if ((ob11Config.GroupLocalTimeRecord as Array<number>).includes(payload.group_id)) {
|
||||
const _sendAndJoinRember = await getLastSentTimeAndJoinTime(payload.group_id);
|
||||
const _sendAndJoinRember = await dbUtil.getLastSentTimeAndJoinTime(payload.group_id);
|
||||
_sendAndJoinRember.forEach((element) => {
|
||||
let MemberData = MemberMap.get(element.user_id);
|
||||
if (MemberData) {
|
||||
|
@@ -35,7 +35,6 @@ import { Data as SysData } from '@/proto/SysMessage';
|
||||
import { Data as DeviceData } from '@/proto/SysMessage.DeviceChange';
|
||||
import { OB11FriendPokeEvent, OB11GroupPokeEvent } from './event/notice/OB11PokeEvent';
|
||||
import { isEqual } from '@/common/utils/helper';
|
||||
import { insertLastSentTime } from "../common/utils/LastSendAndJoinRemberLRU"
|
||||
|
||||
//下面几个其实应该移进Core-Data 缓存实现 但是现在在这里方便
|
||||
//
|
||||
@@ -287,9 +286,9 @@ export class NapCatOnebot11 {
|
||||
}
|
||||
if (msg.post_type === 'message') {
|
||||
logMessage(msg as OB11Message).then().catch(logError);
|
||||
if (msg.message_type == 'group' && msg.group_id) {
|
||||
insertLastSentTime(msg.group_id, msg.user_id, msg.time)
|
||||
}
|
||||
if (msg.message_type == 'group' && msg.group_id && (ob11Config.GroupLocalTimeRecord as Array<number>).find((item) => item == msg.group_id)) {
|
||||
dbUtil.insertLastSentTime(msg.group_id, msg.user_id, msg.time);
|
||||
}
|
||||
} else if (msg.post_type === 'notice') {
|
||||
logNotice(msg).then().catch(logError);
|
||||
} else if (msg.post_type === 'request') {
|
||||
|
Reference in New Issue
Block a user