1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.PleromaAPI.EmojiReactionControllerTest do
6 use Oban.Testing, repo: Pleroma.Repo
7 use Pleroma.Web.ConnCase
10 alias Pleroma.Tests.ObanHelpers
12 alias Pleroma.Web.CommonAPI
14 import Pleroma.Factory
17 Mox.stub_with(Pleroma.UnstubbedConfigMock, Pleroma.Config)
21 test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
23 other_user = insert(:user)
25 note = insert(:note, user: user, data: %{"reactions" => [["👍", [other_user.ap_id], nil]]})
26 activity = insert(:note_activity, note: note, user: user)
30 |> assign(:user, other_user)
31 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
32 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/\u26A0")
33 |> json_response_and_validate_schema(200)
35 assert %{"id" => id} = result
36 assert to_string(activity.id) == id
38 assert result["pleroma"]["emoji_reactions"] == [
44 "account_ids" => [other_user.id]
47 "name" => "\u26A0\uFE0F",
51 "account_ids" => [other_user.id]
55 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
57 ObanHelpers.perform_all()
59 # Reacting with a custom emoji
62 |> assign(:user, other_user)
63 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
64 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:dinosaur:")
65 |> json_response_and_validate_schema(200)
67 assert %{"id" => id} = result
68 assert to_string(activity.id) == id
70 assert result["pleroma"]["emoji_reactions"] == [
75 "url" => "http://localhost:4001/emoji/dino walking.gif",
76 "account_ids" => [other_user.id]
80 # Reacting with a remote emoji
86 ["👍", [other_user.ap_id], nil],
87 ["wow", [other_user.ap_id], "https://remote/emoji/wow"]
92 activity = insert(:note_activity, note: note, user: user)
96 |> assign(:user, user)
97 |> assign(:token, insert(:oauth_token, user: user, scopes: ["write:statuses"]))
98 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:wow@remote:")
101 assert result["pleroma"]["emoji_reactions"] == [
103 "account_ids" => [other_user.id],
110 "name" => "wow@remote",
113 "url" => "https://remote/emoji/wow",
114 "account_ids" => [user.id, other_user.id]
118 # Reacting with a remote custom emoji that hasn't been reacted with yet
124 activity = insert(:note_activity, note: note, user: user)
127 |> assign(:user, user)
128 |> assign(:token, insert(:oauth_token, user: user, scopes: ["write:statuses"]))
129 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:wow@remote:")
130 |> json_response(400)
132 # Reacting with a non-emoji
134 |> assign(:user, other_user)
135 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
136 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/x")
137 |> json_response_and_validate_schema(400)
140 test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
142 other_user = insert(:user)
147 data: %{"reactions" => [["wow", [user.ap_id], "https://remote/emoji/wow"]]}
150 activity = insert(:note_activity, note: note, user: user)
152 ObanHelpers.perform_all()
154 {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
155 {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, ":dinosaur:")
157 {:ok, _reaction_activity} =
158 CommonAPI.react_with_emoji(activity.id, other_user, ":wow@remote:")
160 ObanHelpers.perform_all()
164 |> assign(:user, other_user)
165 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
166 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
168 assert %{"id" => id} = json_response_and_validate_schema(result, 200)
169 assert to_string(activity.id) == id
171 # Remove custom emoji
175 |> assign(:user, other_user)
176 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
177 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/:dinosaur:")
179 assert %{"id" => id} = json_response_and_validate_schema(result, 200)
180 assert to_string(activity.id) == id
182 ObanHelpers.perform_all()
184 object = Object.get_by_ap_id(activity.data["object"])
186 assert object.data["reaction_count"] == 2
188 # Remove custom remote emoji
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/:wow@remote:")
194 |> json_response(200)
196 assert result["pleroma"]["emoji_reactions"] == [
198 "name" => "wow@remote",
201 "url" => "https://remote/emoji/wow",
202 "account_ids" => [user.id]
206 # Remove custom remote emoji that hasn't been reacted with yet
208 |> assign(:user, other_user)
209 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
210 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/:zoop@remote:")
211 |> json_response(400)
214 test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
216 other_user = insert(:user)
217 doomed_user = insert(:user)
219 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
223 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
224 |> json_response_and_validate_schema(200)
228 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
229 {:ok, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
231 User.perform(:delete, doomed_user)
235 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
236 |> json_response_and_validate_schema(200)
238 [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
240 assert represented_user["id"] == other_user.id
244 |> assign(:user, other_user)
245 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"]))
246 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
247 |> json_response_and_validate_schema(200)
249 assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
253 test "GET /api/v1/pleroma/statuses/:id/reactions with legacy format", %{conn: conn} do
255 other_user = insert(:user)
261 "reactions" => [["😿", [other_user.ap_id]]]
265 activity = insert(:note_activity, user: user, note: note)
269 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
270 |> json_response_and_validate_schema(200)
272 other_user_id = other_user.id
280 "accounts" => [%{"id" => ^other_user_id}]
285 test "GET /api/v1/pleroma/statuses/:id/reactions?with_muted=true", %{conn: conn} do
287 user2 = insert(:user)
288 user3 = insert(:user)
290 token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
292 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
294 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user2, "🎅")
295 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user3, "🎅")
299 |> assign(:user, user)
300 |> assign(:token, token)
301 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
302 |> json_response_and_validate_schema(200)
304 assert [%{"name" => "🎅", "count" => 2}] = result
306 User.mute(user, user3)
310 |> assign(:user, user)
311 |> assign(:token, token)
312 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
313 |> json_response_and_validate_schema(200)
315 assert [%{"name" => "🎅", "count" => 1}] = result
319 |> assign(:user, user)
320 |> assign(:token, token)
321 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions?with_muted=true")
322 |> json_response_and_validate_schema(200)
324 assert [%{"name" => "🎅", "count" => 2}] = result
327 test "GET /api/v1/pleroma/statuses/:id/reactions with :show_reactions disabled", %{conn: conn} do
328 clear_config([:instance, :show_reactions], false)
331 other_user = insert(:user)
333 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
334 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
338 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
339 |> json_response_and_validate_schema(200)
344 test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
346 other_user = insert(:user)
348 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
352 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
353 |> json_response_and_validate_schema(200)
357 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
358 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
364 "accounts" => [represented_user],
370 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
371 |> json_response_and_validate_schema(200)
373 assert represented_user["id"] == other_user.id