fix inbound federation
[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
12 @shortdoc "Manages elasticsearch"
13
14 def run(["import_since", d | _rest]) do
15 start_pleroma()
16 {:ok, since, _} = DateTime.from_iso8601(d)
17
18 from(a in Activity, where: not ilike(a.actor, "%/relay") and a.inserted_at > ^since)
19 |> Activity.with_preloaded_object()
20 |> Activity.with_preloaded_user_actor()
21 |> get_all
22 end
23
24 def run(["import" | _rest]) do
25 start_pleroma()
26
27 from(a in Activity, where: not ilike(a.actor, "%/relay"))
28 |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
29 |> Activity.with_preloaded_object()
30 |> Activity.with_preloaded_user_actor()
31 |> get_all
32 end
33
34 defp get_all(query, max_id \\ nil) do
35 IO.puts(max_id)
36 params = %{limit: 2000}
37
38 params =
39 if max_id == nil do
40 params
41 else
42 Map.put(params, :max_id, max_id)
43 end
44
45 res =
46 query
47 |> Pagination.fetch_paginated(params)
48
49 if res == [] do
50 :ok
51 else
52 res
53 |> Enum.filter(fn x ->
54 t =
55 x.object
56 |> Map.get(:data, %{})
57 |> Map.get("type", "")
58
59 t == "Note"
60 end)
61 |> Pleroma.Elasticsearch.bulk_post(:activities)
62
63 get_all(query, List.last(res).id)
64 end
65 end
66 end