make search provider configurable
[akkoma] / lib / pleroma / search / builtin.ex
1 defmodule Pleroma.Search.Builtin do
2 @behaviour Pleroma.Search
3
4 alias Pleroma.Repo
5 alias Pleroma.User
6 alias Pleroma.Activity
7 alias Pleroma.Web.MastodonAPI.AccountView
8 alias Pleroma.Web.MastodonAPI.StatusView
9 alias Pleroma.Web.Endpoint
10
11 require Logger
12
13 @impl Pleroma.Search
14 def search(_conn, %{q: query} = params, options) do
15 version = Keyword.get(options, :version)
16 timeout = Keyword.get(Repo.config(), :timeout, 15_000)
17 default_values = %{"statuses" => [], "accounts" => [], "hashtags" => []}
18
19 default_values
20 |> Enum.map(fn {resource, default_value} ->
21 if params[:type] in [nil, resource] do
22 {resource, fn -> resource_search(version, resource, query, options) end}
23 else
24 {resource, fn -> default_value end}
25 end
26 end)
27 |> Task.async_stream(fn {resource, f} -> {resource, with_fallback(f)} end,
28 timeout: timeout,
29 on_timeout: :kill_task
30 )
31 |> Enum.reduce(default_values, fn
32 {:ok, {resource, result}}, acc ->
33 Map.put(acc, resource, result)
34
35 _error, acc ->
36 acc
37 end)
38 end
39
40 defp resource_search(_, "accounts", query, options) do
41 accounts = with_fallback(fn -> User.search(query, options) end)
42
43 AccountView.render("index.json",
44 users: accounts,
45 for: options[:for_user],
46 embed_relationships: options[:embed_relationships]
47 )
48 end
49
50 defp resource_search(_, "statuses", query, options) do
51 statuses = with_fallback(fn -> Activity.search(options[:for_user], query, options) end)
52
53 StatusView.render("index.json",
54 activities: statuses,
55 for: options[:for_user],
56 as: :activity
57 )
58 end
59
60 defp resource_search(:v2, "hashtags", query, options) do
61 tags_path = Endpoint.url() <> "/tag/"
62
63 query
64 |> prepare_tags(options)
65 |> Enum.map(fn tag ->
66 %{name: tag, url: tags_path <> tag}
67 end)
68 end
69
70 defp resource_search(:v1, "hashtags", query, options) do
71 prepare_tags(query, options)
72 end
73
74 defp prepare_tags(query, options) do
75 tags =
76 query
77 |> preprocess_uri_query()
78 |> String.split(~r/[^#\w]+/u, trim: true)
79 |> Enum.uniq_by(&String.downcase/1)
80
81 explicit_tags = Enum.filter(tags, fn tag -> String.starts_with?(tag, "#") end)
82
83 tags =
84 if Enum.any?(explicit_tags) do
85 explicit_tags
86 else
87 tags
88 end
89
90 tags = Enum.map(tags, fn tag -> String.trim_leading(tag, "#") end)
91
92 tags =
93 if Enum.empty?(explicit_tags) && !options[:skip_joined_tag] do
94 add_joined_tag(tags)
95 else
96 tags
97 end
98
99 Pleroma.Pagination.paginate(tags, options)
100 end
101
102 # If `query` is a URI, returns last component of its path, otherwise returns `query`
103 defp preprocess_uri_query(query) do
104 if query =~ ~r/https?:\/\// do
105 query
106 |> String.trim_trailing("/")
107 |> URI.parse()
108 |> Map.get(:path)
109 |> String.split("/")
110 |> Enum.at(-1)
111 else
112 query
113 end
114 end
115
116 defp add_joined_tag(tags) do
117 tags
118 |> Kernel.++([joined_tag(tags)])
119 |> Enum.uniq_by(&String.downcase/1)
120 end
121
122 defp joined_tag(tags) do
123 tags
124 |> Enum.map(fn tag -> String.capitalize(tag) end)
125 |> Enum.join()
126 end
127
128 defp with_fallback(f, fallback \\ []) do
129 try do
130 f.()
131 rescue
132 error ->
133 Logger.error("#{__MODULE__} search error: #{inspect(error)}")
134 fallback
135 end
136 end
137 end