From fa98d0397a7b9c91144cfda3a63e7b2ab3212f51 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Mon, 9 Dec 2024 17:01:39 +1100 Subject: [PATCH] fix: make sure joined_at is in seconds before saving --- ts/receiver/configMessage.ts | 1 - ts/receiver/groupv2/handleGroupV2Message.ts | 2 +- ts/session/apis/snode_api/swarmPolling.ts | 11 +++++++---- .../libsession_wrapper_user_groups_test.ts | 17 ++++++++++++++++- .../group_sync_job/GroupSyncJob_test.ts | 2 +- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/ts/receiver/configMessage.ts b/ts/receiver/configMessage.ts index 6e300ff29..28894ec18 100644 --- a/ts/receiver/configMessage.ts +++ b/ts/receiver/configMessage.ts @@ -789,7 +789,6 @@ async function handleGroupUpdate(latestEnvelopeTimestamp: number) { for (let index = 0; index < allGroupsInWrapper.length; index++) { const groupInWrapper = allGroupsInWrapper[index]; window.inboxStore?.dispatch(groupInfoActions.handleUserGroupUpdate(groupInWrapper) as any); - await handleSingleGroupUpdate({ groupInWrapper, latestEnvelopeTimestamp, userEdKeypair }); } diff --git a/ts/receiver/groupv2/handleGroupV2Message.ts b/ts/receiver/groupv2/handleGroupV2Message.ts index 645687f70..80b12b3f7 100644 --- a/ts/receiver/groupv2/handleGroupV2Message.ts +++ b/ts/receiver/groupv2/handleGroupV2Message.ts @@ -68,7 +68,7 @@ async function getInitializedGroupObject({ if (!found) { found = { authData: null, - joinedAtSeconds: Date.now(), + joinedAtSeconds: Math.floor(Date.now()/ 1000), name: groupName, priority: 0, pubkeyHex: groupPk, diff --git a/ts/session/apis/snode_api/swarmPolling.ts b/ts/session/apis/snode_api/swarmPolling.ts index a5ea2ba03..7518a2f1f 100644 --- a/ts/session/apis/snode_api/swarmPolling.ts +++ b/ts/session/apis/snode_api/swarmPolling.ts @@ -462,6 +462,7 @@ export class SwarmPolling { ); // We always handle the config messages first (for groups 03 or our own messages) await this.handleUserOrGroupConfMessages({ confMessages, pubkey, type }); + await this.handleRevokedMessages({ revokedMessages, groupPk: pubkey, type }); // Merge results into one list of unique messages @@ -716,15 +717,17 @@ export class SwarmPolling { } private async handleSeenMessages( - messages: Array - ): Promise> { + messages: Array + ): Promise> { if (!messages.length) { return []; } - const incomingHashes = messages.map((m: RetrieveMessageItem) => m.hash); + const incomingHashes = messages.map((m: RetrieveMessageItemWithNamespace) => m.hash); const dupHashes = await Data.getSeenMessagesByHashList(incomingHashes); - const newMessages = messages.filter((m: RetrieveMessageItem) => !dupHashes.includes(m.hash)); + const newMessages = messages.filter( + (m: RetrieveMessageItemWithNamespace) => !dupHashes.includes(m.hash) + ); return newMessages; } diff --git a/ts/test/session/unit/libsession_wrapper/libsession_wrapper_user_groups_test.ts b/ts/test/session/unit/libsession_wrapper/libsession_wrapper_user_groups_test.ts index b917742fd..b0537c8d8 100644 --- a/ts/test/session/unit/libsession_wrapper/libsession_wrapper_user_groups_test.ts +++ b/ts/test/session/unit/libsession_wrapper/libsession_wrapper_user_groups_test.ts @@ -1,6 +1,6 @@ import { expect } from 'chai'; -import { LegacyGroupInfo } from 'libsession_util_nodejs'; +import { LegacyGroupInfo, UserGroupsWrapperNode } from 'libsession_util_nodejs'; import { describe } from 'mocha'; import Sinon from 'sinon'; import { ConversationModel } from '../../../../models/conversation'; @@ -223,6 +223,21 @@ describe('libsession_user_groups', () => { 'joinedAtSeconds in the wrapper should match the inputted group' ).to.equal(group.get('lastJoinedTimestamp')); }); + + it('throws when joined_at is too far in the future', async () => { + const us = await TestUtils.generateUserKeyPairs(); + + const groupWrapper = new UserGroupsWrapperNode(us.ed25519KeyPair.privKeyBytes, null); + + const group = groupWrapper.createGroup(); + group.joinedAtSeconds = 4099680000 - 1; // 4099680000 is 1st january 2100 GMT + groupWrapper.setGroup(group); // shouldn't throw + group.joinedAtSeconds = 4099680000 + 1; // 4099680000 is 1st january 2100 GMT + expect(() => { + groupWrapper.setGroup(group); + }).to.throw(); + }); + it('if disappearing messages is on then the wrapper returned values should match the inputted group', async () => { const group = new ConversationModel({ ...validArgs, diff --git a/ts/test/session/unit/utils/job_runner/group_sync_job/GroupSyncJob_test.ts b/ts/test/session/unit/utils/job_runner/group_sync_job/GroupSyncJob_test.ts index e8cf89f9c..b0e671c65 100644 --- a/ts/test/session/unit/utils/job_runner/group_sync_job/GroupSyncJob_test.ts +++ b/ts/test/session/unit/utils/job_runner/group_sync_job/GroupSyncJob_test.ts @@ -57,7 +57,7 @@ function validUserGroup03WithSecKey(pubkey?: GroupPubkeyType) { secretKey: new Uint8Array(30), destroyed: false, invitePending: false, - joinedAtSeconds: Date.now(), + joinedAtSeconds: Math.floor(Date.now()/1000), kicked: false, priority: 0, pubkeyHex: pubkey || TestUtils.generateFakeClosedGroupV2PkStr(),