Edits history of script submission #22458 for ' Create room (matrix)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    type Matrix = {
      baseUrl: string;
      token: string;
    };
    export async function main(
      matrix_res: Matrix,
      is_direct = false,
      name = "",
      room_alias_name = "",
      room_version = "",
      topic = "",
      visibility: "public" | "private" = "private",
    ) {
      if (!matrix_res.token) {
        throw Error("Creating a room requires an access token.");
      }
      const resp = await fetch(
        `${matrix_res.baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(
          room_id,
        )}/invite`,
        {
          method: "POST",
          headers: {
            Authorization: `Bearer ${matrix_res.token}`,
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            is_direct,
            ...(name && { name }),
            ...(room_alias_name && { room_alias_name }),
            ...(room_version && { room_version }),
            ...(topic && { topic }),
            visibility,
          }),
        },
      );
      if (!resp.ok) {
        throw Error(`Failed to create room: Error HTTP${resp.status}`);
      }
    }
    

    Submitted by hugo989 6 days ago