Skip to content

Commit 83559aa

Browse files
committed
fix: improve offline duplicate message
1 parent 4dbdd3a commit 83559aa

5 files changed

Lines changed: 55 additions & 25 deletions

File tree

src/main/main.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { WebUIServer } from '../webui/BE/server'
3535
import { setFFMpegPath } from '@/common/utils/ffmpeg'
3636
import { pmhq } from '@/ntqqapi/native/pmhq'
3737
import { defaultConfig } from '@/common/defaultConfig'
38+
import { sleep } from '@/common/utils'
3839

3940
declare module 'cordis' {
4041
interface Events {
@@ -132,8 +133,17 @@ async function onLoad() {
132133
selfInfo.online = true
133134

134135
const getSelfInfo = async () => {
135-
const uin = await ctx.ntUserApi.getUinByUid(data[2])
136-
selfInfo.uin = uin
136+
let uin: string
137+
// 循环 5次 获取uin
138+
for (let i = 0; i < 5; i++) {
139+
try {
140+
uin = await ctx.ntUserApi.getUinByUid(data[2])
141+
selfInfo.uin = uin
142+
break
143+
}catch (e) {
144+
await sleep(1000)
145+
}
146+
}
137147
const configUtil = getConfigUtil(true)
138148
config = configUtil.getConfig()
139149
ctx.parallel('llob/config-updated', config)

src/ntqqapi/core.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,27 @@ class Core extends Service {
125125
return returnMsg
126126
}
127127

128-
private handleMessage(msgList: RawMessage[]) {
128+
private async handleMessage(msgList: RawMessage[]) {
129129
for (const message of msgList) {
130130
const msgTime = parseInt(message.msgTime)
131131
if (msgTime < this.startupTime) {
132-
this.ctx.store.getShortIdByMsgId(message.msgId).then(existing=>{
133-
// 已存在的离线消息不处理
134-
if (!existing) {
135-
this.ctx.parallel('nt/offline-message-created', message)
132+
const uniqueId = `${message.peerUid}-${message.msgSeq}-${message.msgRandom}`
133+
const existing = await this.ctx.store.getShortIdByMsgId(uniqueId)
134+
if (!existing){
135+
this.ctx.logger.info(uniqueId)
136+
const peer = {
137+
chatType: message.chatType,
138+
peerUid: message.peerUin,
139+
guildId: '',
136140
}
137-
})
141+
try {
142+
this.ctx.store.createMsgShortId(peer, uniqueId)
143+
}catch (e) {
144+
this.ctx.logger.info(e)
145+
}
146+
// this.ctx.logger.info(message)
147+
this.ctx.parallel('nt/offline-message-created', message)
148+
}
138149
continue
139150
}
140151
if (message.senderUin && message.senderUin !== '0') {

src/ntqqapi/hook.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,20 @@ export function startHook() {
7676
else if (data.type === 'recv' && data.data.cmd === 'trpc.msg.olpush.OlPushService.MsgPush'){
7777
if (getConfigUtil().getConfig().rawMsgPB) {
7878
const msg = parseProtobufFromHex(data.data.pb)
79-
const peerId = msg[1][1][8][1]
80-
const msgRand = msg[1][2][4]
81-
const msgSeq = msg[1][2][5]
82-
const uniqueId = `${peerId}_${msgRand}_${msgSeq}`
83-
if (msgPBMap.size > 1000) {
84-
// 删除最老的记录
85-
const firstKey = msgPBMap.keys().next().value
86-
msgPBMap.delete(firstKey!)
79+
try {
80+
const peerId = msg[1][1][8][1]
81+
const msgRand = msg[1][2][4]
82+
const msgSeq = msg[1][2][5]
83+
const uniqueId = `${peerId}_${msgRand}_${msgSeq}`
84+
if (msgPBMap.size > 1000) {
85+
// 删除最老的记录
86+
const firstKey = msgPBMap.keys().next().value
87+
msgPBMap.delete(firstKey!)
88+
}
89+
msgPBMap.set(uniqueId, data.data.pb)
90+
}catch (e) {
91+
8792
}
88-
msgPBMap.set(uniqueId, data.data.pb)
8993
}
9094
}
9195
})

src/onebot11/entities.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,17 @@ export namespace OB11Entities {
176176
guildId: ''
177177
}
178178
try {
179-
const { replayMsgSeq, replyMsgTime } = replyElement
180-
const record = msg.records.find(msgRecord => msgRecord.msgId === replyElement.sourceMsgIdInRecords)
179+
const { replayMsgSeq: replyMsgSeq, replyMsgTime } = replyElement
180+
let record = msg.records.find(msgRecord => msgRecord.msgId === replyElement.sourceMsgIdInRecords)
181+
const { msgList } = await ctx.ntMsgApi.getMsgsBySeqAndCount(peer, replyMsgSeq, 1, true, true)
182+
if (!record){
183+
record = msgList.find(msg=>msg.msgSeq === replyMsgSeq && msg.msgTime === replyMsgTime)
184+
}
181185
const senderUid = replyElement.senderUidStr || record?.senderUid
182186
if (!record || !replyMsgTime || !senderUid) {
183187
ctx.logger.error('找不到回复消息', replyElement)
184188
continue
185189
}
186-
const { msgList } = await ctx.ntMsgApi.getMsgsBySeqAndCount(peer, replayMsgSeq, 1, true, true)
187190

188191
let replyMsg: RawMessage | undefined
189192
if (record.msgRandom !== '0') {
@@ -640,9 +643,12 @@ export namespace OB11Entities {
640643
msg: RawMessage,
641644
shortId: number
642645
): Promise<OB11FriendRecallNoticeEvent | OB11GroupRecallNoticeEvent | undefined> {
643-
const revokeElement = msg.elements[0].grayTipElement?.revokeElement
646+
const revokeElement = msg.elements[0].grayTipElement?.revokeElement!
644647
if (msg.chatType === ChatType.Group) {
645-
const operator = await ctx.ntGroupApi.getGroupMember(msg.peerUid, revokeElement!.operatorUid)
648+
const operator = await ctx.ntGroupApi.getGroupMember(msg.peerUid, revokeElement.operatorUid)
649+
if (msg.senderUin === '0' || !msg.senderUin){
650+
msg.senderUin = await ctx.ntUserApi.getUinByUid(revokeElement.origMsgSenderUid!)
651+
}
646652
return new OB11GroupRecallNoticeEvent(
647653
parseInt(msg.peerUid),
648654
parseInt(msg.senderUin!),

src/webui/BE/server.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export class WebUIServer extends Service {
147147
})
148148
return
149149
}
150-
150+
151151
// 将存储的明文密码 hash 后与前端传来的 hash 对比
152152
const hashedToken = hashPassword(token)
153153
if (reqToken !== hashedToken) {
@@ -351,10 +351,9 @@ export class WebUIServer extends Service {
351351
await this.startWithPort(forcePort)
352352
}
353353

354-
private setConfig(newConfig: Config) {
354+
public setConfig(newConfig: Config) {
355355
const oldConfig = { ...this.config }
356356
this.config = { onlyLocalhost: newConfig.onlyLocalhost, ...newConfig.webui }
357-
358357
}
359358

360359
async start() {

0 commit comments

Comments
 (0)