From d9fe5bbe0b41de4742446d12ae09e748967e3ada Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Tue, 8 Oct 2024 17:36:07 -0600 Subject: [PATCH] fix: Server not notifying users of E2EE key suggestions (#33435) Co-authored-by: Ricardo Garim <9621276+ricardogarim@users.noreply.github.com> --- .changeset/pink-wombats-wait.md | 6 ++++++ .../server/functions/provideUsersSuggestedGroupKeys.ts | 7 +++++-- apps/meteor/app/e2e/server/methods/updateGroupKey.ts | 8 ++++---- apps/meteor/server/models/raw/Subscriptions.ts | 5 +++-- packages/model-typings/src/models/ISubscriptionsModel.ts | 3 ++- 5 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 .changeset/pink-wombats-wait.md diff --git a/.changeset/pink-wombats-wait.md b/.changeset/pink-wombats-wait.md new file mode 100644 index 0000000000000..5c8d4cb6a0b15 --- /dev/null +++ b/.changeset/pink-wombats-wait.md @@ -0,0 +1,6 @@ +--- +"@rocket.chat/meteor": patch +"@rocket.chat/model-typings": patch +--- + +Fixes an issue causing server to not notify users via websocket of new E2EE keys suggested by other users to them when running in development environments. diff --git a/apps/meteor/app/e2e/server/functions/provideUsersSuggestedGroupKeys.ts b/apps/meteor/app/e2e/server/functions/provideUsersSuggestedGroupKeys.ts index 42408f398ecb9..9f69f17920d19 100644 --- a/apps/meteor/app/e2e/server/functions/provideUsersSuggestedGroupKeys.ts +++ b/apps/meteor/app/e2e/server/functions/provideUsersSuggestedGroupKeys.ts @@ -2,6 +2,7 @@ import type { IRoom, IUser } from '@rocket.chat/core-typings'; import { Rooms, Subscriptions } from '@rocket.chat/models'; import { canAccessRoomIdAsync } from '../../../authorization/server/functions/canAccessRoom'; +import { notifyOnSubscriptionChanged, notifyOnRoomChangedById } from '../../../lib/server/lib/notifyListener'; export const provideUsersSuggestedGroupKeys = async ( userId: IUser['_id'], @@ -21,13 +22,15 @@ export const provideUsersSuggestedGroupKeys = async ( const usersWithSuggestedKeys = []; for await (const user of usersSuggestedGroupKeys[roomId]) { - const { modifiedCount } = await Subscriptions.setGroupE2ESuggestedKey(user._id, roomId, user.key); - if (!modifiedCount) { + const { value } = await Subscriptions.setGroupE2ESuggestedKey(user._id, roomId, user.key); + if (!value) { continue; } + void notifyOnSubscriptionChanged(value); usersWithSuggestedKeys.push(user._id); } await Rooms.removeUsersFromE2EEQueueByRoomId(roomId, usersWithSuggestedKeys); + void notifyOnRoomChangedById(roomId); } }; diff --git a/apps/meteor/app/e2e/server/methods/updateGroupKey.ts b/apps/meteor/app/e2e/server/methods/updateGroupKey.ts index 87182f723e7de..6974ba225d9f9 100644 --- a/apps/meteor/app/e2e/server/methods/updateGroupKey.ts +++ b/apps/meteor/app/e2e/server/methods/updateGroupKey.ts @@ -3,7 +3,7 @@ import { Subscriptions } from '@rocket.chat/models'; import { Meteor } from 'meteor/meteor'; import { methodDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger'; -import { notifyOnSubscriptionChangedById, notifyOnSubscriptionChangedByRoomIdAndUserId } from '../../../lib/server/lib/notifyListener'; +import { notifyOnSubscriptionChangedById, notifyOnSubscriptionChanged } from '../../../lib/server/lib/notifyListener'; declare module '@rocket.chat/ddp-client' { // eslint-disable-next-line @typescript-eslint/naming-convention @@ -34,9 +34,9 @@ Meteor.methods({ } // uid also has subscription to this room - const { modifiedCount } = await Subscriptions.setGroupE2ESuggestedKey(uid, rid, key); - if (modifiedCount) { - void notifyOnSubscriptionChangedByRoomIdAndUserId(rid, uid); + const { value } = await Subscriptions.setGroupE2ESuggestedKey(uid, rid, key); + if (value) { + void notifyOnSubscriptionChanged(value); } } }, diff --git a/apps/meteor/server/models/raw/Subscriptions.ts b/apps/meteor/server/models/raw/Subscriptions.ts index dd4bb91df3259..02eb7927ed4d6 100644 --- a/apps/meteor/server/models/raw/Subscriptions.ts +++ b/apps/meteor/server/models/raw/Subscriptions.ts @@ -30,6 +30,7 @@ import type { AggregationCursor, CountDocumentsOptions, DeleteOptions, + ModifyResult, } from 'mongodb'; import { getDefaultSubscriptionPref } from '../../../app/utils/lib/getDefaultSubscriptionPref'; @@ -585,10 +586,10 @@ export class SubscriptionsRaw extends BaseRaw implements ISubscri return this.updateOne(query, update); } - setGroupE2ESuggestedKey(uid: string, rid: string, key: string): Promise { + setGroupE2ESuggestedKey(uid: string, rid: string, key: string): Promise> { const query = { rid, 'u._id': uid }; const update = { $set: { E2ESuggestedKey: key } }; - return this.updateOne(query, update); + return this.findOneAndUpdate(query, update, { returnDocument: 'after' }); } unsetGroupE2ESuggestedKey(_id: string): Promise { diff --git a/packages/model-typings/src/models/ISubscriptionsModel.ts b/packages/model-typings/src/models/ISubscriptionsModel.ts index 78fb38c8704bf..24fd36a073bab 100644 --- a/packages/model-typings/src/models/ISubscriptionsModel.ts +++ b/packages/model-typings/src/models/ISubscriptionsModel.ts @@ -12,6 +12,7 @@ import type { AggregationCursor, DeleteOptions, CountDocumentsOptions, + ModifyResult, } from 'mongodb'; import type { IBaseModel } from './IBaseModel'; @@ -115,7 +116,7 @@ export interface ISubscriptionsModel extends IBaseModel { setGroupE2EKey(_id: string, key: string): Promise; - setGroupE2ESuggestedKey(uid: string, rid: string, key: string): Promise; + setGroupE2ESuggestedKey(uid: string, rid: string, key: string): Promise>; unsetGroupE2ESuggestedKey(_id: string): Promise;