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
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.OStatus
13 alias Pleroma.Web.OStatus.ActivityRepresenter
15 import Pleroma.Factory
19 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
23 test "an external note activity" do
24 incoming = File.read!("test/fixtures/mastodon-note-cw.xml")
25 {:ok, [activity]} = OStatus.handle_incoming(incoming)
27 user = User.get_cached_by_ap_id(activity.data["actor"])
29 tuple = ActivityRepresenter.to_simple_form(activity, user)
31 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
33 assert String.contains?(
35 ~s{<link type="text/html" href="https://mastodon.social/users/lambadalambda/updates/2314748" rel="alternate"/>}
39 test "a note activity" do
40 note_activity = insert(:note_activity)
42 user = User.get_cached_by_ap_id(note_activity.data["actor"])
45 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
46 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
47 <id>#{note_activity.data["object"]["id"]}</id>
48 <title>New note by #{user.nickname}</title>
49 <content type="html">#{note_activity.data["object"]["content"]}</content>
50 <published>#{note_activity.data["object"]["published"]}</published>
51 <updated>#{note_activity.data["object"]["published"]}</updated>
52 <ostatus:conversation ref="#{note_activity.data["context"]}">#{note_activity.data["context"]}</ostatus:conversation>
53 <link ref="#{note_activity.data["context"]}" rel="ostatus:conversation" />
54 <summary>#{note_activity.data["object"]["summary"]}</summary>
55 <link type="application/atom+xml" href="#{note_activity.data["object"]["id"]}" rel="self" />
56 <link type="text/html" href="#{note_activity.data["object"]["id"]}" rel="alternate" />
57 <category term="2hu"/>
58 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
59 <link name="2hu" rel="emoji" href="corndog.png" />
62 tuple = ActivityRepresenter.to_simple_form(note_activity, user)
64 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
66 assert clean(res) == clean(expected)
69 test "a reply note" do
70 note = insert(:note_activity)
71 answer = insert(:note_activity)
72 object = answer.data["object"]
73 object = Map.put(object, "inReplyTo", note.data["object"]["id"])
75 data = %{answer.data | "object" => object}
76 answer = %{answer | data: data}
78 note_object = Object.get_by_ap_id(note.data["object"]["id"])
81 Object.change(note_object, %{data: Map.put(note_object.data, "external_url", "someurl")})
84 user = User.get_cached_by_ap_id(answer.data["actor"])
87 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
88 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
89 <id>#{answer.data["object"]["id"]}</id>
90 <title>New note by #{user.nickname}</title>
91 <content type="html">#{answer.data["object"]["content"]}</content>
92 <published>#{answer.data["object"]["published"]}</published>
93 <updated>#{answer.data["object"]["published"]}</updated>
94 <ostatus:conversation ref="#{answer.data["context"]}">#{answer.data["context"]}</ostatus:conversation>
95 <link ref="#{answer.data["context"]}" rel="ostatus:conversation" />
96 <summary>2hu</summary>
97 <link type="application/atom+xml" href="#{answer.data["object"]["id"]}" rel="self" />
98 <link type="text/html" href="#{answer.data["object"]["id"]}" rel="alternate" />
99 <category term="2hu"/>
100 <thr:in-reply-to ref="#{note.data["object"]["id"]}" href="someurl" />
101 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
102 <link name="2hu" rel="emoji" href="corndog.png" />
105 tuple = ActivityRepresenter.to_simple_form(answer, user)
107 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
109 assert clean(res) == clean(expected)
112 test "an announce activity" do
113 note = insert(:note_activity)
115 object = Object.get_cached_by_ap_id(note.data["object"]["id"])
117 {:ok, announce, _object} = ActivityPub.announce(user, object)
119 announce = Repo.get(Activity, announce.id)
121 note_user = User.get_cached_by_ap_id(note.data["actor"])
122 note = Repo.get(Activity, note.id)
125 ActivityRepresenter.to_simple_form(note, note_user, true)
126 |> :xmerl.export_simple_content(:xmerl_xml)
130 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
131 <activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
132 <id>#{announce.data["id"]}</id>
133 <title>#{user.nickname} repeated a notice</title>
134 <content type="html">RT #{note.data["object"]["content"]}</content>
135 <published>#{announce.data["published"]}</published>
136 <updated>#{announce.data["published"]}</updated>
137 <ostatus:conversation ref="#{announce.data["context"]}">#{announce.data["context"]}</ostatus:conversation>
138 <link ref="#{announce.data["context"]}" rel="ostatus:conversation" />
139 <link rel="self" type="application/atom+xml" href="#{announce.data["id"]}"/>
143 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
146 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
150 ActivityRepresenter.to_simple_form(announce, user)
151 |> :xmerl.export_simple_content(:xmerl_xml)
154 assert clean(expected) == clean(announce_xml)
157 test "a like activity" do
160 {:ok, like, _note} = ActivityPub.like(user, note)
162 tuple = ActivityRepresenter.to_simple_form(like, user)
165 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
168 <activity:verb>http://activitystrea.ms/schema/1.0/favorite</activity:verb>
169 <id>#{like.data["id"]}</id>
170 <title>New favorite by #{user.nickname}</title>
171 <content type="html">#{user.nickname} favorited something</content>
172 <published>#{like.data["published"]}</published>
173 <updated>#{like.data["published"]}</updated>
175 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
176 <id>#{note.data["id"]}</id>
178 <ostatus:conversation ref="#{like.data["context"]}">#{like.data["context"]}</ostatus:conversation>
179 <link ref="#{like.data["context"]}" rel="ostatus:conversation" />
180 <link rel="self" type="application/atom+xml" href="#{like.data["id"]}"/>
181 <thr:in-reply-to ref="#{note.data["id"]}" />
182 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
185 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
188 assert clean(res) == clean(expected)
191 test "a follow activity" do
192 follower = insert(:user)
193 followed = insert(:user)
196 ActivityPub.insert(%{
198 "actor" => follower.ap_id,
199 "object" => followed.ap_id,
200 "to" => [followed.ap_id]
203 tuple = ActivityRepresenter.to_simple_form(activity, follower)
207 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
210 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
211 <activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
212 <id>#{activity.data["id"]}</id>
213 <title>#{follower.nickname} started following #{activity.data["object"]}</title>
214 <content type="html"> #{follower.nickname} started following #{activity.data["object"]}</content>
215 <published>#{activity.data["published"]}</published>
216 <updated>#{activity.data["published"]}</updated>
218 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
219 <id>#{activity.data["object"]}</id>
220 <uri>#{activity.data["object"]}</uri>
222 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
223 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
224 activity.data["object"]
228 assert clean(res) == clean(expected)
231 test "an unfollow activity" do
232 follower = insert(:user)
233 followed = insert(:user)
234 {:ok, _activity} = ActivityPub.follow(follower, followed)
235 {:ok, activity} = ActivityPub.unfollow(follower, followed)
237 tuple = ActivityRepresenter.to_simple_form(activity, follower)
241 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
244 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
245 <activity:verb>http://activitystrea.ms/schema/1.0/unfollow</activity:verb>
246 <id>#{activity.data["id"]}</id>
247 <title>#{follower.nickname} stopped following #{followed.ap_id}</title>
248 <content type="html"> #{follower.nickname} stopped following #{followed.ap_id}</content>
249 <published>#{activity.data["published"]}</published>
250 <updated>#{activity.data["published"]}</updated>
252 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
253 <id>#{followed.ap_id}</id>
254 <uri>#{followed.ap_id}</uri>
256 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
257 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
262 assert clean(res) == clean(expected)
268 activity = %Activity{
272 "actor" => user.ap_id,
273 "object" => "some_id",
274 "published" => "2017-06-18T12:00:18+00:00"
278 tuple = ActivityRepresenter.to_simple_form(activity, nil)
282 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
285 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
286 <activity:verb>http://activitystrea.ms/schema/1.0/delete</activity:verb>
287 <id>#{activity.data["object"]}</id>
288 <title>An object was deleted</title>
289 <content type="html">An object was deleted</content>
290 <published>#{activity.data["published"]}</published>
291 <updated>#{activity.data["published"]}</updated>
294 assert clean(res) == clean(expected)
297 test "an unknown activity" do
298 tuple = ActivityRepresenter.to_simple_form(%Activity{}, nil)
302 defp clean(string) do
303 String.replace(string, ~r/\s/, "")