Merge remote-tracking branch 'origin/develop' into 2018-12-17-update-frontend
[akkoma] / test / web / twitter_api / views / activity_view_test.exs
1 defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do
2 use Pleroma.DataCase
3
4 alias Pleroma.Web.CommonAPI
5 alias Pleroma.Web.CommonAPI.Utils
6 alias Pleroma.Web.TwitterAPI.ActivityView
7 alias Pleroma.Web.TwitterAPI.UserView
8 alias Pleroma.Web.TwitterAPI.TwitterAPI
9 alias Pleroma.Repo
10 alias Pleroma.Activity
11 alias Pleroma.User
12 alias Pleroma.Web.ActivityPub.ActivityPub
13
14 import Pleroma.Factory
15 import Mock
16
17 test "a create activity with a html status" do
18 text = """
19 #Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg
20 """
21
22 {:ok, activity} = CommonAPI.post(insert(:user), %{"status" => text})
23
24 result = ActivityView.render("activity.json", activity: activity)
25
26 assert result["statusnet_html"] ==
27 "<a data-tag=\"bike\" href=\"http://localhost:4001/tag/bike\">#Bike</a> log - Commute Tuesday<br /><a href=\"https://pla.bike/posts/20181211/\">https://pla.bike/posts/20181211/</a><br /><a data-tag=\"cycling\" href=\"http://localhost:4001/tag/cycling\">#cycling</a> <a data-tag=\"chscycling\" href=\"http://localhost:4001/tag/chscycling\">#CHScycling</a> <a data-tag=\"commute\" href=\"http://localhost:4001/tag/commute\">#commute</a><br />MVIMG_20181211_054020.jpg"
28
29 assert result["text"] ==
30 "#Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg"
31 end
32
33 test "a create activity with a note" do
34 user = insert(:user)
35 other_user = insert(:user, %{nickname: "shp"})
36
37 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
38
39 result = ActivityView.render("activity.json", activity: activity)
40
41 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
42
43 expected = %{
44 "activity_type" => "post",
45 "attachments" => [],
46 "attentions" => [
47 UserView.render("show.json", %{user: other_user})
48 ],
49 "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
50 "external_url" => activity.data["object"]["id"],
51 "fave_num" => 0,
52 "favorited" => false,
53 "id" => activity.id,
54 "in_reply_to_status_id" => nil,
55 "in_reply_to_screen_name" => nil,
56 "in_reply_to_user_id" => nil,
57 "in_reply_to_profileurl" => nil,
58 "in_reply_to_ostatus_uri" => nil,
59 "is_local" => true,
60 "is_post_verb" => true,
61 "possibly_sensitive" => false,
62 "repeat_num" => 0,
63 "repeated" => false,
64 "statusnet_conversation_id" => convo_id,
65 "statusnet_html" =>
66 "Hey <span><a data-user=\"#{other_user.id}\" href=\"#{other_user.ap_id}\">@<span>shp</span></a></span>!",
67 "tags" => [],
68 "text" => "Hey @shp!",
69 "uri" => activity.data["object"]["id"],
70 "user" => UserView.render("show.json", %{user: user}),
71 "visibility" => "direct",
72 "summary" => nil
73 }
74
75 assert result == expected
76 end
77
78 test "a list of activities" do
79 user = insert(:user)
80 other_user = insert(:user, %{nickname: "shp"})
81 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
82
83 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
84
85 mocks = [
86 {
87 TwitterAPI,
88 [],
89 [context_to_conversation_id: fn _ -> false end]
90 },
91 {
92 User,
93 [:passthrough],
94 [get_cached_by_ap_id: fn _ -> nil end]
95 }
96 ]
97
98 with_mocks mocks do
99 [result] = ActivityView.render("index.json", activities: [activity])
100
101 assert result["statusnet_conversation_id"] == convo_id
102 assert result["user"]
103 refute called(TwitterAPI.context_to_conversation_id(:_))
104 refute called(User.get_cached_by_ap_id(user.ap_id))
105 refute called(User.get_cached_by_ap_id(other_user.ap_id))
106 end
107 end
108
109 test "an activity that is a reply" do
110 user = insert(:user)
111 other_user = insert(:user, %{nickname: "shp"})
112
113 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
114
115 {:ok, answer} =
116 CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
117
118 result = ActivityView.render("activity.json", %{activity: answer})
119
120 assert result["in_reply_to_status_id"] == activity.id
121 end
122
123 test "a like activity" do
124 user = insert(:user)
125 other_user = insert(:user, %{nickname: "shp"})
126
127 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
128 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
129
130 result = ActivityView.render("activity.json", activity: like)
131 activity = Pleroma.Activity.get_by_ap_id(activity.data["id"])
132
133 expected = %{
134 "activity_type" => "like",
135 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
136 "external_url" => like.data["id"],
137 "id" => like.id,
138 "in_reply_to_status_id" => activity.id,
139 "is_local" => true,
140 "is_post_verb" => false,
141 "favorited_status" => ActivityView.render("activity.json", activity: activity),
142 "statusnet_html" => "shp favorited a status.",
143 "text" => "shp favorited a status.",
144 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
145 "user" => UserView.render("show.json", user: other_user)
146 }
147
148 assert result == expected
149 end
150
151 test "a like activity for deleted post" do
152 user = insert(:user)
153 other_user = insert(:user, %{nickname: "shp"})
154
155 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
156 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
157 CommonAPI.delete(activity.id, user)
158
159 result = ActivityView.render("activity.json", activity: like)
160
161 expected = %{
162 "activity_type" => "like",
163 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
164 "external_url" => like.data["id"],
165 "id" => like.id,
166 "in_reply_to_status_id" => nil,
167 "is_local" => true,
168 "is_post_verb" => false,
169 "favorited_status" => nil,
170 "statusnet_html" => "shp favorited a status.",
171 "text" => "shp favorited a status.",
172 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
173 "user" => UserView.render("show.json", user: other_user)
174 }
175
176 assert result == expected
177 end
178
179 test "an announce activity" do
180 user = insert(:user)
181 other_user = insert(:user, %{nickname: "shp"})
182
183 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
184 {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)
185
186 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
187
188 activity = Repo.get(Activity, activity.id)
189
190 result = ActivityView.render("activity.json", activity: announce)
191
192 expected = %{
193 "activity_type" => "repeat",
194 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
195 "external_url" => announce.data["id"],
196 "id" => announce.id,
197 "is_local" => true,
198 "is_post_verb" => false,
199 "statusnet_html" => "shp retweeted a status.",
200 "text" => "shp retweeted a status.",
201 "uri" => "tag:#{announce.data["id"]}:objectType=note",
202 "user" => UserView.render("show.json", user: other_user),
203 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
204 "statusnet_conversation_id" => convo_id
205 }
206
207 assert result == expected
208 end
209
210 test "A follow activity" do
211 user = insert(:user)
212 other_user = insert(:user, %{nickname: "shp"})
213
214 {:ok, follower} = User.follow(user, other_user)
215 {:ok, follow} = ActivityPub.follow(follower, other_user)
216
217 result = ActivityView.render("activity.json", activity: follow)
218
219 expected = %{
220 "activity_type" => "follow",
221 "attentions" => [],
222 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
223 "external_url" => follow.data["id"],
224 "id" => follow.id,
225 "in_reply_to_status_id" => nil,
226 "is_local" => true,
227 "is_post_verb" => false,
228 "statusnet_html" => "#{user.nickname} started following shp",
229 "text" => "#{user.nickname} started following shp",
230 "user" => UserView.render("show.json", user: user)
231 }
232
233 assert result == expected
234 end
235
236 test "a delete activity" do
237 user = insert(:user)
238
239 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
240 {:ok, delete} = CommonAPI.delete(activity.id, user)
241
242 result = ActivityView.render("activity.json", activity: delete)
243
244 expected = %{
245 "activity_type" => "delete",
246 "attentions" => [],
247 "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
248 "external_url" => delete.data["id"],
249 "id" => delete.id,
250 "in_reply_to_status_id" => nil,
251 "is_local" => true,
252 "is_post_verb" => false,
253 "statusnet_html" => "deleted notice {{tag",
254 "text" => "deleted notice {{tag",
255 "uri" => delete.data["object"],
256 "user" => UserView.render("show.json", user: user)
257 }
258
259 assert result == expected
260 end
261 end