Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 8 additions & 8 deletions packages/federation-sdk/src/services/message.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export class MessageService {
);
}

const roomInfo = await this.stateService.getRoomInformation(roomId);
const roomVersion = await this.stateService.getRoomVersion(roomId);

const reactionEvent = await this.stateService.buildEvent<'m.reaction'>(
{
Expand All @@ -311,7 +311,7 @@ export class MessageService {
origin_server_ts: Date.now(),
sender: senderUserId,
},
roomInfo.room_version,
roomVersion,
);

await this.stateService.handlePdu(reactionEvent);
Expand All @@ -327,7 +327,7 @@ export class MessageService {
_emoji: string,
senderUserId: UserID,
): Promise<string> {
const roomInfo = await this.stateService.getRoomInformation(roomId);
const roomVersion = await this.stateService.getRoomVersion(roomId);

const redactionEvent =
await this.stateService.buildEvent<'m.room.redaction'>(
Expand All @@ -344,7 +344,7 @@ export class MessageService {
origin_server_ts: Date.now(),
sender: senderUserId,
},
roomInfo.room_version,
roomVersion,
);

await this.stateService.handlePdu(redactionEvent);
Expand All @@ -361,7 +361,7 @@ export class MessageService {
senderUserId: UserID,
eventIdToReplace: EventID,
): Promise<string> {
const roomInfo = await this.stateService.getRoomInformation(roomId);
const roomVersion = await this.stateService.getRoomVersion(roomId);

const redactionEvent = await this.stateService.buildEvent<'m.room.message'>(
{
Expand Down Expand Up @@ -389,7 +389,7 @@ export class MessageService {
origin_server_ts: Date.now(),
sender: senderUserId,
},
roomInfo.room_version,
roomVersion,
);

await this.stateService.handlePdu(redactionEvent);
Expand All @@ -411,7 +411,7 @@ export class MessageService {
throw new ForbiddenError('Cannot delete a message in a tombstoned room');
}

const roomInfo = await this.stateService.getRoomInformation(roomId);
const roomVersion = await this.stateService.getRoomVersion(roomId);

const senderUserId = await this.eventService.getEventById(eventIdToRedact);
if (!senderUserId?.event.sender) {
Expand All @@ -433,7 +433,7 @@ export class MessageService {
origin_server_ts: Date.now(),
sender: senderUserId.event.sender,
},
roomInfo.room_version,
roomVersion,
);

await this.stateService.handlePdu(redactionEvent);
Expand Down
6 changes: 2 additions & 4 deletions packages/federation-sdk/src/services/profiles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ export class ProfilesService {
room_version: RoomVersion;
}> {
const stateService = this.stateService;
const roomInformation = await stateService.getRoomInformation(roomId);

const roomVersion = roomInformation.room_version;
const roomVersion = await this.stateService.getRoomVersion(roomId);

if (!versions.includes(roomVersion)) {
throw new Error(`Unsupported room version: ${roomVersion}`);
Expand All @@ -97,7 +95,7 @@ export class ProfilesService {
origin_server_ts: Date.now(),
sender: userId,
},
roomInformation.room_version,
roomVersion,
);

return {
Expand Down
25 changes: 10 additions & 15 deletions packages/federation-sdk/src/services/room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,12 @@ import { EventStagingRepository } from '../repositories/event-staging.repository
import { EventRepository } from '../repositories/event.repository';
import { RoomRepository } from '../repositories/room.repository';
import { ConfigService } from './config.service';
import { EventAuthorizationService } from './event-authorization.service';
import { EventEmitterService } from './event-emitter.service';
import { EventFetcherService } from './event-fetcher.service';
import { EventService } from './event.service';
import { FederationService } from './federation.service';
import { InviteService } from './invite.service';
import {
RoomInfoNotReadyError,
StateService,
UnknownRoomError,
} from './state.service';
import { StateService, UnknownRoomError } from './state.service';

@singleton()
export class RoomService {
Expand Down Expand Up @@ -575,7 +570,7 @@ export class RoomService {
event: PduForType<'m.room.member'> & { origin: string };
room_version: RoomVersion;
}> {
const roomInfo = await this.stateService.getRoomInformation(roomId);
const roomVersion = await this.stateService.getRoomVersion(roomId);
const leaveEvent = await this.stateService.buildEvent<'m.room.member'>(
{
type: 'm.room.member',
Expand All @@ -588,15 +583,15 @@ export class RoomService {
origin_server_ts: Date.now(),
sender: senderId,
},
roomInfo.room_version,
roomVersion,
);

return {
event: {
...leaveEvent.event,
origin: this.configService.serverName,
},
room_version: roomInfo.room_version,
room_version: roomVersion,
};
}

Expand Down Expand Up @@ -709,7 +704,7 @@ export class RoomService {
}`,
);

const roomInfo = await this.stateService.getRoomInformation(roomId);
const roomVersion = await this.stateService.getRoomVersion(roomId);

const authEventIdsForPowerLevels = await this.eventService.getAuthEventIds(
'm.room.power_levels',
Expand Down Expand Up @@ -764,7 +759,7 @@ export class RoomService {
origin_server_ts: Date.now(),
sender: senderId,
},
roomInfo.room_version,
roomVersion,
);

await this.stateService.handlePdu(kickEvent);
Expand All @@ -783,7 +778,7 @@ export class RoomService {
userId: UserID,
displayName: string,
): Promise<PersistentEventBase<RoomVersion, 'm.room.member'>> {
const roomInfo = await this.stateService.getRoomInformation(roomId);
const roomVersion = await this.stateService.getRoomVersion(roomId);
const currentState = await this.stateService.getLatestRoomState(roomId);

const currentMembership = currentState.get(`m.room.member:${userId}`);
Expand All @@ -806,7 +801,7 @@ export class RoomService {
prev_events: [],
origin_server_ts: Date.now(),
},
roomInfo.room_version,
roomVersion,
);

await this.stateService.handlePdu(memberEvent);
Expand Down Expand Up @@ -834,7 +829,7 @@ export class RoomService {
}`,
);

const roomInfo = await this.stateService.getRoomInformation(roomId);
const roomVersion = await this.stateService.getRoomVersion(roomId);

const authEventIdsForPowerLevels = await this.eventService.getAuthEventIds(
'm.room.power_levels',
Expand Down Expand Up @@ -890,7 +885,7 @@ export class RoomService {
origin_server_ts: Date.now(),
sender: senderId,
},
roomInfo.room_version,
roomVersion,
);

await this.stateService.handlePdu(banEvent);
Expand Down
29 changes: 0 additions & 29 deletions packages/federation-sdk/src/services/state.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ export class UnknownRoomError extends Error {
this.name = 'UnknownRoomError';
}
}
export class RoomInfoNotReadyError extends Error {
constructor(message: string) {
super(message);
this.name = 'RoomInfoNotReadyError';
}
}

@singleton()
export class StateService {
private readonly logger = createLogger('StateService');
Expand All @@ -80,28 +73,6 @@ export class StateService {
private readonly eventService: EventService,
) {}

// TODO: this is a very vague method, better would be to use exactly what needed,
// or getCreateEvent.
// currently AFAIK mostly is used for just room version
async getRoomInformation(roomId: string): Promise<PduCreateEventContent> {
const { event, stateId } =
(await this.eventRepository.findByRoomIdAndType(
roomId,
'm.room.create',
)) ?? {};
if (event?.type !== 'm.room.create') {
throw new RoomInfoNotReadyError(
'Create event mapping not found for room information',
);
}

if (!stateId) {
throw new Error('Create event has no state id, something is very wrong');
}

return event.content;
}

async getRoomVersion(roomId: RoomID): Promise<RoomVersion> {
const createEvent = await this.eventRepository.findByRoomIdAndType(
roomId,
Expand Down
Loading