1 | |
2 |
|
3 | type Matrix = { |
4 | baseUrl: string; |
5 | token: string; |
6 | }; |
7 | export async function main( |
8 | matrix_res: Matrix, |
9 | is_direct = false, |
10 | name = "", |
11 | room_alias_name = "", |
12 | room_version = "", |
13 | topic = "", |
14 | visibility: "public" | "private" = "private", |
15 | ) { |
16 | if (!matrix_res.token) { |
17 | throw Error("Creating a room requires an access token."); |
18 | } |
19 | const resp = await fetch( |
20 | `${matrix_res.baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent( |
21 | room_id, |
22 | )}/invite`, |
23 | { |
24 | method: "POST", |
25 | headers: { |
26 | Authorization: `Bearer ${matrix_res.token}`, |
27 | "Content-Type": "application/json", |
28 | }, |
29 | body: JSON.stringify({ |
30 | is_direct, |
31 | ...(name && { name }), |
32 | ...(room_alias_name && { room_alias_name }), |
33 | ...(room_version && { room_version }), |
34 | ...(topic && { topic }), |
35 | visibility, |
36 | }), |
37 | }, |
38 | ); |
39 | if (!resp.ok) { |
40 | throw Error(`Failed to create room: Error HTTP${resp.status}`); |
41 | } |
42 | } |
43 |
|