Skip to content

Commit 18da108

Browse files
committed
enhance: shortMsgId consistency
1 parent 83559aa commit 18da108

9 files changed

Lines changed: 50 additions & 51 deletions

File tree

src/main/store.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ declare module 'cordis' {
1212
message: {
1313
shortId: number
1414
msgId: string
15+
uniqueMsgId: string
1516
chatType: number
1617
peerUid: string
1718
}
@@ -47,6 +48,7 @@ class Store extends Service {
4748
shortId: 'integer(10)',
4849
chatType: 'unsigned',
4950
msgId: 'string(24)',
51+
uniqueMsgId: 'string(64)',
5052
peerUid: 'string(24)'
5153
}, {
5254
primary: 'shortId'
@@ -75,17 +77,31 @@ class Store extends Service {
7577
})
7678
}
7779

78-
createMsgShortId(peer: Peer, msgId: string): number {
79-
const existingShortId = this.getShortIdByMsgInfo(peer, msgId)
80+
getUniqueMsgId(msg: RawMessage): string {
81+
return `${msg.chatType}-${msg.peerUid}-${msg.msgSeq}-${msg.msgRandom}}`
82+
}
83+
84+
createMsgShortId(msg: RawMessage): number {
85+
const peer = {
86+
chatType: msg.chatType,
87+
peerUid: msg.peerUid,
88+
guildId: ''
89+
}
90+
// QQ 本地给的 msgId 是和 Protobuf 给的不一致
91+
// 并且本地的 msgId 是根据一个本地保存的随机字符串 + 某种算法生成的,如果将 QQ 数据库清空了,这个随机字符串会变
92+
// 这就导致每次清空数据库后收到的同一条消息的 msgId 都不一样
93+
// 所以这里改成用 msgSeq + msgRandom 来生成 shortId,保证清空 QQ 数据库后收到同一条消息收到 shortId 都一致
94+
const uniqueMsgId = this.getUniqueMsgId(msg)
95+
const existingShortId = this.getShortIdByMsgInfo(peer, uniqueMsgId)
8096
if (existingShortId) {
8197
return existingShortId
8298
}
83-
const key = `${msgId}|${peer.chatType}|${peer.peerUid}`
84-
const hash = createHash('md5').update(key).digest()
99+
const hash = createHash('md5').update(uniqueMsgId).digest()
85100
const shortId = hash.readInt32BE() // OneBot 11 要求 message_id 为 int32
86-
this.cache.set(key, shortId)
101+
this.cache.set(uniqueMsgId, shortId)
87102
this.ctx.database.upsert('message', [{
88-
msgId,
103+
msgId: msg.msgId,
104+
uniqueMsgId,
89105
shortId,
90106
chatType: peer.chatType,
91107
peerUid: peer.peerUid
@@ -124,6 +140,20 @@ class Store extends Service {
124140
return (await this.ctx.database.get('message', { msgId }))[0]?.shortId
125141
}
126142

143+
async getShortIdByUniqueMsgId(uniqueMsgId: string): Promise<number | undefined> {
144+
return (await this.ctx.database.get('message', { uniqueMsgId }))[0]?.shortId
145+
}
146+
147+
async checkMsgExist(msg: RawMessage): Promise<boolean> {
148+
const uniqueMsgId = this.getUniqueMsgId(msg)
149+
const existingShortId = await this.getShortIdByUniqueMsgId(uniqueMsgId)
150+
if (existingShortId) {
151+
return true
152+
}
153+
this.createMsgShortId(msg)
154+
return false
155+
}
156+
127157
getShortIdByMsgInfo(peer: Peer, msgId: string) {
128158
const cacheKey = `${msgId}|${peer.chatType}|${peer.peerUid}`
129159
return this.cache.getValue(cacheKey)

src/ntqqapi/core.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,8 @@ class Core extends Service {
129129
for (const message of msgList) {
130130
const msgTime = parseInt(message.msgTime)
131131
if (msgTime < this.startupTime) {
132-
const uniqueId = `${message.peerUid}-${message.msgSeq}-${message.msgRandom}`
133-
const existing = await this.ctx.store.getShortIdByMsgId(uniqueId)
132+
const existing = await this.ctx.store.checkMsgExist(message)
134133
if (!existing){
135-
this.ctx.logger.info(uniqueId)
136-
const peer = {
137-
chatType: message.chatType,
138-
peerUid: message.peerUin,
139-
guildId: '',
140-
}
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)
147134
this.ctx.parallel('nt/offline-message-created', message)
148135
}
149136
continue

src/onebot11/action/go-cqhttp/GetGroupEssence.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class GetEssenceMsgList extends BaseAction<Payload, EssenceMsg[]> {
4242
operator_id: +item.opUin,
4343
operator_nick: item.opNick,
4444
operator_time: item.opTime,
45-
message_id: this.ctx.store.createMsgShortId(peer, sourceMsg.msgId)
45+
message_id: this.ctx.store.createMsgShortId(sourceMsg)
4646
})
4747
}
4848
return data

src/onebot11/action/go-cqhttp/SendForwardMsg.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,7 @@ export class SendForwardMsg extends BaseAction<Payload, Response> {
137137
}),
138138
},
139139
}], 1800)
140-
const msgShortId = this.ctx.store.createMsgShortId({
141-
chatType: msg!.chatType,
142-
peerUid: msg!.peerUid,
143-
guildId: ''
144-
}, msg!.msgId)
140+
const msgShortId = this.ctx.store.createMsgShortId(msg!)
145141
return { message_id: msgShortId, forward_id: resid }
146142
} catch (e) {
147143
this.ctx.logger.error('合并转发失败', e)
@@ -275,11 +271,7 @@ export class SendForwardMsg extends BaseAction<Payload, Response> {
275271
}
276272
const msg = await this.ctx.ntMsgApi.multiForwardMsg(srcPeer!, destPeer, retMsgIds)
277273
const resid = JSON.parse(msg.elements[0].arkElement!.bytesData).meta.detail.resid
278-
const msgShortId = this.ctx.store.createMsgShortId({
279-
chatType: msg.chatType,
280-
peerUid: msg.peerUid,
281-
guildId: ''
282-
}, msg.msgId)
274+
const msgShortId = this.ctx.store.createMsgShortId(msg)
283275
return { message_id: msgShortId, forward_id: resid }
284276
}
285277
}

