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