2021-12-11 17:36:49 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Mix.Tasks.Pleroma.Search do
|
|
|
|
use Mix.Task
|
|
|
|
import Mix.Pleroma
|
|
|
|
import Ecto.Query
|
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.Pagination
|
2021-12-13 20:27:35 +00:00
|
|
|
alias Pleroma.User
|
2021-12-14 12:37:10 +00:00
|
|
|
alias Pleroma.Hashtag
|
2021-12-11 17:36:49 +00:00
|
|
|
|
|
|
|
@shortdoc "Manages elasticsearch"
|
|
|
|
|
2021-12-13 20:27:35 +00:00
|
|
|
def run(["import", "activities" | _rest]) do
|
2021-12-11 17:36:49 +00:00
|
|
|
start_pleroma()
|
|
|
|
|
2021-12-12 17:23:44 +00:00
|
|
|
from(a in Activity, where: not ilike(a.actor, "%/relay"))
|
2021-12-12 18:25:20 +00:00
|
|
|
|> where([a], fragment("(? ->> 'type'::text) = 'Create'", a.data))
|
2021-12-12 17:23:44 +00:00
|
|
|
|> Activity.with_preloaded_object()
|
|
|
|
|> Activity.with_preloaded_user_actor()
|
2021-12-13 20:27:35 +00:00
|
|
|
|> get_all(:activities)
|
2021-12-11 17:36:49 +00:00
|
|
|
end
|
|
|
|
|
2021-12-13 20:27:35 +00:00
|
|
|
def run(["import", "users" | _rest]) do
|
|
|
|
start_pleroma()
|
|
|
|
|
|
|
|
from(u in User, where: not ilike(u.ap_id, "%/relay"))
|
|
|
|
|> get_all(:users)
|
|
|
|
end
|
|
|
|
|
2021-12-14 12:37:10 +00:00
|
|
|
def run(["import", "hashtags" | _rest]) do
|
|
|
|
start_pleroma()
|
|
|
|
|
|
|
|
from(h in Hashtag)
|
|
|
|
|> Pleroma.Repo.all()
|
|
|
|
|> Pleroma.Elasticsearch.bulk_post(:hashtags)
|
|
|
|
end
|
|
|
|
|
2021-12-13 20:27:35 +00:00
|
|
|
defp get_all(query, index, max_id \\ nil) do
|
2021-12-14 12:37:10 +00:00
|
|
|
params = %{limit: 1000}
|
2021-12-12 17:23:44 +00:00
|
|
|
|
|
|
|
params =
|
|
|
|
if max_id == nil do
|
2021-12-11 17:36:49 +00:00
|
|
|
params
|
2021-12-12 17:23:44 +00:00
|
|
|
else
|
2021-12-11 17:36:49 +00:00
|
|
|
Map.put(params, :max_id, max_id)
|
2021-12-12 17:23:44 +00:00
|
|
|
end
|
2021-12-11 17:36:49 +00:00
|
|
|
|
2021-12-12 17:23:44 +00:00
|
|
|
res =
|
|
|
|
query
|
|
|
|
|> Pagination.fetch_paginated(params)
|
2021-12-11 17:36:49 +00:00
|
|
|
|
|
|
|
if res == [] do
|
|
|
|
:ok
|
|
|
|
else
|
|
|
|
res
|
2021-12-13 20:27:35 +00:00
|
|
|
|> Pleroma.Elasticsearch.bulk_post(index)
|
|
|
|
|
|
|
|
get_all(query, index, List.last(res).id)
|
2021-12-11 17:36:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|