Add categories to ostatus output.
[akkoma] / lib / pleroma / web / ostatus / activity_representer.ex
1 defmodule Pleroma.Web.OStatus.ActivityRepresenter do
2 alias Pleroma.{Activity, User}
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 categories = (activity.data["object"]["tag"] || [])
46 |> Enum.map(fn (tag) -> {:category, [term: to_charlist(tag)], []} end)
47
48 [
49 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
50 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
51 {:id, h.(activity.data["object"]["id"])}, # For notes, federate the object id.
52 {:title, ['New note by #{user.nickname}']},
53 {:content, [type: 'html'], h.(activity.data["object"]["content"])},
54 {:published, h.(inserted_at)},
55 {:updated, h.(updated_at)},
56 {:"ostatus:conversation", [], h.(activity.data["context"])},
57 {:link, [href: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
58 {:link, [type: ['application/atom+xml'], href: h.(activity.data["object"]["id"]), rel: 'self'], []}
59 ] ++ categories ++ attachments ++ in_reply_to ++ author ++ mentions
60 end
61
62 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
63 h = fn(str) -> [to_charlist(str)] end
64
65 updated_at = activity.updated_at
66 |> NaiveDateTime.to_iso8601
67 inserted_at = activity.inserted_at
68 |> NaiveDateTime.to_iso8601
69
70 in_reply_to = get_in_reply_to(activity.data)
71 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
72 mentions = activity.data["to"] |> get_mentions
73
74 [
75 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/favorite']},
76 {:id, h.(activity.data["id"])},
77 {:title, ['New favorite by #{user.nickname}']},
78 {:content, [type: 'html'], ['#{user.nickname} favorited something']},
79 {:published, h.(inserted_at)},
80 {:updated, h.(updated_at)},
81 {:"activity:object", [
82 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
83 {:id, h.(activity.data["object"])}, # For notes, federate the object id.
84 ]},
85 {:"ostatus:conversation", [], h.(activity.data["context"])},
86 {:link, [href: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
87 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
88 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
89 ] ++ author ++ mentions
90 end
91
92 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
93 h = fn(str) -> [to_charlist(str)] end
94
95 updated_at = activity.updated_at
96 |> NaiveDateTime.to_iso8601
97 inserted_at = activity.inserted_at
98 |> NaiveDateTime.to_iso8601
99
100 in_reply_to = get_in_reply_to(activity.data)
101 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
102
103 retweeted_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
104 retweeted_user = User.get_cached_by_ap_id(retweeted_activity.data["actor"])
105
106 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
107
108 mentions = activity.data["to"] |> get_mentions
109 [
110 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
111 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/share']},
112 {:id, h.(activity.data["id"])},
113 {:title, ['#{user.nickname} repeated a notice']},
114 {:content, [type: 'html'], ['RT #{retweeted_activity.data["object"]["content"]}']},
115 {:published, h.(inserted_at)},
116 {:updated, h.(updated_at)},
117 {:"ostatus:conversation", [], h.(activity.data["context"])},
118 {:link, [href: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
119 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
120 {:"activity:object", retweeted_xml}
121 ] ++ mentions ++ author
122 end
123
124 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
125 h = fn(str) -> [to_charlist(str)] end
126
127 updated_at = activity.updated_at
128 |> NaiveDateTime.to_iso8601
129 inserted_at = activity.inserted_at
130 |> NaiveDateTime.to_iso8601
131
132 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
133
134 mentions = (activity.data["to"] || []) |> get_mentions
135 [
136 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
137 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/follow']},
138 {:id, h.(activity.data["id"])},
139 {:title, ['#{user.nickname} started following #{activity.data["object"]}']},
140 {:content, [type: 'html'], ['#{user.nickname} started following #{activity.data["object"]}']},
141 {:published, h.(inserted_at)},
142 {:updated, h.(updated_at)},
143 {:"activity:object", [
144 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
145 {:id, h.(activity.data["object"])},
146 {:uri, h.(activity.data["object"])},
147 ]},
148 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
149 ] ++ mentions ++ author
150 end
151
152 # Only undos of follow for now. Will need to get redone once there are more
153 def to_simple_form(%{data: %{"type" => "Undo"}} = activity, user, with_author) do
154 h = fn(str) -> [to_charlist(str)] end
155
156 updated_at = activity.updated_at
157 |> NaiveDateTime.to_iso8601
158 inserted_at = activity.inserted_at
159 |> NaiveDateTime.to_iso8601
160
161 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
162 follow_activity = Activity.get_by_ap_id(activity.data["object"])
163
164 mentions = (activity.data["to"] || []) |> get_mentions
165 [
166 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
167 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
168 {:id, h.(activity.data["id"])},
169 {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
170 {:content, [type: 'html'], ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
171 {:published, h.(inserted_at)},
172 {:updated, h.(updated_at)},
173 {:"activity:object", [
174 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
175 {:id, h.(follow_activity.data["object"])},
176 {:uri, h.(follow_activity.data["object"])},
177 ]},
178 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
179 ] ++ mentions ++ author
180 end
181
182 def wrap_with_entry(simple_form) do
183 [{
184 :entry, [
185 xmlns: 'http://www.w3.org/2005/Atom',
186 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
187 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
188 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
189 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'
190 ], simple_form
191 }]
192 end
193
194 def to_simple_form(_, _, _), do: nil
195 end