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 class=\"hashtag\" 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 class=\"hashtag\" data-tag=\"cycling\" href=\"http://localhost:4001/tag/cycling\">#cycling</a> <a class=\"hashtag\" data-tag=\"chscycling\" href=\"http://localhost:4001/tag/chscycling\">#CHScycling</a> <a class=\"hashtag\" 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)
84 expected = ":woollysocks: meow"
87 "<img height=\"32px\" width=\"32px\" alt=\"woollysocks\" title=\"woollysocks\" src=\"http://localhost:4001/finmoji/128px/woollysocks-128.png\" /> meow"
89 assert result["summary"] == expected
90 assert result["summary_html"] == expected_html
93 test "a create activity with a summary containing invalid HTML" do
95 CommonAPI.post(insert(:user), %{
96 "spoiler_text" => "<span style=\"color: magenta; font-size: 32px;\">meow</span>",
100 result = ActivityView.render("activity.json", activity: activity)
104 assert result["summary"] == expected
105 assert result["summary_html"] == expected
108 test "a create activity with a note" do
110 other_user = insert(:user, %{nickname: "shp"})
112 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
114 result = ActivityView.render("activity.json", activity: activity)
116 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
119 "activity_type" => "post",
122 UserView.render("show.json", %{user: other_user})
124 "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
125 "external_url" => activity.data["object"]["id"],
127 "favorited" => false,
129 "in_reply_to_status_id" => nil,
130 "in_reply_to_screen_name" => nil,
131 "in_reply_to_user_id" => nil,
132 "in_reply_to_profileurl" => nil,
133 "in_reply_to_ostatus_uri" => nil,
135 "is_post_verb" => true,
136 "possibly_sensitive" => false,
140 "statusnet_conversation_id" => convo_id,
142 "summary_html" => "",
144 "Hey <span class=\"h-card\"><a data-user=\"#{other_user.id}\" class=\"u-url mention\" href=\"#{
146 }\">@<span>shp</span></a></span>!",
148 "text" => "Hey @shp!",
149 "uri" => activity.data["object"]["id"],
150 "user" => UserView.render("show.json", %{user: user}),
151 "visibility" => "direct"
154 assert result == expected
157 test "a list of activities" do
159 other_user = insert(:user, %{nickname: "shp"})
160 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
162 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
168 [context_to_conversation_id: fn _ -> false end]
173 [get_cached_by_ap_id: fn _ -> nil end]
178 [result] = ActivityView.render("index.json", activities: [activity])
180 assert result["statusnet_conversation_id"] == convo_id
181 assert result["user"]
182 refute called(TwitterAPI.context_to_conversation_id(:_))
183 refute called(User.get_cached_by_ap_id(user.ap_id))
184 refute called(User.get_cached_by_ap_id(other_user.ap_id))
188 test "an activity that is a reply" do
190 other_user = insert(:user, %{nickname: "shp"})
192 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
195 CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
197 result = ActivityView.render("activity.json", %{activity: answer})
199 assert result["in_reply_to_status_id"] == activity.id
202 test "a like activity" do
204 other_user = insert(:user, %{nickname: "shp"})
206 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
207 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
209 result = ActivityView.render("activity.json", activity: like)
210 activity = Pleroma.Activity.get_by_ap_id(activity.data["id"])
213 "activity_type" => "like",
214 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
215 "external_url" => like.data["id"],
217 "in_reply_to_status_id" => activity.id,
219 "is_post_verb" => false,
220 "favorited_status" => ActivityView.render("activity.json", activity: activity),
221 "statusnet_html" => "shp favorited a status.",
222 "text" => "shp favorited a status.",
223 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
224 "user" => UserView.render("show.json", user: other_user)
227 assert result == expected
230 test "a like activity for deleted post" do
232 other_user = insert(:user, %{nickname: "shp"})
234 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
235 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
236 CommonAPI.delete(activity.id, user)
238 result = ActivityView.render("activity.json", activity: like)
241 "activity_type" => "like",
242 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
243 "external_url" => like.data["id"],
245 "in_reply_to_status_id" => nil,
247 "is_post_verb" => false,
248 "favorited_status" => nil,
249 "statusnet_html" => "shp favorited a status.",
250 "text" => "shp favorited a status.",
251 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
252 "user" => UserView.render("show.json", user: other_user)
255 assert result == expected
258 test "an announce activity" do
260 other_user = insert(:user, %{nickname: "shp"})
262 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
263 {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)
265 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
267 activity = Repo.get(Activity, activity.id)
269 result = ActivityView.render("activity.json", activity: announce)
272 "activity_type" => "repeat",
273 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
274 "external_url" => announce.data["id"],
277 "is_post_verb" => false,
278 "statusnet_html" => "shp retweeted a status.",
279 "text" => "shp retweeted a status.",
280 "uri" => "tag:#{announce.data["id"]}:objectType=note",
281 "user" => UserView.render("show.json", user: other_user),
282 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
283 "statusnet_conversation_id" => convo_id
286 assert result == expected
289 test "A follow activity" do
291 other_user = insert(:user, %{nickname: "shp"})
293 {:ok, follower} = User.follow(user, other_user)
294 {:ok, follow} = ActivityPub.follow(follower, other_user)
296 result = ActivityView.render("activity.json", activity: follow)
299 "activity_type" => "follow",
301 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
302 "external_url" => follow.data["id"],
304 "in_reply_to_status_id" => nil,
306 "is_post_verb" => false,
307 "statusnet_html" => "#{user.nickname} started following shp",
308 "text" => "#{user.nickname} started following shp",
309 "user" => UserView.render("show.json", user: user)
312 assert result == expected
315 test "a delete activity" do
318 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
319 {:ok, delete} = CommonAPI.delete(activity.id, user)
321 result = ActivityView.render("activity.json", activity: delete)
324 "activity_type" => "delete",
326 "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
327 "external_url" => delete.data["id"],
329 "in_reply_to_status_id" => nil,
331 "is_post_verb" => false,
332 "statusnet_html" => "deleted notice {{tag",
333 "text" => "deleted notice {{tag",
334 "uri" => delete.data["object"],
335 "user" => UserView.render("show.json", user: user)
338 assert result == expected
341 test "a peertube video" do
343 ActivityPub.fetch_object_from_id(
344 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
347 %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"])
349 result = ActivityView.render("activity.json", activity: activity)
351 assert length(result["attachments"]) == 1
352 assert result["summary"] == "Friday Night"