X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=inline;f=lib%2Fpleroma%2Fweb%2Fmastodon_api%2Fcontrollers%2Ffilter_controller.ex;h=9b1ae809d33184e1204a969aef027f5c8d574231;hb=e5a7547fbe1c8eccafabc458e1cbec1461bcbc9c;hp=21dc374cd85407a62edfee8bdb267ae2f44d8741;hpb=46aae346f8530d4b9933b8e718e9578a96447f0a;p=akkoma
diff --git a/lib/pleroma/web/mastodon_api/controllers/filter_controller.ex b/lib/pleroma/web/mastodon_api/controllers/filter_controller.ex
index 21dc374cd..9b1ae809d 100644
--- a/lib/pleroma/web/mastodon_api/controllers/filter_controller.ex
+++ b/lib/pleroma/web/mastodon_api/controllers/filter_controller.ex
@@ -1,12 +1,12 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors
+# Copyright © 2017-2021 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.MastodonAPI.FilterController do
use Pleroma.Web, :controller
alias Pleroma.Filter
- alias Pleroma.Plugs.OAuthScopesPlug
+ alias Pleroma.Web.Plugs.OAuthScopesPlug
@oauth_read_actions [:show, :index]
@@ -17,9 +17,11 @@ defmodule Pleroma.Web.MastodonAPI.FilterController do
OAuthScopesPlug,
%{scopes: ["write:filters"]} when action not in @oauth_read_actions
)
-
+
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.FilterOperation
+ action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
+
@doc "GET /api/v1/filters"
def index(%{assigns: %{user: user}} = conn, _) do
filters = Filter.get_filters(user)
@@ -29,25 +31,23 @@ defmodule Pleroma.Web.MastodonAPI.FilterController do
@doc "POST /api/v1/filters"
def create(%{assigns: %{user: user}, body_params: params} = conn, _) do
- query = %Filter{
- user_id: user.id,
- phrase: params.phrase,
- context: params.context,
- hide: params.irreversible,
- whole_word: params.whole_word
- # TODO: support `expires_in` parameter (as in Mastodon API)
- }
-
- {:ok, response} = Filter.create(query)
-
- render(conn, "show.json", filter: response)
+ with {:ok, response} <-
+ params
+ |> Map.put(:user_id, user.id)
+ |> Map.put(:hide, params[:irreversible])
+ |> Map.delete(:irreversible)
+ |> Filter.create() do
+ render(conn, "show.json", filter: response)
+ end
end
@doc "GET /api/v1/filters/:id"
def show(%{assigns: %{user: user}} = conn, %{id: filter_id}) do
- filter = Filter.get(filter_id, user)
-
- render(conn, "show.json", filter: filter)
+ with %Filter{} = filter <- Filter.get(filter_id, user) do
+ render(conn, "show.json", filter: filter)
+ else
+ nil -> {:error, :not_found}
+ end
end
@doc "PUT /api/v1/filters/:id"
@@ -56,28 +56,31 @@ defmodule Pleroma.Web.MastodonAPI.FilterController do
%{id: filter_id}
) do
params =
- params
- |> Map.delete(:irreversible)
- |> Map.put(:hide, params[:irreversible])
- |> Enum.reject(fn {_key, value} -> is_nil(value) end)
- |> Map.new()
-
- # TODO: support `expires_in` parameter (as in Mastodon API)
+ if is_boolean(params[:irreversible]) do
+ params
+ |> Map.put(:hide, params[:irreversible])
+ |> Map.delete(:irreversible)
+ else
+ params
+ end
with %Filter{} = filter <- Filter.get(filter_id, user),
{:ok, %Filter{} = filter} <- Filter.update(filter, params) do
render(conn, "show.json", filter: filter)
+ else
+ nil -> {:error, :not_found}
+ error -> error
end
end
@doc "DELETE /api/v1/filters/:id"
def delete(%{assigns: %{user: user}} = conn, %{id: filter_id}) do
- query = %Filter{
- user_id: user.id,
- filter_id: filter_id
- }
-
- {:ok, _} = Filter.delete(query)
- json(conn, %{})
+ with %Filter{} = filter <- Filter.get(filter_id, user),
+ {:ok, _} <- Filter.delete(filter) do
+ json(conn, %{})
+ else
+ nil -> {:error, :not_found}
+ error -> error
+ end
end
end