1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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 an error user for activities missing users" do
31 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
34 Cachex.clear(:user_cache)
36 result = ActivityView.render("activity.json", activity: activity)
40 test "tries to get a user by nickname if fetching by ap_id doesn't work" do
43 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
47 |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
50 Cachex.clear(:user_cache)
52 result = ActivityView.render("activity.json", activity: activity)
53 assert result["user"]["id"] == user.id
56 test "a create activity with a html status" do
58 #Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg
61 {:ok, activity} = CommonAPI.post(insert(:user), %{"status" => text})
63 result = ActivityView.render("activity.json", activity: activity)
65 assert result["statusnet_html"] ==
66 "<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"
68 assert result["text"] ==
69 "#Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg"
72 test "a create activity with a summary containing emoji" do
74 CommonAPI.post(insert(:user), %{
75 "spoiler_text" => ":woollysocks: meow",
79 result = ActivityView.render("activity.json", activity: activity)
82 "<img height=\"32px\" width=\"32px\" alt=\"woollysocks\" title=\"woollysocks\" src=\"http://localhost:4001/finmoji/128px/woollysocks-128.png\" /> meow"
84 assert result["summary"] == expected
87 test "a create activity with a summary containing invalid HTML" do
89 CommonAPI.post(insert(:user), %{
90 "spoiler_text" => "<span style=\"color: magenta; font-size: 32px;\">meow</span>",
94 result = ActivityView.render("activity.json", activity: activity)
98 assert result["summary"] == expected
101 test "a create activity with a note" do
103 other_user = insert(:user, %{nickname: "shp"})
105 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
107 result = ActivityView.render("activity.json", activity: activity)
109 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
112 "activity_type" => "post",
115 UserView.render("show.json", %{user: other_user})
117 "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
118 "external_url" => activity.data["object"]["id"],
120 "favorited" => false,
122 "in_reply_to_status_id" => nil,
123 "in_reply_to_screen_name" => nil,
124 "in_reply_to_user_id" => nil,
125 "in_reply_to_profileurl" => nil,
126 "in_reply_to_ostatus_uri" => nil,
128 "is_post_verb" => true,
129 "possibly_sensitive" => false,
132 "statusnet_conversation_id" => convo_id,
135 "Hey <span><a data-user=\"#{other_user.id}\" href=\"#{other_user.ap_id}\">@<span>shp</span></a></span>!",
137 "text" => "Hey @shp!",
138 "uri" => activity.data["object"]["id"],
139 "user" => UserView.render("show.json", %{user: user}),
140 "visibility" => "direct"
143 assert result == expected
146 test "a list of activities" do
148 other_user = insert(:user, %{nickname: "shp"})
149 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
151 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
157 [context_to_conversation_id: fn _ -> false end]
162 [get_cached_by_ap_id: fn _ -> nil end]
167 [result] = ActivityView.render("index.json", activities: [activity])
169 assert result["statusnet_conversation_id"] == convo_id
170 assert result["user"]
171 refute called(TwitterAPI.context_to_conversation_id(:_))
172 refute called(User.get_cached_by_ap_id(user.ap_id))
173 refute called(User.get_cached_by_ap_id(other_user.ap_id))
177 test "an activity that is a reply" do
179 other_user = insert(:user, %{nickname: "shp"})
181 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
184 CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
186 result = ActivityView.render("activity.json", %{activity: answer})
188 assert result["in_reply_to_status_id"] == activity.id
191 test "a like activity" do
193 other_user = insert(:user, %{nickname: "shp"})
195 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
196 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
198 result = ActivityView.render("activity.json", activity: like)
199 activity = Pleroma.Activity.get_by_ap_id(activity.data["id"])
202 "activity_type" => "like",
203 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
204 "external_url" => like.data["id"],
206 "in_reply_to_status_id" => activity.id,
208 "is_post_verb" => false,
209 "favorited_status" => ActivityView.render("activity.json", activity: activity),
210 "statusnet_html" => "shp favorited a status.",
211 "text" => "shp favorited a status.",
212 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
213 "user" => UserView.render("show.json", user: other_user)
216 assert result == expected
219 test "a like activity for deleted post" do
221 other_user = insert(:user, %{nickname: "shp"})
223 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
224 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
225 CommonAPI.delete(activity.id, user)
227 result = ActivityView.render("activity.json", activity: like)
230 "activity_type" => "like",
231 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
232 "external_url" => like.data["id"],
234 "in_reply_to_status_id" => nil,
236 "is_post_verb" => false,
237 "favorited_status" => nil,
238 "statusnet_html" => "shp favorited a status.",
239 "text" => "shp favorited a status.",
240 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
241 "user" => UserView.render("show.json", user: other_user)
244 assert result == expected
247 test "an announce activity" do
249 other_user = insert(:user, %{nickname: "shp"})
251 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
252 {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)
254 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
256 activity = Repo.get(Activity, activity.id)
258 result = ActivityView.render("activity.json", activity: announce)
261 "activity_type" => "repeat",
262 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
263 "external_url" => announce.data["id"],
266 "is_post_verb" => false,
267 "statusnet_html" => "shp retweeted a status.",
268 "text" => "shp retweeted a status.",
269 "uri" => "tag:#{announce.data["id"]}:objectType=note",
270 "user" => UserView.render("show.json", user: other_user),
271 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
272 "statusnet_conversation_id" => convo_id
275 assert result == expected
278 test "A follow activity" do
280 other_user = insert(:user, %{nickname: "shp"})
282 {:ok, follower} = User.follow(user, other_user)
283 {:ok, follow} = ActivityPub.follow(follower, other_user)
285 result = ActivityView.render("activity.json", activity: follow)
288 "activity_type" => "follow",
290 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
291 "external_url" => follow.data["id"],
293 "in_reply_to_status_id" => nil,
295 "is_post_verb" => false,
296 "statusnet_html" => "#{user.nickname} started following shp",
297 "text" => "#{user.nickname} started following shp",
298 "user" => UserView.render("show.json", user: user)
301 assert result == expected
304 test "a delete activity" do
307 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
308 {:ok, delete} = CommonAPI.delete(activity.id, user)
310 result = ActivityView.render("activity.json", activity: delete)
313 "activity_type" => "delete",
315 "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
316 "external_url" => delete.data["id"],
318 "in_reply_to_status_id" => nil,
320 "is_post_verb" => false,
321 "statusnet_html" => "deleted notice {{tag",
322 "text" => "deleted notice {{tag",
323 "uri" => delete.data["object"],
324 "user" => UserView.render("show.json", user: user)
327 assert result == expected
330 test "a peertube video" do
332 ActivityPub.fetch_object_from_id(
333 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
336 %Activity{} = activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
338 result = ActivityView.render("activity.json", activity: activity)
340 assert length(result["attachments"]) == 1
341 assert result["summary"] == "Friday Night"