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