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
11 test "an external note activity" do
12 incoming = File.read!("test/fixtures/mastodon-note-cw.xml")
13 {:ok, [activity]} = OStatus.handle_incoming(incoming)
15 user = User.get_cached_by_ap_id(activity.data["actor"])
17 tuple = ActivityRepresenter.to_simple_form(activity, user)
19 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
21 assert String.contains?(
23 ~s{<link type="text/html" href="https://mastodon.social/users/lambadalambda/updates/2314748" rel="alternate"/>}
27 test "a note activity" do
28 note_activity = insert(:note_activity)
30 user = User.get_cached_by_ap_id(note_activity.data["actor"])
33 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
34 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
35 <id>#{note_activity.data["object"]["id"]}</id>
36 <title>New note by #{user.nickname}</title>
37 <content type="html">#{note_activity.data["object"]["content"]}</content>
38 <published>#{note_activity.data["object"]["published"]}</published>
39 <updated>#{note_activity.data["object"]["published"]}</updated>
40 <ostatus:conversation ref="#{note_activity.data["context"]}">#{note_activity.data["context"]}</ostatus:conversation>
41 <link ref="#{note_activity.data["context"]}" rel="ostatus:conversation" />
42 <summary>#{note_activity.data["object"]["summary"]}</summary>
43 <link type="application/atom+xml" href="#{note_activity.data["object"]["id"]}" rel="self" />
44 <link type="text/html" href="#{note_activity.data["object"]["id"]}" rel="alternate" />
45 <category term="2hu"/>
46 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
47 <link name="2hu" rel="emoji" href="corndog.png" />
50 tuple = ActivityRepresenter.to_simple_form(note_activity, user)
52 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
54 assert clean(res) == clean(expected)
57 test "a reply note" do
58 note = insert(:note_activity)
59 answer = insert(:note_activity)
60 object = answer.data["object"]
61 object = Map.put(object, "inReplyTo", note.data["object"]["id"])
63 data = %{answer.data | "object" => object}
64 answer = %{answer | data: data}
66 note_object = Object.get_by_ap_id(note.data["object"]["id"])
69 Object.change(note_object, %{data: Map.put(note_object.data, "external_url", "someurl")})
72 user = User.get_cached_by_ap_id(answer.data["actor"])
75 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
76 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
77 <id>#{answer.data["object"]["id"]}</id>
78 <title>New note by #{user.nickname}</title>
79 <content type="html">#{answer.data["object"]["content"]}</content>
80 <published>#{answer.data["object"]["published"]}</published>
81 <updated>#{answer.data["object"]["published"]}</updated>
82 <ostatus:conversation ref="#{answer.data["context"]}">#{answer.data["context"]}</ostatus:conversation>
83 <link ref="#{answer.data["context"]}" rel="ostatus:conversation" />
84 <summary>2hu</summary>
85 <link type="application/atom+xml" href="#{answer.data["object"]["id"]}" rel="self" />
86 <link type="text/html" href="#{answer.data["object"]["id"]}" rel="alternate" />
87 <category term="2hu"/>
88 <thr:in-reply-to ref="#{note.data["object"]["id"]}" href="someurl" />
89 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
90 <link name="2hu" rel="emoji" href="corndog.png" />
93 tuple = ActivityRepresenter.to_simple_form(answer, user)
95 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
97 assert clean(res) == clean(expected)
100 test "an announce activity" do
101 note = insert(:note_activity)
103 object = Object.get_cached_by_ap_id(note.data["object"]["id"])
105 {:ok, announce, _object} = ActivityPub.announce(user, object)
107 announce = Repo.get(Activity, announce.id)
109 note_user = User.get_cached_by_ap_id(note.data["actor"])
110 note = Repo.get(Activity, note.id)
113 ActivityRepresenter.to_simple_form(note, note_user, true)
114 |> :xmerl.export_simple_content(:xmerl_xml)
118 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
119 <activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
120 <id>#{announce.data["id"]}</id>
121 <title>#{user.nickname} repeated a notice</title>
122 <content type="html">RT #{note.data["object"]["content"]}</content>
123 <published>#{announce.data["published"]}</published>
124 <updated>#{announce.data["published"]}</updated>
125 <ostatus:conversation ref="#{announce.data["context"]}">#{announce.data["context"]}</ostatus:conversation>
126 <link ref="#{announce.data["context"]}" rel="ostatus:conversation" />
127 <link rel="self" type="application/atom+xml" href="#{announce.data["id"]}"/>
131 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
134 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
138 ActivityRepresenter.to_simple_form(announce, user)
139 |> :xmerl.export_simple_content(:xmerl_xml)
142 assert clean(expected) == clean(announce_xml)
145 test "a like activity" do
148 {:ok, like, _note} = ActivityPub.like(user, note)
150 tuple = ActivityRepresenter.to_simple_form(like, user)
153 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
156 <activity:verb>http://activitystrea.ms/schema/1.0/favorite</activity:verb>
157 <id>#{like.data["id"]}</id>
158 <title>New favorite by #{user.nickname}</title>
159 <content type="html">#{user.nickname} favorited something</content>
160 <published>#{like.data["published"]}</published>
161 <updated>#{like.data["published"]}</updated>
163 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
164 <id>#{note.data["id"]}</id>
166 <ostatus:conversation ref="#{like.data["context"]}">#{like.data["context"]}</ostatus:conversation>
167 <link ref="#{like.data["context"]}" rel="ostatus:conversation" />
168 <link rel="self" type="application/atom+xml" href="#{like.data["id"]}"/>
169 <thr:in-reply-to ref="#{note.data["id"]}" />
170 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
173 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
176 assert clean(res) == clean(expected)
179 test "a follow activity" do
180 follower = insert(:user)
181 followed = insert(:user)
184 ActivityPub.insert(%{
186 "actor" => follower.ap_id,
187 "object" => followed.ap_id,
188 "to" => [followed.ap_id]
191 tuple = ActivityRepresenter.to_simple_form(activity, follower)
195 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
198 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
199 <activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
200 <id>#{activity.data["id"]}</id>
201 <title>#{follower.nickname} started following #{activity.data["object"]}</title>
202 <content type="html"> #{follower.nickname} started following #{activity.data["object"]}</content>
203 <published>#{activity.data["published"]}</published>
204 <updated>#{activity.data["published"]}</updated>
206 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
207 <id>#{activity.data["object"]}</id>
208 <uri>#{activity.data["object"]}</uri>
210 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
211 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
212 activity.data["object"]
216 assert clean(res) == clean(expected)
219 test "an unfollow activity" do
220 follower = insert(:user)
221 followed = insert(:user)
222 {:ok, _activity} = ActivityPub.follow(follower, followed)
223 {:ok, activity} = ActivityPub.unfollow(follower, followed)
225 tuple = ActivityRepresenter.to_simple_form(activity, follower)
229 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
232 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
233 <activity:verb>http://activitystrea.ms/schema/1.0/unfollow</activity:verb>
234 <id>#{activity.data["id"]}</id>
235 <title>#{follower.nickname} stopped following #{followed.ap_id}</title>
236 <content type="html"> #{follower.nickname} stopped following #{followed.ap_id}</content>
237 <published>#{activity.data["published"]}</published>
238 <updated>#{activity.data["published"]}</updated>
240 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
241 <id>#{followed.ap_id}</id>
242 <uri>#{followed.ap_id}</uri>
244 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
245 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{
250 assert clean(res) == clean(expected)
256 activity = %Activity{
260 "actor" => user.ap_id,
261 "object" => "some_id",
262 "published" => "2017-06-18T12:00:18+00:00"
266 tuple = ActivityRepresenter.to_simple_form(activity, nil)
270 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary()
273 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
274 <activity:verb>http://activitystrea.ms/schema/1.0/delete</activity:verb>
275 <id>#{activity.data["object"]}</id>
276 <title>An object was deleted</title>
277 <content type="html">An object was deleted</content>
278 <published>#{activity.data["published"]}</published>
279 <updated>#{activity.data["published"]}</updated>
282 assert clean(res) == clean(expected)
285 test "an unknown activity" do
286 tuple = ActivityRepresenter.to_simple_form(%Activity{}, nil)
290 defp clean(string) do
291 String.replace(string, ~r/\s/, "")