Fix conflict
[akkoma] / test / web / ostatus / activity_representer_test.exs
1 defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do
2 use Pleroma.DataCase
3
4 alias Pleroma.Web.OStatus.ActivityRepresenter
5 alias Pleroma.{User, Activity, Object}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Pleroma.Web.OStatus
8
9 import Pleroma.Factory
10
11 test "an external note activity" do
12 incoming = File.read!("test/fixtures/mastodon-note-cw.xml")
13 {:ok, [activity]} = OStatus.handle_incoming(incoming)
14
15 user = User.get_cached_by_ap_id(activity.data["actor"])
16
17 tuple = ActivityRepresenter.to_simple_form(activity, user)
18
19 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
20
21 assert String.contains?(
22 res,
23 ~s{<link type="text/html" href="https://mastodon.social/users/lambadalambda/updates/2314748" rel="alternate"/>}
24 )
25 end
26
27 test "a note activity" do
28 note_activity = insert(:note_activity)
29
30 user = User.get_cached_by_ap_id(note_activity.data["actor"])
31
32 expected = """
33 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
34 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
35 <id>#{note_activity.data["object"]["id"]}</id>
36 <title>New note by #{user.nickname}</title>
37 <content type="html">#{note_activity.data["object"]["content"]}</content>
38 <published>#{note_activity.data["object"]["published"]}</published>
39 <updated>#{note_activity.data["object"]["published"]}</updated>
40 <ostatus:conversation ref="#{note_activity.data["context"]}">#{note_activity.data["context"]}</ostatus:conversation>
41 <link ref="#{note_activity.data["context"]}" rel="ostatus:conversation" />
42 <summary>#{note_activity.data["object"]["summary"]}</summary>
43 <link type="application/atom+xml" href="#{note_activity.data["object"]["id"]}" rel="self" />
44 <link type="text/html" href="#{note_activity.data["object"]["id"]}" rel="alternate" />
45 <category term="2hu"/>
46 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
47 <link name="2hu" rel="emoji" href="corndog.png" />
48 """
49
50 tuple = ActivityRepresenter.to_simple_form(note_activity, user)
51
52 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
53
54 assert clean(res) == clean(expected)
55 end
56
57 test "a reply note" do
58 note = insert(:note_activity)
59 answer = insert(:note_activity)
60 object = answer.data["object"]
61 object = Map.put(object, "inReplyTo", note.data["object"]["id"])
62
63 data = %{answer.data | "object" => object}
64 answer = %{answer | data: data}
65
66 note_object = Object.get_by_ap_id(note.data["object"]["id"])
67
68 Repo.update!(
69 Object.change(note_object, %{data: Map.put(note_object.data, "external_url", "someurl")})
70 )
71
72 user = User.get_cached_by_ap_id(answer.data["actor"])
73
74 expected = """
75 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
76 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
77 <id>#{answer.data["object"]["id"]}</id>
78 <title>New note by #{user.nickname}</title>
79 <content type="html">#{answer.data["object"]["content"]}</content>
80 <published>#{answer.data["object"]["published"]}</published>
81 <updated>#{answer.data["object"]["published"]}</updated>
82 <ostatus:conversation ref="#{answer.data["context"]}">#{answer.data["context"]}</ostatus:conversation>
83 <link ref="#{answer.data["context"]}" rel="ostatus:conversation" />
84 <summary>2hu</summary>
85 <link type="application/atom+xml" href="#{answer.data["object"]["id"]}" rel="self" />
86 <link type="text/html" href="#{answer.data["object"]["id"]}" rel="alternate" />
87 <category term="2hu"/>
88 <thr:in-reply-to ref="#{note.data["object"]["id"]}" href="someurl" />
89 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
90 <link name="2hu" rel="emoji" href="corndog.png" />
91 """
92
93 tuple = ActivityRepresenter.to_simple_form(answer, user)
94
95 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
96
97 assert clean(res) == clean(expected)
98 end
99
100 test "an announce activity" do
101 note = insert(:note_activity)
102 user = insert(:user)
103 object = Object.get_cached_by_ap_id(note.data["object"]["id"])
104
105 {:ok, announce, _object} = ActivityPub.announce(user, object)
106
107 announce = Repo.get(Activity, announce.id)
108
109 note_user = User.get_cached_by_ap_id(note.data["actor"])
110 note = Repo.get(Activity, note.id)
111
112 note_xml =
113 ActivityRepresenter.to_simple_form(note, note_user, true)
114 |> :xmerl.export_simple_content(:xmerl_xml)
115 |> to_string
116
117 expected = """
118 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
119 <activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
120 <id>#{announce.data["id"]}</id>
121 <title>#{user.nickname} repeated a notice</title>
122 <content type="html">RT #{note.data["object"]["content"]}</content>
123 <published>#{announce.data["published"]}</published>
124 <updated>#{announce.data["published"]}</updated>
125 <ostatus:conversation ref="#{announce.data["context"]}">#{announce.data["context"]}</ostatus:conversation>
126 <link ref="#{announce.data["context"]}" rel="ostatus:conversation" />
127 <link rel="self" type="application/atom+xml" href="#{announce.data["id"]}"/>
128 <activity:object>
129 #{note_xml}
130 </activity:object>
131 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
132 note.data["actor"]
133 }"/>
134 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
135 """
136
137 announce_xml =
138 ActivityRepresenter.to_simple_form(announce, user)
139 |> :xmerl.export_simple_content(:xmerl_xml)
140 |> to_string
141
142 assert clean(expected) == clean(announce_xml)
143 end
144
145 test "a like activity" do
146 note = insert(:note)
147 user = insert(:user)
148 {:ok, like, _note} = ActivityPub.like(user, note)
149
150 tuple = ActivityRepresenter.to_simple_form(like, user)
151 refute is_nil(tuple)
152
153 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
154
155 expected = """
156 <activity:verb>http://activitystrea.ms/schema/1.0/favorite</activity:verb>
157 <id>#{like.data["id"]}</id>
158 <title>New favorite by #{user.nickname}</title>
159 <content type="html">#{user.nickname} favorited something</content>
160 <published>#{like.data["published"]}</published>
161 <updated>#{like.data["published"]}</updated>
162 <activity:object>
163 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
164 <id>#{note.data["id"]}</id>
165 </activity:object>
166 <ostatus:conversation ref="#{like.data["context"]}">#{like.data["context"]}</ostatus:conversation>
167 <link ref="#{like.data["context"]}" rel="ostatus:conversation" />
168 <link rel="self" type="application/atom+xml" href="#{like.data["id"]}"/>
169 <thr:in-reply-to ref="#{note.data["id"]}" />
170 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
171 note.data["actor"]
172 }"/>
173 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
174 """
175
176 assert clean(res) == clean(expected)
177 end
178
179 test "a follow activity" do
180 follower = insert(:user)
181 followed = insert(:user)
182
183 {:ok, activity} =
184 ActivityPub.insert(%{
185 "type" => "Follow",
186 "actor" => follower.ap_id,
187 "object" => followed.ap_id,
188 "to" => [followed.ap_id]
189 })
190
191 tuple = ActivityRepresenter.to_simple_form(activity, follower)
192
193 refute is_nil(tuple)
194
195 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
196
197 expected = """
198 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
199 <activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
200 <id>#{activity.data["id"]}</id>
201 <title>#{follower.nickname} started following #{activity.data["object"]}</title>
202 <content type="html"> #{follower.nickname} started following #{activity.data["object"]}</content>
203 <published>#{activity.data["published"]}</published>
204 <updated>#{activity.data["published"]}</updated>
205 <activity:object>
206 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
207 <id>#{activity.data["object"]}</id>
208 <uri>#{activity.data["object"]}</uri>
209 </activity:object>
210 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
211 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
212 activity.data["object"]
213 }"/>
214 """
215
216 assert clean(res) == clean(expected)
217 end
218
219 test "an unfollow activity" do
220 follower = insert(:user)
221 followed = insert(:user)
222 {:ok, _activity} = ActivityPub.follow(follower, followed)
223 {:ok, activity} = ActivityPub.unfollow(follower, followed)
224
225 tuple = ActivityRepresenter.to_simple_form(activity, follower)
226
227 refute is_nil(tuple)
228
229 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
230
231 expected = """
232 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
233 <activity:verb>http://activitystrea.ms/schema/1.0/unfollow</activity:verb>
234 <id>#{activity.data["id"]}</id>
235 <title>#{follower.nickname} stopped following #{followed.ap_id}</title>
236 <content type="html"> #{follower.nickname} stopped following #{followed.ap_id}</content>
237 <published>#{activity.data["published"]}</published>
238 <updated>#{activity.data["published"]}</updated>
239 <activity:object>
240 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
241 <id>#{followed.ap_id}</id>
242 <uri>#{followed.ap_id}</uri>
243 </activity:object>
244 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
245 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
246 followed.ap_id
247 }"/>
248 """
249
250 assert clean(res) == clean(expected)
251 end
252
253 test "a delete" do
254 user = insert(:user)
255
256 activity = %Activity{
257 data: %{
258 "id" => "ap_id",
259 "type" => "Delete",
260 "actor" => user.ap_id,
261 "object" => "some_id",
262 "published" => "2017-06-18T12:00:18+00:00"
263 }
264 }
265
266 tuple = ActivityRepresenter.to_simple_form(activity, nil)
267
268 refute is_nil(tuple)
269
270 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
271
272 expected = """
273 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
274 <activity:verb>http://activitystrea.ms/schema/1.0/delete</activity:verb>
275 <id>#{activity.data["object"]}</id>
276 <title>An object was deleted</title>
277 <content type="html">An object was deleted</content>
278 <published>#{activity.data["published"]}</published>
279 <updated>#{activity.data["published"]}</updated>
280 """
281
282 assert clean(res) == clean(expected)
283 end
284
285 test "an unknown activity" do
286 tuple = ActivityRepresenter.to_simple_form(%Activity{}, nil)
287 assert is_nil(tuple)
288 end
289
290 defp clean(string) do
291 String.replace(string, ~r/\s/, "")
292 end
293 end