1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.ListController do
6 use Pleroma.Web, :controller
8 alias Pleroma.Plugs.OAuthScopesPlug
10 alias Pleroma.Web.MastodonAPI.AccountView
12 plug(:list_by_id_and_user when action not in [:index, :create])
14 plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action in [:index, :show, :list_accounts])
18 %{scopes: ["write:lists"]}
19 when action in [:create, :update, :delete, :add_to_list, :remove_from_list]
22 plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
24 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
27 def index(%{assigns: %{user: user}} = conn, opts) do
28 lists = Pleroma.List.for_user(user, opts)
29 render(conn, "index.json", lists: lists)
33 def create(%{assigns: %{user: user}} = conn, %{"title" => title}) do
34 with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
35 render(conn, "show.json", list: list)
39 # GET /api/v1/lists/:id
40 def show(%{assigns: %{list: list}} = conn, _) do
41 render(conn, "show.json", list: list)
44 # PUT /api/v1/lists/:id
45 def update(%{assigns: %{list: list}} = conn, %{"title" => title}) do
46 with {:ok, list} <- Pleroma.List.rename(list, title) do
47 render(conn, "show.json", list: list)
51 # DELETE /api/v1/lists/:id
52 def delete(%{assigns: %{list: list}} = conn, _) do
53 with {:ok, _list} <- Pleroma.List.delete(list) do
58 # GET /api/v1/lists/:id/accounts
59 def list_accounts(%{assigns: %{user: user, list: list}} = conn, _) do
60 with {:ok, users} <- Pleroma.List.get_following(list) do
62 |> put_view(AccountView)
63 |> render("index.json", for: user, users: users, as: :user)
67 # POST /api/v1/lists/:id/accounts
68 def add_to_list(%{assigns: %{list: list}} = conn, %{"account_ids" => account_ids}) do
69 Enum.each(account_ids, fn account_id ->
70 with %User{} = followed <- User.get_cached_by_id(account_id) do
71 Pleroma.List.follow(list, followed)
78 # DELETE /api/v1/lists/:id/accounts
79 def remove_from_list(%{assigns: %{list: list}} = conn, %{"account_ids" => account_ids}) do
80 Enum.each(account_ids, fn account_id ->
81 with %User{} = followed <- User.get_cached_by_id(account_id) do
82 Pleroma.List.unfollow(list, followed)
89 defp list_by_id_and_user(%{assigns: %{user: user}, params: %{"id" => id}} = conn, _) do
90 case Pleroma.List.get(id, user) do
91 %Pleroma.List{} = list -> assign(conn, :list, list)
92 nil -> conn |> render_error(:not_found, "List not found") |> halt()