Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/platform-crossgram/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ export class QQNTPlatform implements IMPlatform<QQMediaLocator> {
private reactionResourceWarmup?: Promise<void>
private readonly grayTipFilters: readonly string[]
private readonly originSessions = new Map<string, string>()
/** 每个会话一条发送队列的队尾,见 `queueConversationSend`。 */
private readonly conversationSends = new Map<string, Promise<unknown>>()
private readonly splitOutgoingMessages = new Map<string, WireMessage>()
private readonly multiForwardMessages = new Map<string, Promise<WireMessage[]>>()
private readonly multiForwardPreviewJobs = new Map<string, Promise<string | undefined>>()
Expand Down Expand Up @@ -1199,6 +1201,38 @@ export class QQNTPlatform implements IMPlatform<QQMediaLocator> {
conversation: IMConversationRef,
content: IMMessageInput,
options: IMTransferOptions = {},
): Promise<IMMessage<QQMediaLocator>> {
return this.queueConversationSend(conversation.id, () =>
this.deliverMessage(session, conversation, content, options))
}

/**
* 让同一会话的发送按发起顺序落库。
*
* QQ 的房间顺序就是落库顺序,而带媒体的消息要先传完字节才发出送消息请求
* (`QQNTClient.sendMessage` 里 upload 在 POST 之前)。一批消息并发发起时,
* 落库顺序因此变成「上传完成顺序」:带图的那条总是排到最后,相邻的纯文本也可能
* 互相插队。串成一条队列后,发起顺序即落库顺序。
*/
private queueConversationSend<T>(conversationId: string, run: () => Promise<T>): Promise<T> {
const previous = this.conversationSends.get(conversationId) ?? Promise.resolve()
const current = previous.catch(() => undefined).then(run)
this.conversationSends.set(conversationId, current)
// 队尾结算后清掉,避免会话表无限增长;只清自己这一环。
const release = () => {
if (this.conversationSends.get(conversationId) === current) {
this.conversationSends.delete(conversationId)
}
}
current.then(release, release)
return current
}

private async deliverMessage(
session: PlatformSession,
conversation: IMConversationRef,
content: IMMessageInput,
options: IMTransferOptions = {},
): Promise<IMMessage<QQMediaLocator>> {
type ResolvedMentionUser = Awaited<ReturnType<QQNTClient['getUser']>>
const resolvedMentionUsers = new Map<string, Promise<ResolvedMentionUser>>()
Expand Down
31 changes: 31 additions & 0 deletions packages/platform-crossgram/src/platform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4247,6 +4247,37 @@ describe('QQNTPlatform mapping', () => {
expect(progress).toEqual([1, 2])
})

it('lands a burst of sends in issue order even when the media one uploads first', async () => {
const platform = new QQNTPlatform()
const landed: string[] = []
platform.client.sendMessage = vi.fn(async (_conversation: string, parts) => {
const media = parts.some((part) => part.type === 'media')
// 带媒体的发送要先把字节传完才发出送消息请求,这里用等待模拟上传耗时。
if (media) await new Promise((resolve) => setTimeout(resolve, 20))
landed.push(media ? 'image' : 'text')
return {
id: `sent-${landed.length}`, conversationId: '2:group', senderId: 'self', timestamp: 10,
outgoing: true,
parts: parts.map((part) => part.type === 'text' ? part : { type: 'text' as const, text: '' }),
}
})
const image = {
kind: 'image' as const, name: 'a.png', mimeType: 'image/png', size: 3,
source: { size: 3, async *stream() { yield new Uint8Array([1, 2, 3]) } },
}

await Promise.all([
platform.sendMessage(session, { id: '2:group' }, { parts: [
{ type: 'text', text: 'before' },
{ type: 'media', media: image },
{ type: 'text', text: 'after' },
] }),
platform.sendMessage(session, { id: '2:group' }, { parts: [{ type: 'text', text: 'next' }] }),
])

expect(landed).toEqual(['image', 'text'])
})

it('passes the message element order to the bridge', async () => {
const platform = new QQNTPlatform()
const image = (name: string) => ({
Expand Down
Loading