Initial meilisearch implementation, doesn't delete posts yet
[akkoma] / lib / pleroma / web / mastodon_api / controllers / search_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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.Repo
9 alias Pleroma.User
10 alias Pleroma.Web.ControllerHelper
11 alias Pleroma.Web.MastodonAPI.AccountView
12 alias Pleroma.Web.Plugs.OAuthScopesPlug
13 alias Pleroma.Web.Plugs.RateLimiter
14
15 require Logger
16
17 @search_limit 40
18
19 plug(Pleroma.Web.ApiSpec.CastAndValidate)
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 # Note: on private instances auth is required (EnsurePublicOrAuthenticatedPlug is not skipped)
25
26 plug(RateLimiter, [name: :search] when action in [:search, :search2, :account_search])
27
28 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.SearchOperation
29
30 def account_search(%{assigns: %{user: user}} = conn, %{q: query} = params) do
31 accounts = User.search(query, search_options(params, user))
32
33 conn
34 |> put_view(AccountView)
35 |> render("index.json",
36 users: accounts,
37 for: user,
38 as: :user
39 )
40 end
41
42 def search2(conn, params), do: do_search(:v2, conn, params)
43 def search(conn, params), do: do_search(:v1, conn, params)
44
45 defp do_search(version, %{assigns: %{user: user}} = conn, params) do
46 options =
47 search_options(params, user)
48 |> Keyword.put(:version, version)
49
50 search_provider = Pleroma.Config.get([:search, :provider])
51 json(conn, search_provider.search(conn, params, options))
52 end
53
54 defp search_options(params, user) do
55 [
56 resolve: params[:resolve],
57 following: params[:following],
58 limit: min(params[:limit], @search_limit),
59 offset: params[:offset],
60 type: params[:type],
61 author: get_author(params),
62 embed_relationships: ControllerHelper.embed_relationships?(params),
63 for_user: user
64 ]
65 |> Enum.filter(&elem(&1, 1))
66 end
67
68 defp resource_search(_, "accounts", query, options) do
69 accounts = with_fallback(fn -> User.search(query, options) end)
70
71 AccountView.render("index.json",
72 users: accounts,
73 for: options[:for_user],
74 embed_relationships: options[:embed_relationships]
75 )
76 end
77
78 defp resource_search(_, "statuses", query, options) do
79 search_module = Pleroma.Config.get([Pleroma.Search, :module], Pleroma.Activity)
80
81 statuses = with_fallback(fn -> search_module.search(options[:for_user], query, options) end)
82
83 StatusView.render("index.json",
84 activities: statuses,
85 for: options[:for_user],
86 as: :activity
87 )
88 end
89
90 defp resource_search(:v2, "hashtags", query, options) do
91 tags_path = Endpoint.url() <> "/tag/"
92
93 query
94 |> prepare_tags(options)
95 |> Enum.map(fn tag ->
96 %{name: tag, url: tags_path <> tag}
97 end)
98 end
99
100 defp resource_search(:v1, "hashtags", query, options) do
101 prepare_tags(query, options)
102 end
103
104 defp prepare_tags(query, options) do
105 tags =
106 query
107 |> preprocess_uri_query()
108 |> String.split(~r/[^#\w]+/u, trim: true)
109 |> Enum.uniq_by(&String.downcase/1)
110
111 explicit_tags = Enum.filter(tags, fn tag -> String.starts_with?(tag, "#") end)
112
113 tags =
114 if Enum.any?(explicit_tags) do
115 explicit_tags
116 else
117 tags
118 end
119
120 tags = Enum.map(tags, fn tag -> String.trim_leading(tag, "#") end)
121
122 tags =
123 if Enum.empty?(explicit_tags) && !options[:skip_joined_tag] do
124 add_joined_tag(tags)
125 else
126 tags
127 end
128
129 Pleroma.Pagination.paginate(tags, options)
130 end
131
132 defp add_joined_tag(tags) do
133 tags
134 |> Kernel.++([joined_tag(tags)])
135 |> Enum.uniq_by(&String.downcase/1)
136 end
137
138 # If `query` is a URI, returns last component of its path, otherwise returns `query`
139 defp preprocess_uri_query(query) do
140 if query =~ ~r/https?:\/\// do
141 query
142 |> String.trim_trailing("/")
143 |> URI.parse()
144 |> Map.get(:path)
145 |> String.split("/")
146 |> Enum.at(-1)
147 else
148 query
149 end
150 end
151
152 defp joined_tag(tags) do
153 tags
154 |> Enum.map(fn tag -> String.capitalize(tag) end)
155 |> Enum.join()
156 end
157
158 defp with_fallback(f, fallback \\ []) do
159 try do
160 f.()
161 rescue
162 error ->
163 Logger.error("#{__MODULE__} search error: #{inspect(error)}")
164 fallback
165 end
166 end
167
168 defp get_author(%{account_id: account_id}) when is_binary(account_id),
169 do: User.get_cached_by_id(account_id)
170
171 defp get_author(_params), do: nil
172 end