types.ml 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. type 'a announce = {
  63. id : uri;
  64. actor : uri;
  65. cc : uri list;
  66. obj : 'a;
  67. published: Ptime.t option;
  68. to_ : uri list;
  69. (* raw: jsonm; *)
  70. } [@@deriving show, eq]
  71. (** https://www.w3.org/TR/activitystreams-vocabulary/#dfn-accept *)
  72. type 'a accept = {
  73. id : uri;
  74. actor : uri;
  75. obj : 'a;
  76. published: Ptime.t option;
  77. (* raw: jsonm; *)
  78. } [@@deriving show, eq]
  79. (** https://www.w3.org/TR/activitystreams-vocabulary/#dfn-reject *)
  80. type 'a reject = {
  81. id : uri;
  82. actor : uri;
  83. obj : 'a;
  84. published: Ptime.t option;
  85. (* raw: jsonm; *)
  86. } [@@deriving show, eq]
  87. (** https://www.w3.org/TR/activitystreams-vocabulary/#dfn-undo *)
  88. type 'a undo = {
  89. id : uri;
  90. actor : uri;
  91. obj : 'a;
  92. published: Ptime.t option;
  93. (* raw: jsonm; *)
  94. } [@@deriving show, eq]
  95. (** https://www.w3.org/TR/activitystreams-vocabulary/#dfn-delete *)
  96. type 'a delete = {
  97. id : uri;
  98. actor : uri;
  99. obj : 'a;
  100. published: Ptime.t option;
  101. (* raw: jsonm; *)
  102. }
  103. [@@deriving show, eq]
  104. type 'a event = [
  105. `Create of 'a create
  106. | `Update of 'a update
  107. | `Announce of 'a announce
  108. | `Accept of 'a accept
  109. | `Reject of 'a reject
  110. | `Undo of 'a undo
  111. | `Delete of 'a delete
  112. ] [@@deriving show, eq]
  113. type public_key = {
  114. id : uri;
  115. owner: uri option; (* deprecated however mastodon insists https://digitalcourage.social/@sl007/111838268844684366 *)
  116. pem : string;
  117. signatureAlgorithm: string option;
  118. } [@@deriving show, eq]
  119. (* Attachment as seen on typical actor/person profiles, e.g.
  120. * $ curl -L -H 'Accept: application/activity+json' 'https://digitalcourage.social/users/mro'
  121. *
  122. * https://www.w3.org/TR/activitystreams-vocabulary/#dfn-attachment
  123. * https://docs.joinmastodon.org/spec/activitypub/#schema
  124. *
  125. {
  126. "name": "Support",
  127. "value": "<a href=\"https://seppo.social/support\">Seppo.Social/support</a>",
  128. "type": "PropertyValue"
  129. },
  130. *)
  131. type property_value = {
  132. name : string;
  133. name_map : (string * string) list;
  134. value : string;
  135. value_map : (string * string) list;
  136. } [@@deriving show, eq]
  137. (* https://www.w3.org/TR/activitystreams-vocabulary/#dfn-link *)
  138. type link = {
  139. href : uri;
  140. name : string option;
  141. name_map : (string * string) list;
  142. rel : string option;
  143. } [@@deriving show, eq]
  144. (* https://www.w3.org/TR/activitystreams-vocabulary/#dfn-person
  145. * https://www.w3.org/TR/activitystreams-core/#actors
  146. *)
  147. type person = {
  148. id : uri;
  149. inbox : uri;
  150. outbox : uri;
  151. followers : uri option;
  152. following : uri option;
  153. attachment : property_value list;
  154. discoverable : bool;
  155. (* generator https://www.w3.org/TR/activitystreams-vocabulary/#dfn-generator *)
  156. generator : link option;
  157. icon : uri option;
  158. image : uri option;
  159. manually_approves_followers: bool;
  160. name : string option;
  161. name_map : (string * string) list;
  162. preferred_username : string option;
  163. preferred_username_map : (string * string) list;
  164. public_key : public_key;
  165. published : Ptime.t option;
  166. summary : string option;
  167. summary_map : (string * string) list;
  168. url : uri list;
  169. (* raw: jsonm; *)
  170. } [@@deriving show, eq]
  171. (* https://www.w3.org/TR/activitystreams-vocabulary/#dfn-follow *)
  172. type follow = {
  173. id : uri;
  174. actor : uri;
  175. cc : uri list;
  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. actor : uri;
  199. attachment : attachment list;
  200. cc : uri list;
  201. in_reply_to: uri list;
  202. media_type : string option; (* https://www.w3.org/TR/activitystreams-vocabulary/#dfn-mediatype *)
  203. content : string;
  204. content_map: (string * string) list;
  205. published : Ptime.t option;
  206. sensitive : bool;
  207. source : uri option;
  208. summary : string option;
  209. summary_map: (string * string) list;
  210. tags : tag list;
  211. to_ : uri list;
  212. url : uri list;
  213. (*raw: jsonm;*)
  214. } [@@deriving show, eq]
  215. (* https://www.w3.org/TR/activitystreams-vocabulary/#dfn-follow *)
  216. type block = {
  217. id : uri;
  218. actor : uri;
  219. obj : uri;
  220. published: Ptime.t option;
  221. (*raw: jsonm;*)
  222. } [@@deriving show, eq]
  223. (* https://www.w3.org/TR/activitystreams-vocabulary/#dfn-like *)
  224. type like = {
  225. id : uri;
  226. actor : uri;
  227. obj : uri;
  228. published: Ptime.t option;
  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. | `Link of string
  237. | `Note of note
  238. | `Person of person
  239. ] [@@deriving show, eq]
  240. type core_event = core_obj event
  241. [@@deriving show, eq]
  242. type obj = [ core_obj | core_event ]
  243. [@@deriving show, eq]
  244. module Webfinger = struct
  245. type ty = [
  246. | `ActivityJson_ (* we may phase this out completely as Mike pointed out https://www.w3.org/TR/activitypub/#retrieving-objects *)
  247. | `ActivityJsonLd
  248. | `Atom (* RFC4287 *)
  249. | `Html
  250. | `Json
  251. ]
  252. [@@deriving show, eq]
  253. type link =
  254. | Self of ty * uri
  255. | ProfilePage of ty * uri
  256. | Alternate of ty * uri
  257. | OStatusSubscribe of string (* https://www.rfc-editor.org/rfc/rfc6415#section-3.1.1.1 should contain unescaped {} *)
  258. [@@deriving show, eq]
  259. type query_result = {
  260. subject: string;
  261. aliases: string list;
  262. links : link list;
  263. }
  264. [@@deriving show, eq]
  265. let self_link =
  266. List.find_map (function
  267. | Self ((`ActivityJsonLd
  268. | `ActivityJson_
  269. | `Json), url) -> Some url
  270. | _ -> None)
  271. let profile_page =
  272. List.find_map (function
  273. | ProfilePage ((`Html
  274. | `Atom), url) -> Some url
  275. | _ -> None)
  276. let ostatus_subscribe =
  277. List.find_map (function
  278. | OStatusSubscribe tpl -> Some tpl
  279. | _ -> None)
  280. end