Search... Ctrl + K

Is a Matrix user X a joined member of a Matrix room Y? (matrix)
One script reply has been approved by the moderators Verified

Created by jaller94 23 days ago Used 1 times 1 Point

This function throws an error, if you're not a room member. Otherwise, it returns a boolean "is_joined". Furthermore, it returns the user info provided by the server.

No comments yet

Points: 0
deno
One script reply has been approved by the moderators
Ap­pro­ved
import * as wmill from "https://deno.land/x/windmill@v1.47.3/mod.ts"

/**
 * This function throws an error, if you're not a room member.
 * 
 * Expected return values:
 * 
 * {
 *   "is_joined": false,
 *   "user_info": null
 * }
 * 
 * or
 * 
 * {
 *   "is_member": true,
 *   "user_info": {
 *     "avatar_url": "mxc://matrix.org/abc123",
 *     "display_name": "Jane Doe"
 *   }
 * }
 */
export async function main(
  matrix_res: wmill.Resource<"matrix">,
  room_id: string,
  user_id: string,
) {
  const url = `${matrix_res.baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(room_id)}/joined_members`;
  const resp = await fetch(url, {
    headers: {
      "Authorization": `Bearer ${matrix_res.token}`,
    },
  });
  if (!resp.ok) {
    throw Error(`Failed to fetch joined member list: Error HTTP${resp.status}`);
  }
  const data = await resp.json();
  return {
    is_joined: user_id in data?.joined,
    user_info: data?.joined?.[user_id],
  };
}

Submitted by jaller94 23 days ago

Edited 23 days ago

No comments yet