Edits history of script submission #23307 for ' Get Member (gchat)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_space = string
    
    // Dropdown of the spaces, group chats and DMs the user is a member of.
    export async function space(auth: RT.Gchat) {
      const response = await fetch(
        "https://chat.googleapis.com/v1/spaces?pageSize=1000",
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { spaces } = (await response.json()) as {
        spaces?: { name: string; displayName?: string; spaceType: string }[]
      }
      return (spaces ?? []).map((s) => ({
        value: s.name,
        label: s.displayName
          ? `${s.displayName} (${s.name})`
          : `${s.spaceType} ${s.name}`,
      }))
    }
    
    /**
     * Get Member
     * 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).
     */
    export async function main(
      auth: RT.Gchat,
      space: DynSelect_space,
      member: string
    ) {
      const id = member.replace(/^.*members\//, "").replace(/^users\//, "")
      const response = await fetch(`https://chat.googleapis.com/v1/${space}/members/${id}`, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 6 hours ago