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.MastodonAPI.AccountView
15 alias Pleroma.Web.MastodonAPI.StatusView
19 plug(Pleroma.Web.ApiSpec.CastAndValidate)
21 # Note: Mastodon doesn't allow unauthenticated access (requires read:accounts / read:search)
22 plug(OAuthScopesPlug, %{scopes: ["read:search"], fallback: :proceed_unauthenticated})
24 # Note: on private instances auth is required (EnsurePublicOrAuthenticatedPlug is not skipped)
26 plug(RateLimiter, [name: :search] when action in [:search, :search2, :account_search])
28 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.SearchOperation
30 def account_search(%{assigns: %{user: user}} = conn, %{q: query} = params) do
31 accounts = User.search(query, search_options(params, user))
34 |> put_view(AccountView)
35 |> render("index.json", users: accounts, for: user, as: :user)
38 def search2(conn, params), do: do_search(:v2, conn, params)
39 def search(conn, params), do: do_search(:v1, conn, params)
41 defp do_search(version, %{assigns: %{user: user}} = conn, %{q: query} = params) do
42 options = search_options(params, user)
43 timeout = Keyword.get(Repo.config(), :timeout, 15_000)
44 default_values = %{"statuses" => [], "accounts" => [], "hashtags" => []}
48 |> Enum.map(fn {resource, default_value} ->
49 if params[:type] in [nil, resource] do
50 {resource, fn -> resource_search(version, resource, query, options) end}
52 {resource, fn -> default_value end}
55 |> Task.async_stream(fn {resource, f} -> {resource, with_fallback(f)} end,
57 on_timeout: :kill_task
59 |> Enum.reduce(default_values, fn
60 {:ok, {resource, result}}, acc ->
61 Map.put(acc, resource, result)
70 defp search_options(params, user) do
72 resolve: params[:resolve],
73 following: params[:following],
74 limit: params[:limit],
75 offset: params[:offset],
77 author: get_author(params),
80 |> Enum.filter(&elem(&1, 1))
83 defp resource_search(_, "accounts", query, options) do
84 accounts = with_fallback(fn -> User.search(query, options) end)
86 AccountView.render("index.json",
88 for: options[:for_user],
93 defp resource_search(_, "statuses", query, options) do
94 statuses = with_fallback(fn -> Activity.search(options[:for_user], query, options) end)
96 StatusView.render("index.json",
98 for: options[:for_user],
103 defp resource_search(:v2, "hashtags", query, _options) do
104 tags_path = Web.base_url() <> "/tag/"
108 |> Enum.map(fn tag ->
109 tag = String.trim_leading(tag, "#")
110 %{name: tag, url: tags_path <> tag}
114 defp resource_search(:v1, "hashtags", query, _options) do
117 |> Enum.map(fn tag -> String.trim_leading(tag, "#") end)
120 defp prepare_tags(query) do
124 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
127 defp with_fallback(f, fallback \\ []) do
132 Logger.error("#{__MODULE__} search error: #{inspect(error)}")
137 defp get_author(%{account_id: account_id}) when is_binary(account_id),
138 do: User.get_cached_by_id(account_id)
140 defp get_author(_params), do: nil