1 defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do
4 alias Pleroma.Web.CommonAPI
5 alias Pleroma.Web.CommonAPI.Utils
6 alias Pleroma.Web.TwitterAPI.ActivityView
7 alias Pleroma.Web.TwitterAPI.UserView
8 alias Pleroma.Web.TwitterAPI.TwitterAPI
10 alias Pleroma.Activity
12 alias Pleroma.Web.ActivityPub.ActivityPub
14 import Pleroma.Factory
18 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
24 test "a create activity with a html status" do
26 #Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg
29 {:ok, activity} = CommonAPI.post(insert(:user), %{"status" => text})
31 result = ActivityView.render("activity.json", activity: activity)
33 assert result["statusnet_html"] ==
34 "<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"
36 assert result["text"] ==
37 "#Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg"
40 test "a create activity with a note" do
42 other_user = insert(:user, %{nickname: "shp"})
44 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
46 result = ActivityView.render("activity.json", activity: activity)
48 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
51 "activity_type" => "post",
54 UserView.render("show.json", %{user: other_user})
56 "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
57 "external_url" => activity.data["object"]["id"],
61 "in_reply_to_status_id" => nil,
62 "in_reply_to_screen_name" => nil,
63 "in_reply_to_user_id" => nil,
64 "in_reply_to_profileurl" => nil,
65 "in_reply_to_ostatus_uri" => nil,
67 "is_post_verb" => true,
68 "possibly_sensitive" => false,
71 "statusnet_conversation_id" => convo_id,
73 "Hey <span><a data-user=\"#{other_user.id}\" href=\"#{other_user.ap_id}\">@<span>shp</span></a></span>!",
75 "text" => "Hey @shp!",
76 "uri" => activity.data["object"]["id"],
77 "user" => UserView.render("show.json", %{user: user}),
78 "visibility" => "direct",
82 assert result == expected
85 test "a list of activities" do
87 other_user = insert(:user, %{nickname: "shp"})
88 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
90 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
96 [context_to_conversation_id: fn _ -> false end]
101 [get_cached_by_ap_id: fn _ -> nil end]
106 [result] = ActivityView.render("index.json", activities: [activity])
108 assert result["statusnet_conversation_id"] == convo_id
109 assert result["user"]
110 refute called(TwitterAPI.context_to_conversation_id(:_))
111 refute called(User.get_cached_by_ap_id(user.ap_id))
112 refute called(User.get_cached_by_ap_id(other_user.ap_id))
116 test "an activity that is a reply" do
118 other_user = insert(:user, %{nickname: "shp"})
120 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
123 CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
125 result = ActivityView.render("activity.json", %{activity: answer})
127 assert result["in_reply_to_status_id"] == activity.id
130 test "a like activity" do
132 other_user = insert(:user, %{nickname: "shp"})
134 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
135 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
137 result = ActivityView.render("activity.json", activity: like)
138 activity = Pleroma.Activity.get_by_ap_id(activity.data["id"])
141 "activity_type" => "like",
142 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
143 "external_url" => like.data["id"],
145 "in_reply_to_status_id" => activity.id,
147 "is_post_verb" => false,
148 "favorited_status" => ActivityView.render("activity.json", activity: activity),
149 "statusnet_html" => "shp favorited a status.",
150 "text" => "shp favorited a status.",
151 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
152 "user" => UserView.render("show.json", user: other_user)
155 assert result == expected
158 test "a like activity for deleted post" do
160 other_user = insert(:user, %{nickname: "shp"})
162 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
163 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
164 CommonAPI.delete(activity.id, user)
166 result = ActivityView.render("activity.json", activity: like)
169 "activity_type" => "like",
170 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
171 "external_url" => like.data["id"],
173 "in_reply_to_status_id" => nil,
175 "is_post_verb" => false,
176 "favorited_status" => nil,
177 "statusnet_html" => "shp favorited a status.",
178 "text" => "shp favorited a status.",
179 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
180 "user" => UserView.render("show.json", user: other_user)
183 assert result == expected
186 test "an announce activity" do
188 other_user = insert(:user, %{nickname: "shp"})
190 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
191 {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)
193 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
195 activity = Repo.get(Activity, activity.id)
197 result = ActivityView.render("activity.json", activity: announce)
200 "activity_type" => "repeat",
201 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
202 "external_url" => announce.data["id"],
205 "is_post_verb" => false,
206 "statusnet_html" => "shp retweeted a status.",
207 "text" => "shp retweeted a status.",
208 "uri" => "tag:#{announce.data["id"]}:objectType=note",
209 "user" => UserView.render("show.json", user: other_user),
210 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
211 "statusnet_conversation_id" => convo_id
214 assert result == expected
217 test "A follow activity" do
219 other_user = insert(:user, %{nickname: "shp"})
221 {:ok, follower} = User.follow(user, other_user)
222 {:ok, follow} = ActivityPub.follow(follower, other_user)
224 result = ActivityView.render("activity.json", activity: follow)
227 "activity_type" => "follow",
229 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
230 "external_url" => follow.data["id"],
232 "in_reply_to_status_id" => nil,
234 "is_post_verb" => false,
235 "statusnet_html" => "#{user.nickname} started following shp",
236 "text" => "#{user.nickname} started following shp",
237 "user" => UserView.render("show.json", user: user)
240 assert result == expected
243 test "a delete activity" do
246 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
247 {:ok, delete} = CommonAPI.delete(activity.id, user)
249 result = ActivityView.render("activity.json", activity: delete)
252 "activity_type" => "delete",
254 "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
255 "external_url" => delete.data["id"],
257 "in_reply_to_status_id" => nil,
259 "is_post_verb" => false,
260 "statusnet_html" => "deleted notice {{tag",
261 "text" => "deleted notice {{tag",
262 "uri" => delete.data["object"],
263 "user" => UserView.render("show.json", user: user)
266 assert result == expected