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
71 note_object = insert(:note)
72 _note = insert(:note_activity, %{note: note_object})
73 object = insert(:note, %{data: %{"inReplyTo" => note_object.data["id"]}})
74 answer = insert(:note_activity, %{note: object})
77 Object.change(note_object, %{data: Map.put(note_object.data, "external_url", "someurl")})
81 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
82 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
83 <id>#{object.data["id"]}</id>
84 <title>New note by #{user.nickname}</title>
85 <content type="html">#{object.data["content"]}</content>
86 <published>#{object.data["published"]}</published>
87 <updated>#{object.data["published"]}</updated>
88 <ostatus:conversation ref="#{answer.data["context"]}">#{answer.data["context"]}</ostatus:conversation>
89 <link ref="#{answer.data["context"]}" rel="ostatus:conversation" />
90 <summary>2hu</summary>
91 <link type="application/atom+xml" href="#{object.data["id"]}" rel="self" />
92 <link type="text/html" href="#{object.data["id"]}" rel="alternate" />
93 <category term="2hu"/>
94 <thr:in-reply-to ref="#{note_object.data["id"]}" href="someurl" />
95 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
96 <link name="2hu" rel="emoji" href="corndog.png" />
99 tuple = ActivityRepresenter.to_simple_form(answer, user)
101 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
103 assert clean(res) == clean(expected)
106 test "an announce activity" do
107 note = insert(:note_activity)
109 object = Object.get_cached_by_ap_id(note.data["object"]["id"])
111 {:ok, announce, _object} = ActivityPub.announce(user, object)
113 announce = Activity.get_by_id(announce.id)
115 note_user = User.get_cached_by_ap_id(note.data["actor"])
116 note = Activity.get_by_id(note.id)
119 ActivityRepresenter.to_simple_form(note, note_user, true)
120 |> :xmerl.export_simple_content(:xmerl_xml)
124 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
125 <activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
126 <id>#{announce.data["id"]}</id>
127 <title>#{user.nickname} repeated a notice</title>
128 <content type="html">RT #{note.data["object"]["content"]}</content>
129 <published>#{announce.data["published"]}</published>
130 <updated>#{announce.data["published"]}</updated>
131 <ostatus:conversation ref="#{announce.data["context"]}">#{announce.data["context"]}</ostatus:conversation>
132 <link ref="#{announce.data["context"]}" rel="ostatus:conversation" />
133 <link rel="self" type="application/atom+xml" href="#{announce.data["id"]}"/>
137 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
140 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
144 ActivityRepresenter.to_simple_form(announce, user)
145 |> :xmerl.export_simple_content(:xmerl_xml)
148 assert clean(expected) == clean(announce_xml)
151 test "a like activity" do
154 {:ok, like, _note} = ActivityPub.like(user, note)
156 tuple = ActivityRepresenter.to_simple_form(like, user)
159 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
162 <activity:verb>http://activitystrea.ms/schema/1.0/favorite</activity:verb>
163 <id>#{like.data["id"]}</id>
164 <title>New favorite by #{user.nickname}</title>
165 <content type="html">#{user.nickname} favorited something</content>
166 <published>#{like.data["published"]}</published>
167 <updated>#{like.data["published"]}</updated>
169 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
170 <id>#{note.data["id"]}</id>
172 <ostatus:conversation ref="#{like.data["context"]}">#{like.data["context"]}</ostatus:conversation>
173 <link ref="#{like.data["context"]}" rel="ostatus:conversation" />
174 <link rel="self" type="application/atom+xml" href="#{like.data["id"]}"/>
175 <thr:in-reply-to ref="#{note.data["id"]}" />
176 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
179 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
182 assert clean(res) == clean(expected)
185 test "a follow activity" do
186 follower = insert(:user)
187 followed = insert(:user)
190 ActivityPub.insert(%{
192 "actor" => follower.ap_id,
193 "object" => followed.ap_id,
194 "to" => [followed.ap_id]
197 tuple = ActivityRepresenter.to_simple_form(activity, follower)
201 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
204 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
205 <activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
206 <id>#{activity.data["id"]}</id>
207 <title>#{follower.nickname} started following #{activity.data["object"]}</title>
208 <content type="html"> #{follower.nickname} started following #{activity.data["object"]}</content>
209 <published>#{activity.data["published"]}</published>
210 <updated>#{activity.data["published"]}</updated>
212 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
213 <id>#{activity.data["object"]}</id>
214 <uri>#{activity.data["object"]}</uri>
216 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
217 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
218 activity.data["object"]
222 assert clean(res) == clean(expected)
225 test "an unfollow activity" do
226 follower = insert(:user)
227 followed = insert(:user)
228 {:ok, _activity} = ActivityPub.follow(follower, followed)
229 {:ok, activity} = ActivityPub.unfollow(follower, followed)
231 tuple = ActivityRepresenter.to_simple_form(activity, follower)
235 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
238 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
239 <activity:verb>http://activitystrea.ms/schema/1.0/unfollow</activity:verb>
240 <id>#{activity.data["id"]}</id>
241 <title>#{follower.nickname} stopped following #{followed.ap_id}</title>
242 <content type="html"> #{follower.nickname} stopped following #{followed.ap_id}</content>
243 <published>#{activity.data["published"]}</published>
244 <updated>#{activity.data["published"]}</updated>
246 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
247 <id>#{followed.ap_id}</id>
248 <uri>#{followed.ap_id}</uri>
250 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
251 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
256 assert clean(res) == clean(expected)
262 activity = %Activity{
266 "actor" => user.ap_id,
267 "object" => "some_id",
268 "published" => "2017-06-18T12:00:18+00:00"
272 tuple = ActivityRepresenter.to_simple_form(activity, nil)
276 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
279 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
280 <activity:verb>http://activitystrea.ms/schema/1.0/delete</activity:verb>
281 <id>#{activity.data["object"]}</id>
282 <title>An object was deleted</title>
283 <content type="html">An object was deleted</content>
284 <published>#{activity.data["published"]}</published>
285 <updated>#{activity.data["published"]}</updated>
288 assert clean(res) == clean(expected)
291 test "an unknown activity" do
292 tuple = ActivityRepresenter.to_simple_form(%Activity{}, nil)
296 defp clean(string) do
297 String.replace(string, ~r/\s/, "")