logo

pleroma

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

list_controller.ex (3736B)


  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.ListController do
  5. use Pleroma.Web, :controller
  6. alias Pleroma.User
  7. alias Pleroma.Web.MastodonAPI.AccountView
  8. alias Pleroma.Web.Plugs.OAuthScopesPlug
  9. @oauth_read_actions [:index, :show, :list_accounts]
  10. plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
  11. plug(:list_by_id_and_user when action not in [:index, :create])
  12. plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action in @oauth_read_actions)
  13. plug(OAuthScopesPlug, %{scopes: ["write:lists"]} when action not in @oauth_read_actions)
  14. action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
  15. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ListOperation
  16. # GET /api/v1/lists
  17. def index(%{assigns: %{user: user}, private: %{open_api_spex: %{params: params}}} = conn, _) do
  18. lists = Pleroma.List.for_user(user, params)
  19. render(conn, "index.json", lists: lists)
  20. end
  21. # POST /api/v1/lists
  22. def create(
  23. %{assigns: %{user: user}, private: %{open_api_spex: %{body_params: %{title: title}}}} =
  24. conn,
  25. _
  26. ) do
  27. with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
  28. render(conn, "show.json", list: list)
  29. end
  30. end
  31. # GET /api/v1/lists/:idOB
  32. def show(%{assigns: %{list: list}} = conn, _) do
  33. render(conn, "show.json", list: list)
  34. end
  35. # PUT /api/v1/lists/:id
  36. def update(
  37. %{assigns: %{list: list}, private: %{open_api_spex: %{body_params: %{title: title}}}} =
  38. conn,
  39. _
  40. ) do
  41. with {:ok, list} <- Pleroma.List.rename(list, title) do
  42. render(conn, "show.json", list: list)
  43. end
  44. end
  45. # DELETE /api/v1/lists/:id
  46. def delete(%{assigns: %{list: list}} = conn, _) do
  47. with {:ok, _list} <- Pleroma.List.delete(list) do
  48. json(conn, %{})
  49. end
  50. end
  51. # GET /api/v1/lists/:id/accounts
  52. def list_accounts(%{assigns: %{user: user, list: list}} = conn, _) do
  53. with {:ok, users} <- Pleroma.List.get_following(list) do
  54. conn
  55. |> put_view(AccountView)
  56. |> render("index.json", for: user, users: users, as: :user)
  57. end
  58. end
  59. # POST /api/v1/lists/:id/accounts
  60. def add_to_list(
  61. %{
  62. assigns: %{list: list},
  63. private: %{open_api_spex: %{body_params: %{account_ids: account_ids}}}
  64. } = conn,
  65. _
  66. ) do
  67. Enum.each(account_ids, fn account_id ->
  68. with %User{} = followed <- User.get_cached_by_id(account_id) do
  69. Pleroma.List.follow(list, followed)
  70. end
  71. end)
  72. json(conn, %{})
  73. end
  74. # DELETE /api/v1/lists/:id/accounts
  75. def remove_from_list(
  76. %{
  77. private: %{open_api_spex: %{params: %{account_ids: account_ids}}}
  78. } = conn,
  79. _
  80. ) do
  81. do_remove_from_list(conn, account_ids)
  82. end
  83. def remove_from_list(
  84. %{private: %{open_api_spex: %{body_params: %{account_ids: account_ids}}}} = conn,
  85. _
  86. ) do
  87. do_remove_from_list(conn, account_ids)
  88. end
  89. defp do_remove_from_list(%{assigns: %{list: list}} = conn, account_ids) do
  90. Enum.each(account_ids, fn account_id ->
  91. with %User{} = followed <- User.get_cached_by_id(account_id) do
  92. Pleroma.List.unfollow(list, followed)
  93. end
  94. end)
  95. json(conn, %{})
  96. end
  97. defp list_by_id_and_user(
  98. %{assigns: %{user: user}, private: %{open_api_spex: %{params: %{id: id}}}} = conn,
  99. _
  100. ) do
  101. case Pleroma.List.get(id, user) do
  102. %Pleroma.List{} = list -> assign(conn, :list, list)
  103. nil -> conn |> render_error(:not_found, "List not found") |> halt()
  104. end
  105. end
  106. end