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