Add an ostatus representer for like activities.
[akkoma] / lib / pleroma / web / ostatus / activity_representer.ex
1 defmodule Pleroma.Web.OStatus.ActivityRepresenter do
2 alias Pleroma.Activity
3 alias Pleroma.Web.OStatus.UserRepresenter
4 require Logger
5
6 defp get_in_reply_to(%{"object" => %{ "inReplyTo" => in_reply_to}}) do
7 [{:"thr:in-reply-to", [ref: to_charlist(in_reply_to)], []}]
8 end
9
10 defp get_in_reply_to(_), do: []
11
12 defp get_mentions(to) do
13 Enum.map(to, fn (id) ->
14 cond do
15 # Special handling for the AP/Ostatus public collections
16 "https://www.w3.org/ns/activitystreams#Public" == id ->
17 {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/collection", href: "http://activityschema.org/collection/public"], []}
18 # Ostatus doesn't handle follower collections, ignore these.
19 Regex.match?(~r/^#{Pleroma.Web.base_url}.+followers$/, id) ->
20 []
21 true ->
22 {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/person", href: id], []}
23 end
24 end)
25 end
26
27 def to_simple_form(activity, user, with_author \\ false)
28 def to_simple_form(%{data: %{"object" => %{"type" => "Note"}}} = activity, user, with_author) do
29 h = fn(str) -> [to_charlist(str)] end
30
31 updated_at = activity.updated_at
32 |> NaiveDateTime.to_iso8601
33 inserted_at = activity.inserted_at
34 |> NaiveDateTime.to_iso8601
35
36 attachments = Enum.map(activity.data["object"]["attachment"] || [], fn(attachment) ->
37 url = hd(attachment["url"])
38 {:link, [rel: 'enclosure', href: to_charlist(url["href"]), type: to_charlist(url["mediaType"])], []}
39 end)
40
41 in_reply_to = get_in_reply_to(activity.data)
42 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
43 mentions = activity.data["to"] |> get_mentions
44
45 [
46 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
47 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
48 {:id, h.(activity.data["object"]["id"])}, # For notes, federate the object id.
49 {:title, ['New note by #{user.nickname}']},
50 {:content, [type: 'html'], h.(activity.data["object"]["content"])},
51 {:published, h.(inserted_at)},
52 {:updated, h.(updated_at)},
53 {:"ostatus:conversation", [], h.(activity.data["context"])},
54 {:link, [href: h.(activity.data["context"]), rel: 'ostatus:conversation'], []}
55 ] ++ attachments ++ in_reply_to ++ author ++ mentions
56 end
57
58 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
59 h = fn(str) -> [to_charlist(str)] end
60
61 updated_at = activity.updated_at
62 |> NaiveDateTime.to_iso8601
63 inserted_at = activity.inserted_at
64 |> NaiveDateTime.to_iso8601
65
66 in_reply_to = get_in_reply_to(activity.data)
67 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
68 mentions = activity.data["to"] |> get_mentions
69
70 [
71 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/favorite']},
72 {:id, h.(activity.data["id"])},
73 {:title, ['New favorite by #{user.nickname}']},
74 {:content, [type: 'html'], ['#{user.nickname} favorited something']},
75 {:published, h.(inserted_at)},
76 {:updated, h.(updated_at)},
77 {:"activity:object", [
78 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
79 {:id, h.(activity.data["object"])}, # For notes, federate the object id.
80 ]},
81 {:"ostatus:conversation", [], h.(activity.data["context"])},
82 {:link, [href: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
83 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
84 ] ++ author ++ mentions
85 end
86
87 def wrap_with_entry(simple_form) do
88 [{
89 :entry, [
90 xmlns: 'http://www.w3.org/2005/Atom',
91 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
92 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
93 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
94 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'
95 ], simple_form
96 }]
97 end
98
99 def to_simple_form(_,_,_), do: nil
100 end