[#1234] Merge remote-tracking branch 'remotes/upstream/develop' into 1234-mastodon...
[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 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
23
24 # GET /api/v1/lists
25 def index(%{assigns: %{user: user}} = conn, opts) do
26 lists = Pleroma.List.for_user(user, opts)
27 render(conn, "index.json", lists: lists)
28 end
29
30 # POST /api/v1/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)
34 end
35 end
36
37 # GET /api/v1/lists/:id
38 def show(%{assigns: %{list: list}} = conn, _) do
39 render(conn, "show.json", list: list)
40 end
41
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)
46 end
47 end
48
49 # DELETE /api/v1/lists/:id
50 def delete(%{assigns: %{list: list}} = conn, _) do
51 with {:ok, _list} <- Pleroma.List.delete(list) do
52 json(conn, %{})
53 end
54 end
55
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
59 conn
60 |> put_view(AccountView)
61 |> render("accounts.json", for: user, users: users, as: :user)
62 end
63 end
64
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)
70 end
71 end)
72
73 json(conn, %{})
74 end
75
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)
81 end
82 end)
83
84 json(conn, %{})
85 end
86
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()
91 end
92 end
93 end