171a8bae75e6fe20cc3d5df42592df50de9e26e8
[akkoma] / test / web / ostatus / activity_representer_test.exs
1 defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do
2 use Pleroma.DataCase
3
4 alias Pleroma.Web.OStatus.ActivityRepresenter
5 alias Pleroma.{User, Activity, Object}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Pleroma.Web.OStatus
8
9 import Pleroma.Factory
10
11 test "an external note activity" do
12 incoming = File.read!("test/fixtures/mastodon-note-cw.xml")
13 {:ok, [activity]} = OStatus.handle_incoming(incoming)
14
15 user = User.get_cached_by_ap_id(activity.data["actor"])
16
17 tuple = ActivityRepresenter.to_simple_form(activity, user)
18
19 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
20
21 assert String.contains?(res, ~s{<link type="text/html" href="https://mastodon.social/users/lambadalambda/updates/2314748" rel="alternate"/>})
22 end
23
24 test "a note activity" do
25 note_activity = insert(:note_activity)
26
27 user = User.get_cached_by_ap_id(note_activity.data["actor"])
28
29 expected = """
30 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
31 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
32 <id>#{note_activity.data["object"]["id"]}</id>
33 <title>New note by #{user.nickname}</title>
34 <content type="html">#{note_activity.data["object"]["content"]}</content>
35 <published>#{note_activity.data["object"]["published"]}</published>
36 <updated>#{note_activity.data["object"]["published"]}</updated>
37 <ostatus:conversation>#{note_activity.data["context"]}</ostatus:conversation>
38 <link ref="#{note_activity.data["context"]}" rel="ostatus:conversation" />
39 <link type="application/atom+xml" href="#{note_activity.data["object"]["id"]}" rel="self" />
40 <link type="text/html" href="#{note_activity.data["object"]["id"]}" rel="alternate" />
41 <category term="2hu"/>
42 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
43 """
44
45 tuple = ActivityRepresenter.to_simple_form(note_activity, user)
46
47 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
48
49 assert clean(res) == clean(expected)
50 end
51
52 test "a reply note" do
53 note = insert(:note_activity)
54 answer = insert(:note_activity)
55 object = answer.data["object"]
56 object = Map.put(object, "inReplyTo", note.data["object"]["id"])
57
58 data = %{answer.data | "object" => object}
59 answer = %{answer | data: data}
60
61 user = User.get_cached_by_ap_id(answer.data["actor"])
62
63 expected = """
64 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
65 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
66 <id>#{answer.data["object"]["id"]}</id>
67 <title>New note by #{user.nickname}</title>
68 <content type="html">#{answer.data["object"]["content"]}</content>
69 <published>#{answer.data["object"]["published"]}</published>
70 <updated>#{answer.data["object"]["published"]}</updated>
71 <ostatus:conversation>#{answer.data["context"]}</ostatus:conversation>
72 <link ref="#{answer.data["context"]}" rel="ostatus:conversation" />
73 <link type="application/atom+xml" href="#{answer.data["object"]["id"]}" rel="self" />
74 <link type="text/html" href="#{answer.data["object"]["id"]}" rel="alternate" />
75 <category term="2hu"/>
76 <thr:in-reply-to ref="#{note.data["object"]["id"]}" />
77 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
78 """
79
80 tuple = ActivityRepresenter.to_simple_form(answer, user)
81
82 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
83
84 assert clean(res) == clean(expected)
85 end
86
87 test "an announce activity" do
88 note = insert(:note_activity)
89 user = insert(:user)
90 object = Object.get_cached_by_ap_id(note.data["object"]["id"])
91
92 {:ok, announce, object} = ActivityPub.announce(user, object)
93
94 announce = Repo.get(Activity, announce.id)
95
96 note_user = User.get_cached_by_ap_id(note.data["actor"])
97 note = Repo.get(Activity, note.id)
98 note_xml = ActivityRepresenter.to_simple_form(note, note_user, true)
99 |> :xmerl.export_simple_content(:xmerl_xml)
100 |> to_string
101
102 expected = """
103 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
104 <activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
105 <id>#{announce.data["id"]}</id>
106 <title>#{user.nickname} repeated a notice</title>
107 <content type="html">RT #{note.data["object"]["content"]}</content>
108 <published>#{announce.data["published"]}</published>
109 <updated>#{announce.data["published"]}</updated>
110 <ostatus:conversation>#{announce.data["context"]}</ostatus:conversation>
111 <link ref="#{announce.data["context"]}" rel="ostatus:conversation" />
112 <link rel="self" type="application/atom+xml" href="#{announce.data["id"]}"/>
113 <activity:object>
114 #{note_xml}
115 </activity:object>
116 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{note.data["actor"]}"/>
117 """
118
119 announce_xml = ActivityRepresenter.to_simple_form(announce, user)
120 |> :xmerl.export_simple_content(:xmerl_xml)
121 |> to_string
122
123 assert clean(expected) == clean(announce_xml)
124 end
125
126 test "a like activity" do
127 note = insert(:note)
128 user = insert(:user)
129 {:ok, like, _note} = ActivityPub.like(user, note)
130
131 tuple = ActivityRepresenter.to_simple_form(like, user)
132 refute is_nil(tuple)
133
134 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
135
136 expected = """
137 <activity:verb>http://activitystrea.ms/schema/1.0/favorite</activity:verb>
138 <id>#{like.data["id"]}</id>
139 <title>New favorite by #{user.nickname}</title>
140 <content type="html">#{user.nickname} favorited something</content>
141 <published>#{like.data["published"]}</published>
142 <updated>#{like.data["published"]}</updated>
143 <activity:object>
144 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
145 <id>#{note.data["id"]}</id>
146 </activity:object>
147 <ostatus:conversation>#{like.data["context"]}</ostatus:conversation>
148 <link ref="#{like.data["context"]}" rel="ostatus:conversation" />
149 <link rel="self" type="application/atom+xml" href="#{like.data["id"]}"/>
150 <thr:in-reply-to ref="#{note.data["id"]}" />
151 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{note.data["actor"]}"/>
152 """
153
154 assert clean(res) == clean(expected)
155 end
156
157 test "a follow activity" do
158 follower = insert(:user)
159 followed = insert(:user)
160 {:ok, activity} = ActivityPub.insert(%{
161 "type" => "Follow",
162 "actor" => follower.ap_id,
163 "object" => followed.ap_id,
164 "to" => [followed.ap_id]
165 })
166
167 tuple = ActivityRepresenter.to_simple_form(activity, follower)
168
169 refute is_nil(tuple)
170
171 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
172
173 expected = """
174 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
175 <activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
176 <id>#{activity.data["id"]}</id>
177 <title>#{follower.nickname} started following #{activity.data["object"]}</title>
178 <content type="html"> #{follower.nickname} started following #{activity.data["object"]}</content>
179 <published>#{activity.data["published"]}</published>
180 <updated>#{activity.data["published"]}</updated>
181 <activity:object>
182 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
183 <id>#{activity.data["object"]}</id>
184 <uri>#{activity.data["object"]}</uri>
185 </activity:object>
186 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
187 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{activity.data["object"]}"/>
188 """
189
190 assert clean(res) == clean(expected)
191 end
192
193 test "an unfollow activity" do
194 follower = insert(:user)
195 followed = insert(:user)
196 {:ok, _activity} = ActivityPub.follow(follower, followed)
197 {:ok, activity} = ActivityPub.unfollow(follower, followed)
198
199 tuple = ActivityRepresenter.to_simple_form(activity, follower)
200
201 refute is_nil(tuple)
202
203 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
204
205 expected = """
206 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
207 <activity:verb>http://activitystrea.ms/schema/1.0/unfollow</activity:verb>
208 <id>#{activity.data["id"]}</id>
209 <title>#{follower.nickname} stopped following #{followed.ap_id}</title>
210 <content type="html"> #{follower.nickname} stopped following #{followed.ap_id}</content>
211 <published>#{activity.data["published"]}</published>
212 <updated>#{activity.data["published"]}</updated>
213 <activity:object>
214 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
215 <id>#{followed.ap_id}</id>
216 <uri>#{followed.ap_id}</uri>
217 </activity:object>
218 <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
219 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{followed.ap_id}"/>
220 """
221
222 assert clean(res) == clean(expected)
223 end
224
225 test "an unknown activity" do
226 tuple = ActivityRepresenter.to_simple_form(%Activity{}, nil)
227 assert is_nil(tuple)
228 end
229
230 defp clean(string) do
231 String.replace(string, ~r/\s/, "")
232 end
233 end