Skip to content

Commit e39d5b1

Browse files
authored
fix: emit member events outside pdu process (#239)
1 parent 9b5d392 commit e39d5b1

5 files changed

Lines changed: 61 additions & 13 deletions

File tree

packages/federation-sdk/src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import 'reflect-metadata';
22

33
import type { Emitter } from '@rocket.chat/emitter';
4-
import type {
5-
EventStagingStore,
6-
Membership,
7-
MessageType,
8-
} from '@rocket.chat/federation-core';
4+
import type { EventStagingStore } from '@rocket.chat/federation-core';
95
import type {
106
EventID,
117
EventStore,
@@ -91,6 +87,10 @@ export type HomeserverEventSignatures = {
9187
event_id: EventID;
9288
event: PduForType<'m.room.encrypted'>;
9389
};
90+
'homeserver.matrix.room.create': {
91+
event: PduForType<'m.room.create'>;
92+
event_id: EventID;
93+
};
9494
'homeserver.matrix.message': {
9595
event_id: EventID;
9696
event: PduForType<'m.room.message'>;

packages/federation-sdk/src/services/invite.service.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ import {
1212
import { singleton } from 'tsyringe';
1313
import { ConfigService } from './config.service';
1414
import { EventAuthorizationService } from './event-authorization.service';
15+
import { EventEmitterService } from './event-emitter.service';
16+
import { EventService } from './event.service';
1517
import { FederationService } from './federation.service';
16-
import { StateService, UnknownRoomError } from './state.service';
18+
import {
19+
RoomInfoNotReadyError,
20+
StateService,
21+
UnknownRoomError,
22+
} from './state.service';
1723
// TODO: Have better (detailed/specific) event input type
1824
export type ProcessInviteEvent = {
1925
event: EventBase;
@@ -37,6 +43,7 @@ export class InviteService {
3743
private readonly stateService: StateService,
3844
private readonly configService: ConfigService,
3945
private readonly eventAuthorizationService: EventAuthorizationService,
46+
private readonly emitterService: EventEmitterService,
4047
) {}
4148

4249
/**
@@ -103,6 +110,11 @@ export class InviteService {
103110
// without it join events will not be processed if /event/{eventId} causes problems
104111
void federationService.sendEventToAllServersInRoom(inviteEvent);
105112

113+
this.emitterService.emit('homeserver.matrix.membership', {
114+
event_id: inviteEvent.eventId,
115+
event: inviteEvent.event,
116+
});
117+
106118
return {
107119
event_id: inviteEvent.eventId,
108120
event: PersistentEventFactory.createFromRawEvent(
@@ -133,6 +145,11 @@ export class InviteService {
133145
// let everyone know
134146
void federationService.sendEventToAllServersInRoom(inviteEvent);
135147

148+
this.emitterService.emit('homeserver.matrix.membership', {
149+
event_id: inviteEvent.eventId,
150+
event: inviteEvent.event,
151+
});
152+
136153
return {
137154
event_id: inviteEvent.eventId,
138155
event: PersistentEventFactory.createFromRawEvent(
@@ -226,6 +243,11 @@ export class InviteService {
226243

227244
// we do not send transaction here
228245
// the asking server will handle the transactions
246+
247+
this.emitterService.emit('homeserver.matrix.membership', {
248+
event_id: inviteEvent.eventId,
249+
event: inviteEvent.event,
250+
});
229251
}
230252

231253
// we are not the host of the server

packages/federation-sdk/src/services/room.service.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
roomPowerLevelsEvent,
99
} from '@rocket.chat/federation-core';
1010
import { delay, inject, singleton } from 'tsyringe';
11-
import { FederationService } from './federation.service';
1211

1312
import {
1413
ForbiddenError,
@@ -34,11 +33,17 @@ import { EventStagingRepository } from '../repositories/event-staging.repository
3433
import { EventRepository } from '../repositories/event.repository';
3534
import { RoomRepository } from '../repositories/room.repository';
3635
import { ConfigService } from './config.service';
36+
import { EventAuthorizationService } from './event-authorization.service';
3737
import { EventEmitterService } from './event-emitter.service';
3838
import { EventFetcherService } from './event-fetcher.service';
3939
import { EventService } from './event.service';
40+
import { FederationService } from './federation.service';
4041
import { InviteService } from './invite.service';
41-
import { StateService, UnknownRoomError } from './state.service';
42+
import {
43+
RoomInfoNotReadyError,
44+
StateService,
45+
UnknownRoomError,
46+
} from './state.service';
4247

4348
@singleton()
4449
export class RoomService {
@@ -57,6 +62,7 @@ export class RoomService {
5762
private readonly eventRepository: EventRepository,
5863
@inject(delay(() => EventStagingRepository))
5964
private readonly eventStagingRepository: EventStagingRepository,
65+
private readonly emitterService: EventEmitterService,
6066
) {}
6167

6268
private validatePowerLevelChange(
@@ -890,6 +896,11 @@ export class RoomService {
890896

891897
void federationService.sendEventToAllServersInRoom(membershipEvent);
892898

899+
this.emitterService.emit('homeserver.matrix.membership', {
900+
event_id: membershipEvent.eventId,
901+
event: membershipEvent.event,
902+
});
903+
893904
return membershipEvent.eventId;
894905
}
895906

@@ -1268,6 +1279,7 @@ export class RoomService {
12681279

12691280
await this.roomRepository.markRoomAsDeleted(roomId, event.eventId);
12701281

1282+
// TODO: check if all sendEventToAllServersInRoom should be followed by an emitter
12711283
void this.federationService.sendEventToAllServersInRoom(event);
12721284

12731285
logger.info(`Successfully marked room ${roomId} as tombstone`);

packages/federation-sdk/src/services/staging-area.service.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,17 @@ export class StagingAreaService {
268268
private async processNotificationStage(event: EventStagingStore) {
269269
this.logger.debug(`Notifying clients about event ${event._id}`);
270270

271-
const {
272-
_id: eventId,
273-
event: { room_id: roomId },
274-
} = event;
271+
const { _id: eventId, roomId } = event;
275272

276273
switch (true) {
274+
case event.event.type === 'm.room.create':
275+
{
276+
this.eventEmitterService.emit('homeserver.matrix.room.create', {
277+
event_id: eventId,
278+
event: event.event,
279+
});
280+
}
281+
break;
277282
case event.event.type === 'm.room.message':
278283
this.eventEmitterService.emit('homeserver.matrix.message', {
279284
event_id: eventId,

packages/federation-sdk/src/services/state.service.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ export class PartialStateResolutionError extends Error {
5858
export class UnknownRoomError extends Error {
5959
constructor(roomId: RoomID) {
6060
super(`Room ${roomId} does not exist`);
61+
this.name = 'UnknownRoomError';
62+
}
63+
}
64+
export class RoomInfoNotReadyError extends Error {
65+
constructor(message: string) {
66+
super(message);
67+
this.name = 'RoomInfoNotReadyError';
6168
}
6269
}
6370

@@ -82,7 +89,9 @@ export class StateService {
8289
'm.room.create',
8390
)) ?? {};
8491
if (event?.type !== 'm.room.create') {
85-
throw new Error('Create event mapping not found for room information');
92+
throw new RoomInfoNotReadyError(
93+
'Create event mapping not found for room information',
94+
);
8695
}
8796

8897
if (!stateId) {

0 commit comments

Comments
 (0)