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