Skip to content

Commit 5867329

Browse files
ggazzosampaiodiego
andauthored
refactor: replace string types with branded types for RoomID, UserID,… (#243)
Co-authored-by: Diego Sampaio <chinello@gmail.com>
1 parent d57694f commit 5867329

21 files changed

+235
-176
lines changed

packages/core/src/events/isRoomMemberEvent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ export interface RoomMemberEvent extends EventBase {
6565
};
6666
}
6767

68-
export const isRoomMemberEvent = (event: Pdu): event is RoomMemberEvent => {
68+
export const isRoomMemberEvent = (event: Pdu): boolean => {
6969
return event.type === 'm.room.member';
7070
};

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { EventBase, createLogger } from '@rocket.chat/federation-core';
22
import {
3+
EventID,
34
PduForType,
45
PersistentEventBase,
56
PersistentEventFactory,
7+
RoomID,
68
RoomVersion,
9+
UserID,
710
} from '@rocket.chat/federation-room';
811
import { singleton } from 'tsyringe';
912
import { ConfigService } from './config.service';
@@ -33,14 +36,14 @@ export class InviteService {
3336
* Invite a user to an existing room
3437
*/
3538
async inviteUserToRoom(
36-
userId: string,
37-
roomId: string,
38-
sender: string,
39+
userId: UserID,
40+
roomId: RoomID,
41+
sender: UserID,
3942
isDirectMessage = false,
4043
): Promise<{
41-
event_id: string;
44+
event_id: EventID;
4245
event: PersistentEventBase<RoomVersion, 'm.room.member'>;
43-
room_id: string;
46+
room_id: RoomID;
4447
}> {
4548
this.logger.debug(`Inviting ${userId} to room ${roomId}`);
4649

@@ -139,8 +142,8 @@ export class InviteService {
139142

140143
async processInvite(
141144
event: PduForType<'m.room.member'>,
142-
roomId: string,
143-
eventId: string,
145+
roomId: RoomID,
146+
eventId: EventID,
144147
roomVersion: RoomVersion,
145148
) {
146149
// SPEC: when a user invites another user on a different homeserver, a request to that homeserver to have the event signed and verified must be made

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

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import {
1717
type EventID,
1818
type PersistentEventBase,
1919
PersistentEventFactory,
20+
RoomID,
2021
type RoomVersion,
22+
UserID,
2123
} from '@rocket.chat/federation-room';
2224
import { singleton } from 'tsyringe';
2325
import { EventRepository } from '../repositories/event.repository';
@@ -64,10 +66,10 @@ export class MessageService {
6466
) {}
6567

6668
async sendMessage(
67-
roomId: string,
69+
roomId: RoomID,
6870
rawMessage: string,
6971
formattedMessage: string,
70-
senderUserId: string,
72+
senderUserId: UserID,
7173
): Promise<PersistentEventBase> {
7274
const roomVersion = await this.stateService.getRoomVersion(roomId);
7375
if (!roomVersion) {
@@ -106,11 +108,11 @@ export class MessageService {
106108
}
107109

108110
async sendReplyToMessage(
109-
roomId: string,
111+
roomId: RoomID,
110112
rawMessage: string,
111113
formattedMessage: string,
112-
eventToReplyTo: string,
113-
senderUserId: string,
114+
eventToReplyTo: EventID,
115+
senderUserId: UserID,
114116
): Promise<PersistentEventBase> {
115117
const roomVersion = await this.stateService.getRoomVersion(roomId);
116118
if (!roomVersion) {
@@ -154,9 +156,9 @@ export class MessageService {
154156
}
155157

156158
async sendFileMessage(
157-
roomId: string,
159+
roomId: RoomID,
158160
content: FileMessageContent,
159-
senderUserId: string,
161+
senderUserId: UserID,
160162
): Promise<PersistentEventBase> {
161163
const roomVersion = await this.stateService.getRoomVersion(roomId);
162164
if (!roomVersion) {
@@ -190,12 +192,12 @@ export class MessageService {
190192
}
191193

192194
async sendThreadMessage(
193-
roomId: string,
195+
roomId: RoomID,
194196
rawMessage: string,
195197
formattedMessage: string,
196-
senderUserId: string,
197-
threadRootEventId: string,
198-
latestThreadEventId?: string,
198+
senderUserId: UserID,
199+
threadRootEventId: EventID,
200+
latestThreadEventId?: EventID,
199201
): Promise<PersistentEventBase> {
200202
const roomVersion = await this.stateService.getRoomVersion(roomId);
201203
if (!roomVersion) {
@@ -246,12 +248,12 @@ export class MessageService {
246248
}
247249

248250
async sendReplyToInsideThreadMessage(
249-
roomId: string,
251+
roomId: RoomID,
250252
rawMessage: string,
251253
formattedMessage: string,
252-
senderUserId: string,
253-
threadRootEventId: string,
254-
eventToReplyTo: string,
254+
senderUserId: UserID,
255+
threadRootEventId: EventID,
256+
eventToReplyTo: EventID,
255257
): Promise<PersistentEventBase> {
256258
const roomVersion = await this.stateService.getRoomVersion(roomId);
257259
if (!roomVersion) {
@@ -297,10 +299,10 @@ export class MessageService {
297299
}
298300

299301
async sendReaction(
300-
roomId: string,
301-
eventId: string,
302+
roomId: RoomID,
303+
eventId: EventID,
302304
emoji: string,
303-
senderUserId: string,
305+
senderUserId: UserID,
304306
): Promise<string> {
305307
const isTombstoned = await this.roomService.isRoomTombstoned(roomId);
306308
if (isTombstoned) {
@@ -342,10 +344,10 @@ export class MessageService {
342344
}
343345

344346
async unsetReaction(
345-
roomId: string,
347+
roomId: RoomID,
346348
eventIdReactedTo: EventID,
347349
_emoji: string,
348-
senderUserId: string,
350+
senderUserId: UserID,
349351
): Promise<string> {
350352
const roomInfo = await this.stateService.getRoomInformation(roomId);
351353

@@ -375,11 +377,11 @@ export class MessageService {
375377
}
376378

377379
async updateMessage(
378-
roomId: string,
380+
roomId: RoomID,
379381
rawMessage: string,
380382
formattedMessage: string,
381-
senderUserId: string,
382-
eventIdToReplace: string,
383+
senderUserId: UserID,
384+
eventIdToReplace: EventID,
383385
): Promise<string> {
384386
const roomInfo = await this.stateService.getRoomInformation(roomId);
385387

@@ -420,7 +422,7 @@ export class MessageService {
420422
}
421423

422424
async redactMessage(
423-
roomId: string,
425+
roomId: RoomID,
424426
eventIdToRedact: EventID,
425427
): Promise<string> {
426428
const isTombstoned = await this.roomService.isRoomTombstoned(roomId);

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
Pdu,
88
PduForType,
99
PersistentEventFactory,
10+
RoomID,
1011
RoomVersion,
12+
UserID,
1113
} from '@rocket.chat/federation-room';
1214
import { singleton } from 'tsyringe';
1315
import { EventRepository } from '../repositories/event.repository';
@@ -69,12 +71,12 @@ export class ProfilesService {
6971
}
7072

7173
async makeJoin(
72-
roomId: string,
73-
userId: string,
74+
roomId: RoomID,
75+
userId: UserID,
7476
versions: RoomVersion[], // asking server supports these
7577
): Promise<{
7678
event: PduForType<'m.room.member'> & { origin: string };
77-
room_version: string;
79+
room_version: RoomVersion;
7880
}> {
7981
const stateService = this.stateService;
8082
const roomInformation = await stateService.getRoomInformation(roomId);
@@ -110,7 +112,7 @@ export class ProfilesService {
110112
}
111113

112114
async getMissingEvents(
113-
roomId: string,
115+
roomId: RoomID,
114116
earliestEvents: EventID[],
115117
latestEvents: EventID[],
116118
limit = 10,
@@ -126,8 +128,8 @@ export class ProfilesService {
126128
}
127129

128130
async eventAuth(
129-
_roomId: string,
130-
_eventId: string,
131+
_roomId: RoomID,
132+
_eventId: EventID,
131133
): Promise<{ auth_chain: Record<string, string>[] }> {
132134
return {
133135
auth_chain: [],

0 commit comments

Comments
 (0)