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