This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MatrixUtils.ts
165 lines (148 loc) · 6.79 KB
/
MatrixUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (c) 2022. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
import { MatrixRoomAlias } from "./types/core/MatrixRoomAlias";
import { MatrixUserId } from "./types/core/MatrixUserId";
import { MatrixRoomId } from "./types/core/MatrixRoomId";
import { MatrixCreateRoomPreset } from "./types/request/createRoom/types/MatrixCreateRoomPreset";
import { MatrixVisibility } from "./types/request/createRoom/types/MatrixVisibility";
import { MatrixStateEvent } from "./types/core/MatrixStateEvent";
import { LogService } from "../core/LogService";
import { createRoomJoinRulesStateEventDTO, RoomJoinRulesStateEventDTO } from "./types/event/roomJoinRules/RoomJoinRulesStateEventDTO";
import { createRoomJoinRulesStateContentDTO } from "./types/event/roomJoinRules/RoomJoinRulesStateContentDTO";
import { MatrixJoinRule } from "./types/event/roomJoinRules/MatrixJoinRule";
import { createRoomJoinRulesAllowConditionDTO } from "./types/event/roomJoinRules/RoomJoinRulesAllowConditionDTO";
import { RoomMembershipType } from "./types/event/roomJoinRules/RoomMembershipType";
import { createRoomHistoryVisibilityStateEventDTO, RoomHistoryVisibilityStateEventDTO } from "./types/event/roomHistoryVisibility/RoomHistoryVisibilityStateEventDTO";
import { createRoomHistoryVisibilityStateContentDTO } from "./types/event/roomHistoryVisibility/RoomHistoryVisibilityStateContentDTO";
import { MatrixHistoryVisibility } from "./types/event/roomHistoryVisibility/MatrixHistoryVisibility";
import { createRoomGuestAccessStateEventDTO, RoomGuestAccessStateEventDTO } from "./types/event/roomGuestAccess/RoomGuestAccessStateEventDTO";
import { createRoomGuestAccessContentDTO } from "./types/event/roomGuestAccess/RoomGuestAccessContentDTO";
import { MatrixGuestAccess } from "./types/event/roomGuestAccess/MatrixGuestAccess";
import { createRoomMemberStateEventDTO, RoomMemberStateEventDTO } from "./types/event/roomMember/RoomMemberStateEventDTO";
import { createRoomMemberContentDTO } from "./types/event/roomMember/RoomMemberContentDTO";
import { RoomMembershipState } from "./types/event/roomMember/RoomMembershipState";
import { RoomMemberContent3rdPartyInviteDTO } from "./types/event/roomMember/RoomMemberContent3rdPartyInviteDTO";
const LOG = LogService.createLogger('MatrixUtils');
export class MatrixUtils {
public static getUserId (
username: string,
hostname: string
) : MatrixUserId {
return `@${username}:${hostname}`;
}
public static getRoomAlias (
alias: string,
hostname: string
) : MatrixRoomAlias {
return `#${alias}:${hostname}`;
}
public static getRoomId (
id: string,
hostname: string
) : MatrixRoomId {
return `!${id}:${hostname}`;
}
/**
* Get default preset from visibility setting
*
* @param visibility
* @see https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3createroom
*/
public static getRoomPresetFromVisibility (
visibility : MatrixVisibility
) : MatrixCreateRoomPreset {
switch (visibility) {
case MatrixVisibility.PUBLIC : return MatrixCreateRoomPreset.PUBLIC_CHAT;
default:
case MatrixVisibility.PRIVATE : return MatrixCreateRoomPreset.PRIVATE_CHAT;
}
}
public static createRoomJoinRulesEventDTO (
roomId : string,
joinRule: MatrixJoinRule
) : RoomJoinRulesStateEventDTO {
return createRoomJoinRulesStateEventDTO(
createRoomJoinRulesStateContentDTO(
joinRule,
[
createRoomJoinRulesAllowConditionDTO(
RoomMembershipType.M_ROOM_MEMBERSHIP,
roomId
)
]
)
);
}
public static createRoomHistoryVisibilityEventDTO (
visibility: MatrixHistoryVisibility
) : RoomHistoryVisibilityStateEventDTO {
return createRoomHistoryVisibilityStateEventDTO(
createRoomHistoryVisibilityStateContentDTO(
visibility
)
);
}
public static createRoomGuestAccessEventDTO (
access: MatrixGuestAccess
) : RoomGuestAccessStateEventDTO {
return createRoomGuestAccessStateEventDTO(
createRoomGuestAccessContentDTO(access)
);
}
public static createRoomMemberEventDTO (
userId : string,
membership : RoomMembershipState,
reason ?: string | undefined,
avatar_url ?: string | undefined,
displayname ?: string | null | undefined,
is_direct ?: boolean | undefined,
join_authorised_via_users_server ?: string | undefined,
third_party_invite ?: RoomMemberContent3rdPartyInviteDTO
) : RoomMemberStateEventDTO {
return createRoomMemberStateEventDTO(
userId,
createRoomMemberContentDTO(
membership,
reason,
avatar_url,
displayname,
is_direct,
join_authorised_via_users_server,
third_party_invite
)
);
}
/**
* Get initial room state events for specific preset
*
* @param roomId
* @param preset
* @see https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3createroom
*/
public static getRoomStateEventsFromPreset (
roomId : string,
preset : MatrixCreateRoomPreset
) : readonly MatrixStateEvent[] {
switch (preset) {
case MatrixCreateRoomPreset.PRIVATE_CHAT:
return [
this.createRoomJoinRulesEventDTO(roomId, MatrixJoinRule.INVITE),
this.createRoomHistoryVisibilityEventDTO(MatrixHistoryVisibility.SHARED),
this.createRoomGuestAccessEventDTO(MatrixGuestAccess.CAN_JOIN)
];
case MatrixCreateRoomPreset.PUBLIC_CHAT:
return [
this.createRoomJoinRulesEventDTO(roomId, MatrixJoinRule.PUBLIC),
this.createRoomHistoryVisibilityEventDTO(MatrixHistoryVisibility.SHARED),
this.createRoomGuestAccessEventDTO(MatrixGuestAccess.CAN_JOIN)
];
case MatrixCreateRoomPreset.TRUSTED_PRIVATE_CHAT:
return [
this.createRoomJoinRulesEventDTO(roomId, MatrixJoinRule.INVITE),
this.createRoomHistoryVisibilityEventDTO(MatrixHistoryVisibility.SHARED),
this.createRoomGuestAccessEventDTO(MatrixGuestAccess.FORBIDDEN)
];
}
LOG.warn(`MatrixUtils: Warning! Unimplemented preset: ${preset}`);
return [];
}
}