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 "a create activity with a html status" do
30 #Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg
33 {:ok, activity} = CommonAPI.post(insert(:user), %{"status" => text})
35 result = ActivityView.render("activity.json", activity: activity)
37 assert result["statusnet_html"] ==
38 "<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"
40 assert result["text"] ==
41 "#Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg"
44 test "a create activity with a summary containing emoji" do
46 CommonAPI.post(insert(:user), %{
47 "spoiler_text" => ":woollysocks: meow",
51 result = ActivityView.render("activity.json", activity: activity)
53 expected = ":woollysocks: meow"
56 "<img height=\"32px\" width=\"32px\" alt=\"woollysocks\" title=\"woollysocks\" src=\"http://localhost:4001/finmoji/128px/woollysocks-128.png\" /> meow"
58 assert result["summary"] == expected
59 assert result["summary_html"] == expected_html
62 test "a create activity with a summary containing invalid HTML" do
64 CommonAPI.post(insert(:user), %{
65 "spoiler_text" => "<span style=\"color: magenta; font-size: 32px;\">meow</span>",
69 result = ActivityView.render("activity.json", activity: activity)
73 assert result["summary"] == expected
74 assert result["summary_html"] == expected
77 test "a create activity with a note" do
79 other_user = insert(:user, %{nickname: "shp"})
81 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
83 result = ActivityView.render("activity.json", activity: activity)
85 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
88 "activity_type" => "post",
91 UserView.render("show.json", %{user: other_user})
93 "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
94 "external_url" => activity.data["object"]["id"],
98 "in_reply_to_status_id" => nil,
99 "in_reply_to_screen_name" => nil,
100 "in_reply_to_user_id" => nil,
101 "in_reply_to_profileurl" => nil,
102 "in_reply_to_ostatus_uri" => nil,
104 "is_post_verb" => true,
105 "possibly_sensitive" => false,
108 "statusnet_conversation_id" => convo_id,
110 "summary_html" => "",
112 "Hey <span><a data-user=\"#{other_user.id}\" href=\"#{other_user.ap_id}\">@<span>shp</span></a></span>!",
114 "text" => "Hey @shp!",
115 "uri" => activity.data["object"]["id"],
116 "user" => UserView.render("show.json", %{user: user}),
117 "visibility" => "direct"
120 assert result == expected
123 test "a list of activities" do
125 other_user = insert(:user, %{nickname: "shp"})
126 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
128 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
134 [context_to_conversation_id: fn _ -> false end]
139 [get_cached_by_ap_id: fn _ -> nil end]
144 [result] = ActivityView.render("index.json", activities: [activity])
146 assert result["statusnet_conversation_id"] == convo_id
147 assert result["user"]
148 refute called(TwitterAPI.context_to_conversation_id(:_))
149 refute called(User.get_cached_by_ap_id(user.ap_id))
150 refute called(User.get_cached_by_ap_id(other_user.ap_id))
154 test "an activity that is a reply" do
156 other_user = insert(:user, %{nickname: "shp"})
158 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
161 CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
163 result = ActivityView.render("activity.json", %{activity: answer})
165 assert result["in_reply_to_status_id"] == activity.id
168 test "a like activity" do
170 other_user = insert(:user, %{nickname: "shp"})
172 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
173 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
175 result = ActivityView.render("activity.json", activity: like)
176 activity = Pleroma.Activity.get_by_ap_id(activity.data["id"])
179 "activity_type" => "like",
180 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
181 "external_url" => like.data["id"],
183 "in_reply_to_status_id" => activity.id,
185 "is_post_verb" => false,
186 "favorited_status" => ActivityView.render("activity.json", activity: activity),
187 "statusnet_html" => "shp favorited a status.",
188 "text" => "shp favorited a status.",
189 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
190 "user" => UserView.render("show.json", user: other_user)
193 assert result == expected
196 test "a like activity for deleted post" do
198 other_user = insert(:user, %{nickname: "shp"})
200 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
201 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
202 CommonAPI.delete(activity.id, user)
204 result = ActivityView.render("activity.json", activity: like)
207 "activity_type" => "like",
208 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
209 "external_url" => like.data["id"],
211 "in_reply_to_status_id" => nil,
213 "is_post_verb" => false,
214 "favorited_status" => nil,
215 "statusnet_html" => "shp favorited a status.",
216 "text" => "shp favorited a status.",
217 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
218 "user" => UserView.render("show.json", user: other_user)
221 assert result == expected
224 test "an announce activity" do
226 other_user = insert(:user, %{nickname: "shp"})
228 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
229 {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)
231 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
233 activity = Repo.get(Activity, activity.id)
235 result = ActivityView.render("activity.json", activity: announce)
238 "activity_type" => "repeat",
239 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
240 "external_url" => announce.data["id"],
243 "is_post_verb" => false,
244 "statusnet_html" => "shp retweeted a status.",
245 "text" => "shp retweeted a status.",
246 "uri" => "tag:#{announce.data["id"]}:objectType=note",
247 "user" => UserView.render("show.json", user: other_user),
248 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
249 "statusnet_conversation_id" => convo_id
252 assert result == expected
255 test "A follow activity" do
257 other_user = insert(:user, %{nickname: "shp"})
259 {:ok, follower} = User.follow(user, other_user)
260 {:ok, follow} = ActivityPub.follow(follower, other_user)
262 result = ActivityView.render("activity.json", activity: follow)
265 "activity_type" => "follow",
267 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
268 "external_url" => follow.data["id"],
270 "in_reply_to_status_id" => nil,
272 "is_post_verb" => false,
273 "statusnet_html" => "#{user.nickname} started following shp",
274 "text" => "#{user.nickname} started following shp",
275 "user" => UserView.render("show.json", user: user)
278 assert result == expected
281 test "a delete activity" do
284 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
285 {:ok, delete} = CommonAPI.delete(activity.id, user)
287 result = ActivityView.render("activity.json", activity: delete)
290 "activity_type" => "delete",
292 "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
293 "external_url" => delete.data["id"],
295 "in_reply_to_status_id" => nil,
297 "is_post_verb" => false,
298 "statusnet_html" => "deleted notice {{tag",
299 "text" => "deleted notice {{tag",
300 "uri" => delete.data["object"],
301 "user" => UserView.render("show.json", user: user)
304 assert result == expected
307 test "a peertube video" do
309 ActivityPub.fetch_object_from_id(
310 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
313 %Activity{} = activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
315 result = ActivityView.render("activity.json", activity: activity)
317 assert length(result["attachments"]) == 1
318 assert result["summary"] == "Friday Night"