2021-08-15 12:53:04 -06:00
|
|
|
defmodule Pleroma.Search.Meilisearch do
|
|
|
|
require Logger
|
2021-08-16 13:24:31 -06:00
|
|
|
require Pleroma.Constants
|
2021-08-15 12:53:04 -06:00
|
|
|
|
|
|
|
alias Pleroma.Activity
|
|
|
|
|
|
|
|
import Pleroma.Activity.Search
|
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
def search(user, query, options \\ []) do
|
|
|
|
limit = Enum.min([Keyword.get(options, :limit), 40])
|
|
|
|
offset = Keyword.get(options, :offset, 0)
|
|
|
|
author = Keyword.get(options, :author)
|
|
|
|
|
|
|
|
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
|
|
|
|
|
|
|
{:ok, result} =
|
|
|
|
Pleroma.HTTP.post(
|
|
|
|
"#{endpoint}/indexes/objects/search",
|
|
|
|
Jason.encode!(%{q: query, offset: offset, limit: limit})
|
|
|
|
)
|
|
|
|
|
|
|
|
hits = Jason.decode!(result.body)["hits"] |> Enum.map(& &1["ap"])
|
|
|
|
|
|
|
|
try do
|
|
|
|
hits
|
|
|
|
|> Activity.create_by_object_ap_id()
|
|
|
|
|> Activity.with_preloaded_object()
|
|
|
|
|> Activity.with_preloaded_object()
|
|
|
|
|> Activity.restrict_deactivated_users()
|
|
|
|
|> maybe_restrict_local(user)
|
|
|
|
|> maybe_restrict_author(author)
|
|
|
|
|> maybe_restrict_blocked(user)
|
|
|
|
|> maybe_fetch(user, query)
|
2021-08-16 15:30:14 -06:00
|
|
|
|> order_by([object: obj], desc: obj.data["published"])
|
2021-08-15 12:53:04 -06:00
|
|
|
|> Pleroma.Repo.all()
|
|
|
|
rescue
|
|
|
|
_ -> maybe_fetch([], user, query)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_to_index(activity) do
|
|
|
|
object = activity.object
|
|
|
|
|
2021-08-16 13:24:31 -06:00
|
|
|
if activity.data["type"] == "Create" and not is_nil(object) and object.data["type"] == "Note" and
|
|
|
|
Pleroma.Constants.as_public() in object.data["to"] do
|
2021-08-15 12:53:04 -06:00
|
|
|
data = object.data
|
|
|
|
|
|
|
|
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
|
|
|
|
2021-08-16 15:30:14 -06:00
|
|
|
{:ok, published, _} = DateTime.from_iso8601(data["published"])
|
|
|
|
|
2021-08-15 12:53:04 -06:00
|
|
|
{:ok, result} =
|
|
|
|
Pleroma.HTTP.post(
|
|
|
|
"#{endpoint}/indexes/objects/documents",
|
2021-08-16 15:30:14 -06:00
|
|
|
Jason.encode!([
|
|
|
|
%{
|
|
|
|
id: object.id,
|
2021-08-16 15:57:53 -06:00
|
|
|
content: data["content"] |> Pleroma.HTML.filter_tags(),
|
2021-08-16 15:30:14 -06:00
|
|
|
ap: data["id"],
|
|
|
|
published: published |> DateTime.to_unix()
|
|
|
|
}
|
|
|
|
])
|
2021-08-15 12:53:04 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
if not Map.has_key?(Jason.decode!(result.body), "updateId") do
|
|
|
|
Logger.error("Failed to add activity #{activity.id} to index: #{result.body}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-08-16 13:24:31 -06:00
|
|
|
|
|
|
|
def remove_from_index(object) do
|
|
|
|
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
|
|
|
|
|
|
|
{:ok, _} =
|
|
|
|
Pleroma.HTTP.request(
|
|
|
|
:delete,
|
|
|
|
"#{endpoint}/indexes/objects/documents/#{object.id}",
|
|
|
|
"",
|
|
|
|
[],
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
end
|
2021-08-15 12:53:04 -06:00
|
|
|
end
|