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, Object}
12 alias Pleroma.Web.ActivityPub.ActivityPub
14 import Pleroma.Factory
17 test "a create activity with a note" do
19 other_user = insert(:user, %{nickname: "shp"})
21 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
22 object = Object.normalize(activity.data["object"])
24 result = ActivityView.render("activity.json", activity: activity)
26 convo_id = TwitterAPI.context_to_conversation_id(object.data["context"])
29 "activity_type" => "post",
32 UserView.render("show.json", %{user: other_user})
34 "created_at" => object.data["published"] |> Utils.date_to_asctime(),
35 "external_url" => object.data["id"],
39 "in_reply_to_status_id" => nil,
40 "in_reply_to_screen_name" => nil,
41 "in_reply_to_user_id" => nil,
42 "in_reply_to_profileurl" => nil,
43 "in_reply_to_ostatus_uri" => nil,
45 "is_post_verb" => true,
46 "possibly_sensitive" => false,
49 "statusnet_conversation_id" => convo_id,
51 "Hey <span><a href=\"#{other_user.ap_id}\">@<span>shp</span></a></span>!",
53 "text" => "Hey @shp!",
54 "uri" => object.data["id"],
55 "user" => UserView.render("show.json", %{user: user}),
56 "visibility" => "direct",
60 assert result == expected
63 test "a list of activities" do
65 other_user = insert(:user, %{nickname: "shp"})
66 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
67 object = Object.normalize(activity.data["object"])
69 convo_id = TwitterAPI.context_to_conversation_id(object.data["context"])
75 [context_to_conversation_id: fn _ -> false end]
80 [get_cached_by_ap_id: fn _ -> nil end]
85 [result] = ActivityView.render("index.json", activities: [activity])
87 assert result["statusnet_conversation_id"] == convo_id
89 refute called(TwitterAPI.context_to_conversation_id(:_))
90 refute called(User.get_cached_by_ap_id(user.ap_id))
91 refute called(User.get_cached_by_ap_id(other_user.ap_id))
95 test "an activity that is a reply" do
97 other_user = insert(:user, %{nickname: "shp"})
99 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
102 CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
104 result = ActivityView.render("activity.json", %{activity: answer})
106 assert result["in_reply_to_status_id"] == activity.id
109 test "a like activity" do
111 other_user = insert(:user, %{nickname: "shp"})
113 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
114 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
116 result = ActivityView.render("activity.json", activity: like)
119 "activity_type" => "like",
120 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
121 "external_url" => like.data["id"],
123 "in_reply_to_status_id" => activity.id,
125 "is_post_verb" => false,
126 "statusnet_html" => "shp favorited a status.",
127 "text" => "shp favorited a status.",
128 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
129 "user" => UserView.render("show.json", user: other_user)
132 assert result == expected
135 test "a like activity for deleted post" do
137 other_user = insert(:user, %{nickname: "shp"})
139 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
140 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
141 CommonAPI.delete(activity.id, user)
143 result = ActivityView.render("activity.json", activity: like)
146 "activity_type" => "like",
147 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
148 "external_url" => like.data["id"],
150 "in_reply_to_status_id" => nil,
152 "is_post_verb" => false,
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 "an announce activity" do
164 other_user = insert(:user, %{nickname: "shp"})
166 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
167 {:ok, announce, object} = CommonAPI.repeat(activity.id, other_user)
169 convo_id = TwitterAPI.context_to_conversation_id(object.data["context"])
171 activity = Repo.get(Activity, activity.id)
173 result = ActivityView.render("activity.json", activity: announce)
176 "activity_type" => "repeat",
177 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
178 "external_url" => announce.data["id"],
181 "is_post_verb" => false,
182 "statusnet_html" => "shp retweeted a status.",
183 "text" => "shp retweeted a status.",
184 "uri" => "tag:#{announce.data["id"]}:objectType=note",
185 "user" => UserView.render("show.json", user: other_user),
186 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
187 "statusnet_conversation_id" => convo_id
190 assert result == expected
193 test "A follow activity" do
195 other_user = insert(:user, %{nickname: "shp"})
197 {:ok, follower} = User.follow(user, other_user)
198 {:ok, follow} = ActivityPub.follow(follower, other_user)
200 result = ActivityView.render("activity.json", activity: follow)
203 "activity_type" => "follow",
205 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
206 "external_url" => follow.data["id"],
208 "in_reply_to_status_id" => nil,
210 "is_post_verb" => false,
211 "statusnet_html" => "#{user.nickname} started following shp",
212 "text" => "#{user.nickname} started following shp",
213 "user" => UserView.render("show.json", user: user)
216 assert result == expected
219 test "a delete activity" do
222 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
223 {:ok, delete} = CommonAPI.delete(activity.id, user)
225 result = ActivityView.render("activity.json", activity: delete)
228 "activity_type" => "delete",
230 "created_at" => delete.data["published"] |> Utils.date_to_asctime(),
231 "external_url" => delete.data["id"],
233 "in_reply_to_status_id" => nil,
235 "is_post_verb" => false,
236 "statusnet_html" => "deleted notice {{tag",
237 "text" => "deleted notice {{tag",
238 "uri" => delete.data["object"],
239 "user" => UserView.render("show.json", user: user)
242 assert result == expected