aboutsummaryrefslogtreecommitdiff
path: root/lib/models/channel/channel.ml
blob: 52eda0228540d068560c85a2338dd140f8ca9677 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
open Core
include Channel_t

exception Invalid_message
exception No_message_found

let send_message ?embed ?content ?file ?(tts=false) ch =
    let embed = match embed with
    | Some e -> Embed.yojson_of_t e
    | None -> `Null in
    let content = match content with
    | Some c -> `String c
    | None -> `Null in
    let file = match file with
    | Some f -> `String f
    | None -> `Null in
    let () = match embed, content with
    | `Null, `Null -> raise Invalid_message
    | _ -> () in
    Http.create_message (get_id ch) (`Assoc [
        ("embed", embed);
        ("content", content);
        ("file", file);
        ("tts", `Bool tts);
    ])

let say content ch =
    send_message ~content ch

let delete ch =
    Http.delete_channel (get_id ch)

let get_message ~id ch =
    Http.get_message (get_id ch) id

let get_messages ?(mode=`Around) ~id ?(limit=50) ch =
    let kind = match mode with
    | `Around -> "around", id
    | `Before -> "before", id
    | `After -> "after", id
    in
    Http.get_messages (get_id ch) limit kind

let broadcast_typing ch =
    Http.broadcast_typing (get_id ch)

let get_pins ch =
    Http.get_pinned_messages (get_id ch)

let bulk_delete msgs ch =
    let msgs = `List (List.map ~f:(fun id -> `Intlit (Int64.to_string id)) msgs) in
    Http.bulk_delete (get_id ch) msgs