Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 2 additions & 22 deletions apps/meteor/ee/server/hooks/federation/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { api, FederationMatrix } from '@rocket.chat/core-services';

Check failure on line 1 in apps/meteor/ee/server/hooks/federation/index.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

'api' is defined but never used
import { isEditedMessage, type IMessage, type IRoom, type IUser } from '@rocket.chat/core-typings';
import { License } from '@rocket.chat/license';

Check failure on line 3 in apps/meteor/ee/server/hooks/federation/index.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

'License' is defined but never used
import { MatrixBridgedRoom, Rooms } from '@rocket.chat/models';

import notifications from '../../../../app/notifications/server/lib/Notifications';

Check failure on line 6 in apps/meteor/ee/server/hooks/federation/index.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

'notifications' is defined but never used
import { settings } from '../../../../app/settings/server';

Check failure on line 7 in apps/meteor/ee/server/hooks/federation/index.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

'settings' is defined but never used
import { callbacks } from '../../../../lib/callbacks';
import { afterLeaveRoomCallback } from '../../../../lib/callbacks/afterLeaveRoomCallback';
import { afterRemoveFromRoomCallback } from '../../../../lib/callbacks/afterRemoveFromRoomCallback';
Expand All @@ -17,8 +19,6 @@
callbacks.add('federation.afterCreateFederatedRoom', async (room, { owner, originalMemberList: members, options }) => {
if (FederationActions.shouldPerformFederationAction(room)) {
const federatedRoomId = options?.federatedRoomId;
// TODO: move this to the hooks folder
setupTypingEventListenerForRoom(room._id);

if (!federatedRoomId) {
// if room if exists, we don't want to create it again
Expand Down Expand Up @@ -227,23 +227,3 @@
callbacks.priority.HIGH,
'federation-matrix-after-create-direct-room',
);

// TODO: THIS IS NOT READY FOR PRODUCTION! IMPOSSIBLE TO ADD ONE LISTENER PER ROOM!
const setupTypingEventListenerForRoom = (roomId: string): void => {
notifications.streamRoom.on(`${roomId}/user-activity`, (username, activity) => {
if (Array.isArray(activity) && (!activity.length || activity.includes('user-typing'))) {
void api.broadcast('user.typing', {
user: { username },
isTyping: activity.includes('user-typing'),
roomId,
});
}
});
};

export const setupInternalEDUEventListeners = async () => {
const federatedRooms = await Rooms.findFederatedRooms({ projection: { _id: 1 } }).toArray();
for (const room of federatedRooms) {
setupTypingEventListenerForRoom(room._id);
}
};
6 changes: 0 additions & 6 deletions apps/meteor/ee/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ import './local-services/ldap/service';
import './methods/getReadReceipts';
import './patches';
import './hooks/federation';
import { License } from '@rocket.chat/license';

export * from './apps/startup';
export { registerEEBroker } from './startup';

await License.onLicense('federation', async () => {
const { setupInternalEDUEventListeners } = await import('./hooks/federation');
await setupInternalEDUEventListeners();
});
9 changes: 9 additions & 0 deletions apps/meteor/ee/server/startup/federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { settings } from '../../../app/settings/server';
import { registerFederationRoutes } from '../api/federation';
import { StreamerCentral } from '../../../server/modules/streamer/streamer.module';

Check failure on line 9 in apps/meteor/ee/server/startup/federation.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

`../../../server/modules/streamer/streamer.module` import should occur before import of `../api/federation`

const logger = new Logger('Federation');

Expand All @@ -27,6 +28,14 @@
logger.debug('Starting federation-matrix service');
federationMatrixService = await FederationMatrix.create(InstanceStatus.id());

StreamerCentral.on('broadcast', (name, eventName, args) => {
if (name === 'notify-room' && eventName.endsWith('user-activity')) {
const [rid] = eventName.split('/');
const [user, activity] = args;
void federationMatrixService.notifyUserTyping(rid, user, activity.includes('user-typing'));
}
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

try {
api.registerService(federationMatrixService);
await registerFederationRoutes(federationMatrixService);
Expand Down
55 changes: 24 additions & 31 deletions apps/meteor/server/modules/listeners/listeners.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,39 @@ export class ListenersModule {
notifications.notifyRoom(rid, 'videoconf', callId);
});

service.onEvent('presence.status', ({ user }) => this.handlePresence({ user }, notifications));
service.onEvent('presence.status', ({ user }) => {
const { _id, username, name, status, statusText, roles } = user;
if (!status || !username) {
return;
}

notifications.notifyUserInThisInstance(_id, 'userData', {
type: 'updated',
id: _id,
diff: {
status,
...(statusText && { statusText }),
},
unset: {},
});

notifications.notifyLoggedInThisInstance('user-status', [_id, username, STATUS_MAP[status], statusText, name, roles]);

if (_id) {
notifications.sendPresence(_id, username, STATUS_MAP[status], statusText);
}
});

service.onEvent('user.updateCustomStatus', (userStatus) => {
notifications.notifyLoggedInThisInstance('updateCustomUserStatus', {
userStatusData: userStatus,
});
});

service.onEvent('federation-matrix.user.typing', ({ isTyping, roomId, username }) => {
notifications.notifyRoom(roomId, 'user-activity', username, isTyping ? ['user-typing'] : []);
service.onEvent('user.activity', ({ isTyping, roomId, user }) => {
notifications.notifyRoom(roomId, 'user-activity', user, isTyping ? ['user-typing'] : []);
});

service.onEvent('federation-matrix.user.presence.status', ({ user }) => this.handlePresence({ user }, notifications));

service.onEvent('watch.messages', async ({ message }) => {
if (!message.rid) {
return;
Expand Down Expand Up @@ -491,30 +510,4 @@ export class ListenersModule {
notifications.streamRoomMessage.emit(roomId, acknowledgeMessage);
});
}

private handlePresence(
{ user }: { user: Pick<IUser, '_id' | 'username' | 'status' | 'statusText' | 'name' | 'roles'> },
notifications: NotificationsModule,
): void {
const { _id, username, name, status, statusText, roles } = user;
if (!status || !username) {
return;
}

notifications.notifyUserInThisInstance(_id, 'userData', {
type: 'updated',
id: _id,
diff: {
status,
...(statusText && { statusText }),
},
unset: {},
});

notifications.notifyLoggedInThisInstance('user-status', [_id, username, STATUS_MAP[status], statusText, name, roles]);

if (_id) {
notifications.sendPresence(_id, username, STATUS_MAP[status], statusText);
}
}
}
39 changes: 21 additions & 18 deletions ee/packages/federation-matrix/src/FederationMatrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,7 @@ export class FederationMatrix extends ServiceClass implements IFederationMatrixS
instance.homeserverServices = getAllServices();
MatrixMediaService.setHomeserverServices(instance.homeserverServices);
instance.buildMatrixHTTPRoutes();
instance.onEvent('user.typing', async ({ isTyping, roomId, user: { username } }): Promise<void> => {
if (!roomId || !username) {
return;
}
const externalRoomId = await MatrixBridgedRoom.getExternalRoomId(roomId);
if (!externalRoomId) {
return;
}
const localUser = await Users.findOneByUsername(username, { projection: { _id: 1 } });
if (!localUser) {
return;
}
const externalUserId = await MatrixBridgedUser.getExternalUserIdByLocalUserId(localUser._id);
if (!externalUserId) {
return;
}
void instance.homeserverServices.edu.sendTypingNotification(externalRoomId, externalUserId, isTyping);
});

instance.onEvent(
'presence.status',
async ({ user }: { user: Pick<IUser, '_id' | 'username' | 'status' | 'statusText' | 'name' | 'roles'> }): Promise<void> => {
Expand Down Expand Up @@ -1002,4 +985,24 @@ export class FederationMatrix extends ServiceClass implements IFederationMatrixS
}
await this.homeserverServices.room.setPowerLevelForUser(matrixRoomId, senderMatrixUserId, matrixUserId, powerLevel);
}

async notifyUserTyping(rid: string, user: string, isTyping: boolean) {
if (!rid || !user) {
return;
}
const externalRoomId = await MatrixBridgedRoom.getExternalRoomId(rid);
if (!externalRoomId) {
return;
}
const localUser = await Users.findOneByUsername(user, { projection: { _id: 1 } });
if (!localUser) {
return;
}
const externalUserId = await MatrixBridgedUser.getExternalUserIdByLocalUserId(localUser._id);
if (!externalUserId) {
return;
}

void this.homeserverServices.edu.sendTypingNotification(externalRoomId, externalUserId, isTyping);
}
}
7 changes: 4 additions & 3 deletions ee/packages/federation-matrix/src/events/edu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
}

const user = await Users.findOneById(matrixUser.uid, { projection: { _id: 1, username: 1 } });
if (!user || !user.username) {

Check warning on line 26 in ee/packages/federation-matrix/src/events/edu.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Prefer using an optional chain expression instead, as it's more concise and easier to read
logger.debug(`User not found for uid: ${matrixUser.uid}`);
return;
}

void api.broadcast('federation-matrix.user.typing', {
username: user.username,
void api.broadcast('user.activity', {
user: user.username,
isTyping: data.typing,
roomId: matrixRoom,
});
Expand Down Expand Up @@ -71,8 +71,9 @@
);

const { _id, username, statusText, roles, name } = user;
void api.broadcast('federation-matrix.user.presence.status', {
void api.broadcast('presence.status', {
user: { status, _id, username, statusText, roles, name },
previousStatus: undefined,
});
logger.debug(`Updated presence for user ${matrixUser.uid} to ${status} from Matrix federation`);
} catch (error) {
Expand Down
7 changes: 1 addition & 6 deletions packages/core-services/src/events/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,7 @@ export type EventSignatures = {
scope?: string;
}): void;
'user.updateCustomStatus'(userStatus: Omit<ICustomUserStatus, '_updatedAt'>): void;
'user.typing'(data: { user: Partial<IUser>; isTyping: boolean; roomId: string }): void;
'federation-matrix.user.typing'(data: { username: string; isTyping: boolean; roomId: string }): void;
'federation-matrix.user.presence.status'(data: {
user: Pick<IUser, '_id' | 'username' | 'status' | 'statusText' | 'name' | 'roles'>;
previousStatus?: UserStatus;
}): void;
'user.activity'(data: { user: string; isTyping: boolean; roomId: string }): void;
'user.video-conference'(data: {
userId: IUser['_id'];
action: string;
Expand Down
Loading