Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(identity): let a session retake a lid row after a release

Yudhi Armyndharis86c7580aaad8
diff --git a/src/engine/identity/lid-mapping-store.service.ts b/src/engine/identity/lid-mapping-store.service.ts
index fa526ab81..4fc4a9ea6 100644
--- a/src/engine/identity/lid-mapping-store.service.ts
+++ b/src/engine/identity/lid-mapping-store.service.ts
@@ -351,6 +351,10 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {
* that never got here. The rows stay: the table is shared. Never throws: the session is already gone.
*/
async releaseSession(sessionId: string): Promise<void> {
+ // A session that already reported a lid's phone writes nothing when it reports it again, so a row left
+ // without an owner here would stay so. Every set is dropped, even if the update fails, so each live
+ // session's next report takes its rows back.
+ this.seenBy.clear();
try {
// updatedAt is kept: no mapping changed, and the preload ranks rows by it.
await this.repo.update({ sessionId }, { sessionId: null, updatedAt: () => '"updatedAt"' });
diff --git a/src/engine/identity/lid-mapping-store.spec.ts b/src/engine/identity/lid-mapping-store.spec.ts
index 42eda6167..728ac2766 100644
--- a/src/engine/identity/lid-mapping-store.spec.ts
+++ b/src/engine/identity/lid-mapping-store.spec.ts
@@ -954,6 +954,36 @@ describe('LidMappingStoreService: a negative result on the shared table', () =>
warn.mockClear();
});
+ it('gives a released row back to a session that reported its phone before, on its next report', async () => {
+ await store.remember('111', '628111', s1);
+ await store.remember('111', '628111', s2);
+ await ds.getRepository(Session).delete({ id: s2 });
+ await store.releaseSession(s2);
+
+ await store.remember('111', '628111', s1);
+ expect(await row('111')).toMatchObject({ phone: '628111', sessionId: s1 });
+ await store.remember('111', null, s3);
+
+ expect(await row('111')).toMatchObject({ phone: '628111', sessionId: s1 });
+ expect(await store.findPhoneForLid('111@lid')).toBe('628111');
+ expect(await store.lidsForPhonesPersisted(['628111'])).toEqual({ 628111: ['111'] });
+ });
+
+ it('does so when the release could not write the table as well', async () => {
+ await store.remember('111', '628111', s1);
+ await store.remember('111', '628111', s2);
+ await ds.getRepository(Session).delete({ id: s2 });
+ jest.spyOn(repo, 'update').mockRejectedValueOnce(new Error('connection lost'));
+ await store.releaseSession(s2);
+ expect(warn).toHaveBeenCalledTimes(1);
+ warn.mockClear();
+
+ await store.remember('111', '628111', s1);
+ await store.remember('111', null, s3);
+
+ expect(await row('111')).toMatchObject({ phone: '628111', sessionId: s1 });
+ });
+
it('a phone from any session still replaces the row', async () => {
await store.remember('111', null, s1);
await store.remember('111', '628111', s2);
diff --git a/src/modules/auth/chat-scope.service.spec.ts b/src/modules/auth/chat-scope.service.spec.ts
index 8b7c89e0f..bf813e9ac 100644
--- a/src/modules/auth/chat-scope.service.spec.ts
+++ b/src/modules/auth/chat-scope.service.spec.ts
@@ -173,6 +173,19 @@ describe('ChatScopeService over the shared lid table, with two sessions', () =>
expect(await admitted()).toEqual([false, false]);
});
+ it('still admits it after the latest session to report the phone is deleted, once an earlier one reports it again', async () => {
+ const s3 = '00000003-0000-4000-8000-000000000000';
+ await ds.getRepository(Session).insert({ id: s3, name: s3 });
+ await store.remember(LID, PHONE, s2);
+ await ds.getRepository(Session).delete({ id: s2 });
+ await store.releaseSession(s2);
+
+ await store.remember(LID, PHONE, s1);
+ await store.remember(LID, null, s3);
+
+ expect(await admitted()).toEqual([true, true]);
+ });
+
it('stops admitting it once the session that stored the phone is deleted and another answers no phone', async () => {
await ds.getRepository(Session).delete({ id: s1 });