1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.CommonAPI.Utils
15 alias Pleroma.Web.TwitterAPI.ActivityView
16 alias Pleroma.Web.TwitterAPI.UserView
18 import Pleroma.Factory
22 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
28 test "returns a temporary ap_id based user for activities missing db users" do
31 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
34 Cachex.clear(:user_cache)
36 %{"user" => tw_user} = ActivityView.render("activity.json", activity: activity)
38 assert tw_user["screen_name"] == "erroruser@example.com"
39 assert tw_user["name"] == user.ap_id
40 assert tw_user["statusnet_profile_url"] == user.ap_id
43 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
46 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
50 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
53 Cachex.clear(:user_cache)
55 result = ActivityView.render("activity.json", activity: activity)
56 assert result["user"]["id"] == user.id
59 test "tells if the message is muted for some reason" do
61 other_user = insert(:user)
63 {:ok, user} = User.mute(user, other_user)
65 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"})
66 status = ActivityView.render("activity.json", %{activity: activity})
68 assert status["muted"] == false
70 status = ActivityView.render("activity.json", %{activity: activity, for: user})
72 assert status["muted"] == true
75 test "a create activity with a html status" do
77 #Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg
80 {:ok, activity} = CommonAPI.post(insert(:user), %{"status" => text})
82 result = ActivityView.render("activity.json", activity: activity)
84 assert result["statusnet_html"] ==
85 "<a class=\"hashtag\" data-tag=\"bike\" href=\"http://localhost:4001/tag/bike\" rel=\"tag\">#Bike</a> log - Commute Tuesday<br /><a href=\"https://pla.bike/posts/20181211/\">https://pla.bike/posts/20181211/</a><br /><a class=\"hashtag\" data-tag=\"cycling\" href=\"http://localhost:4001/tag/cycling\" rel=\"tag\">#cycling</a> <a class=\"hashtag\" data-tag=\"chscycling\" href=\"http://localhost:4001/tag/chscycling\" rel=\"tag\">#CHScycling</a> <a class=\"hashtag\" data-tag=\"commute\" href=\"http://localhost:4001/tag/commute\" rel=\"tag\">#commute</a><br />MVIMG_20181211_054020.jpg"
87 assert result["text"] ==
88 "#Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg"
91 test "a create activity with a summary containing emoji" do
93 CommonAPI.post(insert(:user), %{
94 "spoiler_text" => ":firefox: meow",
98 result = ActivityView.render("activity.json", activity: activity)
100 expected = ":firefox: meow"
103 "<img class=\"emoji\" alt=\"firefox\" title=\"firefox\" src=\"http://localhost:4001/emoji/Firefox.gif\" /> meow"
105 assert result["summary"] == expected
106 assert result["summary_html"] == expected_html
109 test "a create activity with a summary containing invalid HTML" do
111 CommonAPI.post(insert(:user), %{
112 "spoiler_text" => "<span style=\"color: magenta; font-size: 32px;\">meow</span>",
116 result = ActivityView.render("activity.json", activity: activity)
120 assert result["summary"] == expected
121 assert result["summary_html"] == expected
124 test "a create activity with a note" do
126 other_user = insert(:user, %{nickname: "shp"})
128 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
129 object = Object.normalize(activity.data["object"])
131 result = ActivityView.render("activity.json", activity: activity)
133 convo_id = Utils.context_to_conversation_id(object.data["context"])
136 "activity_type" => "post",
139 UserView.render("show.json", %{user: other_user})
141 "created_at" => object.data["published"] |> Utils.date_to_asctime(),
142 "external_url" => object.data["id"],
144 "favorited" => false,
146 "in_reply_to_status_id" => nil,
147 "in_reply_to_screen_name" => nil,
148 "in_reply_to_user_id" => nil,
149 "in_reply_to_profileurl" => nil,
150 "in_reply_to_ostatus_uri" => nil,
152 "is_post_verb" => true,
153 "possibly_sensitive" => false,
157 "statusnet_conversation_id" => convo_id,
159 "summary_html" => "",
161 "Hey <span class=\"h-card\"><a data-user=\"#{other_user.id}\" class=\"u-url mention\" href=\"#{
163 }\">@<span>shp</span></a></span>!",
165 "text" => "Hey @shp!",
166 "uri" => object.data["id"],
167 "user" => UserView.render("show.json", %{user: user}),
168 "visibility" => "direct",
173 assert result == expected
176 test "a list of activities" do
178 other_user = insert(:user, %{nickname: "shp"})
179 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
180 object = Object.normalize(activity.data["object"])
182 convo_id = Utils.context_to_conversation_id(object.data["context"])
188 [context_to_conversation_id: fn _ -> false end]
193 [get_cached_by_ap_id: fn _ -> nil end]
198 [result] = ActivityView.render("index.json", activities: [activity])
200 assert result["statusnet_conversation_id"] == convo_id
201 assert result["user"]
202 refute called(Utils.context_to_conversation_id(:_))
203 refute called(User.get_cached_by_ap_id(user.ap_id))
204 refute called(User.get_cached_by_ap_id(other_user.ap_id))
208 test "an activity that is a reply" do
210 other_user = insert(:user, %{nickname: "shp"})
212 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
215 CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
217 result = ActivityView.render("activity.json", %{activity: answer})
219 assert result["in_reply_to_status_id"] == activity.id
222 test "a like activity" do
224 other_user = insert(:user, %{nickname: "shp"})
226 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
227 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
229 result = ActivityView.render("activity.json", activity: like)
230 activity = Pleroma.Activity.get_by_ap_id(activity.data["id"])
233 "activity_type" => "like",
234 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
235 "external_url" => like.data["id"],
237 "in_reply_to_status_id" => activity.id,
239 "is_post_verb" => false,
240 "favorited_status" => ActivityView.render("activity.json", activity: activity),
241 "statusnet_html" => "shp favorited a status.",
242 "text" => "shp favorited a status.",
243 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
244 "user" => UserView.render("show.json", user: other_user)
247 assert result == expected
250 test "a like activity for deleted post" do
252 other_user = insert(:user, %{nickname: "shp"})
254 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
255 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
256 CommonAPI.delete(activity.id, user)
258 result = ActivityView.render("activity.json", activity: like)
261 "activity_type" => "like",
262 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
263 "external_url" => like.data["id"],
265 "in_reply_to_status_id" => nil,
267 "is_post_verb" => false,
268 "favorited_status" => nil,
269 "statusnet_html" => "shp favorited a status.",
270 "text" => "shp favorited a status.",
271 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
272 "user" => UserView.render("show.json", user: other_user)
275 assert result == expected
278 test "an announce activity" do
280 other_user = insert(:user, %{nickname: "shp"})
282 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
283 {:ok, announce, object} = CommonAPI.repeat(activity.id, other_user)
285 convo_id = Utils.context_to_conversation_id(object.data["context"])
287 activity = Activity.get_by_id(activity.id)
289 result = ActivityView.render("activity.json", activity: announce)
292 "activity_type" => "repeat",
293 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
294 "external_url" => announce.data["id"],
297 "is_post_verb" => false,
298 "statusnet_html" => "shp retweeted a status.",
299 "text" => "shp retweeted a status.",
300 "uri" => "tag:#{announce.data["id"]}:objectType=note",
301 "user" => UserView.render("show.json", user: other_user),
302 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
303 "statusnet_conversation_id" => convo_id
306 assert result == expected
309 test "A follow activity" do
311 other_user = insert(:user, %{nickname: "shp"})
313 {:ok, follower} = User.follow(user, other_user)
314 {:ok, follow} = ActivityPub.follow(follower, other_user)
316 result = ActivityView.render("activity.json", activity: follow)
319 "activity_type" => "follow",
321 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
322 "external_url" => follow.data["id"],
324 "in_reply_to_status_id" => nil,
326 "is_post_verb" => false,
327 "statusnet_html" => "#{user.nickname} started following shp",
328 "text" => "#{user.nickname} started following shp",
329 "user" => UserView.render("show.json", user: user)
332 assert result == expected
335 test "a delete activity" do
338 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
339 {:ok, delete} = CommonAPI.delete(activity.id, user)
341 result = ActivityView.render("activity.json", activity: delete)
344 "activity_type" => "delete",
346 "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
347 "external_url" => delete.data["id"],
349 "in_reply_to_status_id" => nil,
351 "is_post_verb" => false,
352 "statusnet_html" => "deleted notice {{tag",
353 "text" => "deleted notice {{tag",
354 "uri" => delete.data["object"],
355 "user" => UserView.render("show.json", user: user)
358 assert result == expected
361 test "a peertube video" do
363 Pleroma.Object.Fetcher.fetch_object_from_id(
364 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
367 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
369 result = ActivityView.render("activity.json", activity: activity)
371 assert length(result["attachments"]) == 1
372 assert result["summary"] == "Friday Night"
375 test "special characters are not escaped in text field for status created" do
376 text = "<3 is on the way"
378 {:ok, activity} = CommonAPI.post(insert(:user), %{"status" => text})
380 result = ActivityView.render("activity.json", activity: activity)
382 assert result["text"] == text