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 note" do
46 other_user = insert(:user, %{nickname: "shp"})
48 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
50 result = ActivityView.render("activity.json", activity: activity)
52 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
55 "activity_type" => "post",
58 UserView.render("show.json", %{user: other_user})
60 "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
61 "external_url" => activity.data["object"]["id"],
65 "in_reply_to_status_id" => nil,
66 "in_reply_to_screen_name" => nil,
67 "in_reply_to_user_id" => nil,
68 "in_reply_to_profileurl" => nil,
69 "in_reply_to_ostatus_uri" => nil,
71 "is_post_verb" => true,
72 "possibly_sensitive" => false,
75 "statusnet_conversation_id" => convo_id,
77 "Hey <span><a data-user=\"#{other_user.id}\" href=\"#{other_user.ap_id}\">@<span>shp</span></a></span>!",
79 "text" => "Hey @shp!",
80 "uri" => activity.data["object"]["id"],
81 "user" => UserView.render("show.json", %{user: user}),
82 "visibility" => "direct",
86 assert result == expected
89 test "a list of activities" do
91 other_user = insert(:user, %{nickname: "shp"})
92 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
94 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
100 [context_to_conversation_id: fn _ -> false end]
105 [get_cached_by_ap_id: fn _ -> nil end]
110 [result] = ActivityView.render("index.json", activities: [activity])
112 assert result["statusnet_conversation_id"] == convo_id
113 assert result["user"]
114 refute called(TwitterAPI.context_to_conversation_id(:_))
115 refute called(User.get_cached_by_ap_id(user.ap_id))
116 refute called(User.get_cached_by_ap_id(other_user.ap_id))
120 test "an activity that is a reply" do
122 other_user = insert(:user, %{nickname: "shp"})
124 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
127 CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
129 result = ActivityView.render("activity.json", %{activity: answer})
131 assert result["in_reply_to_status_id"] == activity.id
134 test "a like activity" do
136 other_user = insert(:user, %{nickname: "shp"})
138 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
139 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
141 result = ActivityView.render("activity.json", activity: like)
142 activity = Pleroma.Activity.get_by_ap_id(activity.data["id"])
145 "activity_type" => "like",
146 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
147 "external_url" => like.data["id"],
149 "in_reply_to_status_id" => activity.id,
151 "is_post_verb" => false,
152 "favorited_status" => ActivityView.render("activity.json", activity: activity),
153 "statusnet_html" => "shp favorited a status.",
154 "text" => "shp favorited a status.",
155 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
156 "user" => UserView.render("show.json", user: other_user)
159 assert result == expected
162 test "a like activity for deleted post" do
164 other_user = insert(:user, %{nickname: "shp"})
166 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
167 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
168 CommonAPI.delete(activity.id, user)
170 result = ActivityView.render("activity.json", activity: like)
173 "activity_type" => "like",
174 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
175 "external_url" => like.data["id"],
177 "in_reply_to_status_id" => nil,
179 "is_post_verb" => false,
180 "favorited_status" => nil,
181 "statusnet_html" => "shp favorited a status.",
182 "text" => "shp favorited a status.",
183 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
184 "user" => UserView.render("show.json", user: other_user)
187 assert result == expected
190 test "an announce activity" do
192 other_user = insert(:user, %{nickname: "shp"})
194 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
195 {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)
197 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
199 activity = Repo.get(Activity, activity.id)
201 result = ActivityView.render("activity.json", activity: announce)
204 "activity_type" => "repeat",
205 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
206 "external_url" => announce.data["id"],
209 "is_post_verb" => false,
210 "statusnet_html" => "shp retweeted a status.",
211 "text" => "shp retweeted a status.",
212 "uri" => "tag:#{announce.data["id"]}:objectType=note",
213 "user" => UserView.render("show.json", user: other_user),
214 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
215 "statusnet_conversation_id" => convo_id
218 assert result == expected
221 test "A follow activity" do
223 other_user = insert(:user, %{nickname: "shp"})
225 {:ok, follower} = User.follow(user, other_user)
226 {:ok, follow} = ActivityPub.follow(follower, other_user)
228 result = ActivityView.render("activity.json", activity: follow)
231 "activity_type" => "follow",
233 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
234 "external_url" => follow.data["id"],
236 "in_reply_to_status_id" => nil,
238 "is_post_verb" => false,
239 "statusnet_html" => "#{user.nickname} started following shp",
240 "text" => "#{user.nickname} started following shp",
241 "user" => UserView.render("show.json", user: user)
244 assert result == expected
247 test "a delete activity" do
250 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
251 {:ok, delete} = CommonAPI.delete(activity.id, user)
253 result = ActivityView.render("activity.json", activity: delete)
256 "activity_type" => "delete",
258 "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
259 "external_url" => delete.data["id"],
261 "in_reply_to_status_id" => nil,
263 "is_post_verb" => false,
264 "statusnet_html" => "deleted notice {{tag",
265 "text" => "deleted notice {{tag",
266 "uri" => delete.data["object"],
267 "user" => UserView.render("show.json", user: user)
270 assert result == expected
273 test "a peertube video" do
275 ActivityPub.fetch_object_from_id(
276 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
279 %Activity{} = activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
281 result = ActivityView.render("activity.json", activity: activity)
283 assert length(result["attachments"]) == 1
284 assert result["summary"] == "Friday Night"