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
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.CommonAPI.Utils
14 alias Pleroma.Web.TwitterAPI.ActivityView
15 alias Pleroma.Web.TwitterAPI.TwitterAPI
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" => ":woollysocks: meow",
98 result = ActivityView.render("activity.json", activity: activity)
100 expected = ":woollysocks: meow"
103 "<img height=\"32px\" width=\"32px\" alt=\"woollysocks\" title=\"woollysocks\" src=\"http://localhost:4001/finmoji/128px/woollysocks-128.png\" /> 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"})
130 result = ActivityView.render("activity.json", activity: activity)
132 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
135 "activity_type" => "post",
138 UserView.render("show.json", %{user: other_user})
140 "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
141 "external_url" => activity.data["object"]["id"],
143 "favorited" => false,
145 "in_reply_to_status_id" => nil,
146 "in_reply_to_screen_name" => nil,
147 "in_reply_to_user_id" => nil,
148 "in_reply_to_profileurl" => nil,
149 "in_reply_to_ostatus_uri" => nil,
151 "is_post_verb" => true,
152 "possibly_sensitive" => false,
156 "statusnet_conversation_id" => convo_id,
158 "summary_html" => "",
160 "Hey <span class=\"h-card\"><a data-user=\"#{other_user.id}\" class=\"u-url mention\" href=\"#{
162 }\">@<span>shp</span></a></span>!",
164 "text" => "Hey @shp!",
165 "uri" => activity.data["object"]["id"],
166 "user" => UserView.render("show.json", %{user: user}),
167 "visibility" => "direct",
172 assert result == expected
175 test "a list of activities" do
177 other_user = insert(:user, %{nickname: "shp"})
178 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
180 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
186 [context_to_conversation_id: fn _ -> false end]
191 [get_cached_by_ap_id: fn _ -> nil end]
196 [result] = ActivityView.render("index.json", activities: [activity])
198 assert result["statusnet_conversation_id"] == convo_id
199 assert result["user"]
200 refute called(TwitterAPI.context_to_conversation_id(:_))
201 refute called(User.get_cached_by_ap_id(user.ap_id))
202 refute called(User.get_cached_by_ap_id(other_user.ap_id))
206 test "an activity that is a reply" do
208 other_user = insert(:user, %{nickname: "shp"})
210 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
213 CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
215 result = ActivityView.render("activity.json", %{activity: answer})
217 assert result["in_reply_to_status_id"] == activity.id
220 test "a like activity" do
222 other_user = insert(:user, %{nickname: "shp"})
224 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
225 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
227 result = ActivityView.render("activity.json", activity: like)
228 activity = Pleroma.Activity.get_by_ap_id(activity.data["id"])
231 "activity_type" => "like",
232 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
233 "external_url" => like.data["id"],
235 "in_reply_to_status_id" => activity.id,
237 "is_post_verb" => false,
238 "favorited_status" => ActivityView.render("activity.json", activity: activity),
239 "statusnet_html" => "shp favorited a status.",
240 "text" => "shp favorited a status.",
241 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
242 "user" => UserView.render("show.json", user: other_user)
245 assert result == expected
248 test "a like activity for deleted post" do
250 other_user = insert(:user, %{nickname: "shp"})
252 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
253 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
254 CommonAPI.delete(activity.id, user)
256 result = ActivityView.render("activity.json", activity: like)
259 "activity_type" => "like",
260 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
261 "external_url" => like.data["id"],
263 "in_reply_to_status_id" => nil,
265 "is_post_verb" => false,
266 "favorited_status" => nil,
267 "statusnet_html" => "shp favorited a status.",
268 "text" => "shp favorited a status.",
269 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
270 "user" => UserView.render("show.json", user: other_user)
273 assert result == expected
276 test "an announce activity" do
278 other_user = insert(:user, %{nickname: "shp"})
280 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
281 {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)
283 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
285 activity = Repo.get(Activity, activity.id)
287 result = ActivityView.render("activity.json", activity: announce)
290 "activity_type" => "repeat",
291 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
292 "external_url" => announce.data["id"],
295 "is_post_verb" => false,
296 "statusnet_html" => "shp retweeted a status.",
297 "text" => "shp retweeted a status.",
298 "uri" => "tag:#{announce.data["id"]}:objectType=note",
299 "user" => UserView.render("show.json", user: other_user),
300 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
301 "statusnet_conversation_id" => convo_id
304 assert result == expected
307 test "A follow activity" do
309 other_user = insert(:user, %{nickname: "shp"})
311 {:ok, follower} = User.follow(user, other_user)
312 {:ok, follow} = ActivityPub.follow(follower, other_user)
314 result = ActivityView.render("activity.json", activity: follow)
317 "activity_type" => "follow",
319 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
320 "external_url" => follow.data["id"],
322 "in_reply_to_status_id" => nil,
324 "is_post_verb" => false,
325 "statusnet_html" => "#{user.nickname} started following shp",
326 "text" => "#{user.nickname} started following shp",
327 "user" => UserView.render("show.json", user: user)
330 assert result == expected
333 test "a delete activity" do
336 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
337 {:ok, delete} = CommonAPI.delete(activity.id, user)
339 result = ActivityView.render("activity.json", activity: delete)
342 "activity_type" => "delete",
344 "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
345 "external_url" => delete.data["id"],
347 "in_reply_to_status_id" => nil,
349 "is_post_verb" => false,
350 "statusnet_html" => "deleted notice {{tag",
351 "text" => "deleted notice {{tag",
352 "uri" => delete.data["object"],
353 "user" => UserView.render("show.json", user: user)
356 assert result == expected
359 test "a peertube video" do
361 ActivityPub.fetch_object_from_id(
362 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
365 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
367 result = ActivityView.render("activity.json", activity: activity)
369 assert length(result["attachments"]) == 1
370 assert result["summary"] == "Friday Night"