Merge pull request 'purge chat and shout endpoints' (#97) from purge-chat into develop
[akkoma] / lib / pleroma / web / mastodon_api / controllers / domain_block_controller.ex
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.MastodonAPI.DomainBlockController do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.User
9 alias Pleroma.Web.Plugs.OAuthScopesPlug
10
11 plug(Pleroma.Web.ApiSpec.CastAndValidate)
12 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.DomainBlockOperation
13
14 plug(
15 OAuthScopesPlug,
16 %{scopes: ["follow", "read:blocks"]} when action == :index
17 )
18
19 plug(
20 OAuthScopesPlug,
21 %{scopes: ["follow", "write:blocks"]} when action != :index
22 )
23
24 @doc "GET /api/v1/domain_blocks"
25 def index(%{assigns: %{user: user}} = conn, _) do
26 json(conn, Map.get(user, :domain_blocks, []))
27 end
28
29 @doc "POST /api/v1/domain_blocks"
30 def create(%{assigns: %{user: blocker}, body_params: %{domain: domain}} = conn, _params) do
31 User.block_domain(blocker, domain)
32 json(conn, %{})
33 end
34
35 def create(%{assigns: %{user: blocker}} = conn, %{domain: domain}) do
36 User.block_domain(blocker, domain)
37 json(conn, %{})
38 end
39
40 @doc "DELETE /api/v1/domain_blocks"
41 def delete(%{assigns: %{user: blocker}, body_params: %{domain: domain}} = conn, _params) do
42 User.unblock_domain(blocker, domain)
43 json(conn, %{})
44 end
45
46 def delete(%{assigns: %{user: blocker}} = conn, %{domain: domain}) do
47 User.unblock_domain(blocker, domain)
48 json(conn, %{})
49 end
50 end