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
8 alias Pleroma.Web.CommonAPI
9 alias Pleroma.Web.CommonAPI.Utils
10 alias Pleroma.Web.TwitterAPI.ActivityView
11 alias Pleroma.Web.TwitterAPI.UserView
12 alias Pleroma.Web.TwitterAPI.TwitterAPI
14 alias Pleroma.Activity
16 alias Pleroma.Web.ActivityPub.ActivityPub
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 "a create activity with a html status" do
61 #Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg
64 {:ok, activity} = CommonAPI.post(insert(:user), %{"status" => text})
66 result = ActivityView.render("activity.json", activity: activity)
68 assert result["statusnet_html"] ==
69 "<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"
71 assert result["text"] ==
72 "#Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg"
75 test "a create activity with a summary containing emoji" do
77 CommonAPI.post(insert(:user), %{
78 "spoiler_text" => ":woollysocks: meow",
82 result = ActivityView.render("activity.json", activity: activity)
85 "<img height=\"32px\" width=\"32px\" alt=\"woollysocks\" title=\"woollysocks\" src=\"http://localhost:4001/finmoji/128px/woollysocks-128.png\" /> meow"
87 assert result["summary"] == expected
90 test "a create activity with a summary containing invalid HTML" do
92 CommonAPI.post(insert(:user), %{
93 "spoiler_text" => "<span style=\"color: magenta; font-size: 32px;\">meow</span>",
97 result = ActivityView.render("activity.json", activity: activity)
101 assert result["summary"] == expected
104 test "a create activity with a note" do
106 other_user = insert(:user, %{nickname: "shp"})
108 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
110 result = ActivityView.render("activity.json", activity: activity)
112 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
115 "activity_type" => "post",
118 UserView.render("show.json", %{user: other_user})
120 "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
121 "external_url" => activity.data["object"]["id"],
123 "favorited" => false,
125 "in_reply_to_status_id" => nil,
126 "in_reply_to_screen_name" => nil,
127 "in_reply_to_user_id" => nil,
128 "in_reply_to_profileurl" => nil,
129 "in_reply_to_ostatus_uri" => nil,
131 "is_post_verb" => true,
132 "possibly_sensitive" => false,
136 "statusnet_conversation_id" => convo_id,
139 "Hey <span><a data-user=\"#{other_user.id}\" href=\"#{other_user.ap_id}\">@<span>shp</span></a></span>!",
141 "text" => "Hey @shp!",
142 "uri" => activity.data["object"]["id"],
143 "user" => UserView.render("show.json", %{user: user}),
144 "visibility" => "direct"
147 assert result == expected
150 test "a list of activities" do
152 other_user = insert(:user, %{nickname: "shp"})
153 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
155 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
161 [context_to_conversation_id: fn _ -> false end]
166 [get_cached_by_ap_id: fn _ -> nil end]
171 [result] = ActivityView.render("index.json", activities: [activity])
173 assert result["statusnet_conversation_id"] == convo_id
174 assert result["user"]
175 refute called(TwitterAPI.context_to_conversation_id(:_))
176 refute called(User.get_cached_by_ap_id(user.ap_id))
177 refute called(User.get_cached_by_ap_id(other_user.ap_id))
181 test "an activity that is a reply" do
183 other_user = insert(:user, %{nickname: "shp"})
185 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
188 CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
190 result = ActivityView.render("activity.json", %{activity: answer})
192 assert result["in_reply_to_status_id"] == activity.id
195 test "a like activity" do
197 other_user = insert(:user, %{nickname: "shp"})
199 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
200 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
202 result = ActivityView.render("activity.json", activity: like)
203 activity = Pleroma.Activity.get_by_ap_id(activity.data["id"])
206 "activity_type" => "like",
207 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
208 "external_url" => like.data["id"],
210 "in_reply_to_status_id" => activity.id,
212 "is_post_verb" => false,
213 "favorited_status" => ActivityView.render("activity.json", activity: activity),
214 "statusnet_html" => "shp favorited a status.",
215 "text" => "shp favorited a status.",
216 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
217 "user" => UserView.render("show.json", user: other_user)
220 assert result == expected
223 test "a like activity for deleted post" do
225 other_user = insert(:user, %{nickname: "shp"})
227 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
228 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
229 CommonAPI.delete(activity.id, user)
231 result = ActivityView.render("activity.json", activity: like)
234 "activity_type" => "like",
235 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
236 "external_url" => like.data["id"],
238 "in_reply_to_status_id" => nil,
240 "is_post_verb" => false,
241 "favorited_status" => nil,
242 "statusnet_html" => "shp favorited a status.",
243 "text" => "shp favorited a status.",
244 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
245 "user" => UserView.render("show.json", user: other_user)
248 assert result == expected
251 test "an announce activity" do
253 other_user = insert(:user, %{nickname: "shp"})
255 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
256 {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)
258 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
260 activity = Repo.get(Activity, activity.id)
262 result = ActivityView.render("activity.json", activity: announce)
265 "activity_type" => "repeat",
266 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
267 "external_url" => announce.data["id"],
270 "is_post_verb" => false,
271 "statusnet_html" => "shp retweeted a status.",
272 "text" => "shp retweeted a status.",
273 "uri" => "tag:#{announce.data["id"]}:objectType=note",
274 "user" => UserView.render("show.json", user: other_user),
275 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
276 "statusnet_conversation_id" => convo_id
279 assert result == expected
282 test "A follow activity" do
284 other_user = insert(:user, %{nickname: "shp"})
286 {:ok, follower} = User.follow(user, other_user)
287 {:ok, follow} = ActivityPub.follow(follower, other_user)
289 result = ActivityView.render("activity.json", activity: follow)
292 "activity_type" => "follow",
294 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
295 "external_url" => follow.data["id"],
297 "in_reply_to_status_id" => nil,
299 "is_post_verb" => false,
300 "statusnet_html" => "#{user.nickname} started following shp",
301 "text" => "#{user.nickname} started following shp",
302 "user" => UserView.render("show.json", user: user)
305 assert result == expected
308 test "a delete activity" do
311 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
312 {:ok, delete} = CommonAPI.delete(activity.id, user)
314 result = ActivityView.render("activity.json", activity: delete)
317 "activity_type" => "delete",
319 "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
320 "external_url" => delete.data["id"],
322 "in_reply_to_status_id" => nil,
324 "is_post_verb" => false,
325 "statusnet_html" => "deleted notice {{tag",
326 "text" => "deleted notice {{tag",
327 "uri" => delete.data["object"],
328 "user" => UserView.render("show.json", user: user)
331 assert result == expected
334 test "a peertube video" do
336 ActivityPub.fetch_object_from_id(
337 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
340 %Activity{} = activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
342 result = ActivityView.render("activity.json", activity: activity)
344 assert length(result["attachments"]) == 1
345 assert result["summary"] == "Friday Night"