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)
54 "<img height=\"32px\" width=\"32px\" alt=\"woollysocks\" title=\"woollysocks\" src=\"http://localhost:4001/finmoji/128px/woollysocks-128.png\" /> meow"
56 assert result["summary"] == expected
59 test "a create activity with a summary containing invalid HTML" do
61 CommonAPI.post(insert(:user), %{
62 "spoiler_text" => "<span style=\"color: magenta; font-size: 32px;\">meow</span>",
66 result = ActivityView.render("activity.json", activity: activity)
70 assert result["summary"] == expected
73 test "a create activity with a note" do
75 other_user = insert(:user, %{nickname: "shp"})
77 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
79 result = ActivityView.render("activity.json", activity: activity)
81 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
84 "activity_type" => "post",
87 UserView.render("show.json", %{user: other_user})
89 "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
90 "external_url" => activity.data["object"]["id"],
94 "in_reply_to_status_id" => nil,
95 "in_reply_to_screen_name" => nil,
96 "in_reply_to_user_id" => nil,
97 "in_reply_to_profileurl" => nil,
98 "in_reply_to_ostatus_uri" => nil,
100 "is_post_verb" => true,
101 "possibly_sensitive" => false,
104 "statusnet_conversation_id" => convo_id,
107 "Hey <span><a data-user=\"#{other_user.id}\" href=\"#{other_user.ap_id}\">@<span>shp</span></a></span>!",
109 "text" => "Hey @shp!",
110 "uri" => activity.data["object"]["id"],
111 "user" => UserView.render("show.json", %{user: user}),
112 "visibility" => "direct"
115 assert result == expected
118 test "a list of activities" do
120 other_user = insert(:user, %{nickname: "shp"})
121 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
123 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
129 [context_to_conversation_id: fn _ -> false end]
134 [get_cached_by_ap_id: fn _ -> nil end]
139 [result] = ActivityView.render("index.json", activities: [activity])
141 assert result["statusnet_conversation_id"] == convo_id
142 assert result["user"]
143 refute called(TwitterAPI.context_to_conversation_id(:_))
144 refute called(User.get_cached_by_ap_id(user.ap_id))
145 refute called(User.get_cached_by_ap_id(other_user.ap_id))
149 test "an activity that is a reply" do
151 other_user = insert(:user, %{nickname: "shp"})
153 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
156 CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
158 result = ActivityView.render("activity.json", %{activity: answer})
160 assert result["in_reply_to_status_id"] == activity.id
163 test "a like activity" do
165 other_user = insert(:user, %{nickname: "shp"})
167 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
168 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
170 result = ActivityView.render("activity.json", activity: like)
171 activity = Pleroma.Activity.get_by_ap_id(activity.data["id"])
174 "activity_type" => "like",
175 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
176 "external_url" => like.data["id"],
178 "in_reply_to_status_id" => activity.id,
180 "is_post_verb" => false,
181 "favorited_status" => ActivityView.render("activity.json", activity: activity),
182 "statusnet_html" => "shp favorited a status.",
183 "text" => "shp favorited a status.",
184 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
185 "user" => UserView.render("show.json", user: other_user)
188 assert result == expected
191 test "a like activity for deleted post" 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)
197 CommonAPI.delete(activity.id, user)
199 result = ActivityView.render("activity.json", activity: like)
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" => nil,
208 "is_post_verb" => false,
209 "favorited_status" => nil,
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 "an announce activity" do
221 other_user = insert(:user, %{nickname: "shp"})
223 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
224 {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)
226 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
228 activity = Repo.get(Activity, activity.id)
230 result = ActivityView.render("activity.json", activity: announce)
233 "activity_type" => "repeat",
234 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
235 "external_url" => announce.data["id"],
238 "is_post_verb" => false,
239 "statusnet_html" => "shp retweeted a status.",
240 "text" => "shp retweeted a status.",
241 "uri" => "tag:#{announce.data["id"]}:objectType=note",
242 "user" => UserView.render("show.json", user: other_user),
243 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
244 "statusnet_conversation_id" => convo_id
247 assert result == expected
250 test "A follow activity" do
252 other_user = insert(:user, %{nickname: "shp"})
254 {:ok, follower} = User.follow(user, other_user)
255 {:ok, follow} = ActivityPub.follow(follower, other_user)
257 result = ActivityView.render("activity.json", activity: follow)
260 "activity_type" => "follow",
262 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
263 "external_url" => follow.data["id"],
265 "in_reply_to_status_id" => nil,
267 "is_post_verb" => false,
268 "statusnet_html" => "#{user.nickname} started following shp",
269 "text" => "#{user.nickname} started following shp",
270 "user" => UserView.render("show.json", user: user)
273 assert result == expected
276 test "a delete activity" do
279 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
280 {:ok, delete} = CommonAPI.delete(activity.id, user)
282 result = ActivityView.render("activity.json", activity: delete)
285 "activity_type" => "delete",
287 "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
288 "external_url" => delete.data["id"],
290 "in_reply_to_status_id" => nil,
292 "is_post_verb" => false,
293 "statusnet_html" => "deleted notice {{tag",
294 "text" => "deleted notice {{tag",
295 "uri" => delete.data["object"],
296 "user" => UserView.render("show.json", user: user)
299 assert result == expected
302 test "a peertube video" do
304 ActivityPub.fetch_object_from_id(
305 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
308 %Activity{} = activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
310 result = ActivityView.render("activity.json", activity: activity)
312 assert length(result["attachments"]) == 1
313 assert result["summary"] == "Friday Night"