1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
25 def index(%{assigns: %{user: user}} = conn, opts) do
26 lists = Pleroma.List.for_user(user, opts)
27 render(conn, "index.json", lists: lists)
31 def create(%{assigns: %{user: user}} = conn, %{"title" => title}) do
32 with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
33 render(conn, "show.json", list: list)
37 # GET /api/v1/lists/:id
38 def show(%{assigns: %{list: list}} = conn, _) do
39 render(conn, "show.json", list: list)
42 # PUT /api/v1/lists/:id
43 def update(%{assigns: %{list: list}} = conn, %{"title" => title}) do
44 with {:ok, list} <- Pleroma.List.rename(list, title) do
45 render(conn, "show.json", list: list)
49 # DELETE /api/v1/lists/:id
50 def delete(%{assigns: %{list: list}} = conn, _) do
51 with {:ok, _list} <- Pleroma.List.delete(list) do
56 # GET /api/v1/lists/:id/accounts
57 def list_accounts(%{assigns: %{user: user, list: list}} = conn, _) do
58 with {:ok, users} <- Pleroma.List.get_following(list) do
60 |> put_view(AccountView)
61 |> render("accounts.json", for: user, users: users, as: :user)
65 # POST /api/v1/lists/:id/accounts
66 def add_to_list(%{assigns: %{list: list}} = conn, %{"account_ids" => account_ids}) 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)
76 # DELETE /api/v1/lists/:id/accounts
77 def remove_from_list(%{assigns: %{list: list}} = conn, %{"account_ids" => account_ids}) do
78 Enum.each(account_ids, fn account_id ->
79 with %User{} = followed <- User.get_cached_by_id(account_id) do
80 Pleroma.List.unfollow(list, followed)
87 defp list_by_id_and_user(%{assigns: %{user: user}, params: %{"id" => id}} = conn, _) do
88 case Pleroma.List.get(id, user) do
89 %Pleroma.List{} = list -> assign(conn, :list, list)
90 nil -> conn |> render_error(:not_found, "List not found") |> halt()