2021-08-15 18:53:04 +00:00
|
|
|
defmodule Pleroma.Search.Meilisearch do
|
|
|
|
require Logger
|
2021-08-16 19:24:31 +00:00
|
|
|
require Pleroma.Constants
|
2021-08-15 18:53:04 +00:00
|
|
|
|
|
|
|
alias Pleroma.Activity
|
2023-11-12 10:49:50 +00:00
|
|
|
alias Pleroma.Config.Getting, as: Config
|
2021-08-15 18:53:04 +00:00
|
|
|
|
2021-11-17 19:29:49 +00:00
|
|
|
import Pleroma.Search.DatabaseSearch
|
2021-08-15 18:53:04 +00:00
|
|
|
import Ecto.Query
|
|
|
|
|
2021-12-20 19:38:50 +00:00
|
|
|
@behaviour Pleroma.Search.SearchBackend
|
|
|
|
|
2021-11-14 18:42:18 +00:00
|
|
|
defp meili_headers do
|
2023-11-12 10:49:50 +00:00
|
|
|
private_key = Config.get([Pleroma.Search.Meilisearch, :private_key])
|
2021-08-15 18:53:04 +00:00
|
|
|
|
2021-10-29 10:58:24 +00:00
|
|
|
[{"Content-Type", "application/json"}] ++
|
2022-03-22 17:29:17 +00:00
|
|
|
if is_nil(private_key), do: [], else: [{"Authorization", "Bearer #{private_key}"}]
|
2021-08-23 16:35:21 +00:00
|
|
|
end
|
|
|
|
|
2021-11-22 18:39:54 +00:00
|
|
|
def meili_get(path) do
|
2023-11-12 10:49:50 +00:00
|
|
|
endpoint = Config.get([Pleroma.Search.Meilisearch, :url])
|
2021-08-23 17:21:46 +00:00
|
|
|
|
2021-11-22 18:39:54 +00:00
|
|
|
result =
|
2021-08-23 17:21:46 +00:00
|
|
|
Pleroma.HTTP.get(
|
|
|
|
Path.join(endpoint, path),
|
|
|
|
meili_headers()
|
|
|
|
)
|
|
|
|
|
2021-11-22 18:39:54 +00:00
|
|
|
with {:ok, res} <- result do
|
|
|
|
{:ok, Jason.decode!(res.body)}
|
|
|
|
end
|
2021-08-23 17:21:46 +00:00
|
|
|
end
|
|
|
|
|
2021-11-22 18:39:54 +00:00
|
|
|
def meili_post(path, params) do
|
2023-11-12 10:49:50 +00:00
|
|
|
endpoint = Config.get([Pleroma.Search.Meilisearch, :url])
|
2021-08-15 18:53:04 +00:00
|
|
|
|
2021-11-22 18:39:54 +00:00
|
|
|
result =
|
2021-08-15 18:53:04 +00:00
|
|
|
Pleroma.HTTP.post(
|
2021-08-23 16:35:21 +00:00
|
|
|
Path.join(endpoint, path),
|
|
|
|
Jason.encode!(params),
|
|
|
|
meili_headers()
|
|
|
|
)
|
|
|
|
|
2021-11-22 18:39:54 +00:00
|
|
|
with {:ok, res} <- result do
|
|
|
|
{:ok, Jason.decode!(res.body)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def meili_put(path, params) do
|
2023-11-12 10:49:50 +00:00
|
|
|
endpoint = Config.get([Pleroma.Search.Meilisearch, :url])
|
2021-11-22 18:39:54 +00:00
|
|
|
|
|
|
|
result =
|
|
|
|
Pleroma.HTTP.request(
|
|
|
|
:put,
|
|
|
|
Path.join(endpoint, path),
|
|
|
|
Jason.encode!(params),
|
|
|
|
meili_headers(),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
|
|
|
with {:ok, res} <- result do
|
|
|
|
{:ok, Jason.decode!(res.body)}
|
|
|
|
end
|
2021-08-23 16:35:21 +00:00
|
|
|
end
|
|
|
|
|
2022-08-26 21:31:36 +00:00
|
|
|
def meili_delete(path) do
|
2023-11-12 10:49:50 +00:00
|
|
|
endpoint = Config.get([Pleroma.Search.Meilisearch, :url])
|
|
|
|
|
|
|
|
with {:ok, _} <-
|
|
|
|
Pleroma.HTTP.request(
|
|
|
|
:delete,
|
|
|
|
Path.join(endpoint, path),
|
|
|
|
"",
|
|
|
|
meili_headers(),
|
|
|
|
[]
|
|
|
|
) do
|
|
|
|
:ok
|
|
|
|
else
|
|
|
|
_ -> :error
|
|
|
|
end
|
2021-08-23 16:35:21 +00:00
|
|
|
end
|
|
|
|
|
2022-08-26 21:19:08 +00:00
|
|
|
@impl true
|
2021-08-23 16:35:21 +00:00
|
|
|
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)
|
|
|
|
|
2021-11-22 18:39:54 +00:00
|
|
|
res =
|
|
|
|
meili_post(
|
2021-08-23 16:35:21 +00:00
|
|
|
"/indexes/objects/search",
|
|
|
|
%{q: query, offset: offset, limit: limit}
|
2021-08-15 18:53:04 +00:00
|
|
|
)
|
|
|
|
|
2021-11-22 18:39:54 +00:00
|
|
|
with {:ok, result} <- res do
|
|
|
|
hits = result["hits"] |> Enum.map(& &1["ap"])
|
|
|
|
|
|
|
|
try do
|
|
|
|
hits
|
|
|
|
|> Activity.create_by_object_ap_id()
|
|
|
|
|> Activity.with_preloaded_object()
|
|
|
|
|> Activity.restrict_deactivated_users()
|
|
|
|
|> maybe_restrict_local(user)
|
|
|
|
|> maybe_restrict_author(author)
|
|
|
|
|> maybe_restrict_blocked(user)
|
|
|
|
|> maybe_fetch(user, query)
|
|
|
|
|> order_by([object: obj], desc: obj.data["published"])
|
|
|
|
|> Pleroma.Repo.all()
|
|
|
|
rescue
|
|
|
|
_ -> maybe_fetch([], user, query)
|
|
|
|
end
|
2021-08-15 18:53:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-22 19:53:18 +00:00
|
|
|
def object_to_search_data(object) do
|
2021-11-22 18:39:54 +00:00
|
|
|
# Only index public or unlisted Notes
|
2021-08-22 19:53:18 +00:00
|
|
|
if not is_nil(object) and object.data["type"] == "Note" and
|
2021-11-22 18:39:54 +00:00
|
|
|
not is_nil(object.data["content"]) and
|
|
|
|
(Pleroma.Constants.as_public() in object.data["to"] or
|
|
|
|
Pleroma.Constants.as_public() in object.data["cc"]) and
|
2022-08-26 21:09:37 +00:00
|
|
|
object.data["content"] not in ["", "."] do
|
2021-08-15 18:53:04 +00:00
|
|
|
data = object.data
|
|
|
|
|
2021-08-22 19:53:18 +00:00
|
|
|
content_str =
|
|
|
|
case data["content"] do
|
|
|
|
[nil | rest] -> to_string(rest)
|
|
|
|
str -> str
|
|
|
|
end
|
|
|
|
|
|
|
|
content =
|
2022-08-26 22:43:59 +00:00
|
|
|
with {:ok, scrubbed} <-
|
|
|
|
FastSanitize.Sanitizer.scrub(content_str, Pleroma.HTML.Scrubber.SearchIndexing),
|
2021-08-22 19:53:18 +00:00
|
|
|
trimmed <- String.trim(scrubbed) do
|
|
|
|
trimmed
|
|
|
|
end
|
|
|
|
|
2022-08-26 21:09:37 +00:00
|
|
|
# Make sure we have a non-empty string
|
|
|
|
if content != "" do
|
2021-08-22 19:53:18 +00:00
|
|
|
{:ok, published, _} = DateTime.from_iso8601(data["published"])
|
|
|
|
|
|
|
|
%{
|
|
|
|
id: object.id,
|
|
|
|
content: content,
|
|
|
|
ap: data["id"],
|
|
|
|
published: published |> DateTime.to_unix()
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-08-15 18:53:04 +00:00
|
|
|
|
2021-12-20 19:38:50 +00:00
|
|
|
@impl true
|
2021-08-22 19:53:18 +00:00
|
|
|
def add_to_index(activity) do
|
2021-08-23 18:15:15 +00:00
|
|
|
maybe_search_data = object_to_search_data(activity.object)
|
2021-08-22 19:53:18 +00:00
|
|
|
|
|
|
|
if activity.data["type"] == "Create" and maybe_search_data do
|
2021-08-23 16:35:21 +00:00
|
|
|
result =
|
2021-11-22 18:39:54 +00:00
|
|
|
meili_put(
|
2021-08-23 16:35:21 +00:00
|
|
|
"/indexes/objects/documents",
|
|
|
|
[maybe_search_data]
|
2021-08-15 18:53:04 +00:00
|
|
|
)
|
|
|
|
|
2021-11-22 18:39:54 +00:00
|
|
|
with {:ok, res} <- result,
|
2022-08-26 22:11:50 +00:00
|
|
|
true <- Map.has_key?(res, "updateId") do
|
2022-08-26 21:31:36 +00:00
|
|
|
# Added successfully
|
|
|
|
:ok
|
2021-11-22 18:39:54 +00:00
|
|
|
else
|
|
|
|
_ ->
|
2022-08-26 21:31:36 +00:00
|
|
|
# There was an error, report it
|
2021-11-22 18:39:54 +00:00
|
|
|
Logger.error("Failed to add activity #{activity.id} to index: #{inspect(result)}")
|
2022-08-26 21:31:36 +00:00
|
|
|
{:error, result}
|
2021-08-15 18:53:04 +00:00
|
|
|
end
|
2022-08-26 21:31:36 +00:00
|
|
|
else
|
|
|
|
# The post isn't something we can search, that's ok
|
|
|
|
:ok
|
2021-08-15 18:53:04 +00:00
|
|
|
end
|
|
|
|
end
|
2021-08-16 19:24:31 +00:00
|
|
|
|
2021-12-20 19:38:50 +00:00
|
|
|
@impl true
|
2021-08-16 19:24:31 +00:00
|
|
|
def remove_from_index(object) do
|
2022-08-26 21:31:36 +00:00
|
|
|
meili_delete("/indexes/objects/documents/#{object.id}")
|
2021-08-16 19:24:31 +00:00
|
|
|
end
|
2021-08-15 18:53:04 +00:00
|
|
|
end
|