package activitypub

  1. Overview
  2. Docs

Source file actor.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
(*********************************************************************************)
(*                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                                          *)
(*                                                                               *)
(*********************************************************************************)

(** Actor pre-implementation. *)

module AP = Rdf.Activitypub
module Sec = Rdf.Security

let iri_manually_approves_followers = Rdf.Activitystreams.activitystreams_ "manuallyApprovesFollowers"

(** Pre-implementation of {!Types.actor}. *)
class virtual actor ?g (id:Types.id) =
  object(self)
    method virtual id : Types.id
    method private virtual g_ : Rdf.Graph.graph
    method private virtual new_collection : ?g:Rdf.Graph.graph -> Types.id -> Types.collection
    method private virtual new_ordered_collection : ?g:Rdf.Graph.graph -> Types.id -> Types.ordered_collection

    method private collection pred =
      Option.map (fun iri -> self#new_collection (Rdf.Term.Iri iri))
        (Object.G.iri_obj_option self#g_ id pred)

    method private mand_ordered_collection pred =
      let iri =
        match Object.G.iri_obj_option self#g_ self#id pred with
        | None ->
            Log.warn (fun m -> m "%a has no %a" Rdf.Term.pp_term self#id Iri.pp pred);
            Iri.of_string ""
      | Some iri -> iri
      in
      self#new_ordered_collection (Rdf.Term.Iri iri)

    method inbox = self#mand_ordered_collection AP.inbox
    method outbox = self#mand_ordered_collection AP.outbox
    method following = self#collection AP.following
    method followers = self#collection AP.followers
    method liked = self#collection AP.liked
    method manually_approves_followers = Option.value ~default:false
      (Object.G.first_bool_obj self#g_ self#id iri_manually_approves_followers)
    method streams = List.map
      (fun iri ->  self#new_collection (Rdf.Term.Iri iri))
        (Rdf.Graph.iri_objects_of self#g_ self#id AP.streams)

    method preferred_username = Object.G.first_string_obj self#g_ self#id AP.preferredUsername

    method public_keypem =
      let on_keynode term =
        match Object.G.first_string_obj self#g_ term Sec.publicKeyPem with
        | None -> None
        | Some str ->
            match X509.Public_key.decode_pem str with
            | Ok pub_key -> Some pub_key
            | Error (`Msg msg) ->
                Log.warn (fun m -> m "%a: invalid pubKeyPem: %s" Rdf.Term.pp_term self#id msg);
                  None
      in
      match self#g_.find ~sub:self#id ~pred:Sec.publicKey () with
      | [] -> on_keynode self#id
      | (_,_,term) :: _ -> on_keynode term

    method public_key_iri =
      match Rdf.Graph.iri_objects_of self#g_ ~sub:self#id ~pred:Sec.publicKey with
      | [] -> None
      | iri :: _ -> Some iri

    method private_keypem : X509.Private_key.t option Lwt.t = Lwt.return_none
      (*match Object.G.first_string_obj self#g_ self#id Sec.privateKeyPem with
      | None -> None
      | Some str ->
          match X509.Private_key.decode_pem (Cstruct.of_string str) with
          | Ok pub_key -> Some pub_key
          | Error (`Msg msg) ->
              Log.warn (fun m -> m "%a: invalid privateKeyPem: %s" Rdf.Term.pp_term self#id msg);
              None*)

  end