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
|
|
|
|
|
2021-11-17 12:29:49 -07:00
|
|
|
import Pleroma.Search.DatabaseSearch
|
2021-08-15 12:53:04 -06:00
|
|
|
|
2021-12-20 12:38:50 -07:00
|
|
|
@behaviour Pleroma.Search.SearchBackend
|
|
|
|
|
2024-05-14 23:05:28 -06:00
|
|
|
defp meili_headers(key) do
|
|
|
|
key_header =
|
|
|
|
if is_nil(key), do: [], else: [{"Authorization", "Bearer #{key}"}]
|
|
|
|
|
|
|
|
[{"Content-Type", "application/json"} | key_header]
|
|
|
|
end
|
|
|
|
|
|
|
|
defp meili_headers_admin do
|
2021-08-23 10:35:21 -06:00
|
|
|
private_key = Pleroma.Config.get([Pleroma.Search.Meilisearch, :private_key])
|
2024-05-14 23:05:28 -06:00
|
|
|
meili_headers(private_key)
|
|
|
|
end
|
2021-08-15 12:53:04 -06:00
|
|
|
|
2024-05-14 23:05:28 -06:00
|
|
|
defp meili_headers_search do
|
|
|
|
search_key =
|
|
|
|
Pleroma.Config.get([Pleroma.Search.Meilisearch, :search_key]) ||
|
|
|
|
Pleroma.Config.get([Pleroma.Search.Meilisearch, :private_key])
|
|
|
|
|
|
|
|
meili_headers(search_key)
|
2021-08-23 10:35:21 -06:00
|
|
|
end
|
|
|
|
|
2021-11-22 11:39:54 -07:00
|
|
|
def meili_get(path) do
|
2021-08-23 11:21:46 -06:00
|
|
|
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
|
|
|
|
2021-11-22 11:39:54 -07:00
|
|
|
result =
|
2021-08-23 11:21:46 -06:00
|
|
|
Pleroma.HTTP.get(
|
|
|
|
Path.join(endpoint, path),
|
2024-05-14 23:05:28 -06:00
|
|
|
meili_headers_admin()
|
2021-08-23 11:21:46 -06:00
|
|
|
)
|
|
|
|
|
2021-11-22 11:39:54 -07:00
|
|
|
with {:ok, res} <- result do
|
|
|
|
{:ok, Jason.decode!(res.body)}
|
|
|
|
end
|
2021-08-23 11:21:46 -06:00
|
|
|
end
|
|
|
|
|
2024-05-14 23:05:28 -06:00
|
|
|
defp meili_search(params) do
|
2021-08-15 12:53:04 -06:00
|
|
|
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
|
|
|
|
2021-11-22 11:39:54 -07:00
|
|
|
result =
|
2021-08-15 12:53:04 -06:00
|
|
|
Pleroma.HTTP.post(
|
2024-05-14 23:05:28 -06:00
|
|
|
Path.join(endpoint, "/indexes/objects/search"),
|
2021-08-23 10:35:21 -06:00
|
|
|
Jason.encode!(params),
|
2024-05-14 23:05:28 -06:00
|
|
|
meili_headers_search()
|
2021-08-23 10:35:21 -06:00
|
|
|
)
|
|
|
|
|
2021-11-22 11:39:54 -07:00
|
|
|
with {:ok, res} <- result do
|
|
|
|
{:ok, Jason.decode!(res.body)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def meili_put(path, params) do
|
|
|
|
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
|
|
|
|
|
|
|
result =
|
|
|
|
Pleroma.HTTP.request(
|
|
|
|
:put,
|
|
|
|
Path.join(endpoint, path),
|
|
|
|
Jason.encode!(params),
|
2024-05-14 23:05:28 -06:00
|
|
|
meili_headers_admin(),
|
2021-11-22 11:39:54 -07:00
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
|
|
|
with {:ok, res} <- result do
|
|
|
|
{:ok, Jason.decode!(res.body)}
|
|
|
|
end
|
2021-08-23 10:35:21 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def meili_delete!(path) do
|
|
|
|
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
|
|
|
|
|
|
|
|
{:ok, _} =
|
|
|
|
Pleroma.HTTP.request(
|
|
|
|
:delete,
|
|
|
|
Path.join(endpoint, path),
|
|
|
|
"",
|
2024-05-14 23:05:28 -06:00
|
|
|
meili_headers_admin(),
|
2021-10-28 15:38:00 -06:00
|
|
|
[]
|
2021-08-23 10:35:21 -06:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
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 11:39:54 -07:00
|
|
|
res =
|
2024-05-14 23:05:28 -06:00
|
|
|
meili_search(%{q: query, offset: offset, limit: limit})
|
2021-08-15 12:53:04 -06:00
|
|
|
|
2021-11-22 11:39:54 -07:00
|
|
|
with {:ok, result} <- res do
|
|
|
|
hits = result["hits"] |> Enum.map(& &1["ap"])
|
|
|
|
|
|
|
|
try do
|
|
|
|
hits
|
2024-05-02 14:44:48 -06:00
|
|
|
|> Activity.get_presorted_create_by_object_ap_id()
|
2021-11-22 11:39:54 -07:00
|
|
|
|> Activity.with_preloaded_object()
|
|
|
|
|> Activity.restrict_deactivated_users()
|
|
|
|
|> maybe_restrict_local(user)
|
|
|
|
|> maybe_restrict_author(author)
|
|
|
|
|> maybe_restrict_blocked(user)
|
|
|
|
|> maybe_fetch(user, query)
|
|
|
|
|> Pleroma.Repo.all()
|
|
|
|
rescue
|
|
|
|
_ -> maybe_fetch([], user, query)
|
|
|
|
end
|
2021-08-15 12:53:04 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-22 13:53:18 -06:00
|
|
|
def object_to_search_data(object) do
|
2021-11-22 11:39:54 -07:00
|
|
|
# Only index public or unlisted Notes
|
2021-08-22 13:53:18 -06:00
|
|
|
if not is_nil(object) and object.data["type"] == "Note" and
|
2021-11-22 11:39:54 -07: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
|
|
|
|
String.length(object.data["content"]) > 1 do
|
2021-08-15 12:53:04 -06:00
|
|
|
data = object.data
|
|
|
|
|
2021-08-22 13:53:18 -06:00
|
|
|
content_str =
|
|
|
|
case data["content"] do
|
|
|
|
[nil | rest] -> to_string(rest)
|
|
|
|
str -> str
|
|
|
|
end
|
|
|
|
|
|
|
|
content =
|
|
|
|
with {:ok, scrubbed} <- FastSanitize.strip_tags(content_str),
|
|
|
|
trimmed <- String.trim(scrubbed) do
|
|
|
|
trimmed
|
|
|
|
end
|
|
|
|
|
2022-12-08 02:04:20 -07:00
|
|
|
if String.length(content) > 1 and not is_nil(data["published"]) do
|
2021-08-22 13:53:18 -06: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 12:53:04 -06:00
|
|
|
|
2021-12-20 12:38:50 -07:00
|
|
|
@impl true
|
2021-08-22 13:53:18 -06:00
|
|
|
def add_to_index(activity) do
|
2021-08-23 12:15:15 -06:00
|
|
|
maybe_search_data = object_to_search_data(activity.object)
|
2021-08-22 13:53:18 -06:00
|
|
|
|
|
|
|
if activity.data["type"] == "Create" and maybe_search_data do
|
2021-08-23 10:35:21 -06:00
|
|
|
result =
|
2021-11-22 11:39:54 -07:00
|
|
|
meili_put(
|
2021-08-23 10:35:21 -06:00
|
|
|
"/indexes/objects/documents",
|
|
|
|
[maybe_search_data]
|
2021-08-15 12:53:04 -06:00
|
|
|
)
|
|
|
|
|
2021-11-22 11:39:54 -07:00
|
|
|
with {:ok, res} <- result,
|
2022-09-20 04:36:21 -06:00
|
|
|
true <- Map.has_key?(res, "taskUid") do
|
2022-12-14 05:38:48 -07:00
|
|
|
{:ok, res}
|
2021-11-22 11:39:54 -07:00
|
|
|
else
|
2022-12-14 05:38:48 -07:00
|
|
|
err ->
|
2021-11-22 11:39:54 -07:00
|
|
|
Logger.error("Failed to add activity #{activity.id} to index: #{inspect(result)}")
|
2022-12-14 05:38:48 -07:00
|
|
|
{:error, err}
|
2021-08-15 12:53:04 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-08-16 13:24:31 -06:00
|
|
|
|
2021-12-20 12:38:50 -07:00
|
|
|
@impl true
|
2021-08-16 13:24:31 -06:00
|
|
|
def remove_from_index(object) do
|
2021-08-23 10:35:21 -06:00
|
|
|
meili_delete!("/indexes/objects/documents/#{object.id}")
|
2021-08-16 13:24:31 -06:00
|
|
|
end
|
2021-08-15 12:53:04 -06:00
|
|
|
end
|