open Ppx_yojson_conv_lib.Yojson_conv.Primitives type t = { link : string; summary : string; tags : string list; published : string; author : string; author_link : string; } [@@deriving yojson] 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:""