summaryrefslogtreecommitdiff
path: root/lib/pleroma/web/admin_api/controllers/instance_controller.ex
blob: 117a722801b1e3ea8a469e0ea9c7a971c6fcfc52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.AdminAPI.InstanceController do
  use Pleroma.Web, :controller

  import Pleroma.Web.ControllerHelper, only: [fetch_integer_param: 3]

  alias Pleroma.Instances.Instance
  alias Pleroma.Web.ActivityPub.ActivityPub
  alias Pleroma.Web.AdminAPI
  alias Pleroma.Web.Plugs.OAuthScopesPlug

  require Logger

  @default_page_size 50

  plug(
    OAuthScopesPlug,
    %{scopes: ["admin:read:statuses"]}
    when action in [:list_statuses]
  )

  plug(
    OAuthScopesPlug,
    %{scopes: ["admin:write:accounts", "admin:write:statuses"]}
    when action in [:delete]
  )

  action_fallback(AdminAPI.FallbackController)

  def list_statuses(conn, %{"instance" => instance} = params) do
    with_reblogs = params["with_reblogs"] == "true" || params["with_reblogs"] == true
    {page, page_size} = page_params(params)

    result =
      ActivityPub.fetch_statuses(nil, %{
        instance: instance,
        limit: page_size,
        offset: (page - 1) * page_size,
        exclude_reblogs: not with_reblogs,
        total: true
      })

    conn
    |> put_view(AdminAPI.StatusView)
    |> render("index.json", %{total: result[:total], activities: result[:items], as: :activity})
  end

  def delete(conn, %{"instance" => instance}) do
    with {:ok, _job} <- Instance.delete_users_and_activities(instance) do
      json(conn, instance)
    end
  end

  defp page_params(params) do
    {
      fetch_integer_param(params, "page", 1),
      fetch_integer_param(params, "page_size", @default_page_size)
    }
  end
end