aboutsummaryrefslogtreecommitdiff
path: root/lib/post.ml
blob: 744e296b025d5e40c636abbfee7c0003d910e0c6 (plain)
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
53
54
open Ppx_yojson_conv_lib.Yojson_conv.Primitives

type t = {
  link : string;
  summary : string;
  tags : string list;
  published : string;
  author : string;
}

type mastodon_tag = {
  kind : string; [@key "type"]
  name : string;
  href : string;
}
[@@deriving yojson] [@@yojson.allow_extra_fields]

type mastodon_object = { tag : mastodon_tag list; url : string }
[@@deriving yojson] [@@yojson.allow_extra_fields]

type mastodon_post = {
  actor : string;
  published : string;
  obj : mastodon_object; [@key "object"]
}
[@@deriving yojson] [@@yojson.allow_extra_fields]

let mastodon_actor post = post.actor

let post_of_mastodon_post actor_name = function
  | { published; obj = { tag; url }; _ } ->
      let tags =
        List.filter_map
          (fun { name; kind; _ } ->
            (* This would normally be filtered with yojson, but I couldn't get
               it to work *)
            if kind = "Hashtag" then Some name else None)
          tag
      in
      { link = url; summary = published; tags; published; author = actor_name }

let get_tld (link : string) =
  Uri.of_string link |> Uri.host_with_default ~default:""

let db = ref []

let add (entry : t) =
  db := entry :: !db;
  print_endline (entry.link ^ " added to db");
  print_endline ("New db size: " ^ string_of_int (List.length !db))

let get_all () =
  print_endline ("db size: " ^ string_of_int (List.length !db));
  !db