

















@@ -239,6 +239,7 @@ export async function attachWebInboxToSocket(
239239debounceKey?: string;
240240durableId?: string;
241241readReceipt?: WhatsAppReadReceiptTarget;
242+receiveOrder?: number;
242243};
243244const durableInboundJournal = createWhatsAppDurableInboundReceiveJournal(options.accountId);
244245const inboundDebounceMs = Math.max(0, Math.trunc(options.debounceMs ?? 0));
@@ -262,6 +263,14 @@ export async function attachWebInboxToSocket(
262263};
263264const shouldDebounceInboundMessage = (msg: WebInboundMessage): boolean =>
264265options.shouldDebounce?.(msg) ?? true;
266+const orderDebouncedInboundEntries = (entries: QueuedInboundMessage[]) =>
267+entries.toSorted((a, b) => {
268+const timestampDiff = (a.timestamp ?? 0) - (b.timestamp ?? 0);
269+if (timestampDiff !== 0) {
270+return timestampDiff;
271+}
272+return (a.receiveOrder ?? 0) - (b.receiveOrder ?? 0);
273+});
265274266275const finalizeInboundDelivery = async (
267276entries: QueuedInboundMessage[],
@@ -312,23 +321,24 @@ export async function attachWebInboxToSocket(
312321});
313322activeInboundFlushes.add(flushTask);
314323try {
315-const last = entries.at(-1);
324+const orderedEntries = orderDebouncedInboundEntries(entries);
325+const last = orderedEntries.at(-1);
316326if (!last) {
317327return;
318328}
319329try {
320-if (entries.length === 1) {
330+if (orderedEntries.length === 1) {
321331await options.onMessage(last);
322-await finalizeInboundDelivery(entries);
332+await finalizeInboundDelivery(orderedEntries);
323333return;
324334}
325335const mentioned = new Set<string>();
326-for (const entry of entries) {
336+for (const entry of orderedEntries) {
327337for (const jid of entry.mentions ?? entry.mentionedJids ?? []) {
328338mentioned.add(jid);
329339}
330340}
331-const combinedBody = entries
341+const combinedBody = orderedEntries
332342.map((entry) => entry.body)
333343.filter(Boolean)
334344.join("\n");
@@ -340,9 +350,9 @@ export async function attachWebInboxToSocket(
340350isBatched: true,
341351};
342352await options.onMessage(combinedMessage);
343-await finalizeInboundDelivery(entries);
353+await finalizeInboundDelivery(orderedEntries);
344354} catch (error) {
345-await finalizeInboundDelivery(entries, error);
355+await finalizeInboundDelivery(orderedEntries, error);
346356throw error;
347357}
348358} finally {
@@ -739,6 +749,7 @@ export async function attachWebInboxToSocket(
739749const processDurableInboundMessage = async (
740750msg: WAMessage,
741751upsertType: string | undefined,
752+receiveOrder: number | undefined,
742753stored?: {
743754id: string;
744755payload: WhatsAppDurableInboundPayload;
@@ -827,6 +838,7 @@ export async function attachWebInboxToSocket(
827838await enqueueInboundMessage(msg, inbound, enriched, {
828839 durableId,
829840readReceipt: deliveryReadReceipt,
841+ receiveOrder,
830842});
831843};
832844@@ -836,6 +848,7 @@ export async function attachWebInboxToSocket(
836848await processDurableInboundMessage(
837849deserializeWhatsAppDurableInboundMessage(record.payload.message),
838850record.payload.upsertType,
851+record.payload.receivedAt,
839852{
840853id: record.id,
841854payload: record.payload,
@@ -917,6 +930,7 @@ export async function attachWebInboxToSocket(
917930durable: {
918931durableId?: string;
919932readReceipt?: WhatsAppReadReceiptTarget;
933+receiveOrder?: number;
920934},
921935) => {
922936const chatJid = inbound.remoteJid;
@@ -1023,6 +1037,7 @@ export async function attachWebInboxToSocket(
10231037dedupeKey: inbound.id ? `${options.accountId}:${inbound.remoteJid}:${inbound.id}` : undefined,
10241038durableId: durable.durableId,
10251039readReceipt: durable.readReceipt,
1040+receiveOrder: durable.receiveOrder,
10261041};
10271042const debounceKey = buildInboundDebounceKey(inboundMessage);
10281043if (debounceKey) {
@@ -1053,11 +1068,13 @@ export async function attachWebInboxToSocket(
10531068};
1054106910551070const pendingMessageHandlers = new Set<Promise<void>>();
1071+let nextReceiveOrder = 0;
10561072const handleMessagesUpsert = async (upsert: { type?: string; messages?: Array<WAMessage> }) => {
10571073if (upsert.type !== "notify" && upsert.type !== "append") {
10581074return;
10591075}
10601076for (const msg of upsert.messages ?? []) {
1077+const receiveOrder = nextReceiveOrder++;
10611078if (
10621079await maybeResolveWhatsAppApprovalReaction({
10631080cfg: options.loadConfig?.() ?? options.cfg,
@@ -1072,7 +1089,7 @@ export async function attachWebInboxToSocket(
10721089continue;
10731090}
107410911075-await processDurableInboundMessage(msg, upsert.type);
1092+await processDurableInboundMessage(msg, upsert.type, receiveOrder);
10761093}
10771094};
10781095const handleMessagesUpsertEvent = (upsert: { type?: string; messages?: Array<WAMessage> }) => {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。