types.ml 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. (* inspired by https://github.com/Gopiandcode/ocamlot/src/master/lib/activitypub/decode.ml *)
  2. (* keep this module agnostic of the json library.
  3. * So we discard the 'raw' json for now and keep only the data
  4. * we expect.
  5. *
  6. * Jsonm.lexeme has no equal, so raw could not be equaled.
  7. *)
  8. type jsonm = Jsonm.lexeme
  9. let pp_jsonm = Jsonm.pp_lexeme
  10. (* let equal_jsonm l r = Jsonm.equal l r *)
  11. type uri = Uri.t
  12. let pp_uri = Uri.pp
  13. let equal_uri = Uri.equal
  14. (** https://www.w3.org/TR/activitystreams-core/#collections *)
  15. type 'a collection_page = {
  16. id : uri;
  17. current : uri option;
  18. first : uri option;
  19. is_ordered : bool;
  20. items : 'a list;
  21. last : uri option;
  22. next : uri option;
  23. part_of : uri option;
  24. prev : uri option;
  25. total_items: int option;
  26. } [@@deriving show, eq]
  27. (** https://www.w3.org/TR/activitystreams-core/#collections *)
  28. type 'a collection = {
  29. id : uri;
  30. current : uri option;
  31. first : uri option;
  32. is_ordered : bool;
  33. items : 'a list option;
  34. last : uri option;
  35. total_items: int option;
  36. } [@@deriving show, eq]
  37. (** https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
  38. https://www.w3.org/TR/activitystreams-vocabulary/#dfn-create *)
  39. type 'a create = {
  40. id : uri;
  41. actor : uri;
  42. cc : uri list;
  43. direct_message: bool;
  44. obj : 'a;
  45. published : Ptime.t option;
  46. to_ : uri list;
  47. (* raw: jsonm; *)
  48. } [@@deriving show, eq]
  49. (** https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
  50. https://www.w3.org/TR/activitystreams-vocabulary/#dfn-update *)
  51. type 'a update = {
  52. id : uri;
  53. actor : uri;
  54. cc : uri list;
  55. direct_message: bool;
  56. obj : 'a;
  57. published : Ptime.t option;
  58. to_ : uri list;
  59. (* raw: jsonm; *)
  60. } [@@deriving show, eq]
  61. (** https://www.w3.org/TR/activitystreams-vocabulary/#dfn-announce
  62. https://www.w3.org/TR/activitypub/#announce-activity-inbox *)
  63. type announce = {
  64. id : uri;
  65. actor : uri;
  66. cc : uri list;
  67. obj : uri;
  68. published: Ptime.t option;
  69. to_ : uri list;
  70. (* raw: jsonm; *)
  71. } [@@deriving show, eq]
  72. (** https://www.w3.org/TR/activitystreams-vocabulary/#dfn-accept *)
  73. type 'a accept = {
  74. id : uri;
  75. actor : uri;
  76. obj : 'a;
  77. end_time : Ptime.t option; (* https://www.w3.org/TR/activitystreams-vocabulary/#dfn-endtime *)
  78. published: Ptime.t option;
  79. (* raw: jsonm; *)
  80. } [@@deriving show, eq]
  81. (** https://www.w3.org/TR/activitystreams-vocabulary/#dfn-reject *)
  82. type 'a reject = {
  83. id : uri;
  84. actor : uri;
  85. obj : 'a;
  86. published: Ptime.t option;
  87. (* raw: jsonm; *)
  88. } [@@deriving show, eq]
  89. (** https://www.w3.org/TR/activitystreams-vocabulary/#dfn-undo *)
  90. type 'a undo = {
  91. id : uri;
  92. actor : uri;
  93. obj : 'a;
  94. published: Ptime.t option;
  95. (* raw: jsonm; *)
  96. } [@@deriving show, eq]
  97. (** https://www.w3.org/TR/activitystreams-vocabulary/#dfn-delete *)
  98. type 'a delete = {
  99. id : uri;
  100. actor : uri;
  101. obj : 'a;
  102. published: Ptime.t option;
  103. (* raw: jsonm; *)
  104. }
  105. [@@deriving show, eq]
  106. type 'a event = [
  107. `Create of 'a create
  108. | `Update of 'a update
  109. | `Accept of 'a accept
  110. | `Reject of 'a reject
  111. | `Undo of 'a undo
  112. | `Delete of 'a delete
  113. ] [@@deriving show, eq]
  114. type public_key = {
  115. id : uri;
  116. owner: uri option; (* deprecated however mastodon insists https://digitalcourage.social/@sl007/111838268844684366 *)
  117. pem : string;
  118. signatureAlgorithm: string option;
  119. } [@@deriving show, eq]
  120. (** Attachment as seen on typical actor/person profiles, e.g.
  121. $ curl -L -H 'Accept: application/activity+json' 'https://digitalcourage.social/users/mro'
  122. https://www.w3.org/TR/activitystreams-vocabulary/#dfn-attachment
  123. https://docs.joinmastodon.org/spec/activitypub/#schema
  124. {
  125. "name": "Support",
  126. "value": "<a href=\"https://seppo.social/support\">Seppo.Social/support</a>",
  127. "type": "PropertyValue"
  128. },
  129. *)
  130. type property_value = {
  131. name : string;
  132. name_map : (string * string) list;
  133. value : string;
  134. value_map : (string * string) list;
  135. } [@@deriving show, eq]
  136. (** https://www.w3.org/TR/activitystreams-vocabulary/#dfn-link *)
  137. type link = {
  138. href : uri;
  139. name : string option;
  140. name_map : (string * string) list;
  141. rel : string option;
  142. } [@@deriving show, eq]
  143. (** https://www.w3.org/TR/activitystreams-vocabulary/#dfn-person
  144. https://www.w3.org/TR/activitystreams-core/#actors
  145. *)
  146. type person = {
  147. id : uri;
  148. inbox : uri;
  149. outbox : uri;
  150. followers : uri option;
  151. following : uri option;
  152. attachment : property_value list;
  153. discoverable : bool;
  154. (* generator https://www.w3.org/TR/activitystreams-vocabulary/#dfn-generator *)
  155. generator : link option;
  156. icon : uri list;
  157. image : uri option;
  158. manually_approves_followers: bool;
  159. name : string option;
  160. name_map : (string * string) list;
  161. preferred_username : string option;
  162. preferred_username_map : (string * string) list;
  163. public_key : public_key;
  164. published : Ptime.t option;
  165. summary : string option;
  166. summary_map : (string * string) list;
  167. url : uri list;
  168. (* raw: jsonm; *)
  169. } [@@deriving show, eq]
  170. (** https://www.w3.org/TR/activitystreams-vocabulary/#dfn-follow *)
  171. type follow = {
  172. id : uri;
  173. actor : uri;
  174. cc : uri list;
  175. end_time : Ptime.t option; (* https://www.w3.org/TR/activitystreams-vocabulary/#dfn-endtime *)
  176. object_ : uri;
  177. state : [`Pending | `Cancelled ] option;
  178. to_ : uri list;
  179. (* raw: jsonm; *)
  180. } [@@deriving show, eq]
  181. (** https://www.w3.org/TR/activitystreams-vocabulary/#dfn-tag *)
  182. type tag = {
  183. ty : [`Mention | `Hashtag ];
  184. href: uri;
  185. name: string;
  186. } [@@deriving show, eq]
  187. (** https://www.w3.org/TR/activitystreams-vocabulary/#dfn-attachment *)
  188. type attachment = {
  189. type_ : string option;
  190. (* https://www.w3.org/TR/activitystreams-vocabulary/#dfn-mediatype *)
  191. media_type: string option;
  192. name : string option;
  193. url : uri;
  194. } [@@deriving show, eq]
  195. (** https://www.w3.org/TR/activitystreams-vocabulary/#dfn-note *)
  196. type note = {
  197. id : uri;
  198. agent : string option; (* extension to easily persist the sending agent *)
  199. attachment : attachment list;
  200. attributed_to: uri;
  201. cc : uri list;
  202. in_reply_to: uri list;
  203. reaction_inbox : uri option; (* extension: where to send reactions *)
  204. media_type : string option; (* https://www.w3.org/TR/activitystreams-vocabulary/#dfn-mediatype *)
  205. content_map: (string * string) list;
  206. published : Ptime.t option; (* https://www.w3.org/TR/activitystreams-vocabulary/#dfn-published *)
  207. sensitive : bool; (* https://github.com/swicg/general/issues/7 *)
  208. source : uri option;
  209. summary_map: (string * string) list; (* https://www.w3.org/TR/activitystreams-vocabulary/#dfn-summary *)
  210. tags : tag list;
  211. to_ : uri list;
  212. (* https://www.w3.org/TR/activitystreams-vocabulary/#dfn-updated *)
  213. url : uri list;
  214. (*raw: jsonm;*)
  215. } [@@deriving show, eq]
  216. (** https://www.w3.org/TR/activitystreams-vocabulary/#dfn-follow *)
  217. type block = {
  218. id : uri;
  219. actor : uri;
  220. obj : uri;
  221. published: Ptime.t option;
  222. (*raw: jsonm;*)
  223. } [@@deriving show, eq]
  224. (** https://www.w3.org/TR/activitystreams-vocabulary/#dfn-like *)
  225. type like = {
  226. id : uri;
  227. actor : uri;
  228. obj : uri;
  229. (*raw: jsonm;*)
  230. }
  231. [@@deriving show, eq]
  232. type core_obj = [
  233. | `Block of block
  234. | `Follow of follow
  235. | `Like of like
  236. | `Announce of announce
  237. | `Link of string
  238. | `Note of note
  239. | `Person of person
  240. ] [@@deriving show, eq]
  241. type core_event = core_obj event
  242. [@@deriving show, eq]
  243. type obj = [ core_obj | core_event ]
  244. [@@deriving show, eq]
  245. (** https://www.rfc-editor.org/rfc/rfc7033 *)
  246. module Webfinger = struct
  247. type ty = [
  248. | `ActivityJson_ (** we may phase this out completely as Mike pointed out https://www.w3.org/TR/activitypub/#retrieving-objects *)
  249. | `ActivityJsonLd
  250. | `Atom (** RFC4287 *)
  251. | `Html
  252. | `Json
  253. ]
  254. [@@deriving show, eq]
  255. type link =
  256. | Self of ty * uri
  257. | ProfilePage of ty * uri
  258. | Alternate of ty * uri
  259. | OStatusSubscribe of string (** https://www.rfc-editor.org/rfc/rfc6415#section-3.1.1.1 should contain unescaped {} *)
  260. [@@deriving show, eq]
  261. type query_result = {
  262. subject: string;
  263. aliases: string list;
  264. links : link list;
  265. }
  266. [@@deriving show, eq]
  267. let self_link =
  268. List.find_map (function
  269. | Self ((`ActivityJsonLd
  270. | `ActivityJson_
  271. | `Json), url) -> Some url
  272. | _ -> None)
  273. let profile_page =
  274. List.find_map (function
  275. | ProfilePage ((`Html
  276. | `Atom), url) -> Some url
  277. | _ -> None)
  278. let ostatus_subscribe =
  279. List.find_map (function
  280. | OStatusSubscribe tpl -> Some tpl
  281. | _ -> None)
  282. end