Skip to content

Commit b6fe68b

Browse files
committed
add error handling to xmpp slash command
1 parent 3dee320 commit b6fe68b

4 files changed

Lines changed: 36 additions & 9 deletions

File tree

apps/meteor/ee/server/startup/federation.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Users } from '@rocket.chat/models';
88

99
import { settings } from '../../../app/settings/server';
1010
import { slashCommands } from '../../../app/utils/server/slashCommand';
11+
import { i18n } from '../../../server/lib/i18n';
1112
import { StreamerCentral } from '../../../server/modules/streamer/streamer.module';
1213
import { registerFederationRoutes } from '../api/federation';
1314

@@ -96,14 +97,36 @@ export const startFederationService = async (): Promise<void> => {
9697

9798
slashCommands.add({
9899
command: 'xmpp',
99-
callback: async ({ params, message: _message, userId }: SlashCommandCallbackParams<'xmpp'>): Promise<void> => {
100+
callback: async ({ params, message, userId }: SlashCommandCallbackParams<'xmpp'>): Promise<void> => {
101+
// the helper advertises `#channel`, so accept the leading # and strip it before joining
102+
const channel = params.trim().replace(/^#/, '');
103+
if (!channel) {
104+
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
105+
msg: i18n.t('Federation_XMPP_Join_Channel_Required', {
106+
lng: settings.get('Language') || 'en',
107+
}),
108+
});
109+
return;
110+
}
111+
100112
const user = await Users.findOneById(userId);
101113
if (!user) {
102114
logger.error({ msg: 'User not found for joining xmpp room', userId });
103115
return;
104116
}
105117

106-
await FederationMatrixService.joinXMPPChatRoom(params.trim(), user);
118+
const joined = await FederationMatrixService.joinXMPPChatRoom(channel, user);
119+
120+
const lng = settings.get<string>('Language') || 'en';
121+
if (joined) {
122+
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
123+
msg: `${i18n.t('Federation_XMPP_Join_Channel_Success', { lng })}`,
124+
});
125+
} else {
126+
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
127+
msg: `${i18n.t('Federation_XMPP_Join_Channel_Failed', { lng })}`,
128+
});
129+
}
107130
},
108131
options: {
109132
description: 'Join xmpp rooms',

ee/packages/federation-matrix/src/FederationMatrix.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,20 +1038,21 @@ export class FederationMatrix extends ServiceClass implements IFederationMatrixS
10381038
);
10391039
}
10401040

1041-
async joinXMPPChatRoom(roomAlias: string, user: IUser): Promise<void> {
1041+
async joinXMPPChatRoom(roomAlias: string, user: IUser): Promise<boolean> {
10421042
try {
10431043
if (isUserNativeFederated(user)) {
10441044
throw new Error('Federated users cannot join XMPP chat rooms');
10451045
}
10461046

1047-
const result = await federationSDK.joinXMPPChatRoom(roomAlias, userIdSchema.parse(`@${user.username}:${this.serverName}`));
1048-
1049-
console.log(result);
1047+
await federationSDK.joinXMPPChatRoom(roomAlias, userIdSchema.parse(`@${user.username}:${this.serverName}`));
10501048

10511049
this.logger.info({ msg: 'User joined XMPP chat room successfully', username: user.username, roomAlias });
1050+
1051+
return true;
10521052
} catch (err) {
1053-
this.logger.error({ msg: 'Failed to join XMPP chat room', err });
1054-
throw err;
1053+
this.logger.error({ msg: 'Failed to join XMPP chat room', err, username: user.username, roomAlias });
1054+
1055+
return false;
10551056
}
10561057
}
10571058

packages/core-services/src/types/IFederationMatrixService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ export interface IFederationMatrixService {
3434
canUserAccessFederation(user: IUser): Promise<boolean>;
3535
notifyRoomRead(params: { room: IRoomNativeFederated; userId: string; threadId?: string }): Promise<void>;
3636
updateUserName(user: IUser): Promise<void>;
37-
joinXMPPChatRoom(roomAlias: string, user: IUser): Promise<void>;
37+
joinXMPPChatRoom(roomAlias: string, user: IUser): Promise<boolean>;
3838
saveFederationMessage(event: { event: PduForType<'m.room.message'>; event_id: EventID }): Promise<void>;
3939
}

packages/i18n/src/locales/en.i18n.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,6 +2310,9 @@
23102310
"Federation_Service_Allow_List_Description": "Restrict federation to the given allow list of domains.",
23112311
"Federation_Service_Validate_User_Domain": "Users email restrictions",
23122312
"Federation_Service_Validate_User_Domain_Description": "Restrict access to verified email addresses that match your Federated Domain.",
2313+
"Federation_XMPP_Join_Channel_Required": "Please provide a channel to join. Usage: `/xmpp #channel`",
2314+
"Federation_XMPP_Join_Channel_Success": "You joined the XMPP channel.",
2315+
"Federation_XMPP_Join_Channel_Failed": "Could not join the XMPP channel. Please try again later.",
23132316
"Field": "Field",
23142317
"Field_removed": "Field removed",
23152318
"Field_required": "Field required",

0 commit comments

Comments
 (0)