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