remove all endpoints marked as deprecated (#91)
[akkoma] / lib / pleroma / web / mastodon_api / controllers / search_controller.ex
index 920ff5980993e5e03ff9985d0ab8c75389d51cb7..bd12069c154131af4ad0a75487c371327760520e 100644 (file)
@@ -1,11 +1,10 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.Web.MastodonAPI.SearchController do
   use Pleroma.Web, :controller
 
-  alias Pleroma.Activity
   alias Pleroma.Repo
   alias Pleroma.User
   alias Pleroma.Web.ControllerHelper
@@ -17,6 +16,8 @@ defmodule Pleroma.Web.MastodonAPI.SearchController do
 
   require Logger
 
+  @search_limit 40
+
   plug(Pleroma.Web.ApiSpec.CastAndValidate)
 
   # Note: Mastodon doesn't allow unauthenticated access (requires read:accounts / read:search)
@@ -24,7 +25,7 @@ defmodule Pleroma.Web.MastodonAPI.SearchController do
 
   # Note: on private instances auth is required (EnsurePublicOrAuthenticatedPlug is not skipped)
 
-  plug(RateLimiter, [name: :search] when action in [:search, :search2, :account_search])
+  plug(RateLimiter, [name: :search] when action in [:search2, :account_search])
 
   defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.SearchOperation
 
@@ -41,47 +42,9 @@ defmodule Pleroma.Web.MastodonAPI.SearchController do
   end
 
   def search2(conn, params), do: do_search(:v2, conn, params)
-  def search(conn, params), do: do_search(:v1, conn, params)
 
   defp do_search(version, %{assigns: %{user: user}} = conn, %{q: query} = params) do
     query = String.trim(query)
-    options = search_options(params, user)
-    if Pleroma.Config.get([:search, :provider]) == :elasticsearch do
-      elasticsearch_search(conn, query, options)
-    else
-      builtin_search(version, conn, params)
-    end
-  end
-
-  defp elasticsearch_search(%{assigns: %{user: user}} = conn, query, options) do
-    with {:ok, raw_results} <- Pleroma.Elasticsearch.search(query) do
-      results = raw_results
-      |> Map.get(:body, %{})
-      |> Map.get("hits", %{})
-      |> Map.get("hits", [])
-      |> Enum.map(fn result -> result["_id"] end)
-      |> Pleroma.Activity.all_by_ids_with_object()
-      
-      json(
-        conn,
-        %{
-          accounts: [],
-          hashtags: [],
-          statuses: StatusView.render("index.json",
-            activities: results,
-            for: user,
-            as: :activity
-        )}
-      )
-    else
-      {:error, _} ->
-        conn
-        |> put_status(:internal_server_error)
-        |> json(%{error: "Search failed"})
-    end
-  end
-
-  defp builtin_search(version, %{assigns: %{user: user}} = conn, %{q: query} = params) do
     options = search_options(params, user)
     timeout = Keyword.get(Repo.config(), :timeout, 15_000)
     default_values = %{"statuses" => [], "accounts" => [], "hashtags" => []}
@@ -114,7 +77,7 @@ defmodule Pleroma.Web.MastodonAPI.SearchController do
     [
       resolve: params[:resolve],
       following: params[:following],
-      limit: params[:limit],
+      limit: min(params[:limit], @search_limit),
       offset: params[:offset],
       type: params[:type],
       author: get_author(params),
@@ -135,7 +98,7 @@ defmodule Pleroma.Web.MastodonAPI.SearchController do
   end
 
   defp resource_search(_, "statuses", query, options) do
-    statuses = with_fallback(fn -> Activity.search(options[:for_user], query, options) end)
+    statuses = with_fallback(fn -> Pleroma.Search.search(query, options) end)
 
     StatusView.render("index.json",
       activities: statuses,
@@ -154,10 +117,6 @@ defmodule Pleroma.Web.MastodonAPI.SearchController do
     end)
   end
 
-  defp resource_search(:v1, "hashtags", query, options) do
-    prepare_tags(query, options)
-  end
-
   defp prepare_tags(query, options) do
     tags =
       query