Fix tests
[akkoma] / lib / pleroma / web / mastodon_api / controllers / search_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.SearchController do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.Activity
9 alias Pleroma.Plugs.OAuthScopesPlug
10 alias Pleroma.Plugs.RateLimiter
11 alias Pleroma.Repo
12 alias Pleroma.User
13 alias Pleroma.Web
14 alias Pleroma.Web.ControllerHelper
15 alias Pleroma.Web.MastodonAPI.AccountView
16 alias Pleroma.Web.MastodonAPI.StatusView
17
18 require Logger
19
20 # Note: Mastodon doesn't allow unauthenticated access (requires read:accounts / read:search)
21 plug(OAuthScopesPlug, %{scopes: ["read:search"], fallback: :proceed_unauthenticated})
22
23 plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
24
25 plug(RateLimiter, :search when action in [:search, :search2, :account_search])
26
27 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
28 accounts = User.search(query, search_options(params, user))
29
30 conn
31 |> put_view(AccountView)
32 |> render("index.json", users: accounts, for: user, as: :user)
33 end
34
35 def search2(conn, params), do: do_search(:v2, conn, params)
36 def search(conn, params), do: do_search(:v1, conn, params)
37
38 defp do_search(version, %{assigns: %{user: user}} = conn, %{"q" => query} = params) do
39 options = search_options(params, user)
40 timeout = Keyword.get(Repo.config(), :timeout, 15_000)
41 default_values = %{"statuses" => [], "accounts" => [], "hashtags" => []}
42
43 result =
44 default_values
45 |> Enum.map(fn {resource, default_value} ->
46 if params["type"] == nil or params["type"] == resource do
47 {resource, fn -> resource_search(version, resource, query, options) end}
48 else
49 {resource, fn -> default_value end}
50 end
51 end)
52 |> Task.async_stream(fn {resource, f} -> {resource, with_fallback(f)} end,
53 timeout: timeout,
54 on_timeout: :kill_task
55 )
56 |> Enum.reduce(default_values, fn
57 {:ok, {resource, result}}, acc ->
58 Map.put(acc, resource, result)
59
60 _error, acc ->
61 acc
62 end)
63
64 json(conn, result)
65 end
66
67 defp search_options(params, user) do
68 [
69 resolve: params["resolve"] == "true",
70 following: params["following"] == "true",
71 limit: ControllerHelper.fetch_integer_param(params, "limit"),
72 offset: ControllerHelper.fetch_integer_param(params, "offset"),
73 type: params["type"],
74 author: get_author(params),
75 for_user: user
76 ]
77 |> Enum.filter(&elem(&1, 1))
78 end
79
80 defp resource_search(_, "accounts", query, options) do
81 accounts = with_fallback(fn -> User.search(query, options) end)
82 AccountView.render("index.json", users: accounts, for: options[:for_user], as: :user)
83 end
84
85 defp resource_search(_, "statuses", query, options) do
86 statuses = with_fallback(fn -> Activity.search(options[:for_user], query, options) end)
87 StatusView.render("index.json", activities: statuses, for: options[:for_user], as: :activity)
88 end
89
90 defp resource_search(:v2, "hashtags", query, _options) do
91 tags_path = Web.base_url() <> "/tag/"
92
93 query
94 |> prepare_tags()
95 |> Enum.map(fn tag ->
96 tag = String.trim_leading(tag, "#")
97 %{name: tag, url: tags_path <> tag}
98 end)
99 end
100
101 defp resource_search(:v1, "hashtags", query, _options) do
102 query
103 |> prepare_tags()
104 |> Enum.map(fn tag -> String.trim_leading(tag, "#") end)
105 end
106
107 defp prepare_tags(query) do
108 query
109 |> String.split()
110 |> Enum.uniq()
111 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
112 end
113
114 defp with_fallback(f, fallback \\ []) do
115 try do
116 f.()
117 rescue
118 error ->
119 Logger.error("#{__MODULE__} search error: #{inspect(error)}")
120 fallback
121 end
122 end
123
124 defp get_author(%{"account_id" => account_id}) when is_binary(account_id),
125 do: User.get_cached_by_id(account_id)
126
127 defp get_author(_params), do: nil
128 end