-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Expand file tree
/
Copy pathrooms.ts
More file actions
210 lines (192 loc) · 4.65 KB
/
rooms.ts
File metadata and controls
210 lines (192 loc) · 4.65 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import type { HomeserverServices } from '@rocket.chat/federation-sdk';
import { Router } from '@rocket.chat/http-router';
import { ajv } from '@rocket.chat/rest-typings/dist/v1/Ajv';
import { isAuthenticatedMiddleware } from '../middlewares/isAuthenticated';
const PublicRoomsQuerySchema = {
type: 'object',
properties: {
include_all_networks: {
type: 'boolean',
description: 'Include all networks (ignored)',
},
limit: {
type: 'number',
description: 'Maximum number of rooms to return',
},
},
required: ['include_all_networks', 'limit'],
};
const isPublicRoomsQueryProps = ajv.compile(PublicRoomsQuerySchema);
const RoomObjectSchema = {
type: 'object',
properties: {
avatar_url: {
type: 'string',
description: 'Room avatar URL',
},
canonical_alias: {
type: 'string',
description: 'Room canonical alias',
nullable: true,
},
guest_can_join: {
type: 'boolean',
description: 'Whether guests can join the room',
},
join_rule: {
type: 'string',
description: 'Room join rule',
},
name: {
type: 'string',
description: 'Room name',
},
num_joined_members: {
type: 'number',
description: 'Number of joined members',
nullable: true,
},
room_id: {
type: 'string',
description: 'Room ID',
},
room_type: {
type: 'string',
description: 'Room type',
nullable: true,
},
topic: {
type: 'string',
description: 'Room topic',
nullable: true,
},
world_readable: {
type: 'boolean',
description: 'Whether the room is world readable',
},
},
required: ['avatar_url', 'guest_can_join', 'join_rule', 'name', 'room_id', 'world_readable'],
};
const PublicRoomsResponseSchema = {
type: 'object',
properties: {
chunk: {
type: 'array',
items: RoomObjectSchema,
description: 'Array of public rooms',
},
},
required: ['chunk'],
};
const isPublicRoomsResponseProps = ajv.compile(PublicRoomsResponseSchema);
const PublicRoomsPostBodySchema = {
type: 'object',
properties: {
include_all_networks: {
type: 'string',
description: 'Include all networks (ignored)',
nullable: true,
},
limit: {
type: 'number',
description: 'Maximum number of rooms to return',
nullable: true,
},
filter: {
type: 'object',
properties: {
generic_search_term: {
type: 'string',
description: 'Generic search term for filtering rooms',
nullable: true,
},
room_types: {
type: 'array',
items: {
type: ['string', 'null'],
},
description: 'Array of room types to filter by',
nullable: true,
},
},
},
},
required: ['filter'],
};
const isPublicRoomsPostBodyProps = ajv.compile(PublicRoomsPostBodySchema);
export const getMatrixRoomsRoutes = (services: HomeserverServices) => {
const { state, federationAuth } = services;
return new Router('/federation')
.use(isAuthenticatedMiddleware(federationAuth))
.get(
'/v1/publicRooms',
{
query: isPublicRoomsQueryProps,
response: {
200: isPublicRoomsResponseProps,
},
tags: ['Federation'],
license: ['federation'],
},
async () => {
const defaultObj = {
join_rule: 'public',
guest_can_join: false, // trying to reduce required endpoint hits
world_readable: false, // ^^^
avatar_url: '', // ?? don't have any yet
};
const publicRooms = await state.getAllPublicRoomIdsAndNames();
return {
body: {
chunk: publicRooms.map((room) => ({
...defaultObj,
...room,
})),
},
statusCode: 200,
};
},
)
.post(
'/v1/publicRooms',
{
body: isPublicRoomsPostBodyProps,
response: {
200: isPublicRoomsResponseProps,
},
tags: ['Federation'],
license: ['federation'],
},
async (c) => {
const body = await c.req.json();
const defaultObj = {
join_rule: 'public',
guest_can_join: false, // trying to reduce required endpoint hits
world_readable: false, // ^^^
avatar_url: '', // ?? don't have any yet
};
const { filter } = body;
const publicRooms = await state.getAllPublicRoomIdsAndNames();
return {
body: {
chunk: publicRooms
.filter((r) => {
if (filter.generic_search_term) {
return r.name.toLowerCase().includes(filter.generic_search_term.toLowerCase());
}
// Today only one room type is supported (https://spec.matrix.org/v1.15/client-server-api/#types)
// TODO: https://rocketchat.atlassian.net/browse/FDR-152 -> Implement logic to handle custom room types
// if (filter.room_types) {
// }
return true;
})
.map((room) => ({
...defaultObj,
...room,
})),
},
statusCode: 200,
};
},
);
};