Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

84 行
2.3KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 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. @doc "GET /api/v1/filters"
  17. def index(%{assigns: %{user: user}} = conn, _) do
  18. filters = Filter.get_filters(user)
  19. render(conn, "index.json", filters: filters)
  20. end
  21. @doc "POST /api/v1/filters"
  22. def create(%{assigns: %{user: user}, body_params: params} = conn, _) do
  23. query = %Filter{
  24. user_id: user.id,
  25. phrase: params.phrase,
  26. context: params.context,
  27. hide: params.irreversible,
  28. whole_word: params.whole_word
  29. # TODO: support `expires_in` parameter (as in Mastodon API)
  30. }
  31. {:ok, response} = Filter.create(query)
  32. render(conn, "show.json", filter: response)
  33. end
  34. @doc "GET /api/v1/filters/:id"
  35. def show(%{assigns: %{user: user}} = conn, %{id: filter_id}) do
  36. filter = Filter.get(filter_id, user)
  37. render(conn, "show.json", filter: filter)
  38. end
  39. @doc "PUT /api/v1/filters/:id"
  40. def update(
  41. %{assigns: %{user: user}, body_params: params} = conn,
  42. %{id: filter_id}
  43. ) do
  44. params =
  45. params
  46. |> Map.delete(:irreversible)
  47. |> Map.put(:hide, params[:irreversible])
  48. |> Enum.reject(fn {_key, value} -> is_nil(value) end)
  49. |> Map.new()
  50. # TODO: support `expires_in` parameter (as in Mastodon API)
  51. with %Filter{} = filter <- Filter.get(filter_id, user),
  52. {:ok, %Filter{} = filter} <- Filter.update(filter, params) do
  53. render(conn, "show.json", filter: filter)
  54. end
  55. end
  56. @doc "DELETE /api/v1/filters/:id"
  57. def delete(%{assigns: %{user: user}} = conn, %{id: filter_id}) do
  58. query = %Filter{
  59. user_id: user.id,
  60. filter_id: filter_id
  61. }
  62. {:ok, _} = Filter.delete(query)
  63. json(conn, %{})
  64. end
  65. end