Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): look up a status post's id only when an engine asks

Yudhi Armyndharis8431a55d7c60
diff --git a/src/modules/session/message-projector.service.spec.ts b/src/modules/session/message-projector.service.spec.ts
index 188fb1172..f9af50508 100644
--- a/src/modules/session/message-projector.service.spec.ts
+++ b/src/modules/session/message-projector.service.spec.ts
@@ -1771,6 +1771,20 @@ describe('MessageProjector (inbound projection)', () => {
expect(projector.handleInboundMessage(SESSION_ID, retired, makeIncoming())).toBeUndefined();
});
+ it('answers nothing for an ephemeral message it skips', () => {
+ process.env.STORE_EPHEMERAL_MESSAGES = 'false';
+ try {
+ const engine = makeEngine();
+ engines.set(SESSION_ID, engine);
+
+ expect(
+ projector.handleInboundMessage(SESSION_ID, engine, makeIncoming({ ephemeralDuration: 86400 })),
+ ).toBeUndefined();
+ } finally {
+ delete process.env.STORE_EPHEMERAL_MESSAGES;
+ }
+ });
+
it('settles with nothing when the hook chain fails', async () => {
const engine = makeEngine();
engines.set(SESSION_ID, engine);
@@ -1807,6 +1821,24 @@ describe('MessageProjector (inbound projection)', () => {
expect(statusStore.ingest).toHaveBeenCalledTimes(1);
});
+ it('looks its id up when the outcome is asked for, and once, not for an engine that ignores it', async () => {
+ statusStore.ingest.mockResolvedValue({ row: {}, created: true });
+ const engine = makeEngine();
+ engines.set(SESSION_ID, engine);
+
+ const receipt = projector.handleInboundMessage(
+ SESSION_ID,
+ engine,
+ makeIncoming({ ...status, author: '[email protected]' }),
+ );
+ await flush();
+ expect(statusStore.ingest).toHaveBeenCalledTimes(1);
+ expect(messageRepository.findOne).not.toHaveBeenCalled();
+
+ await expect(Promise.all([receipt?.settled, receipt?.settled])).resolves.toEqual([undefined, undefined]);
+ expect(messageRepository.findOne).toHaveBeenCalledTimes(1);
+ });
+
it('reports nothing for a story no message shares an id with', async () => {
statusStore.ingest.mockResolvedValue({ row: {}, created: true });
const engine = makeEngine();
diff --git a/src/modules/session/message-projector.service.ts b/src/modules/session/message-projector.service.ts
index dace336f1..95936aafa 100644
--- a/src/modules/session/message-projector.service.ts
+++ b/src/modules/session/message-projector.service.ts
@@ -345,7 +345,8 @@ export class MessageProjector {
* ticket (PostgreSQL), the hook chain starts in the chat's arrival order and the commit takes the
* turn admitInbound reserved; whatever path returns early drops the ticket. The receipt, for an
* engine that wants to know what became of its message, settles once the commit ends (see
- * {@link InboundOutcome}); there is none when the message is not committed at all.
+ * {@link InboundOutcome}). There is none when the engine is no longer live or the message is skipped as
+ * ephemeral; one that settles with nothing says nothing, as when the hook chain failed.
*/
handleInboundMessage(
id: string,
@@ -373,8 +374,15 @@ export class MessageProjector {
// The status store holds the payload until the ingest settles.
turn?.holdUntil(ingest);
// Its own table takes the status whatever the id, and a story is never a row of `messages`: one
- // that finds a message there under its id reuses that id.
- return { settled: ingest.then(() => this.judgeRepeat(id, message)) };
+ // that finds a message there under its id reuses that id. Only an engine that keeps its own copy of
+ // a message reads the outcome, so the lookup is made when it is asked for, and once.
+ let judged: Promise<InboundOutcome | undefined> | undefined;
+ const judge = (): Promise<InboundOutcome | undefined> => ingest.then(() => this.judgeRepeat(id, message));
+ return {
+ get settled(): Promise<InboundOutcome | undefined> {
+ return (judged ??= judge());
+ },
+ };
}
if (this.shouldSkipEphemeralMessage(id, message)) return;
this.logger.debug(`Message received from ${message.from}`, {