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