0

Get Member

by
Published today

Get one member of a Chat space. Requires chat.memberships.readonly on the connection.

Script gchat Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 hours ago
1
//native
2

3
export type DynSelect_space = string
4

5
// Dropdown of the spaces, group chats and DMs the user is a member of.
6
export async function space(auth: RT.Gchat) {
7
  const response = await fetch(
8
    "https://chat.googleapis.com/v1/spaces?pageSize=1000",
9
    {
10
      headers: {
11
        Authorization: `Bearer ${auth.token}`,
12
        Accept: "application/json",
13
      },
14
    }
15
  )
16
  if (!response.ok) {
17
    throw new Error(`${response.status} ${await response.text()}`)
18
  }
19
  const { spaces } = (await response.json()) as {
20
    spaces?: { name: string; displayName?: string; spaceType: string }[]
21
  }
22
  return (spaces ?? []).map((s) => ({
23
    value: s.name,
24
    label: s.displayName
25
      ? `${s.displayName} (${s.name})`
26
      : `${s.spaceType} ${s.name}`,
27
  }))
28
}
29

30
/**
31
 * Get Member
32
 * Get one membership of a Chat space: role, state and the member's user or group. `member` is the membership name from List Members (`spaces/…/members/123…`), its trailing id, or the user's email (`ada@example.com`). Requires chat.memberships.readonly on the connection (not in the default scopes).
33
 */
34
export async function main(
35
  auth: RT.Gchat,
36
  space: DynSelect_space,
37
  member: string
38
) {
39
  const id = member.replace(/^.*members\//, "").replace(/^users\//, "")
40
  const response = await fetch(`https://chat.googleapis.com/v1/${space}/members/${id}`, {
41
    headers: {
42
      Authorization: `Bearer ${auth.token}`,
43
      Accept: "application/json",
44
    },
45
  })
46
  if (!response.ok) {
47
    throw new Error(`${response.status} ${await response.text()}`)
48
  }
49

50
  return await response.json()
51
}
52