1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
9 alias Pleroma.Plugs.OAuthScopesPlug
10 alias Pleroma.Plugs.RateLimiter
14 alias Pleroma.Web.ControllerHelper
15 alias Pleroma.Web.MastodonAPI.AccountView
16 alias Pleroma.Web.MastodonAPI.StatusView
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 options = search_options(params, user)
48 timeout = Keyword.get(Repo.config(), :timeout, 15_000)
49 default_values = %{"statuses" => [], "accounts" => [], "hashtags" => []}
53 |> Enum.map(fn {resource, default_value} ->
54 if params[:type] in [nil, resource] do
55 {resource, fn -> resource_search(version, resource, query, options) end}
57 {resource, fn -> default_value end}
60 |> Task.async_stream(fn {resource, f} -> {resource, with_fallback(f)} end,
62 on_timeout: :kill_task
64 |> Enum.reduce(default_values, fn
65 {:ok, {resource, result}}, acc ->
66 Map.put(acc, resource, result)
75 defp search_options(params, user) do
77 resolve: params[:resolve],
78 following: params[:following],
79 limit: params[:limit],
80 offset: params[:offset],
82 author: get_author(params),
83 embed_relationships: ControllerHelper.embed_relationships?(params),
86 |> Enum.filter(&elem(&1, 1))
89 defp resource_search(_, "accounts", query, options) do
90 accounts = with_fallback(fn -> User.search(query, options) end)
92 AccountView.render("index.json",
94 for: options[:for_user],
96 embed_relationships: options[:embed_relationships]
100 defp resource_search(_, "statuses", query, options) do
101 statuses = with_fallback(fn -> Activity.search(options[:for_user], query, options) end)
103 StatusView.render("index.json",
104 activities: statuses,
105 for: options[:for_user],
110 defp resource_search(:v2, "hashtags", query, _options) do
111 tags_path = Web.base_url() <> "/tag/"
115 |> Enum.map(fn tag ->
116 tag = String.trim_leading(tag, "#")
117 %{name: tag, url: tags_path <> tag}
121 defp resource_search(:v1, "hashtags", query, _options) do
124 |> Enum.map(fn tag -> String.trim_leading(tag, "#") end)
127 defp prepare_tags(query) do
131 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
134 defp with_fallback(f, fallback \\ []) do
139 Logger.error("#{__MODULE__} search error: #{inspect(error)}")
144 defp get_author(%{account_id: account_id}) when is_binary(account_id),
145 do: User.get_cached_by_id(account_id)
147 defp get_author(_params), do: nil