Allow reacting with remote emoji when they exist on the post (#200)
[akkoma] / test / pleroma / web / pleroma_api / controllers / emoji_reaction_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.EmojiReactionControllerTest do
6 use Oban.Testing, repo: Pleroma.Repo
7 use Pleroma.Web.ConnCase
8
9 alias Pleroma.Object
10 alias Pleroma.Tests.ObanHelpers
11 alias Pleroma.User
12 alias Pleroma.Web.CommonAPI
13
14 import Pleroma.Factory
15
16 test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
17 user = insert(:user)
18 other_user = insert(:user)
19
20 note = insert(:note, user: user, data: %{"reactions" => [["👍", [other_user.ap_id], nil]]})
21 activity = insert(:note_activity, note: note, user: user)
22
23 result =
24 conn
25 |> assign(:user, other_user)
26 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
27 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/\u26A0")
28 |> json_response_and_validate_schema(200)
29
30 assert %{"id" => id} = result
31 assert to_string(activity.id) == id
32
33 assert result["pleroma"]["emoji_reactions"] == [
34 %{
35 "name" => "👍",
36 "count" => 1,
37 "me" => true,
38 "url" => nil,
39 "account_ids" => [other_user.id]
40 },
41 %{
42 "name" => "\u26A0\uFE0F",
43 "count" => 1,
44 "me" => true,
45 "url" => nil,
46 "account_ids" => [other_user.id]
47 }
48 ]
49
50 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
51
52 ObanHelpers.perform_all()
53
54 # Reacting with a custom emoji
55 result =
56 conn
57 |> assign(:user, other_user)
58 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
59 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:dinosaur:")
60 |> json_response_and_validate_schema(200)
61
62 assert %{"id" => id} = result
63 assert to_string(activity.id) == id
64
65 assert result["pleroma"]["emoji_reactions"] == [
66 %{
67 "name" => "dinosaur",
68 "count" => 1,
69 "me" => true,
70 "url" => "http://localhost:4001/emoji/dino walking.gif",
71 "account_ids" => [other_user.id]
72 }
73 ]
74
75 # Reacting with a remote emoji
76 note =
77 insert(:note,
78 user: user,
79 data: %{"reactions" => [["wow", [other_user.ap_id], "https://remote/emoji/wow"]]}
80 )
81
82 activity = insert(:note_activity, note: note, user: user)
83
84 result =
85 conn
86 |> assign(:user, user)
87 |> assign(:token, insert(:oauth_token, user: user, scopes: ["write:statuses"]))
88 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:wow@remote:")
89 |> json_response(200)
90
91 assert result["pleroma"]["emoji_reactions"] == [
92 %{
93 "name" => "wow@remote",
94 "count" => 2,
95 "me" => true,
96 "url" => "https://remote/emoji/wow",
97 "account_ids" => [user.id, other_user.id]
98 }
99 ]
100
101 # Reacting with a remote custom emoji that hasn't been reacted with yet
102 note =
103 insert(:note,
104 user: user
105 )
106
107 activity = insert(:note_activity, note: note, user: user)
108
109 assert conn
110 |> assign(:user, user)
111 |> assign(:token, insert(:oauth_token, user: user, scopes: ["write:statuses"]))
112 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:wow@remote:")
113 |> json_response(400)
114
115 # Reacting with a non-emoji
116 assert conn
117 |> assign(:user, other_user)
118 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
119 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/x")
120 |> json_response_and_validate_schema(400)
121 end
122
123 test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
124 user = insert(:user)
125 other_user = insert(:user)
126
127 note =
128 insert(:note,
129 user: user,
130 data: %{"reactions" => [["wow", [user.ap_id], "https://remote/emoji/wow"]]}
131 )
132
133 activity = insert(:note_activity, note: note, user: user)
134
135 ObanHelpers.perform_all()
136
137 {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
138 {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, ":dinosaur:")
139
140 {:ok, _reaction_activity} =
141 CommonAPI.react_with_emoji(activity.id, other_user, ":wow@remote:")
142
143 ObanHelpers.perform_all()
144
145 result =
146 conn
147 |> assign(:user, other_user)
148 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
149 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
150
151 assert %{"id" => id} = json_response_and_validate_schema(result, 200)
152 assert to_string(activity.id) == id
153
154 # Remove custom emoji
155
156 result =
157 conn
158 |> assign(:user, other_user)
159 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
160 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/:dinosaur:")
161
162 assert %{"id" => id} = json_response_and_validate_schema(result, 200)
163 assert to_string(activity.id) == id
164
165 ObanHelpers.perform_all()
166
167 object = Object.get_by_ap_id(activity.data["object"])
168
169 assert object.data["reaction_count"] == 2
170
171 # Remove custom remote emoji
172 result =
173 conn
174 |> assign(:user, other_user)
175 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
176 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/:wow@remote:")
177 |> json_response(200)
178
179 assert result["pleroma"]["emoji_reactions"] == [
180 %{
181 "name" => "wow@remote",
182 "count" => 1,
183 "me" => false,
184 "url" => "https://remote/emoji/wow",
185 "account_ids" => [user.id]
186 }
187 ]
188
189 # Remove custom remote emoji that hasn't been reacted with yet
190 assert conn
191 |> assign(:user, other_user)
192 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
193 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/:zoop@remote:")
194 |> json_response(400)
195 end
196
197 test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
198 user = insert(:user)
199 other_user = insert(:user)
200 doomed_user = insert(:user)
201
202 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
203
204 result =
205 conn
206 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
207 |> json_response_and_validate_schema(200)
208
209 assert result == []
210
211 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
212 {:ok, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
213
214 User.perform(:delete, doomed_user)
215
216 result =
217 conn
218 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
219 |> json_response_and_validate_schema(200)
220
221 [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
222
223 assert represented_user["id"] == other_user.id
224
225 result =
226 conn
227 |> assign(:user, other_user)
228 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"]))
229 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
230 |> json_response_and_validate_schema(200)
231
232 assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
233 result
234 end
235
236 test "GET /api/v1/pleroma/statuses/:id/reactions?with_muted=true", %{conn: conn} do
237 user = insert(:user)
238 user2 = insert(:user)
239 user3 = insert(:user)
240
241 token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
242
243 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
244
245 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user2, "🎅")
246 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user3, "🎅")
247
248 result =
249 conn
250 |> assign(:user, user)
251 |> assign(:token, token)
252 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
253 |> json_response_and_validate_schema(200)
254
255 assert [%{"name" => "🎅", "count" => 2}] = result
256
257 User.mute(user, user3)
258
259 result =
260 conn
261 |> assign(:user, user)
262 |> assign(:token, token)
263 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
264 |> json_response_and_validate_schema(200)
265
266 assert [%{"name" => "🎅", "count" => 1}] = result
267
268 result =
269 conn
270 |> assign(:user, user)
271 |> assign(:token, token)
272 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions?with_muted=true")
273 |> json_response_and_validate_schema(200)
274
275 assert [%{"name" => "🎅", "count" => 2}] = result
276 end
277
278 test "GET /api/v1/pleroma/statuses/:id/reactions with :show_reactions disabled", %{conn: conn} do
279 clear_config([:instance, :show_reactions], false)
280
281 user = insert(:user)
282 other_user = insert(:user)
283
284 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
285 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
286
287 result =
288 conn
289 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
290 |> json_response_and_validate_schema(200)
291
292 assert result == []
293 end
294
295 test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
296 user = insert(:user)
297 other_user = insert(:user)
298
299 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
300
301 result =
302 conn
303 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
304 |> json_response_and_validate_schema(200)
305
306 assert result == []
307
308 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
309 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
310
311 assert [
312 %{
313 "name" => "🎅",
314 "count" => 1,
315 "accounts" => [represented_user],
316 "me" => false,
317 "url" => nil
318 }
319 ] =
320 conn
321 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
322 |> json_response_and_validate_schema(200)
323
324 assert represented_user["id"] == other_user.id
325 end
326 end