package activitypub_gui

  1. Overview
  2. Docs

Source file utils.ml

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
(*********************************************************************************)
(*                OCaml-ActivityPub                                              *)
(*                                                                               *)
(*    Copyright (C) 2023-2024 INRIA All rights reserved.                         *)
(*    Author: Maxence Guesdon, INRIA Saclay                                      *)
(*                                                                               *)
(*    This program is free software; you can redistribute it and/or modify       *)
(*    it under the terms of the GNU Lesser General Public License version        *)
(*    3 as published by the Free Software Foundation.                            *)
(*                                                                               *)
(*    This program is distributed in the hope that it will be useful,            *)
(*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *)
(*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *)
(*    GNU General Public License for more details.                               *)
(*                                                                               *)
(*    You should have received a copy of the GNU General Public License          *)
(*    along with this program; if not, write to the Free Software                *)
(*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                   *)
(*    02111-1307  USA                                                            *)
(*                                                                               *)
(*    Contact: maxence.guesdon@inria.fr                                          *)
(*                                                                               *)
(*********************************************************************************)

(** Utilities *)

module AP = Activitypub

let string_of_object_type iri =
  match Iri.fragment iri with
  | Some s -> s
  | None ->
      match Iri.path iri with
      | Absolute [] | Relative [] -> ""
      | Absolute l | Relative l -> List.hd (List.rev l)

let date_to_since =
  let units = [
      60. *. 60. *. 24. *. 365., "year" ;
      60. *. 60. *. 24. *. 30., "month" ;
      60. *. 60. *. 24., "day" ;
      60. *. 60., "hour" ;
      60., "minute" ;
    ]
  in
  let rec f secs = function
  | [] -> "just now"
  | (s, label) :: q ->
      let p = secs /. s in
      if p >= 1. then
        Printf.sprintf "%d %s%s" (truncate p) label
          (if p > 2. then "s" else "")
      else
        f secs q
  in
  let to_unit diff =
    let secs = Ptime.Span.to_float_s diff in
    f secs units
  in
  fun d ->
    let now = AP.Utils.ptime_now () in
    let span = Ptime.diff now d in
    to_unit span