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
10 alias Pleroma.Web.ControllerHelper
11 alias Pleroma.Web.MastodonAPI.AccountView
12 alias Pleroma.Web.Plugs.OAuthScopesPlug
13 alias Pleroma.Web.Plugs.RateLimiter
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",
42 def search2(conn, params), do: do_search(:v2, conn, params)
43 def search(conn, params), do: do_search(:v1, conn, params)
45 defp do_search(version, %{assigns: %{user: user}} = conn, params) do
47 search_options(params, user)
48 |> Keyword.put(:version, version)
50 search_provider = Pleroma.Config.get([:search, :provider])
51 json(conn, search_provider.search(conn, params, options))
54 defp search_options(params, user) do
56 resolve: params[:resolve],
57 following: params[:following],
58 limit: min(params[:limit], @search_limit),
59 offset: params[:offset],
61 author: get_author(params),
62 embed_relationships: ControllerHelper.embed_relationships?(params),
65 |> Enum.filter(&elem(&1, 1))
68 defp resource_search(_, "accounts", query, options) do
69 accounts = with_fallback(fn -> User.search(query, options) end)
71 AccountView.render("index.json",
73 for: options[:for_user],
74 embed_relationships: options[:embed_relationships]
78 defp resource_search(_, "statuses", query, options) do
79 statuses = with_fallback(fn -> Pleroma.Search.DatabaseSearch.search(query, options) end)
81 StatusView.render("index.json",
83 for: options[:for_user],
88 defp resource_search(:v2, "hashtags", query, options) do
89 tags_path = Endpoint.url() <> "/tag/"
92 |> prepare_tags(options)
94 %{name: tag, url: tags_path <> tag}
98 defp resource_search(:v1, "hashtags", query, options) do
99 prepare_tags(query, options)
102 defp prepare_tags(query, options) do
105 |> preprocess_uri_query()
106 |> String.split(~r/[^#\w]+/u, trim: true)
107 |> Enum.uniq_by(&String.downcase/1)
109 explicit_tags = Enum.filter(tags, fn tag -> String.starts_with?(tag, "#") end)
112 if Enum.any?(explicit_tags) do
118 tags = Enum.map(tags, fn tag -> String.trim_leading(tag, "#") end)
121 if Enum.empty?(explicit_tags) && !options[:skip_joined_tag] do
127 Pleroma.Pagination.paginate(tags, options)
130 defp add_joined_tag(tags) do
132 |> Kernel.++([joined_tag(tags)])
133 |> Enum.uniq_by(&String.downcase/1)
136 # If `query` is a URI, returns last component of its path, otherwise returns `query`
137 defp preprocess_uri_query(query) do
138 if query =~ ~r/https?:\/\// do
140 |> String.trim_trailing("/")
150 defp joined_tag(tags) do
152 |> Enum.map(fn tag -> String.capitalize(tag) end)
156 defp with_fallback(f, fallback \\ []) do
161 Logger.error("#{__MODULE__} search error: #{inspect(error)}")
166 defp get_author(%{account_id: account_id}) when is_binary(account_id),
167 do: User.get_cached_by_id(account_id)
169 defp get_author(_params), do: nil