fix: report first temp msg

This commit is contained in:
linyuchen
2024-03-31 21:45:17 +08:00
parent 655225e027
commit 0c59371ed1
5 changed files with 87 additions and 33 deletions

View File

@@ -1,10 +1,10 @@
{ {
"manifest_version": 4, "manifest_version": 4,
"type": "extension", "type": "extension",
"name": "LLOneBot v3.20.5", "name": "LLOneBot v3.20.6",
"slug": "LLOneBot", "slug": "LLOneBot",
"description": "使你的NTQQ支持OneBot11协议进行QQ机器人开发, 不支持商店在线更新", "description": "使你的NTQQ支持OneBot11协议进行QQ机器人开发, 不支持商店在线更新",
"version": "3.20.5", "version": "3.20.6",
"icon": "./icon.jpg", "icon": "./icon.jpg",
"authors": [ "authors": [
{ {

View File

@@ -27,17 +27,27 @@ export class NTQQMsgApi {
}) })
} }
static async activateGroupChat(groupCode: string) { static async activateChat(peer: Peer) {
// await this.fetchRecentContact(); // await this.fetchRecentContact();
// await sleep(500); // await sleep(500);
return await callNTQQApi<GeneralCallResult>({ return await callNTQQApi<GeneralCallResult>({
methodName: NTQQApiMethod.ADD_ACTIVE_CHAT, methodName: NTQQApiMethod.ACTIVE_CHAT_PREVIEW,
args: [{peer:{peerUid: groupCode, chatType: ChatType.group}, cnt: 20}, null] args: [{peer, cnt: 20}, null]
})
}
static async activateChatAndGetHistory(peer: Peer) {
// await this.fetchRecentContact();
// await sleep(500);
return await callNTQQApi<GeneralCallResult>({
methodName: NTQQApiMethod.ACTIVE_CHAT_HISTORY,
// 参数似乎不是这样
args: [{peer, cnt: 20}, null]
}) })
} }
static async getMsgHistory(peer: Peer, msgId: string, count: number) { static async getMsgHistory(peer: Peer, msgId: string, count: number) {
// 消息时间从旧到新
return await callNTQQApi<GeneralCallResult & {msgList: RawMessage[]}>({ return await callNTQQApi<GeneralCallResult & {msgList: RawMessage[]}>({
methodName: isQQ998 ? NTQQApiMethod.HISTORY_MSG_998 : NTQQApiMethod.HISTORY_MSG, methodName: isQQ998 ? NTQQApiMethod.ACTIVE_CHAT_HISTORY : NTQQApiMethod.HISTORY_MSG,
args: [{ args: [{
peer, peer,
msgId, msgId,

View File

@@ -12,10 +12,12 @@ import {dbUtil} from "../common/db";
import {NTQQGroupApi} from "./api/group"; import {NTQQGroupApi} from "./api/group";
import {log} from "../common/utils/log"; import {log} from "../common/utils/log";
import {sleep} from "../common/utils/helper"; import {sleep} from "../common/utils/helper";
import {OB11Constructor} from "../onebot11/constructor";
export let hookApiCallbacks: Record<string, (apiReturn: any) => void> = {} export let hookApiCallbacks: Record<string, (apiReturn: any) => void> = {}
export let ReceiveCmdS = { export let ReceiveCmdS = {
RECENT_CONTACT: "nodeIKernelRecentContactListener/onRecentContactListChangedVer2",
UPDATE_MSG: "nodeIKernelMsgListener/onMsgInfoListUpdate", UPDATE_MSG: "nodeIKernelMsgListener/onMsgInfoListUpdate",
UPDATE_ACTIVE_MSG: "nodeIKernelMsgListener/onActiveMsgInfoUpdate", UPDATE_ACTIVE_MSG: "nodeIKernelMsgListener/onActiveMsgInfoUpdate",
NEW_MSG: `nodeIKernelMsgListener/onRecvMsg`, NEW_MSG: `nodeIKernelMsgListener/onRecvMsg`,
@@ -72,7 +74,7 @@ export function hookNTQQApiReceive(window: BrowserWindow) {
if (!isLogger) { if (!isLogger) {
try { try {
HOOK_LOG && log(`received ntqq api message: ${channel}`, args) HOOK_LOG && log(`received ntqq api message: ${channel}`, args)
}catch (e) { } catch (e) {
log("hook log error", e, args) log("hook log error", e, args)
} }
} }
@@ -108,7 +110,7 @@ export function hookNTQQApiReceive(window: BrowserWindow) {
delete hookApiCallbacks[callbackId]; delete hookApiCallbacks[callbackId];
} }
} }
}catch (e) { } catch (e) {
log("hookNTQQApiReceive error", e.stack.toString(), args) log("hookNTQQApiReceive error", e.stack.toString(), args)
} }
originalSend.call(window.webContents, channel, ...args); originalSend.call(window.webContents, channel, ...args);
@@ -131,9 +133,9 @@ export function hookNTQQApiCall(window: BrowserWindow) {
} }
if (!isLogger) { if (!isLogger) {
try{ try {
HOOK_LOG && log("call NTQQ api", thisArg, args); HOOK_LOG && log("call NTQQ api", thisArg, args);
}catch (e) { } catch (e) {
} }
} }
@@ -159,7 +161,7 @@ export function hookNTQQApiCall(window: BrowserWindow) {
let ret = target.apply(thisArg, args); let ret = target.apply(thisArg, args);
try { try {
HOOK_LOG && log("call NTQQ invoke api return", ret) HOOK_LOG && log("call NTQQ invoke api return", ret)
}catch (e) { } catch (e) {
} }
return ret; return ret;
@@ -191,19 +193,20 @@ export function removeReceiveHook(id: string) {
} }
let activatedGroups: string[] = []; let activatedGroups: string[] = [];
async function updateGroups(_groups: Group[], needUpdate: boolean = true) { async function updateGroups(_groups: Group[], needUpdate: boolean = true) {
for (let group of _groups) { for (let group of _groups) {
log("update group", group) log("update group", group)
if (!activatedGroups.includes(group.groupCode)) { // if (!activatedGroups.includes(group.groupCode)) {
NTQQMsgApi.activateGroupChat(group.groupCode).then((r) => { NTQQMsgApi.activateChat({peerUid: group.groupCode, chatType: ChatType.group}).then((r) => {
activatedGroups.push(group.groupCode); // activatedGroups.push(group.groupCode);
// log(`激活群聊天窗口${group.groupName}(${group.groupCode})`, r) // log(`激活群聊天窗口${group.groupName}(${group.groupCode})`, r)
// if (r.result !== 0) { // if (r.result !== 0) {
// setTimeout(() => NTQQMsgApi.activateGroupChat(group.groupCode).then(r => log(`再次激活群聊天窗口${group.groupName}(${group.groupCode})`, r)), 500); // setTimeout(() => NTQQMsgApi.activateGroupChat(group.groupCode).then(r => log(`再次激活群聊天窗口${group.groupName}(${group.groupCode})`, r)), 500);
// }else { // }else {
// } // }
}).catch(log) }).catch(log)
} // }
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);
@@ -222,7 +225,7 @@ async function updateGroups(_groups: Group[], needUpdate: boolean = true) {
} }
} }
async function processGroupEvent(payload: {groupList: Group[]}) { async function processGroupEvent(payload: { groupList: Group[] }) {
try { try {
const newGroupList = payload.groupList; const newGroupList = payload.groupList;
for (const group of newGroupList) { for (const group of newGroupList) {
@@ -289,13 +292,17 @@ registerReceiveHook<{ groupList: Group[], updateType: number }>(ReceiveCmdS.GROU
} }
}) })
registerReceiveHook<{groupCode: string, dataSource: number, members: Set<GroupMember>}>(ReceiveCmdS.GROUP_MEMBER_INFO_UPDATE, async (payload) => { registerReceiveHook<{
groupCode: string,
dataSource: number,
members: Set<GroupMember>
}>(ReceiveCmdS.GROUP_MEMBER_INFO_UPDATE, async (payload) => {
const groupCode = payload.groupCode; const groupCode = payload.groupCode;
const members = Array.from(payload.members.values()); const members = Array.from(payload.members.values());
// log("群成员信息变动", groupCode, members) // log("群成员信息变动", groupCode, members)
for(const member of members) { for (const member of members) {
const existMember = await getGroupMember(groupCode, member.uin); const existMember = await getGroupMember(groupCode, member.uin);
if (existMember){ if (existMember) {
Object.assign(existMember, member); Object.assign(existMember, member);
} }
} }
@@ -322,6 +329,7 @@ registerReceiveHook<{
for (const fData of payload.data) { for (const fData of payload.data) {
const _friends = fData.buddyList; const _friends = fData.buddyList;
for (let friend of _friends) { for (let friend of _friends) {
NTQQMsgApi.activateChat({peerUid: friend.uid, chatType: ChatType.friend}).then()
let existFriend = friends.find(f => f.uin == friend.uin) let existFriend = friends.find(f => f.uin == friend.uin)
if (!existFriend) { if (!existFriend) {
friends.push(friend) friends.push(friend)
@@ -338,9 +346,9 @@ registerReceiveHook<{ msgList: Array<RawMessage> }>([ReceiveCmdS.NEW_MSG, Receiv
const uid = message.senderUid; const uid = message.senderUid;
const uin = message.senderUin; const uin = message.senderUin;
if (uid && uin) { if (uid && uin) {
if (message.chatType === ChatType.temp){ if (message.chatType === ChatType.temp) {
dbUtil.getReceivedTempUinMap().then(receivedTempUinMap=>{ dbUtil.getReceivedTempUinMap().then(receivedTempUinMap => {
if (!receivedTempUinMap[uin]){ if (!receivedTempUinMap[uin]) {
receivedTempUinMap[uin] = uid; receivedTempUinMap[uin] = uid;
dbUtil.setReceivedTempUinMap(receivedTempUinMap) dbUtil.setReceivedTempUinMap(receivedTempUinMap)
} }
@@ -410,3 +418,39 @@ registerReceiveHook<{ msgRecord: RawMessage }>(ReceiveCmdS.SELF_SEND_MSG, ({msgR
registerReceiveHook<{ info: { status: number } }>(ReceiveCmdS.SELF_STATUS, (info) => { registerReceiveHook<{ info: { status: number } }>(ReceiveCmdS.SELF_STATUS, (info) => {
selfInfo.online = info.info.status !== 20 selfInfo.online = info.info.status !== 20
}) })
let activatedPeerUids: string[] = []
registerReceiveHook<{
changedRecentContactLists: {
listType: number, sortedContactList: string[],
changedList: {
id: string, // peerUid
chatType: ChatType
}[]
}[]
}>(ReceiveCmdS.RECENT_CONTACT, async (payload) => {
for (const recentContact of payload.changedRecentContactLists) {
for (const changedContact of recentContact.changedList) {
if (activatedPeerUids.includes(changedContact.id)) continue;
activatedPeerUids.push(changedContact.id)
const peer = {peerUid: changedContact.id, chatType: changedContact.chatType}
if (changedContact.chatType === ChatType.temp) {
log("收到临时会话消息", peer)
NTQQMsgApi.activateChatAndGetHistory(peer).then(
() => {
NTQQMsgApi.getMsgHistory(peer, "", 20).then(({msgList}) => {
let lastTempMsg = msgList.pop()
log("激活窗口之前的第一条临时会话消息:", lastTempMsg)
if ((Date.now() / 1000) - parseInt(lastTempMsg.msgTime) < 5) {
OB11Constructor.message(lastTempMsg).then(r => postOB11Event(r))
}
})
}
)
} else {
NTQQMsgApi.activateChat(peer).then()
}
}
}
})

View File

@@ -22,8 +22,8 @@ export enum NTQQApiClass {
export enum NTQQApiMethod { export enum NTQQApiMethod {
RECENT_CONTACT = "nodeIKernelRecentContactService/fetchAndSubscribeABatchOfRecentContact", RECENT_CONTACT = "nodeIKernelRecentContactService/fetchAndSubscribeABatchOfRecentContact",
ADD_ACTIVE_CHAT = "nodeIKernelMsgService/getAioFirstViewLatestMsgsAndAddActiveChat", // 激活群助手内的聊天窗口,这样才能收到消息 ACTIVE_CHAT_PREVIEW = "nodeIKernelMsgService/getAioFirstViewLatestMsgsAndAddActiveChat", // 激活聊天窗口,有时候必须这样才能收到消息, 并返回最新预览消息
HISTORY_MSG_998 = "nodeIKernelMsgService/getMsgsIncludeSelfAndAddActiveChat", ACTIVE_CHAT_HISTORY = "nodeIKernelMsgService/getMsgsIncludeSelfAndAddActiveChat", // 激活聊天窗口,有时候必须这样才能收到消息, 并返回历史消息
HISTORY_MSG = "nodeIKernelMsgService/getMsgsIncludeSelf", HISTORY_MSG = "nodeIKernelMsgService/getMsgsIncludeSelf",
GET_MULTI_MSG = "nodeIKernelMsgService/getMultiMsg", GET_MULTI_MSG = "nodeIKernelMsgService/getMultiMsg",

View File

@@ -1 +1 @@
export const version = "3.20.5" export const version = "3.20.6"