Add import functionality
[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.Elasticsearch
10 alias Pleroma.Activity
11 alias Pleroma.Repo
12 alias Pleroma.Pagination
13
14 @shortdoc "Manages elasticsearch"
15
16 def run(["import" | rest]) do
17 start_pleroma()
18
19 query = from(a in Activity, where: not ilike(a.actor, "%/relay"))
20 |> Activity.with_preloaded_object
21 |> Activity.with_preloaded_user_actor
22 |> get_all
23 end
24
25 defp get_all(query, max_id \\ nil) do
26 params = %{limit: 20}
27 params = if max_id == nil do
28 params
29 else
30 Map.put(params, :max_id, max_id)
31 end
32
33 res = query
34 |> Pagination.fetch_paginated(params)
35
36 if res == [] do
37 :ok
38 else
39 res
40 |> Pleroma.Elasticsearch.bulk_post(:activities)
41
42 get_all(query, List.last(res).id)
43 end
44 end
45
46 end