feat: auto delete receive file

This commit is contained in:
linyuchen
2024-02-25 02:17:18 +08:00
parent 6170307241
commit d9d7e9e830
4 changed files with 41 additions and 12 deletions

View File

@@ -39,7 +39,8 @@ export class ConfigUtil {
enableLocalFile2Url: false, enableLocalFile2Url: false,
debug: false, debug: false,
log: false, log: false,
reportSelfMessage: false reportSelfMessage: false,
autoDeleteFile: false,
}; };
if (!fs.existsSync(this.configPath)) { if (!fs.existsSync(this.configPath)) {

View File

@@ -18,4 +18,5 @@ export interface Config {
debug?: boolean debug?: boolean
reportSelfMessage?: boolean reportSelfMessage?: boolean
log?: boolean log?: boolean
autoDeleteFile?: boolean
} }

View File

@@ -1,5 +1,5 @@
import {BrowserWindow} from 'electron'; import {BrowserWindow} from 'electron';
import {log, sleep} from "../common/utils"; import {getConfigUtil, log, sleep} from "../common/utils";
import {NTQQApi, NTQQApiClass, sendMessagePool} from "./ntcall"; import {NTQQApi, NTQQApiClass, sendMessagePool} from "./ntcall";
import {Group, RawMessage, User} from "./types"; import {Group, RawMessage, User} from "./types";
import {addHistoryMsg, friends, groups, msgHistory} from "../common/data"; import {addHistoryMsg, friends, groups, msgHistory} from "../common/data";
@@ -8,6 +8,7 @@ import {OB11GroupIncreaseEvent} from "../onebot11/event/notice/OB11GroupIncrease
import {v4 as uuidv4} from "uuid" import {v4 as uuidv4} from "uuid"
import {postOB11Event} from "../onebot11/server/postOB11Event"; import {postOB11Event} from "../onebot11/server/postOB11Event";
import {HOOK_LOG} from "../common/config"; import {HOOK_LOG} from "../common/config";
import fs from "fs";
export let hookApiCallbacks: Record<string, (apiReturn: any) => void> = {} export let hookApiCallbacks: Record<string, (apiReturn: any) => void> = {}
@@ -124,8 +125,7 @@ async function updateGroups(_groups: Group[], needUpdate: boolean = true) {
let existGroup = groups.find(g => g.groupCode == group.groupCode); let existGroup = groups.find(g => g.groupCode == group.groupCode);
if (existGroup) { if (existGroup) {
Object.assign(existGroup, group); Object.assign(existGroup, group);
} } else {
else {
groups.push(group); groups.push(group);
existGroup = group; existGroup = group;
} }
@@ -166,8 +166,7 @@ async function processGroupEvent(payload) {
} }
} }
} } else if (existGroup.memberCount < group.memberCount) {
else if (existGroup.memberCount < group.memberCount) {
const oldMembers = existGroup.members; const oldMembers = existGroup.members;
const oldMembersSet = new Set<string>(); const oldMembersSet = new Set<string>();
for (const member of oldMembers) { for (const member of oldMembers) {
@@ -189,8 +188,7 @@ async function processGroupEvent(payload) {
} }
updateGroups(newGroupList, false).then(); updateGroups(newGroupList, false).then();
} } catch (e) {
catch (e) {
updateGroups(payload.groupList).then(); updateGroups(payload.groupList).then();
console.log(e); console.log(e);
} }
@@ -199,8 +197,7 @@ async function processGroupEvent(payload) {
registerReceiveHook<{ groupList: Group[], updateType: number }>(ReceiveCmd.GROUPS, (payload) => { registerReceiveHook<{ groupList: Group[], updateType: number }>(ReceiveCmd.GROUPS, (payload) => {
if (payload.updateType != 2) { if (payload.updateType != 2) {
updateGroups(payload.groupList).then(); updateGroups(payload.groupList).then();
} } else {
else {
if (process.platform == "win32") { if (process.platform == "win32") {
processGroupEvent(payload).then(); processGroupEvent(payload).then();
} }
@@ -209,8 +206,7 @@ registerReceiveHook<{ groupList: Group[], updateType: number }>(ReceiveCmd.GROUP
registerReceiveHook<{ groupList: Group[], updateType: number }>(ReceiveCmd.GROUPS_UNIX, (payload) => { registerReceiveHook<{ groupList: Group[], updateType: number }>(ReceiveCmd.GROUPS_UNIX, (payload) => {
if (payload.updateType != 2) { if (payload.updateType != 2) {
updateGroups(payload.groupList).then(); updateGroups(payload.groupList).then();
} } else {
else {
if (process.platform != "win32") { if (process.platform != "win32") {
processGroupEvent(payload).then(); processGroupEvent(payload).then();
} }
@@ -234,9 +230,32 @@ registerReceiveHook<{
}) })
registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload) => { registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload) => {
const {autoDeleteFile} = getConfigUtil().getConfig();
for (const message of payload.msgList) { for (const message of payload.msgList) {
// log("收到新消息push到历史记录", message) // log("收到新消息push到历史记录", message)
addHistoryMsg(message) addHistoryMsg(message)
// 清理文件
if (!autoDeleteFile) {
continue
}
for (const msgElement of message.elements) {
setTimeout(() => {
const picPath = msgElement.picElement?.sourcePath;
const pttPath = msgElement.pttElement?.filePath;
const pathList = [picPath, pttPath];
if (msgElement.picElement){
pathList.push(...Object.values(msgElement.picElement.thumbPath));
}
// log("需要清理的文件", pathList);
for (const path of pathList) {
if (path) {
fs.unlink(picPath, () => {
log("删除文件成功", path)
});
}
}
}, 60 * 1000)
}
} }
const msgIds = Object.keys(msgHistory); const msgIds = Object.keys(msgHistory);
if (msgIds.length > 30000) { if (msgIds.length > 30000) {

View File

@@ -145,6 +145,13 @@ async function onSettingWindowCreated(view: Element) {
</div> </div>
<setting-switch id="log" ${config.log ? "is-active" : ""}></setting-switch> <setting-switch id="log" ${config.log ? "is-active" : ""}></setting-switch>
</setting-item> </setting-item>
<setting-item data-direction="row" class="hostItem vertical-list-item">
<div>
<div>自动删除收到的文件</div>
<div class="tips">一分钟后会删除收到的图片语音</div>
</div>
<setting-switch id="autoDeleteFile" ${config.autoDeleteFile ? "is-active" : ""}></setting-switch>
</setting-item>
</setting-panel> </setting-panel>
</setting-section> </setting-section>
</div> </div>
@@ -226,6 +233,7 @@ async function onSettingWindowCreated(view: Element) {
switchClick("switchFileUrl", "enableLocalFile2Url"); switchClick("switchFileUrl", "enableLocalFile2Url");
switchClick("reportSelfMessage", "reportSelfMessage"); switchClick("reportSelfMessage", "reportSelfMessage");
switchClick("log", "log"); switchClick("log", "log");
switchClick("autoDeleteFile", "autoDeleteFile");
doc.getElementById("save")?.addEventListener("click", doc.getElementById("save")?.addEventListener("click",
() => { () => {