Merge remote-tracking branch 'ilja/fix_tagpolicy_to_also_work_on_updates' into develop
[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.Search.DatabaseSearch
8 import Ecto.Query
9
10 @behaviour Pleroma.Search.SearchBackend
11
12 defp meili_headers do
13 private_key = Pleroma.Config.get([Pleroma.Search.Meilisearch, :private_key])
14
15 [{"Content-Type", "application/json"}] ++
16 if is_nil(private_key), do: [], else: [{"Authorization", "Bearer #{private_key}"}]
17 end
18
19 def meili_get(path) do
20 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
21
22 result =
23 Pleroma.HTTP.get(
24 Path.join(endpoint, path),
25 meili_headers()
26 )
27
28 with {:ok, res} <- result do
29 {:ok, Jason.decode!(res.body)}
30 end
31 end
32
33 def meili_post(path, params) do
34 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
35
36 result =
37 Pleroma.HTTP.post(
38 Path.join(endpoint, path),
39 Jason.encode!(params),
40 meili_headers()
41 )
42
43 with {:ok, res} <- result do
44 {:ok, Jason.decode!(res.body)}
45 end
46 end
47
48 def meili_put(path, params) do
49 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
50
51 result =
52 Pleroma.HTTP.request(
53 :put,
54 Path.join(endpoint, path),
55 Jason.encode!(params),
56 meili_headers(),
57 []
58 )
59
60 with {:ok, res} <- result do
61 {:ok, Jason.decode!(res.body)}
62 end
63 end
64
65 def meili_delete!(path) do
66 endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
67
68 {:ok, _} =
69 Pleroma.HTTP.request(
70 :delete,
71 Path.join(endpoint, path),
72 "",
73 meili_headers(),
74 []
75 )
76 end
77
78 def search(user, query, options \\ []) do
79 limit = Enum.min([Keyword.get(options, :limit), 40])
80 offset = Keyword.get(options, :offset, 0)
81 author = Keyword.get(options, :author)
82
83 res =
84 meili_post(
85 "/indexes/objects/search",
86 %{q: query, offset: offset, limit: limit}
87 )
88
89 with {:ok, result} <- res do
90 hits = result["hits"] |> Enum.map(& &1["ap"])
91
92 try do
93 hits
94 |> Activity.create_by_object_ap_id()
95 |> Activity.with_preloaded_object()
96 |> Activity.with_preloaded_object()
97 |> Activity.restrict_deactivated_users()
98 |> maybe_restrict_local(user)
99 |> maybe_restrict_author(author)
100 |> maybe_restrict_blocked(user)
101 |> maybe_fetch(user, query)
102 |> order_by([object: obj], desc: obj.data["published"])
103 |> Pleroma.Repo.all()
104 rescue
105 _ -> maybe_fetch([], user, query)
106 end
107 end
108 end
109
110 def object_to_search_data(object) do
111 # Only index public or unlisted Notes
112 if not is_nil(object) and object.data["type"] == "Note" and
113 not is_nil(object.data["content"]) and
114 (Pleroma.Constants.as_public() in object.data["to"] or
115 Pleroma.Constants.as_public() in object.data["cc"]) and
116 String.length(object.data["content"]) > 1 do
117 data = object.data
118
119 content_str =
120 case data["content"] do
121 [nil | rest] -> to_string(rest)
122 str -> str
123 end
124
125 content =
126 with {:ok, scrubbed} <- FastSanitize.strip_tags(content_str),
127 trimmed <- String.trim(scrubbed) do
128 trimmed
129 end
130
131 if String.length(content) > 1 do
132 {:ok, published, _} = DateTime.from_iso8601(data["published"])
133
134 %{
135 id: object.id,
136 content: content,
137 ap: data["id"],
138 published: published |> DateTime.to_unix()
139 }
140 end
141 end
142 end
143
144 @impl true
145 def add_to_index(activity) do
146 maybe_search_data = object_to_search_data(activity.object)
147
148 if activity.data["type"] == "Create" and maybe_search_data do
149 result =
150 meili_put(
151 "/indexes/objects/documents",
152 [maybe_search_data]
153 )
154
155 with {:ok, res} <- result,
156 true <- Map.has_key?(res, "taskUid") do
157 # Do nothing
158 else
159 _ ->
160 Logger.error("Failed to add activity #{activity.id} to index: #{inspect(result)}")
161 end
162 end
163 end
164
165 @impl true
166 def remove_from_index(object) do
167 meili_delete!("/indexes/objects/documents/#{object.id}")
168 end
169 end