Skip to content

Commit 84d84f7

Browse files
committed
Normalize Zalo provider timestamps
1 parent 4a023cf commit 84d84f7

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

extensions/zalo/src/monitor.image.polling.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ describe("Zalo polling image handling", () => {
6868
finalizeInboundContextMock,
6969
recordInboundSessionMock,
7070
});
71+
expect(finalizeInboundContextMock).toHaveBeenCalledWith(
72+
expect.objectContaining({ Timestamp: 1774084566880 }),
73+
);
7174

7275
abort.abort();
7376
await run;

extensions/zalo/src/monitor.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const ZALO_TEXT_LIMIT = 2000;
6464
const DEFAULT_MEDIA_MAX_MB = 5;
6565
const WEBHOOK_CLEANUP_TIMEOUT_MS = 5_000;
6666
const ZALO_TYPING_TIMEOUT_MS = 5_000;
67+
const UNIX_MILLISECONDS_THRESHOLD = 1_000_000_000_000;
6768

6869
type ZaloCoreRuntime = ReturnType<typeof getZaloRuntime>;
6970
type ZaloStatusSink = (patch: { lastInboundAt?: number; lastOutboundAt?: number }) => void;
@@ -93,6 +94,13 @@ type ZaloUpdateProcessingParams = ZaloProcessingContext & {
9394
let zaloWebhookModulePromise: Promise<ZaloWebhookModule> | undefined;
9495
const hostedMediaRouteRefs = new Map<string, { count: number; unregisters: Array<() => void> }>();
9596

97+
function resolveZaloTimestampMs(date: number | undefined): number | undefined {
98+
if (!date) {
99+
return undefined;
100+
}
101+
return date >= UNIX_MILLISECONDS_THRESHOLD ? date : date * 1000;
102+
}
103+
96104
function loadZaloWebhookModule(): Promise<ZaloWebhookModule> {
97105
zaloWebhookModulePromise ??= import("./monitor.webhook.js");
98106
return zaloWebhookModulePromise;
@@ -564,18 +572,19 @@ async function processMessageWithPipeline(params: ZaloMessagePipelineParams): Pr
564572
}
565573

566574
const fromLabel = isGroup ? `group:${chatId}` : senderName || `user:${senderId}`;
575+
const timestamp = resolveZaloTimestampMs(date);
567576
const { storePath, body } = buildEnvelope({
568577
channel: "Zalo",
569578
from: fromLabel,
570-
timestamp: date ? date * 1000 : undefined,
579+
timestamp,
571580
body: rawBody,
572581
});
573582

574583
const ctxPayload = core.channel.inbound.buildContext({
575584
channel: "zalo",
576585
accountId: route.accountId,
577586
messageId: message_id,
578-
timestamp: date ? date * 1000 : undefined,
587+
timestamp,
579588
from: isGroup ? `zalo:group:${chatId}` : `zalo:${senderId}`,
580589
sender: {
581590
id: senderId,

0 commit comments

Comments
 (0)