logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

filter_controller.ex (2475B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.MastodonAPI.FilterController do
  5. use Pleroma.Web, :controller
  6. alias Pleroma.Filter
  7. alias Pleroma.Web.Plugs.OAuthScopesPlug
  8. @oauth_read_actions [:show, :index]
  9. plug(Pleroma.Web.ApiSpec.CastAndValidate)
  10. plug(OAuthScopesPlug, %{scopes: ["read:filters"]} when action in @oauth_read_actions)
  11. plug(
  12. OAuthScopesPlug,
  13. %{scopes: ["write:filters"]} when action not in @oauth_read_actions
  14. )
  15. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.FilterOperation
  16. action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
  17. @doc "GET /api/v1/filters"
  18. def index(%{assigns: %{user: user}} = conn, _) do
  19. filters = Filter.get_filters(user)
  20. render(conn, "index.json", filters: filters)
  21. end
  22. @doc "POST /api/v1/filters"
  23. def create(%{assigns: %{user: user}, body_params: params} = conn, _) do
  24. with {:ok, response} <-
  25. params
  26. |> Map.put(:user_id, user.id)
  27. |> Map.put(:hide, params[:irreversible])
  28. |> Map.delete(:irreversible)
  29. |> Filter.create() do
  30. render(conn, "show.json", filter: response)
  31. end
  32. end
  33. @doc "GET /api/v1/filters/:id"
  34. def show(%{assigns: %{user: user}} = conn, %{id: filter_id}) do
  35. with %Filter{} = filter <- Filter.get(filter_id, user) do
  36. render(conn, "show.json", filter: filter)
  37. else
  38. nil -> {:error, :not_found}
  39. end
  40. end
  41. @doc "PUT /api/v1/filters/:id"
  42. def update(
  43. %{assigns: %{user: user}, body_params: params} = conn,
  44. %{id: filter_id}
  45. ) do
  46. params =
  47. if is_boolean(params[:irreversible]) do
  48. params
  49. |> Map.put(:hide, params[:irreversible])
  50. |> Map.delete(:irreversible)
  51. else
  52. params
  53. end
  54. with %Filter{} = filter <- Filter.get(filter_id, user),
  55. {:ok, %Filter{} = filter} <- Filter.update(filter, params) do
  56. render(conn, "show.json", filter: filter)
  57. else
  58. nil -> {:error, :not_found}
  59. error -> error
  60. end
  61. end
  62. @doc "DELETE /api/v1/filters/:id"
  63. def delete(%{assigns: %{user: user}} = conn, %{id: filter_id}) do
  64. with %Filter{} = filter <- Filter.get(filter_id, user),
  65. {:ok, _} <- Filter.delete(filter) do
  66. json(conn, %{})
  67. else
  68. nil -> {:error, :not_found}
  69. error -> error
  70. end
  71. end
  72. end