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