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)
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 Enum.map([other_user, user], fn u -> UserView.render("show.json", %{user: u}) end),
123 "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
124 "external_url" => activity.data["object"]["id"],
126 "favorited" => false,
128 "in_reply_to_status_id" => nil,
129 "in_reply_to_screen_name" => nil,
130 "in_reply_to_user_id" => nil,
131 "in_reply_to_profileurl" => nil,
132 "in_reply_to_ostatus_uri" => nil,
134 "is_post_verb" => true,
135 "possibly_sensitive" => false,
139 "statusnet_conversation_id" => convo_id,
141 "summary_html" => "",
143 "Hey <span><a data-user=\"#{other_user.id}\" href=\"#{other_user.ap_id}\">@<span>shp</span></a></span>!",
145 "text" => "Hey @shp!",
146 "uri" => activity.data["object"]["id"],
147 "user" => UserView.render("show.json", %{user: user}),
148 "visibility" => "direct"
151 assert result == expected
154 test "a list of activities" do
156 other_user = insert(:user, %{nickname: "shp"})
157 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
159 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
165 [context_to_conversation_id: fn _ -> false end]
170 [get_cached_by_ap_id: fn _ -> nil end]
175 [result] = ActivityView.render("index.json", activities: [activity])
177 assert result["statusnet_conversation_id"] == convo_id
178 assert result["user"]
179 refute called(TwitterAPI.context_to_conversation_id(:_))
180 refute called(User.get_cached_by_ap_id(user.ap_id))
181 refute called(User.get_cached_by_ap_id(other_user.ap_id))
185 test "an activity that is a reply" do
187 other_user = insert(:user, %{nickname: "shp"})
189 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
192 CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
194 result = ActivityView.render("activity.json", %{activity: answer})
196 assert result["in_reply_to_status_id"] == activity.id
199 test "a like activity" do
201 other_user = insert(:user, %{nickname: "shp"})
203 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
204 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
206 result = ActivityView.render("activity.json", activity: like)
207 activity = Pleroma.Activity.get_by_ap_id(activity.data["id"])
210 "activity_type" => "like",
211 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
212 "external_url" => like.data["id"],
214 "in_reply_to_status_id" => activity.id,
216 "is_post_verb" => false,
217 "favorited_status" => ActivityView.render("activity.json", activity: activity),
218 "statusnet_html" => "shp favorited a status.",
219 "text" => "shp favorited a status.",
220 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
221 "user" => UserView.render("show.json", user: other_user)
224 assert result == expected
227 test "a like activity for deleted post" do
229 other_user = insert(:user, %{nickname: "shp"})
231 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
232 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
233 CommonAPI.delete(activity.id, user)
235 result = ActivityView.render("activity.json", activity: like)
238 "activity_type" => "like",
239 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
240 "external_url" => like.data["id"],
242 "in_reply_to_status_id" => nil,
244 "is_post_verb" => false,
245 "favorited_status" => nil,
246 "statusnet_html" => "shp favorited a status.",
247 "text" => "shp favorited a status.",
248 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
249 "user" => UserView.render("show.json", user: other_user)
252 assert result == expected
255 test "an announce activity" do
257 other_user = insert(:user, %{nickname: "shp"})
259 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
260 {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)
262 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
264 activity = Repo.get(Activity, activity.id)
266 result = ActivityView.render("activity.json", activity: announce)
269 "activity_type" => "repeat",
270 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
271 "external_url" => announce.data["id"],
274 "is_post_verb" => false,
275 "statusnet_html" => "shp retweeted a status.",
276 "text" => "shp retweeted a status.",
277 "uri" => "tag:#{announce.data["id"]}:objectType=note",
278 "user" => UserView.render("show.json", user: other_user),
279 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
280 "statusnet_conversation_id" => convo_id
283 assert result == expected
286 test "A follow activity" do
288 other_user = insert(:user, %{nickname: "shp"})
290 {:ok, follower} = User.follow(user, other_user)
291 {:ok, follow} = ActivityPub.follow(follower, other_user)
293 result = ActivityView.render("activity.json", activity: follow)
296 "activity_type" => "follow",
298 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
299 "external_url" => follow.data["id"],
301 "in_reply_to_status_id" => nil,
303 "is_post_verb" => false,
304 "statusnet_html" => "#{user.nickname} started following shp",
305 "text" => "#{user.nickname} started following shp",
306 "user" => UserView.render("show.json", user: user)
309 assert result == expected
312 test "a delete activity" do
315 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
316 {:ok, delete} = CommonAPI.delete(activity.id, user)
318 result = ActivityView.render("activity.json", activity: delete)
321 "activity_type" => "delete",
323 "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
324 "external_url" => delete.data["id"],
326 "in_reply_to_status_id" => nil,
328 "is_post_verb" => false,
329 "statusnet_html" => "deleted notice {{tag",
330 "text" => "deleted notice {{tag",
331 "uri" => delete.data["object"],
332 "user" => UserView.render("show.json", user: user)
335 assert result == expected
338 test "a peertube video" do
340 ActivityPub.fetch_object_from_id(
341 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
344 %Activity{} = activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
346 result = ActivityView.render("activity.json", activity: activity)
348 assert length(result["attachments"]) == 1
349 assert result["summary"] == "Friday Night"