1 defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do
4 alias Pleroma.Web.OStatus.ActivityRepresenter
5 alias Pleroma.{User, Activity, Object}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Pleroma.Web.OStatus
13 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 test "an external note activity" do
18 incoming = File.read!("test/fixtures/mastodon-note-cw.xml")
19 {:ok, [activity]} = OStatus.handle_incoming(incoming)
21 user = User.get_cached_by_ap_id(activity.data["actor"])
23 tuple = ActivityRepresenter.to_simple_form(activity, user)
25 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
27 assert String.contains?(
29 ~s{<link type="text/html" href="https://mastodon.social/users/lambadalambda/updates/2314748" rel="alternate"/>}
33 test "a note activity" do
34 note_activity = insert(:note_activity)
36 user = User.get_cached_by_ap_id(note_activity.data["actor"])
39 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
40 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
41 <id>#{note_activity.data["object"]["id"]}</id>
42 <title>New note by #{user.nickname}</title>
43 <content type="html">#{note_activity.data["object"]["content"]}</content>
44 <published>#{note_activity.data["object"]["published"]}</published>
45 <updated>#{note_activity.data["object"]["published"]}</updated>
46 <ostatus:conversation ref="#{note_activity.data["context"]}">#{note_activity.data["context"]}</ostatus:conversation>
47 <link ref="#{note_activity.data["context"]}" rel="ostatus:conversation" />
48 <summary>#{note_activity.data["object"]["summary"]}</summary>
49 <link type="application/atom+xml" href="#{note_activity.data["object"]["id"]}" rel="self" />
50 <link type="text/html" href="#{note_activity.data["object"]["id"]}" rel="alternate" />
51 <category term="2hu"/>
52 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
53 <link name="2hu" rel="emoji" href="corndog.png" />
56 tuple = ActivityRepresenter.to_simple_form(note_activity, user)
58 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
60 assert clean(res) == clean(expected)
63 test "a reply note" do
64 note = insert(:note_activity)
65 answer = insert(:note_activity)
66 object = answer.data["object"]
67 object = Map.put(object, "inReplyTo", note.data["object"]["id"])
69 data = %{answer.data | "object" => object}
70 answer = %{answer | data: data}
72 note_object = Object.get_by_ap_id(note.data["object"]["id"])
75 Object.change(note_object, %{data: Map.put(note_object.data, "external_url", "someurl")})
78 user = User.get_cached_by_ap_id(answer.data["actor"])
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>#{answer.data["object"]["id"]}</id>
84 <title>New note by #{user.nickname}</title>
85 <content type="html">#{answer.data["object"]["content"]}</content>
86 <published>#{answer.data["object"]["published"]}</published>
87 <updated>#{answer.data["object"]["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="#{answer.data["object"]["id"]}" rel="self" />
92 <link type="text/html" href="#{answer.data["object"]["id"]}" rel="alternate" />
93 <category term="2hu"/>
94 <thr:in-reply-to ref="#{note.data["object"]["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 = Repo.get(Activity, announce.id)
115 note_user = User.get_cached_by_ap_id(note.data["actor"])
116 note = Repo.get(Activity, 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/, "")