Skip to content

Commit

Permalink
[NEW] remove user from matrix room (#25905)
Browse files Browse the repository at this point in the history
* fix: bridge port

* feat: leave matrix room when user leaves rocket room

* refactor: leave room method

* refactor: setup callbacks

* fix: add after leave room callback

* fix: fix lint and revert hardcoded port

* fix: remove dead code

* chore: fix lint

Co-authored-by: Marcos Defendi <marcos.defendi@rocket.chat>
  • Loading branch information
carlosrodrigues94 and MarcosSpessatto committed Jun 22, 2022
1 parent 1c165c0 commit 23d1fe0
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { IFederationBridge } from '../domain/IFederationBridge';
import { RocketChatRoomAdapter } from '../infrastructure/rocket-chat/adapters/Room';
import { RocketChatSettingsAdapter } from '../infrastructure/rocket-chat/adapters/Settings';
import { RocketChatUserAdapter } from '../infrastructure/rocket-chat/adapters/User';
import { FederationCreateDMAndInviteUserDto, FederationRoomSendExternalMessageDto } from './input/RoomSenderDto';
import {
FederationAfterLeaveRoomDto,
FederationCreateDMAndInviteUserDto,
FederationRoomSendExternalMessageDto,
} from './input/RoomSenderDto';

export class FederationRoomServiceSender {
constructor(
Expand Down Expand Up @@ -80,6 +84,22 @@ export class FederationRoomServiceSender {
await this.rocketRoomAdapter.addUserToRoom(federatedRoom, federatedInviteeUser, federatedInviterUser);
}

public async leaveRoom(afterLeaveRoomInput: FederationAfterLeaveRoomDto): Promise<void> {
const { internalRoomId, internalUserId } = afterLeaveRoomInput;

const federatedRoom = await this.rocketRoomAdapter.getFederatedRoomByInternalId(internalRoomId);
if (!federatedRoom) {
return;
}

const federatedUser = await this.rocketUserAdapter.getFederatedUserByInternalId(internalUserId);
if (!federatedUser) {
return;
}

await this.bridge.leaveRoom(federatedRoom.externalId, federatedUser.externalId);
}

public async sendMessageFromRocketChat(roomSendExternalMessageInput: FederationRoomSendExternalMessageDto): Promise<IMessage> {
const { internalRoomId, internalSenderId, message } = roomSendExternalMessageInput;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ export class FederationRoomSendExternalMessageDto {

message: IMessage;
}

export class FederationAfterLeaveRoomDto {
internalRoomId: string;

internalUserId: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface IFederationBridge {
sendMessage(externalRoomId: string, externaSenderId: string, text: string): Promise<void>;
createUser(username: string, name: string, domain: string): Promise<string>;
isUserIdFromTheSameHomeserver(externalUserId: string, domain: string): boolean;
leaveRoom(externalRoomId: string, externalUserId: string): Promise<void>;
}

export enum EVENT_ORIGIN {
Expand Down
5 changes: 5 additions & 0 deletions apps/meteor/app/federation-v2/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const federationRoomServiceReceiver = FederationFactory.buildRoomServiceReceiver
rocketSettingsAdapter,
federation,
);

const federationEventsHandler = FederationFactory.buildEventHandlers(federationRoomServiceReceiver);

export const federationRoomServiceSender = FederationFactory.buildRoomServiceSender(
Expand All @@ -30,8 +31,12 @@ export const federationRoomServiceSender = FederationFactory.buildRoomServiceSen

export const runFederation = async (): Promise<void> => {
queueInstance.setHandler(federationEventsHandler.handleEvent.bind(federationEventsHandler), FEDERATION_PROCESSING_CONCURRENCY);

await federation.start();

await rocketSettingsAdapter.onFederationEnabledStatusChanged(federation.onFederationAvailabilityChanged.bind(federation));

FederationFactory.setupListeners(federationRoomServiceSender);
};

export const stopFederation = async (): Promise<void> => {
Expand Down
10 changes: 10 additions & 0 deletions apps/meteor/app/federation-v2/server/infrastructure/Factory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IRoom, IUser } from '@rocket.chat/core-typings';

import { FederationRoomServiceReceiver } from '../application/RoomServiceReceiver';
import { FederationRoomServiceSender } from '../application/RoomServiceSender';
import { MatrixBridge } from './matrix/Bridge';
Expand All @@ -9,6 +11,8 @@ import { RocketChatRoomAdapter } from './rocket-chat/adapters/Room';
import { RocketChatSettingsAdapter } from './rocket-chat/adapters/Settings';
import { RocketChatUserAdapter } from './rocket-chat/adapters/User';
import { IFederationBridge } from '../domain/IFederationBridge';
import { FederationHooks } from './rocket-chat/hooks';
import { FederationRoomSenderConverter } from './rocket-chat/converters/RoomSender';

export class FederationFactory {
public static buildRocketSettingsAdapter(): RocketChatSettingsAdapter {
Expand Down Expand Up @@ -73,4 +77,10 @@ export class FederationFactory {
new MatrixRoomMessageSentHandler(roomServiceReceive),
];
}

public static setupListeners(roomServiceSender: FederationRoomServiceSender): void {
FederationHooks.afterLeaveRoom(async (user: IUser, room: IRoom) =>
roomServiceSender.leaveRoom(FederationRoomSenderConverter.toAfterLeaveRoom(user._id, room._id)),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ export class MatrixBridge implements IFederationBridge {
`);
}

public async leaveRoom(externalRoomId: string, externalUserId: string): Promise<void> {
await this.bridgeInstance.getIntent(externalUserId).leave(externalRoomId);
}

protected async createInstance(): Promise<void> {
bridgeLogger.info('Performing Dynamic Import of matrix-appservice-bridge');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { IMessage } from '@rocket.chat/core-typings';

import { FederationCreateDMAndInviteUserDto, FederationRoomSendExternalMessageDto } from '../../../application/input/RoomSenderDto';
import {
FederationAfterLeaveRoomDto,
FederationCreateDMAndInviteUserDto,
FederationRoomSendExternalMessageDto,
} from '../../../application/input/RoomSenderDto';

export class FederationRoomSenderConverter {
public static toCreateDirectMessageRoomDto(
Expand Down Expand Up @@ -31,4 +35,11 @@ export class FederationRoomSenderConverter {
message,
});
}

public static toAfterLeaveRoom(internalUserId: string, internalRoomId: string): FederationAfterLeaveRoomDto {
return Object.assign(new FederationAfterLeaveRoomDto(), {
internalRoomId,
internalUserId,
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IRoom, IUser } from '@rocket.chat/core-typings';

import { callbacks } from '../../../../../../lib/callbacks';

export class FederationHooks {
public static afterLeaveRoom(callback: Function): void {
callbacks.add(
'afterLeaveRoom',
(user: IUser, room: IRoom | undefined): void => {
if (!room?.federated) {
return;
}
Promise.await(callback(user, room));
},
callbacks.priority.HIGH,
'federation-v2-after-leave-room',
);
}
}

0 comments on commit 23d1fe0

Please sign in to comment.