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
8 import Pleroma.Web.ControllerHelper, only: [fetch_integer_param: 2, skip_relationships?: 1]
10 alias Pleroma.Activity
11 alias Pleroma.Plugs.OAuthScopesPlug
12 alias Pleroma.Plugs.RateLimiter
16 alias Pleroma.Web.MastodonAPI.AccountView
17 alias Pleroma.Web.MastodonAPI.StatusView
21 # Note: Mastodon doesn't allow unauthenticated access (requires read:accounts / read:search)
22 plug(OAuthScopesPlug, %{scopes: ["read:search"], fallback: :proceed_unauthenticated})
24 plug(RateLimiter, [name: :search] when action in [:search, :search2, :account_search])
26 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
27 accounts = User.search(query, search_options(params, user))
30 |> put_view(AccountView)
31 |> render("index.json", users: accounts, for: user, as: :user)
34 def search2(conn, params), do: do_search(:v2, conn, params)
35 def search(conn, params), do: do_search(:v1, conn, params)
37 defp do_search(version, %{assigns: %{user: user}} = conn, %{"q" => query} = params) do
38 options = search_options(params, user)
39 timeout = Keyword.get(Repo.config(), :timeout, 15_000)
40 default_values = %{"statuses" => [], "accounts" => [], "hashtags" => []}
44 |> Enum.map(fn {resource, default_value} ->
45 if params["type"] in [nil, resource] do
46 {resource, fn -> resource_search(version, resource, query, options) end}
48 {resource, fn -> default_value end}
51 |> Task.async_stream(fn {resource, f} -> {resource, with_fallback(f)} end,
53 on_timeout: :kill_task
55 |> Enum.reduce(default_values, fn
56 {:ok, {resource, result}}, acc ->
57 Map.put(acc, resource, result)
66 defp search_options(params, user) do
68 skip_relationships: skip_relationships?(params),
69 resolve: params["resolve"] == "true",
70 following: params["following"] == "true",
71 limit: fetch_integer_param(params, "limit"),
72 offset: fetch_integer_param(params, "offset"),
74 author: get_author(params),
77 |> Enum.filter(&elem(&1, 1))
80 defp resource_search(_, "accounts", query, options) do
81 accounts = with_fallback(fn -> User.search(query, options) end)
83 AccountView.render("index.json",
85 for: options[:for_user],
87 skip_relationships: false
91 defp resource_search(_, "statuses", query, options) do
92 statuses = with_fallback(fn -> Activity.search(options[:for_user], query, options) end)
94 StatusView.render("index.json",
96 for: options[:for_user],
98 skip_relationships: options[:skip_relationships]
102 defp resource_search(:v2, "hashtags", query, _options) do
103 tags_path = Web.base_url() <> "/tag/"
107 |> Enum.map(fn tag ->
108 tag = String.trim_leading(tag, "#")
109 %{name: tag, url: tags_path <> tag}
113 defp resource_search(:v1, "hashtags", query, _options) do
116 |> Enum.map(fn tag -> String.trim_leading(tag, "#") end)
119 defp prepare_tags(query) do
123 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
126 defp with_fallback(f, fallback \\ []) do
131 Logger.error("#{__MODULE__} search error: #{inspect(error)}")
136 defp get_author(%{"account_id" => account_id}) when is_binary(account_id),
137 do: User.get_cached_by_id(account_id)
139 defp get_author(_params), do: nil