Get presence history of a channel
One script reply has been approved by the moderators Verified

Get presence on a channel

Created by hugo697 138 days ago
Submitted by hugo697 Bun
Verified 138 days ago
1
//native
2
type Ably = {
3
  apiKey: string;
4
};
5
/**
6
 * Get presence history of a channel
7
 * Get presence on a channel
8
 */
9
export async function main(
10
  auth: Ably,
11
  channel_id: string,
12
  format: "json" | "jsonp" | "msgpack" | "html" | undefined,
13
  start: string | undefined,
14
  limit: string | undefined,
15
  end: string | undefined,
16
  direction: "forwards" | "backwards" | undefined,
17
  X_Ably_Version: string,
18
) {
19
  const url = new URL(
20
    `https://rest.ably.io/channels/${channel_id}/presence/history`,
21
  );
22
  for (const [k, v] of [
23
    ["format", format],
24
    ["start", start],
25
    ["limit", limit],
26
    ["end", end],
27
    ["direction", direction],
28
  ]) {
29
    if (v !== undefined && v !== "" && k !== undefined) {
30
      url.searchParams.append(k, v);
31
    }
32
  }
33
  const response = await fetch(url, {
34
    method: "GET",
35
    headers: {
36
      "X-Ably-Version": X_Ably_Version,
37
      Authorization: "Bearer " + auth.apiKey,
38
    },
39
    body: undefined,
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.json();
46
}
47