Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.1KB

  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.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)
  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}} = conn, opts) do
  18. lists = Pleroma.List.for_user(user, opts)
  19. render(conn, "index.json", lists: lists)
  20. end
  21. # POST /api/v1/lists
  22. def create(%{assigns: %{user: user}, body_params: %{title: title}} = conn, _) do
  23. with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
  24. render(conn, "show.json", list: list)
  25. end
  26. end
  27. # GET /api/v1/lists/:id
  28. def show(%{assigns: %{list: list}} = conn, _) do
  29. render(conn, "show.json", list: list)
  30. end
  31. # PUT /api/v1/lists/:id
  32. def update(%{assigns: %{list: list}, body_params: %{title: title}} = conn, _) do
  33. with {:ok, list} <- Pleroma.List.rename(list, title) do
  34. render(conn, "show.json", list: list)
  35. end
  36. end
  37. # DELETE /api/v1/lists/:id
  38. def delete(%{assigns: %{list: list}} = conn, _) do
  39. with {:ok, _list} <- Pleroma.List.delete(list) do
  40. json(conn, %{})
  41. end
  42. end
  43. # GET /api/v1/lists/:id/accounts
  44. def list_accounts(%{assigns: %{user: user, list: list}} = conn, _) do
  45. with {:ok, users} <- Pleroma.List.get_following(list) do
  46. conn
  47. |> put_view(AccountView)
  48. |> render("index.json", for: user, users: users, as: :user)
  49. end
  50. end
  51. # POST /api/v1/lists/:id/accounts
  52. def add_to_list(%{assigns: %{list: list}, body_params: %{account_ids: account_ids}} = conn, _) do
  53. Enum.each(account_ids, fn account_id ->
  54. with %User{} = followed <- User.get_cached_by_id(account_id) do
  55. Pleroma.List.follow(list, followed)
  56. end
  57. end)
  58. json(conn, %{})
  59. end
  60. # DELETE /api/v1/lists/:id/accounts
  61. def remove_from_list(
  62. %{assigns: %{list: list}, params: %{account_ids: account_ids}} = conn,
  63. _
  64. ) do
  65. Enum.each(account_ids, fn account_id ->
  66. with %User{} = followed <- User.get_cached_by_id(account_id) do
  67. Pleroma.List.unfollow(list, followed)
  68. end
  69. end)
  70. json(conn, %{})
  71. end
  72. def remove_from_list(%{body_params: params} = conn, _) do
  73. remove_from_list(%{conn | params: params}, %{})
  74. end
  75. defp list_by_id_and_user(%{assigns: %{user: user}, params: %{id: id}} = conn, _) do
  76. case Pleroma.List.get(id, user) do
  77. %Pleroma.List{} = list -> assign(conn, :list, list)
  78. nil -> conn |> render_error(:not_found, "List not found") |> halt()
  79. end
  80. end
  81. end