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 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
24 |> assign(:user, other_user)
25 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
26 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
27 |> json_response_and_validate_schema(200)
29 # We return the status, but this our implementation detail.
30 assert %{"id" => id} = result
31 assert to_string(activity.id) == id
33 assert result["pleroma"]["emoji_reactions"] == [
34 %{"name" => "☕", "count" => 1, "me" => true, "url" => nil}
37 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
39 ObanHelpers.perform_all()
40 # Reacting with a custom emoji
43 |> assign(:user, other_user)
44 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
45 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:dinosaur:")
46 |> json_response_and_validate_schema(200)
48 # We return the status, but this our implementation detail.
49 assert %{"id" => id} = result
50 assert to_string(activity.id) == id
52 assert result["pleroma"]["emoji_reactions"] == [
57 "url" => "http://localhost:4001/emoji/dino walking.gif"
61 # Reacting with a non-emoji
63 |> assign(:user, other_user)
64 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
65 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/x")
66 |> json_response_and_validate_schema(400)
69 test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
71 other_user = insert(:user)
73 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
74 {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
75 {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, ":dinosaur:")
77 ObanHelpers.perform_all()
81 |> assign(:user, other_user)
82 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
83 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
85 assert %{"id" => id} = json_response_and_validate_schema(result, 200)
86 assert to_string(activity.id) == id
92 |> assign(:user, other_user)
93 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
94 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/:dinosaur:")
96 assert %{"id" => id} = json_response_and_validate_schema(result, 200)
97 assert to_string(activity.id) == id
99 ObanHelpers.perform_all()
101 object = Object.get_by_ap_id(activity.data["object"])
103 assert object.data["reaction_count"] == 0
106 test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
108 other_user = insert(:user)
109 doomed_user = insert(:user)
111 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
115 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
116 |> json_response_and_validate_schema(200)
120 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
121 {:ok, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
123 User.perform(:delete, doomed_user)
127 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
128 |> json_response_and_validate_schema(200)
130 [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
132 assert represented_user["id"] == other_user.id
136 |> assign(:user, other_user)
137 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"]))
138 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
139 |> json_response_and_validate_schema(200)
141 assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
145 test "GET /api/v1/pleroma/statuses/:id/reactions?with_muted=true", %{conn: conn} do
147 user2 = insert(:user)
148 user3 = insert(:user)
150 token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
152 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
154 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user2, "🎅")
155 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user3, "🎅")
159 |> assign(:user, user)
160 |> assign(:token, token)
161 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
162 |> json_response_and_validate_schema(200)
164 assert [%{"name" => "🎅", "count" => 2}] = result
166 User.mute(user, user3)
170 |> assign(:user, user)
171 |> assign(:token, token)
172 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
173 |> json_response_and_validate_schema(200)
175 assert [%{"name" => "🎅", "count" => 1}] = result
179 |> assign(:user, user)
180 |> assign(:token, token)
181 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions?with_muted=true")
182 |> json_response_and_validate_schema(200)
184 assert [%{"name" => "🎅", "count" => 2}] = result
187 test "GET /api/v1/pleroma/statuses/:id/reactions with :show_reactions disabled", %{conn: conn} do
188 clear_config([:instance, :show_reactions], false)
191 other_user = insert(:user)
193 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
194 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
198 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
199 |> json_response_and_validate_schema(200)
204 test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
206 other_user = insert(:user)
208 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
212 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
213 |> json_response_and_validate_schema(200)
217 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
218 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
224 "accounts" => [represented_user],
230 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
231 |> json_response_and_validate_schema(200)
233 assert represented_user["id"] == other_user.id