1
Set room name
One script reply has been approved by the moderators Verified
Created by jaller94 460 days ago Viewed 4744 times
0
Submitted by jaller94 Deno
Verified 460 days ago
1
/**
2
 * Set the name of a Matrix room.
3
 *
4
 * HTTP endpoint: https://spec.matrix.org/v1.5/client-server-api/#put_matrixclientv3roomsroomidstateeventtypestatekey
5
 *
6
 * State event description: https://spec.matrix.org/v1.5/client-server-api/#mroomname
7
 */
8
type Matrix = {
9
  baseUrl: string;
10
  token: string;
11
};
12
export async function main(
13
  matrix_res: Matrix,
14
  room_id: string,
15
  name: string = "",
16
) {
17
  const url = `${
18
    matrix_res.baseUrl
19
  }/_matrix/client/v3/rooms/${encodeURIComponent(room_id)}/state/m.room.name/`;
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.stringify({
27
      name,
28
    }),
29
  });
30
  if (!resp.ok) {
31
    throw Error(`Failed to set room name: Error HTTP${resp.status}`);
32
  }
33
  return await resp.json();
34
}
35