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.MastodonAPI.FilterController do
6 use Pleroma.Web, :controller
9 alias Pleroma.Web.Plugs.OAuthScopesPlug
11 @oauth_read_actions [:show, :index]
13 plug(Pleroma.Web.ApiSpec.CastAndValidate)
14 plug(OAuthScopesPlug, %{scopes: ["read:filters"]} when action in @oauth_read_actions)
18 %{scopes: ["write:filters"]} when action not in @oauth_read_actions
21 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.FilterOperation
23 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
25 @doc "GET /api/v1/filters"
26 def index(%{assigns: %{user: user}} = conn, _) do
27 filters = Filter.get_filters(user)
29 render(conn, "index.json", filters: filters)
32 @doc "POST /api/v1/filters"
33 def create(%{assigns: %{user: user}, body_params: params} = conn, _) do
34 with {:ok, response} <-
36 |> Map.put(:user_id, user.id)
37 |> Map.put(:hide, params[:irreversible])
38 |> Map.delete(:irreversible)
40 render(conn, "show.json", filter: response)
44 @doc "GET /api/v1/filters/:id"
45 def show(%{assigns: %{user: user}} = conn, %{id: filter_id}) do
46 with %Filter{} = filter <- Filter.get(filter_id, user) do
47 render(conn, "show.json", filter: filter)
49 nil -> {:error, :not_found}
53 @doc "PUT /api/v1/filters/:id"
55 %{assigns: %{user: user}, body_params: params} = conn,
59 if is_boolean(params[:irreversible]) do
61 |> Map.put(:hide, params[:irreversible])
62 |> Map.delete(:irreversible)
67 with %Filter{} = filter <- Filter.get(filter_id, user),
68 {:ok, %Filter{} = filter} <- Filter.update(filter, params) do
69 render(conn, "show.json", filter: filter)
71 nil -> {:error, :not_found}
76 @doc "DELETE /api/v1/filters/:id"
77 def delete(%{assigns: %{user: user}} = conn, %{id: filter_id}) do
78 with %Filter{} = filter <- Filter.get(filter_id, user),
79 {:ok, _} <- Filter.delete(filter) do
82 nil -> {:error, :not_found}