1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.SearchController do
6 use Pleroma.Web, :controller
11 alias Pleroma.Web.ControllerHelper
12 alias Pleroma.Web.Endpoint
13 alias Pleroma.Web.MastodonAPI.AccountView
14 alias Pleroma.Web.MastodonAPI.StatusView
15 alias Pleroma.Web.Plugs.OAuthScopesPlug
16 alias Pleroma.Web.Plugs.RateLimiter
20 plug(Pleroma.Web.ApiSpec.CastAndValidate)
22 # Note: Mastodon doesn't allow unauthenticated access (requires read:accounts / read:search)
23 plug(OAuthScopesPlug, %{scopes: ["read:search"], fallback: :proceed_unauthenticated})
25 # Note: on private instances auth is required (EnsurePublicOrAuthenticatedPlug is not skipped)
27 plug(RateLimiter, [name: :search] when action in [:search, :search2, :account_search])
29 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.SearchOperation
31 def account_search(%{assigns: %{user: user}} = conn, %{q: query} = params) do
32 accounts = User.search(query, search_options(params, user))
35 |> put_view(AccountView)
36 |> render("index.json",
43 def search2(conn, params), do: do_search(:v2, conn, params)
44 def search(conn, params), do: do_search(:v1, conn, params)
46 defp do_search(version, %{assigns: %{user: user}} = conn, %{q: query} = params) do
47 query = String.trim(query)
48 options = search_options(params, user)
49 if Pleroma.Config.get([:search, :provider]) == :elasticsearch do
50 elasticsearch_search(conn, query, options)
52 builtin_search(version, conn, params)
56 defp elasticsearch_search(%{assigns: %{user: user}} = conn, query, options) do
57 with {:ok, raw_results} <- Pleroma.Elasticsearch.search(query) do
59 |> Map.get(:body, %{})
60 |> Map.get("hits", %{})
61 |> Map.get("hits", [])
62 |> Enum.map(fn result -> result["_id"] end)
63 |> Pleroma.Activity.all_by_ids_with_object()
70 statuses: StatusView.render("index.json",
79 |> put_status(:internal_server_error)
80 |> json(%{error: "Search failed"})
84 defp builtin_search(version, %{assigns: %{user: user}} = conn, %{q: query} = params) do
85 options = search_options(params, user)
86 timeout = Keyword.get(Repo.config(), :timeout, 15_000)
87 default_values = %{"statuses" => [], "accounts" => [], "hashtags" => []}
91 |> Enum.map(fn {resource, default_value} ->
92 if params[:type] in [nil, resource] do
93 {resource, fn -> resource_search(version, resource, query, options) end}
95 {resource, fn -> default_value end}
98 |> Task.async_stream(fn {resource, f} -> {resource, with_fallback(f)} end,
100 on_timeout: :kill_task
102 |> Enum.reduce(default_values, fn
103 {:ok, {resource, result}}, acc ->
104 Map.put(acc, resource, result)
113 defp search_options(params, user) do
115 resolve: params[:resolve],
116 following: params[:following],
117 limit: params[:limit],
118 offset: params[:offset],
120 author: get_author(params),
121 embed_relationships: ControllerHelper.embed_relationships?(params),
124 |> Enum.filter(&elem(&1, 1))
127 defp resource_search(_, "accounts", query, options) do
128 accounts = with_fallback(fn -> User.search(query, options) end)
130 AccountView.render("index.json",
132 for: options[:for_user],
133 embed_relationships: options[:embed_relationships]
137 defp resource_search(_, "statuses", query, options) do
138 statuses = with_fallback(fn -> Activity.search(options[:for_user], query, options) end)
140 StatusView.render("index.json",
141 activities: statuses,
142 for: options[:for_user],
147 defp resource_search(:v2, "hashtags", query, options) do
148 tags_path = Endpoint.url() <> "/tag/"
151 |> prepare_tags(options)
152 |> Enum.map(fn tag ->
153 %{name: tag, url: tags_path <> tag}
157 defp resource_search(:v1, "hashtags", query, options) do
158 prepare_tags(query, options)
161 defp prepare_tags(query, options) do
164 |> preprocess_uri_query()
165 |> String.split(~r/[^#\w]+/u, trim: true)
166 |> Enum.uniq_by(&String.downcase/1)
168 explicit_tags = Enum.filter(tags, fn tag -> String.starts_with?(tag, "#") end)
171 if Enum.any?(explicit_tags) do
177 tags = Enum.map(tags, fn tag -> String.trim_leading(tag, "#") end)
180 if Enum.empty?(explicit_tags) && !options[:skip_joined_tag] do
186 Pleroma.Pagination.paginate(tags, options)
189 defp add_joined_tag(tags) do
191 |> Kernel.++([joined_tag(tags)])
192 |> Enum.uniq_by(&String.downcase/1)
195 # If `query` is a URI, returns last component of its path, otherwise returns `query`
196 defp preprocess_uri_query(query) do
197 if query =~ ~r/https?:\/\// do
199 |> String.trim_trailing("/")
209 defp joined_tag(tags) do
211 |> Enum.map(fn tag -> String.capitalize(tag) end)
215 defp with_fallback(f, fallback \\ []) do
220 Logger.error("#{__MODULE__} search error: #{inspect(error)}")
225 defp get_author(%{account_id: account_id}) when is_binary(account_id),
226 do: User.get_cached_by_id(account_id)
228 defp get_author(_params), do: nil