Add MastoFEController
[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.RateLimiter
10 alias Pleroma.Repo
11 alias Pleroma.User
12 alias Pleroma.Web
13 alias Pleroma.Web.ControllerHelper
14 alias Pleroma.Web.MastodonAPI.AccountView
15 alias Pleroma.Web.MastodonAPI.StatusView
16
17 require Logger
18 plug(RateLimiter, :search when action in [:search, :search2, :account_search])
19
20 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
21 accounts = User.search(query, search_options(params, user))
22
23 conn
24 |> put_view(AccountView)
25 |> render("index.json", users: accounts, for: user, as: :user)
26 end
27
28 def search2(conn, params), do: do_search(:v2, conn, params)
29 def search(conn, params), do: do_search(:v1, conn, params)
30
31 defp do_search(version, %{assigns: %{user: user}} = conn, %{"q" => query} = params) do
32 options = search_options(params, user)
33 timeout = Keyword.get(Repo.config(), :timeout, 15_000)
34 default_values = %{"statuses" => [], "accounts" => [], "hashtags" => []}
35
36 result =
37 default_values
38 |> Enum.map(fn {resource, default_value} ->
39 if params["type"] == nil or params["type"] == resource do
40 {resource, fn -> resource_search(version, resource, query, options) end}
41 else
42 {resource, fn -> default_value end}
43 end
44 end)
45 |> Task.async_stream(fn {resource, f} -> {resource, with_fallback(f)} end,
46 timeout: timeout,
47 on_timeout: :kill_task
48 )
49 |> Enum.reduce(default_values, fn
50 {:ok, {resource, result}}, acc ->
51 Map.put(acc, resource, result)
52
53 _error, acc ->
54 acc
55 end)
56
57 json(conn, result)
58 end
59
60 defp search_options(params, user) do
61 [
62 resolve: params["resolve"] == "true",
63 following: params["following"] == "true",
64 limit: ControllerHelper.fetch_integer_param(params, "limit"),
65 offset: ControllerHelper.fetch_integer_param(params, "offset"),
66 type: params["type"],
67 author: get_author(params),
68 for_user: user
69 ]
70 |> Enum.filter(&elem(&1, 1))
71 end
72
73 defp resource_search(_, "accounts", query, options) do
74 accounts = with_fallback(fn -> User.search(query, options) end)
75 AccountView.render("index.json", users: accounts, for: options[:for_user], as: :user)
76 end
77
78 defp resource_search(_, "statuses", query, options) do
79 statuses = with_fallback(fn -> Activity.search(options[:for_user], query, options) end)
80 StatusView.render("index.json", activities: statuses, for: options[:for_user], as: :activity)
81 end
82
83 defp resource_search(:v2, "hashtags", query, _options) do
84 tags_path = Web.base_url() <> "/tag/"
85
86 query
87 |> prepare_tags()
88 |> Enum.map(fn tag ->
89 tag = String.trim_leading(tag, "#")
90 %{name: tag, url: tags_path <> tag}
91 end)
92 end
93
94 defp resource_search(:v1, "hashtags", query, _options) do
95 query
96 |> prepare_tags()
97 |> Enum.map(fn tag -> String.trim_leading(tag, "#") end)
98 end
99
100 defp prepare_tags(query) do
101 query
102 |> String.split()
103 |> Enum.uniq()
104 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
105 end
106
107 defp with_fallback(f, fallback \\ []) do
108 try do
109 f.()
110 rescue
111 error ->
112 Logger.error("#{__MODULE__} search error: #{inspect(error)}")
113 fallback
114 end
115 end
116
117 defp get_author(%{"account_id" => account_id}) when is_binary(account_id),
118 do: User.get_cached_by_id(account_id)
119
120 defp get_author(_params), do: nil
121 end