Merge branch 'update-elixir' into 'develop'
[akkoma] / lib / pleroma / web / admin_api / controllers / instance_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.AdminAPI.InstanceController do
6 use Pleroma.Web, :controller
7
8 import Pleroma.Web.ControllerHelper, only: [fetch_integer_param: 3]
9
10 alias Pleroma.Instances.Instance
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.AdminAPI
13 alias Pleroma.Web.Plugs.OAuthScopesPlug
14
15 require Logger
16
17 @default_page_size 50
18
19 plug(
20 OAuthScopesPlug,
21 %{scopes: ["admin:read:statuses"]}
22 when action in [:list_statuses]
23 )
24
25 plug(
26 OAuthScopesPlug,
27 %{scopes: ["admin:write:accounts", "admin:write:statuses"]}
28 when action in [:delete]
29 )
30
31 action_fallback(AdminAPI.FallbackController)
32
33 def list_statuses(conn, %{"instance" => instance} = params) do
34 with_reblogs = params["with_reblogs"] == "true" || params["with_reblogs"] == true
35 {page, page_size} = page_params(params)
36
37 result =
38 ActivityPub.fetch_statuses(nil, %{
39 instance: instance,
40 limit: page_size,
41 offset: (page - 1) * page_size,
42 exclude_reblogs: not with_reblogs,
43 total: true
44 })
45
46 conn
47 |> put_view(AdminAPI.StatusView)
48 |> render("index.json", %{total: result[:total], activities: result[:items], as: :activity})
49 end
50
51 def delete(conn, %{"instance" => instance}) do
52 with {:ok, _job} <- Instance.delete_users_and_activities(instance) do
53 json(conn, instance)
54 end
55 end
56
57 defp page_params(params) do
58 {
59 fetch_integer_param(params, "page", 1),
60 fetch_integer_param(params, "page_size", @default_page_size)
61 }
62 end
63 end