Implement meilisearch auth

This commit is contained in:
Ekaterina Vaartis 2021-08-23 19:35:21 +03:00 committed by FloatingGhost
parent 5360cc1097
commit dbf556cdcf
2 changed files with 88 additions and 56 deletions

View file

@ -9,15 +9,14 @@ defmodule Mix.Tasks.Pleroma.Search.Meilisearch do
import Mix.Pleroma import Mix.Pleroma
import Ecto.Query import Ecto.Query
import Pleroma.Search.Meilisearch, only: [meili_post!: 2, meili_delete!: 1]
def run(["index"]) do def run(["index"]) do
start_pleroma() start_pleroma()
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url]) meili_post!(
"/indexes/objects/settings/ranking-rules",
{:ok, _} = [
Pleroma.HTTP.post(
"#{endpoint}/indexes/objects/settings/ranking-rules",
Jason.encode!([
"desc(published)", "desc(published)",
"typo", "typo",
"words", "words",
@ -25,15 +24,14 @@ def run(["index"]) do
"attribute", "attribute",
"wordsPosition", "wordsPosition",
"exactness" "exactness"
]) ]
) )
{:ok, _} = meili_post!(
Pleroma.HTTP.post( "/indexes/objects/settings/searchable-attributes",
"#{endpoint}/indexes/objects/settings/searchable-attributes", [
Jason.encode!([
"content" "content"
]) ]
) )
chunk_size = 10_000 chunk_size = 10_000
@ -64,14 +62,14 @@ def run(["index"]) do
{[objects], new_acc} {[objects], new_acc}
end) end)
|> Stream.each(fn objects -> |> Stream.each(fn objects ->
{:ok, result} = result =
Pleroma.HTTP.post( meili_post!(
"#{endpoint}/indexes/objects/documents", "/indexes/objects/documents",
Jason.encode!(objects) objects
) )
if not Map.has_key?(Jason.decode!(result.body), "updateId") do if not Map.has_key?(result, "updateId") do
IO.puts("Failed to index: #{result}") IO.puts("Failed to index: #{inspect(result)}")
end end
end) end)
|> Stream.run() |> Stream.run()
@ -85,11 +83,26 @@ def run(["index"]) do
def run(["clear"]) do def run(["clear"]) do
start_pleroma() start_pleroma()
meili_delete!("/indexes/objects/documents")
end
def run(["show-private-key", master_key]) do
start_pleroma()
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url]) endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
{:ok, _} = {:ok, result} =
Pleroma.HTTP.request(:delete, "#{endpoint}/indexes/objects/documents", "", [], Pleroma.HTTP.get(
timeout: :infinity Path.join(endpoint, "/keys"),
[{"X-Meili-API-Key", master_key}]
) )
decoded = Jason.decode!(result.body)
if decoded["private"] do
IO.puts(decoded["private"])
else
IO.puts("Error fetching the key, check the master key is correct: #{inspect(decoded)}")
end
end end
end end

View file

@ -7,20 +7,50 @@ defmodule Pleroma.Search.Meilisearch do
import Pleroma.Activity.Search import Pleroma.Activity.Search
import Ecto.Query import Ecto.Query
defp meili_headers() do
private_key = Pleroma.Config.get([Pleroma.Search.Meilisearch, :private_key])
if is_nil(private_key), do: [], else: [{"X-Meili-API-Key", private_key}]
end
def meili_post!(path, params) do
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
{:ok, result} =
Pleroma.HTTP.post(
Path.join(endpoint, path),
Jason.encode!(params),
meili_headers()
)
Jason.decode!(result.body)
end
def meili_delete!(path) do
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
{:ok, _} =
Pleroma.HTTP.request(
:delete,
Path.join(endpoint, path),
"",
meili_headers(),
timeout: :infinity
)
end
def search(user, query, options \\ []) do def search(user, query, options \\ []) do
limit = Enum.min([Keyword.get(options, :limit), 40]) limit = Enum.min([Keyword.get(options, :limit), 40])
offset = Keyword.get(options, :offset, 0) offset = Keyword.get(options, :offset, 0)
author = Keyword.get(options, :author) author = Keyword.get(options, :author)
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url]) result =
meili_post!(
{:ok, result} = "/indexes/objects/search",
Pleroma.HTTP.post( %{q: query, offset: offset, limit: limit}
"#{endpoint}/indexes/objects/search",
Jason.encode!(%{q: query, offset: offset, limit: limit})
) )
hits = Jason.decode!(result.body)["hits"] |> Enum.map(& &1["ap"]) hits = result["hits"] |> Enum.map(& &1["ap"])
try do try do
hits hits
@ -73,30 +103,19 @@ def add_to_index(activity) do
maybe_search_data = object_to_search_data(activity) maybe_search_data = object_to_search_data(activity)
if activity.data["type"] == "Create" and maybe_search_data do if activity.data["type"] == "Create" and maybe_search_data do
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url]) result =
meili_post!(
{:ok, result} = "/indexes/objects/documents",
Pleroma.HTTP.post( [maybe_search_data]
"#{endpoint}/indexes/objects/documents",
Jason.encode!([maybe_search_data])
) )
if not Map.has_key?(Jason.decode!(result.body), "updateId") do if not Map.has_key?(result, "updateId") do
Logger.error("Failed to add activity #{activity.id} to index: #{result.body}") Logger.error("Failed to add activity #{activity.id} to index: #{inspect(result)}")
end end
end end
end end
def remove_from_index(object) do def remove_from_index(object) do
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url]) meili_delete!("/indexes/objects/documents/#{object.id}")
{:ok, _} =
Pleroma.HTTP.request(
:delete,
"#{endpoint}/indexes/objects/documents/#{object.id}",
"",
[],
[]
)
end end
end end