Automatic checks of authentication / instance publicity. Definition of missing OAuth...
[akkoma] / lib / pleroma / web / mastodon_api / controllers / search_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 import Pleroma.Web.ControllerHelper, only: [fetch_integer_param: 2, skip_relationships?: 1]
9
10 alias Pleroma.Activity
11 alias Pleroma.Plugs.OAuthScopesPlug
12 alias Pleroma.Plugs.RateLimiter
13 alias Pleroma.Repo
14 alias Pleroma.User
15 alias Pleroma.Web
16 alias Pleroma.Web.MastodonAPI.AccountView
17 alias Pleroma.Web.MastodonAPI.StatusView
18
19 require Logger
20
21 # Note: Mastodon doesn't allow unauthenticated access (requires read:accounts / read:search)
22 plug(OAuthScopesPlug, %{scopes: ["read:search"], fallback: :proceed_unauthenticated})
23
24 plug(RateLimiter, [name: :search] when action in [:search, :search2, :account_search])
25
26 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
27 accounts = User.search(query, search_options(params, user))
28
29 conn
30 |> put_view(AccountView)
31 |> render("index.json", users: accounts, for: user, as: :user)
32 end
33
34 def search2(conn, params), do: do_search(:v2, conn, params)
35 def search(conn, params), do: do_search(:v1, conn, params)
36
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" => []}
41
42 result =
43 default_values
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}
47 else
48 {resource, fn -> default_value end}
49 end
50 end)
51 |> Task.async_stream(fn {resource, f} -> {resource, with_fallback(f)} end,
52 timeout: timeout,
53 on_timeout: :kill_task
54 )
55 |> Enum.reduce(default_values, fn
56 {:ok, {resource, result}}, acc ->
57 Map.put(acc, resource, result)
58
59 _error, acc ->
60 acc
61 end)
62
63 json(conn, result)
64 end
65
66 defp search_options(params, user) do
67 [
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"),
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
83 AccountView.render("index.json",
84 users: accounts,
85 for: options[:for_user],
86 as: :user,
87 skip_relationships: false
88 )
89 end
90
91 defp resource_search(_, "statuses", query, options) do
92 statuses = with_fallback(fn -> Activity.search(options[:for_user], query, options) end)
93
94 StatusView.render("index.json",
95 activities: statuses,
96 for: options[:for_user],
97 as: :activity,
98 skip_relationships: options[:skip_relationships]
99 )
100 end
101
102 defp resource_search(:v2, "hashtags", query, _options) do
103 tags_path = Web.base_url() <> "/tag/"
104
105 query
106 |> prepare_tags()
107 |> Enum.map(fn tag ->
108 tag = String.trim_leading(tag, "#")
109 %{name: tag, url: tags_path <> tag}
110 end)
111 end
112
113 defp resource_search(:v1, "hashtags", query, _options) do
114 query
115 |> prepare_tags()
116 |> Enum.map(fn tag -> String.trim_leading(tag, "#") end)
117 end
118
119 defp prepare_tags(query) do
120 query
121 |> String.split()
122 |> Enum.uniq()
123 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
124 end
125
126 defp with_fallback(f, fallback \\ []) do
127 try do
128 f.()
129 rescue
130 error ->
131 Logger.error("#{__MODULE__} search error: #{inspect(error)}")
132 fallback
133 end
134 end
135
136 defp get_author(%{"account_id" => account_id}) when is_binary(account_id),
137 do: User.get_cached_by_id(account_id)
138
139 defp get_author(_params), do: nil
140 end