extra cool
[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 alias Pleroma.Hashtag
13
14 @shortdoc "Manages elasticsearch"
15
16 def run(["import", "activities" | _rest]) do
17 start_pleroma()
18
19 from(a in Activity, where: not ilike(a.actor, "%/relay"))
20 |> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
21 |> Activity.with_preloaded_object()
22 |> Activity.with_preloaded_user_actor()
23 |> get_all(:activities)
24 end
25
26 def run(["import", "users" | _rest]) do
27 start_pleroma()
28
29 from(u in User, where: u.nickname not in ["internal.fetch", "relay"])
30 |> get_all(:users)
31 end
32
33 def run(["import", "hashtags" | _rest]) do
34 start_pleroma()
35
36 from(h in Hashtag)
37 |> Pleroma.Repo.all()
38 |> Pleroma.Elasticsearch.bulk_post(:hashtags)
39 end
40
41 defp get_all(query, index, max_id \\ nil) do
42 params = %{limit: 1000}
43
44 params =
45 if max_id == nil do
46 params
47 else
48 Map.put(params, :max_id, max_id)
49 end
50
51 res =
52 query
53 |> Pagination.fetch_paginated(params)
54
55 if res == [] do
56 :ok
57 else
58 res
59 |> Pleroma.Elasticsearch.bulk_post(index)
60
61 get_all(query, index, List.last(res).id)
62 end
63 end
64 end