add user import
[akkoma] / lib / mix / tasks / pleroma / search.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 Mix.Tasks.Pleroma.Search do
6 use Mix.Task
7 import Mix.Pleroma
8 import Ecto.Query
9 alias Pleroma.Activity
10 alias Pleroma.Pagination
11 alias Pleroma.User
12
13 @shortdoc "Manages elasticsearch"
14
15 def run(["import", "activities" | _rest]) do
16 start_pleroma()
17
18 from(a in Activity, where: not ilike(a.actor, "%/relay"))
19 |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
20 |> Activity.with_preloaded_object()
21 |> Activity.with_preloaded_user_actor()
22 |> get_all(:activities)
23 end
24
25 def run(["import", "users" | _rest]) do
26 start_pleroma()
27
28 from(u in User, where: not ilike(u.ap_id, "%/relay"))
29 |> get_all(:users)
30 end
31
32 defp get_all(query, index, max_id \\ nil) do
33 params = %{limit: 2000}
34
35 params =
36 if max_id == nil do
37 params
38 else
39 Map.put(params, :max_id, max_id)
40 end
41
42 res =
43 query
44 |> Pagination.fetch_paginated(params)
45
46 if res == [] do
47 :ok
48 else
49 res
50 |> Pleroma.Elasticsearch.bulk_post(index)
51
52 get_all(query, index, List.last(res).id)
53 end
54 end
55 end