total rebase
[anni] / lib / pleroma / search / database_search.ex
old mode 100755 (executable)
new mode 100644 (file)
similarity index 86%
rename from lib/pleroma/activity/search.ex
rename to lib/pleroma/search/database_search.ex
index 0b9b24a..31bfc7e
@@ -1,9 +1,10 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
-defmodule Pleroma.Activity.Search do
+defmodule Pleroma.Search.DatabaseSearch do
   alias Pleroma.Activity
+  alias Pleroma.Config
   alias Pleroma.Object.Fetcher
   alias Pleroma.Pagination
   alias Pleroma.User
@@ -13,25 +14,21 @@ defmodule Pleroma.Activity.Search do
 
   import Ecto.Query
 
+  @behaviour Pleroma.Search.SearchBackend
+
+  @impl true
   def search(user, search_query, options \\ []) do
-    index_type = if Pleroma.Config.get([:database, :rum_enabled]), do: :rum, else: :gin
+    index_type = if Config.get([:database, :rum_enabled]), do: :rum, else: :gin
     limit = Enum.min([Keyword.get(options, :limit), 40])
     offset = Keyword.get(options, :offset, 0)
     author = Keyword.get(options, :author)
 
-    search_function =
-      if :persistent_term.get({Pleroma.Repo, :postgres_version}) >= 11 do
-        :websearch
-      else
-        :plain
-      end
-
     try do
       Activity
       |> Activity.with_preloaded_object()
       |> Activity.restrict_deactivated_users()
       |> restrict_public(user)
-      |> query_with(index_type, search_query, search_function)
+      |> query_with(index_type, search_query, :websearch)
       |> maybe_restrict_local(user)
       |> maybe_restrict_author(author)
       |> maybe_restrict_blocked(user)
@@ -45,6 +42,12 @@ defmodule Pleroma.Activity.Search do
     end
   end
 
+  @impl true
+  def add_to_index(_activity), do: :ok
+
+  @impl true
+  def remove_from_index(_object), do: :ok
+
   def maybe_restrict_author(query, %User{} = author) do
     Activity.Queries.by_author(query, author)
   end
@@ -136,8 +139,8 @@ defmodule Pleroma.Activity.Search do
     )
   end
 
-  defp maybe_restrict_local(q, user) do
-    limit = Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
+  def maybe_restrict_local(q, user) do
+    limit = Config.get([:instance, :limit_to_local_content], :unauthenticated)
 
     case {limit, user} do
       {:all, _} -> restrict_local(q)
@@ -149,7 +152,7 @@ defmodule Pleroma.Activity.Search do
 
   defp restrict_local(q), do: where(q, local: true)
 
-  defp maybe_fetch(activities, user, search_query) do
+  def maybe_fetch(activities, user, search_query) do
     with true <- Regex.match?(~r/https?:/, search_query),
          {:ok, object} <- Fetcher.fetch_object_from_id(search_query),
          %Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),