aboutsummaryrefslogtreecommitdiff
path: root/lib/post.ml
blob: dbca9366a4e1413770009a84a70ed136efd4a014 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
open Ppx_yojson_conv_lib.Yojson_conv.Primitives

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

let published t = t.published

(* RSS *)

let to_rss_entry post =
  Xml.tag "entry" []
    [
      Xml.tag "title" [] [ Xml.entry (post.author ^ " - " ^ post.link) ];
      Xml.tag "id" [] [ Xml.entry post.link ];
      Xml.tag "updated" [] [ Xml.entry post.published ];
      Xml.tag "summary"
        [ ("xml:lang", "\"en\""); ("type", "\"html\"") ]
        [ Xml.entry post.summary ];
    ]

(* DB *)

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

let latest_post () = match !db with [] -> None | x :: _ -> Some x

(* Mastodon integration *)

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;
  content : 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; content }; _ } ->
      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 = content;
        tags;
        published;
        author = actor_name;
        author_link = url;
      }

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