1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do
8 alias Pleroma.Web.OStatus.ActivityRepresenter
9 alias Pleroma.{User, Activity, Object}
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.OStatus
13 import Pleroma.Factory
17 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
21 test "an external note activity" do
22 incoming = File.read!("test/fixtures/mastodon-note-cw.xml")
23 {:ok, [activity]} = OStatus.handle_incoming(incoming)
25 user = User.get_cached_by_ap_id(activity.data["actor"])
27 tuple = ActivityRepresenter.to_simple_form(activity, user)
29 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
31 assert String.contains?(
33 ~s{<link type="text/html" href="https://mastodon.social/users/lambadalambda/updates/2314748" rel="alternate"/>}
37 test "a note activity" do
38 note_activity = insert(:note_activity)
40 user = User.get_cached_by_ap_id(note_activity.data["actor"])
43 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
44 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
45 <id>#{note_activity.data["object"]["id"]}</id>
46 <title>New note by #{user.nickname}</title>
47 <content type="html">#{note_activity.data["object"]["content"]}</content>
48 <published>#{note_activity.data["object"]["published"]}</published>
49 <updated>#{note_activity.data["object"]["published"]}</updated>
50 <ostatus:conversation ref="#{note_activity.data["context"]}">#{note_activity.data["context"]}</ostatus:conversation>
51 <link ref="#{note_activity.data["context"]}" rel="ostatus:conversation" />
52 <summary>#{note_activity.data["object"]["summary"]}</summary>
53 <link type="application/atom+xml" href="#{note_activity.data["object"]["id"]}" rel="self" />
54 <link type="text/html" href="#{note_activity.data["object"]["id"]}" rel="alternate" />
55 <category term="2hu"/>
56 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
57 <link name="2hu" rel="emoji" href="corndog.png" />
60 tuple = ActivityRepresenter.to_simple_form(note_activity, user)
62 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
64 assert clean(res) == clean(expected)
67 test "a reply note" do
68 note = insert(:note_activity)
69 answer = insert(:note_activity)
70 object = answer.data["object"]
71 object = Map.put(object, "inReplyTo", note.data["object"]["id"])
73 data = %{answer.data | "object" => object}
74 answer = %{answer | data: data}
76 note_object = Object.get_by_ap_id(note.data["object"]["id"])
79 Object.change(note_object, %{data: Map.put(note_object.data, "external_url", "someurl")})
82 user = User.get_cached_by_ap_id(answer.data["actor"])
85 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
86 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
87 <id>#{answer.data["object"]["id"]}</id>
88 <title>New note by #{user.nickname}</title>
89 <content type="html">#{answer.data["object"]["content"]}</content>
90 <published>#{answer.data["object"]["published"]}</published>
91 <updated>#{answer.data["object"]["published"]}</updated>
92 <ostatus:conversation ref="#{answer.data["context"]}">#{answer.data["context"]}</ostatus:conversation>
93 <link ref="#{answer.data["context"]}" rel="ostatus:conversation" />
94 <summary>2hu</summary>
95 <link type="application/atom+xml" href="#{answer.data["object"]["id"]}" rel="self" />
96 <link type="text/html" href="#{answer.data["object"]["id"]}" rel="alternate" />
97 <category term="2hu"/>
98 <thr:in-reply-to ref="#{note.data["object"]["id"]}" href="someurl" />
99 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
100 <link name="2hu" rel="emoji" href="corndog.png" />
103 tuple = ActivityRepresenter.to_simple_form(answer, user)
105 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
107 assert clean(res) == clean(expected)
110 test "an announce activity" do
111 note = insert(:note_activity)
113 object = Object.get_cached_by_ap_id(note.data["object"]["id"])
115 {:ok, announce, _object} = ActivityPub.announce(user, object)
117 announce = Repo.get(Activity, announce.id)
119 note_user = User.get_cached_by_ap_id(note.data["actor"])
120 note = Repo.get(Activity, note.id)
123 ActivityRepresenter.to_simple_form(note, note_user, true)
124 |> :xmerl.export_simple_content(:xmerl_xml)
128 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
129 <activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
130 <id>#{announce.data["id"]}</id>
131 <title>#{user.nickname} repeated a notice</title>
132 <content type="html">RT #{note.data["object"]["content"]}</content>
133 <published>#{announce.data["published"]}</published>
134 <updated>#{announce.data["published"]}</updated>
135 <ostatus:conversation ref="#{announce.data["context"]}">#{announce.data["context"]}</ostatus:conversation>
136 <link ref="#{announce.data["context"]}" rel="ostatus:conversation" />
137 <link rel="self" type="application/atom+xml" href="#{announce.data["id"]}"/>
141 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
144 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
148 ActivityRepresenter.to_simple_form(announce, user)
149 |> :xmerl.export_simple_content(:xmerl_xml)
152 assert clean(expected) == clean(announce_xml)
155 test "a like activity" do
158 {:ok, like, _note} = ActivityPub.like(user, note)
160 tuple = ActivityRepresenter.to_simple_form(like, user)
163 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
166 <activity:verb>http://activitystrea.ms/schema/1.0/favorite</activity:verb>
167 <id>#{like.data["id"]}</id>
168 <title>New favorite by #{user.nickname}</title>
169 <content type="html">#{user.nickname} favorited something</content>
170 <published>#{like.data["published"]}</published>
171 <updated>#{like.data["published"]}</updated>
173 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
174 <id>#{note.data["id"]}</id>
176 <ostatus:conversation ref="#{like.data["context"]}">#{like.data["context"]}</ostatus:conversation>
177 <link ref="#{like.data["context"]}" rel="ostatus:conversation" />
178 <link rel="self" type="application/atom+xml" href="#{like.data["id"]}"/>
179 <thr:in-reply-to ref="#{note.data["id"]}" />
180 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
183 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
186 assert clean(res) == clean(expected)
189 test "a follow activity" do
190 follower = insert(:user)
191 followed = insert(:user)
194 ActivityPub.insert(%{
196 "actor" => follower.ap_id,
197 "object" => followed.ap_id,
198 "to" => [followed.ap_id]
201 tuple = ActivityRepresenter.to_simple_form(activity, follower)
205 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
208 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
209 <activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
210 <id>#{activity.data["id"]}</id>
211 <title>#{follower.nickname} started following #{activity.data["object"]}</title>
212 <content type="html"> #{follower.nickname} started following #{activity.data["object"]}</content>
213 <published>#{activity.data["published"]}</published>
214 <updated>#{activity.data["published"]}</updated>
216 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
217 <id>#{activity.data["object"]}</id>
218 <uri>#{activity.data["object"]}</uri>
220 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
221 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
222 activity.data["object"]
226 assert clean(res) == clean(expected)
229 test "an unfollow activity" do
230 follower = insert(:user)
231 followed = insert(:user)
232 {:ok, _activity} = ActivityPub.follow(follower, followed)
233 {:ok, activity} = ActivityPub.unfollow(follower, followed)
235 tuple = ActivityRepresenter.to_simple_form(activity, follower)
239 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
242 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
243 <activity:verb>http://activitystrea.ms/schema/1.0/unfollow</activity:verb>
244 <id>#{activity.data["id"]}</id>
245 <title>#{follower.nickname} stopped following #{followed.ap_id}</title>
246 <content type="html"> #{follower.nickname} stopped following #{followed.ap_id}</content>
247 <published>#{activity.data["published"]}</published>
248 <updated>#{activity.data["published"]}</updated>
250 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
251 <id>#{followed.ap_id}</id>
252 <uri>#{followed.ap_id}</uri>
254 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
255 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
260 assert clean(res) == clean(expected)
266 activity = %Activity{
270 "actor" => user.ap_id,
271 "object" => "some_id",
272 "published" => "2017-06-18T12:00:18+00:00"
276 tuple = ActivityRepresenter.to_simple_form(activity, nil)
280 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
283 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
284 <activity:verb>http://activitystrea.ms/schema/1.0/delete</activity:verb>
285 <id>#{activity.data["object"]}</id>
286 <title>An object was deleted</title>
287 <content type="html">An object was deleted</content>
288 <published>#{activity.data["published"]}</published>
289 <updated>#{activity.data["published"]}</updated>
292 assert clean(res) == clean(expected)
295 test "an unknown activity" do
296 tuple = ActivityRepresenter.to_simple_form(%Activity{}, nil)
300 defp clean(string) do
301 String.replace(string, ~r/\s/, "")