Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / lib / pleroma / web / mastodon_api / controllers / list_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.ListController do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.Plugs.OAuthScopesPlug
9 alias Pleroma.User
10 alias Pleroma.Web.MastodonAPI.AccountView
11
12 plug(:list_by_id_and_user when action not in [:index, :create])
13
14 plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action in [:index, :show, :list_accounts])
15
16 plug(
17 OAuthScopesPlug,
18 %{scopes: ["write:lists"]}
19 when action in [:create, :update, :delete, :add_to_list, :remove_from_list]
20 )
21
22 plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
23
24 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
25
26 # GET /api/v1/lists
27 def index(%{assigns: %{user: user}} = conn, opts) do
28 lists = Pleroma.List.for_user(user, opts)
29 render(conn, "index.json", lists: lists)
30 end
31
32 # POST /api/v1/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)
36 end
37 end
38
39 # GET /api/v1/lists/:id
40 def show(%{assigns: %{list: list}} = conn, _) do
41 render(conn, "show.json", list: list)
42 end
43
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)
48 end
49 end
50
51 # DELETE /api/v1/lists/:id
52 def delete(%{assigns: %{list: list}} = conn, _) do
53 with {:ok, _list} <- Pleroma.List.delete(list) do
54 json(conn, %{})
55 end
56 end
57
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
61 conn
62 |> put_view(AccountView)
63 |> render("index.json", for: user, users: users, as: :user)
64 end
65 end
66
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)
72 end
73 end)
74
75 json(conn, %{})
76 end
77
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)
83 end
84 end)
85
86 json(conn, %{})
87 end
88
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()
93 end
94 end
95 end