Skip to content

Commit 5a0d940

Browse files
committed
chore: remove federationMatrix calls from CE code
1 parent f12881d commit 5a0d940

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

apps/meteor/app/lib/server/methods/addUsersToRoom.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { api, FederationMatrix } from '@rocket.chat/core-services';
1+
import { api } from '@rocket.chat/core-services';
22
import type { IUser, SubscriptionStatus } from '@rocket.chat/core-typings';
33
import { isRoomNativeFederated } from '@rocket.chat/core-typings';
44
import type { ServerMethods } from '@rocket.chat/ddp-client';
@@ -107,30 +107,26 @@ export const addUsersToRoomMethod = async (userId: string, data: { rid: string;
107107

108108
const subscription = await Subscriptions.findOneByRoomIdAndUserId(data.rid, newUser._id);
109109
if (!subscription) {
110-
// no clear and easy way to avoid federation logic here, since we must trigger an invite
111-
// and set the status to INVITED when dealing with federated users
112110
let inviteOptions: { status?: SubscriptionStatus; inviterUsername?: string } = {};
113111
if (isRoomNativeFederated(room) && user && newUser.username) {
114-
await FederationMatrix.inviteUsersToRoom(room, [newUser.username], user);
115112
inviteOptions = {
116113
status: 'INVITED',
117114
inviterUsername: user.username,
118115
};
119116
}
120117

121118
return addUserToRoom(data.rid, newUser, user, inviteOptions);
122-
} else {
123-
if (!newUser.username) {
124-
return;
125-
}
126-
void api.broadcast('notify.ephemeralMessage', userId, data.rid, {
127-
msg: i18n.t('Username_is_already_in_here', {
128-
postProcess: 'sprintf',
129-
sprintf: [newUser.username],
130-
lng: user?.language,
131-
}),
132-
});
133119
}
120+
if (!newUser.username) {
121+
return;
122+
}
123+
void api.broadcast('notify.ephemeralMessage', userId, data.rid, {
124+
msg: i18n.t('Username_is_already_in_here', {
125+
postProcess: 'sprintf',
126+
sprintf: [newUser.username],
127+
lng: user?.language,
128+
}),
129+
});
134130
}),
135131
);
136132

apps/meteor/ee/server/hooks/federation/index.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { FederationMatrix } from '@rocket.chat/core-services';
1+
import { FederationMatrix, Authorization, MeteorError } from '@rocket.chat/core-services';
22
import { isEditedMessage, type IMessage, type IRoom, type IUser } from '@rocket.chat/core-typings';
33
import { Rooms } from '@rocket.chat/models';
44

55
import { callbacks } from '../../../../lib/callbacks';
66
import { afterLeaveRoomCallback } from '../../../../lib/callbacks/afterLeaveRoomCallback';
77
import { afterRemoveFromRoomCallback } from '../../../../lib/callbacks/afterRemoveFromRoomCallback';
8-
import { beforeAddUsersToRoom } from '../../../../lib/callbacks/beforeAddUserToRoom';
8+
import { beforeAddUsersToRoom, beforeAddUserToRoom } from '../../../../lib/callbacks/beforeAddUserToRoom';
99
import { beforeChangeRoomRole } from '../../../../lib/callbacks/beforeChangeRoomRole';
1010
import { FederationActions } from '../../../../server/services/room/hooks/BeforeFederationActions';
1111

@@ -76,6 +76,23 @@ beforeAddUsersToRoom.add(async ({ usernames }, room) => {
7676
}
7777
});
7878

79+
beforeAddUserToRoom.add(
80+
async ({ user, inviter }, room) => {
81+
if (!user.username || !inviter) {
82+
return;
83+
}
84+
85+
if (FederationActions.shouldPerformFederationAction(room)) {
86+
if (!(await Authorization.hasPermission(user._id, 'access-federation'))) {
87+
throw new MeteorError('error-not-authorized-federation', 'Not authorized to access federation');
88+
}
89+
await FederationMatrix.inviteUsersToRoom(room, [user.username], inviter);
90+
}
91+
},
92+
callbacks.priority.MEDIUM,
93+
'native-federation-on-before-add-users-to-room',
94+
);
95+
7996
callbacks.add(
8097
'afterSetReaction',
8198
async (message: IMessage, params): Promise<void> => {

ee/packages/federation-matrix/src/events/member.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ async function handleInvite({
133133
throw new Error(`Room not found or could not be created: ${roomId}`);
134134
}
135135

136+
const subscription = await Subscriptions.findOneByRoomIdAndUserId(room._id, inviteeUser._id);
137+
if (subscription) {
138+
return;
139+
}
140+
136141
await Room.addUserToRoom(room._id, inviteeUser, inviterUser, {
137142
status: 'INVITED',
138143
inviterUsername: inviterUser.username,

0 commit comments

Comments
 (0)