Edits history of script submission #22462 for ' Is a Matrix user X a joined member of a Matrix room Y? (matrix)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * 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"
     *   }
     * }
     */
    type Matrix = {
      baseUrl: string;
      token: string;
    };
    export async function main(
      matrix_res: 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 hugo989 6 days ago