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