212bdd473cbed041fa06cf6278a7a2f16ea7606b
[akkoma] / lib / pleroma / search / meilisearch.ex
1 defmodule Pleroma.Search.Meilisearch do
2 require Logger
3 require Pleroma.Constants
4
5 alias Pleroma.Activity
6
7 import Pleroma.Activity.Search
8 import Ecto.Query
9
10 defp meili_headers() do
11 private_key = Pleroma.Config.get([Pleroma.Search.Meilisearch, :private_key])
12
13 if is_nil(private_key), do: [], else: [{"X-Meili-API-Key", private_key}]
14 end
15
16 def meili_get!(path) do
17 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
18
19 {:ok, result} =
20 Pleroma.HTTP.get(
21 Path.join(endpoint, path),
22 meili_headers()
23 )
24
25 Jason.decode!(result.body)
26 end
27
28 def meili_post!(path, params) do
29 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
30
31 {:ok, result} =
32 Pleroma.HTTP.post(
33 Path.join(endpoint, path),
34 Jason.encode!(params),
35 meili_headers()
36 )
37
38 Jason.decode!(result.body)
39 end
40
41 def meili_delete!(path) do
42 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
43
44 {:ok, _} =
45 Pleroma.HTTP.request(
46 :delete,
47 Path.join(endpoint, path),
48 "",
49 meili_headers(),
50 timeout: :infinity
51 )
52 end
53
54 def search(user, query, options \\ []) do
55 limit = Enum.min([Keyword.get(options, :limit), 40])
56 offset = Keyword.get(options, :offset, 0)
57 author = Keyword.get(options, :author)
58
59 result =
60 meili_post!(
61 "/indexes/objects/search",
62 %{q: query, offset: offset, limit: limit}
63 )
64
65 hits = result["hits"] |> Enum.map(& &1["ap"])
66
67 try do
68 hits
69 |> Activity.create_by_object_ap_id()
70 |> Activity.with_preloaded_object()
71 |> Activity.with_preloaded_object()
72 |> Activity.restrict_deactivated_users()
73 |> maybe_restrict_local(user)
74 |> maybe_restrict_author(author)
75 |> maybe_restrict_blocked(user)
76 |> maybe_fetch(user, query)
77 |> order_by([object: obj], desc: obj.data["published"])
78 |> Pleroma.Repo.all()
79 rescue
80 _ -> maybe_fetch([], user, query)
81 end
82 end
83
84 def object_to_search_data(object) do
85 if not is_nil(object) and object.data["type"] == "Note" and
86 Pleroma.Constants.as_public() in object.data["to"] do
87 data = object.data
88
89 content_str =
90 case data["content"] do
91 [nil | rest] -> to_string(rest)
92 str -> str
93 end
94
95 content =
96 with {:ok, scrubbed} <- FastSanitize.strip_tags(content_str),
97 trimmed <- String.trim(scrubbed) do
98 trimmed
99 end
100
101 if String.length(content) > 1 do
102 {:ok, published, _} = DateTime.from_iso8601(data["published"])
103
104 %{
105 id: object.id,
106 content: content,
107 ap: data["id"],
108 published: published |> DateTime.to_unix()
109 }
110 end
111 end
112 end
113
114 def add_to_index(activity) do
115 maybe_search_data = object_to_search_data(activity.object)
116
117 if activity.data["type"] == "Create" and maybe_search_data do
118 result =
119 meili_post!(
120 "/indexes/objects/documents",
121 [maybe_search_data]
122 )
123
124 if not Map.has_key?(result, "updateId") do
125 Logger.error("Failed to add activity #{activity.id} to index: #{inspect(result)}")
126 end
127 end
128 end
129
130 def remove_from_index(object) do
131 meili_delete!("/indexes/objects/documents/#{object.id}")
132 end
133 end