1 | |
2 |
|
3 | type Matrix = { |
4 | baseUrl: string; |
5 | token: string; |
6 | }; |
7 | export async function main( |
8 | matrix_res: Matrix, |
9 | room_id: string, |
10 | type: string, |
11 | state_key?: string, |
12 | json_content: string, |
13 | ) { |
14 | let url = `${matrix_res.baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent( |
15 | room_id, |
16 | )}/state/${encodeURIComponent(type)}/`; |
17 | if (state_key) { |
18 | url += encodeURIComponent(state_key); |
19 | } |
20 | const resp = await fetch(url, { |
21 | method: "PUT", |
22 | headers: { |
23 | Authorization: `Bearer ${matrix_res.token}`, |
24 | "Content-Type": "application/json", |
25 | }, |
26 | body: json_content, |
27 | }); |
28 | if (!resp.ok) { |
29 | throw Error(`Failed to read room state: Error HTTP${resp.status}`); |
30 | } |
31 | return await resp.json(); |
32 | } |
33 |
|