1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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
16 test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
18 other_user = insert(:user)
20 note = insert(:note, user: user, data: %{"reactions" => [["👍", [other_user.ap_id], nil]]})
21 activity = insert(:note_activity, note: note, user: user)
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)
30 assert %{"id" => id} = result
31 assert to_string(activity.id) == id
33 assert result["pleroma"]["emoji_reactions"] == [
39 "account_ids" => [other_user.id]
42 "name" => "\u26A0\uFE0F",
46 "account_ids" => [other_user.id]
50 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
52 ObanHelpers.perform_all()
54 # Reacting with a custom emoji
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)
62 assert %{"id" => id} = result
63 assert to_string(activity.id) == id
65 assert result["pleroma"]["emoji_reactions"] == [
70 "url" => "http://localhost:4001/emoji/dino walking.gif",
71 "account_ids" => [other_user.id]
75 # Reacting with a remote emoji
79 data: %{"reactions" => [["wow", [other_user.ap_id], "https://remote/emoji/wow"]]}
82 activity = insert(:note_activity, note: note, user: user)
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:")
91 assert result["pleroma"]["emoji_reactions"] == [
93 "name" => "wow@remote",
96 "url" => "https://remote/emoji/wow",
97 "account_ids" => [user.id, other_user.id]
101 # Reacting with a remote custom emoji that hasn't been reacted with yet
107 activity = insert(:note_activity, note: note, user: user)
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)
115 # Reacting with a non-emoji
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)
123 test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
125 other_user = insert(:user)
130 data: %{"reactions" => [["wow", [user.ap_id], "https://remote/emoji/wow"]]}
133 activity = insert(:note_activity, note: note, user: user)
135 ObanHelpers.perform_all()
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:")
140 {:ok, _reaction_activity} =
141 CommonAPI.react_with_emoji(activity.id, other_user, ":wow@remote:")
143 ObanHelpers.perform_all()
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/☕")
151 assert %{"id" => id} = json_response_and_validate_schema(result, 200)
152 assert to_string(activity.id) == id
154 # Remove custom emoji
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:")
162 assert %{"id" => id} = json_response_and_validate_schema(result, 200)
163 assert to_string(activity.id) == id
165 ObanHelpers.perform_all()
167 object = Object.get_by_ap_id(activity.data["object"])
169 assert object.data["reaction_count"] == 2
171 # Remove custom remote emoji
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)
179 assert result["pleroma"]["emoji_reactions"] == [
181 "name" => "wow@remote",
184 "url" => "https://remote/emoji/wow",
185 "account_ids" => [user.id]
189 # Remove custom remote emoji that hasn't been reacted with yet
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)
197 test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
199 other_user = insert(:user)
200 doomed_user = insert(:user)
202 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
206 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
207 |> json_response_and_validate_schema(200)
211 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
212 {:ok, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
214 User.perform(:delete, doomed_user)
218 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
219 |> json_response_and_validate_schema(200)
221 [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
223 assert represented_user["id"] == other_user.id
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)
232 assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
236 test "GET /api/v1/pleroma/statuses/:id/reactions?with_muted=true", %{conn: conn} do
238 user2 = insert(:user)
239 user3 = insert(:user)
241 token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
243 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
245 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user2, "🎅")
246 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user3, "🎅")
250 |> assign(:user, user)
251 |> assign(:token, token)
252 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
253 |> json_response_and_validate_schema(200)
255 assert [%{"name" => "🎅", "count" => 2}] = result
257 User.mute(user, user3)
261 |> assign(:user, user)
262 |> assign(:token, token)
263 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
264 |> json_response_and_validate_schema(200)
266 assert [%{"name" => "🎅", "count" => 1}] = result
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)
275 assert [%{"name" => "🎅", "count" => 2}] = result
278 test "GET /api/v1/pleroma/statuses/:id/reactions with :show_reactions disabled", %{conn: conn} do
279 clear_config([:instance, :show_reactions], false)
282 other_user = insert(:user)
284 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
285 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
289 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
290 |> json_response_and_validate_schema(200)
295 test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
297 other_user = insert(:user)
299 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
303 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
304 |> json_response_and_validate_schema(200)
308 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
309 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
315 "accounts" => [represented_user],
321 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
322 |> json_response_and_validate_schema(200)
324 assert represented_user["id"] == other_user.id