src/onebot11/action/llonebot/msg/ForwardSingleMsg.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ abstract class ForwardSingleMsg extends BaseAction<Payload, Response> {
4646
}
4747

4848
// 创建消息id
49-
const msgShortId = this.ctx.store.createMsgShortId({
50-
chatType: ret[0].chatType,
51-
peerUid: ret[0].peerUid,
52-
guildId: ''
53-
}, ret[0].msgId)
49+
const msgShortId = this.ctx.store.createMsgShortId(ret[0])
5450
return { message_id: msgShortId }
5551
}
5652
}

src/onebot11/action/msg/SendMsg.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnData> {
3838
if (!returnMsg) {
3939
throw new Error('消息发送失败')
4040
}
41-
const msgShortId = this.ctx.store.createMsgShortId({
42-
chatType: returnMsg.chatType,
43-
peerUid: returnMsg.peerUid,
44-
guildId: ''
45-
}, returnMsg.msgId)
41+
const msgShortId = this.ctx.store.createMsgShortId(returnMsg)
4642
return { message_id: msgShortId }
4743
}
4844
}

src/onebot11/adapter.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class OneBot11Adapter extends Service {
217217
// this.dispatch(privateEvent)
218218
// }
219219
// })
220-
const shortId = this.ctx.store.createMsgShortId(peer, message.msgId)
220+
const shortId = this.ctx.store.createMsgShortId(message)
221221

222222
OB11Entities.recallEvent(this.ctx, message, shortId).then((recallEvent) => {
223223
if (recallEvent) {
@@ -316,7 +316,9 @@ class OneBot11Adapter extends Service {
316316
if (input.senderUid === selfInfo.uid) {
317317
this.handleMsg(input, true, true)
318318
}
319-
this.handleMsg(input, false, true)
319+
else {
320+
this.handleMsg(input, false, true)
321+
}
320322
})
321323
this.ctx.on('nt/message-deleted', input => {
322324
this.handleRecallMsg(input)
@@ -525,7 +527,7 @@ class OneBot11Adapter extends Service {
525527
this.ctx.logger.error('解析群表情回应失败:未找到消息')
526528
return
527529
}
528-
const messageId = this.ctx.store.createMsgShortId(peer, targetMsg.msgList[0].msgId)
530+
const messageId = this.ctx.store.createMsgShortId(targetMsg.msgList[0])
529531
const event = new OB11GroupMsgEmojiLikeEvent(
530532
notify.groupCode,
531533
userId,

src/onebot11/entities.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ export namespace OB11Entities {
5555
): Promise<OB11Message | undefined> {
5656
if (!msg.senderUin || msg.senderUin === '0' || msg.msgType === 1) return //跳过空消息
5757
const selfUin = selfInfo.uin
58-
const msgShortId = ctx.store.createMsgShortId({
59-
chatType: msg.chatType,
60-
peerUid: msg.peerUid,
61-
guildId: ''
62-
}, msg.msgId)
58+
const msgShortId = ctx.store.createMsgShortId(msg)
6359
const resMsg: OB11Message = {
6460
self_id: Number(selfUin),
6561
user_id: Number(msg.senderUin),
@@ -213,7 +209,7 @@ export namespace OB11Entities {
213209
messageSegment = {
214210
type: OB11MessageDataType.Reply,
215211
data: {
216-
id: ctx.store.createMsgShortId(peer, replyMsg.msgId).toString()
212+
id: ctx.store.createMsgShortId(replyMsg).toString()
217213
}
218214
}
219215
} catch (e) {

src/onebot11/event/notice/OB11GroupEssenceEvent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class GroupEssenceEvent extends OB11GroupNoticeEvent {
4343
if (!sourceMsg) return
4444
return new GroupEssenceEvent(
4545
parseInt(groupCode),
46-
ctx.store.createMsgShortId(peer, sourceMsg.msgId),
46+
ctx.store.createMsgShortId(sourceMsg!),
4747
parseInt(essence.items[0]?.msgSenderUin ?? sourceMsg.senderUin),
4848
parseInt(essence.items[0]?.opUin ?? '0'),
4949
)

0 commit comments

Comments
 (0)