Activity.Search: fallback on status resolution on DB Timeout
[akkoma] / lib / pleroma / activity / search.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.Activity.Search do
6 alias Pleroma.Activity
7 alias Pleroma.Object.Fetcher
8 alias Pleroma.Pagination
9 alias Pleroma.User
10 alias Pleroma.Web.ActivityPub.Visibility
11
12 require Pleroma.Constants
13
14 import Ecto.Query
15
16 def search(user, search_query, options \\ []) do
17 index_type = if Pleroma.Config.get([:database, :rum_enabled]), do: :rum, else: :gin
18 limit = Enum.min([Keyword.get(options, :limit), 40])
19 offset = Keyword.get(options, :offset, 0)
20 author = Keyword.get(options, :author)
21
22 search_function =
23 if :persistent_term.get({Pleroma.Repo, :postgres_version}) >= 11 do
24 :websearch
25 else
26 :plain
27 end
28
29 try do
30 Activity
31 |> Activity.with_preloaded_object()
32 |> Activity.restrict_deactivated_users()
33 |> restrict_public()
34 |> query_with(index_type, search_query, search_function)
35 |> maybe_restrict_local(user)
36 |> maybe_restrict_author(author)
37 |> maybe_restrict_blocked(user)
38 |> Pagination.fetch_paginated(
39 %{"offset" => offset, "limit" => limit, "skip_order" => index_type == :rum},
40 :offset
41 )
42 |> maybe_fetch(user, search_query)
43 rescue
44 _ -> maybe_fetch([], user, search_query)
45 end
46 end
47
48 def maybe_restrict_author(query, %User{} = author) do
49 Activity.Queries.by_author(query, author)
50 end
51
52 def maybe_restrict_author(query, _), do: query
53
54 def maybe_restrict_blocked(query, %User{} = user) do
55 Activity.Queries.exclude_authors(query, User.blocked_users_ap_ids(user))
56 end
57
58 def maybe_restrict_blocked(query, _), do: query
59
60 defp restrict_public(q) do
61 from([a, o] in q,
62 where: fragment("?->>'type' = 'Create'", a.data),
63 where: ^Pleroma.Constants.as_public() in a.recipients
64 )
65 end
66
67 defp query_with(q, :gin, search_query, :plain) do
68 from([a, o] in q,
69 where:
70 fragment(
71 "to_tsvector(?->>'content') @@ plainto_tsquery(?)",
72 o.data,
73 ^search_query
74 )
75 )
76 end
77
78 defp query_with(q, :gin, search_query, :websearch) do
79 from([a, o] in q,
80 where:
81 fragment(
82 "to_tsvector(?->>'content') @@ websearch_to_tsquery(?)",
83 o.data,
84 ^search_query
85 )
86 )
87 end
88
89 defp query_with(q, :rum, search_query, :plain) do
90 from([a, o] in q,
91 where:
92 fragment(
93 "? @@ plainto_tsquery(?)",
94 o.fts_content,
95 ^search_query
96 ),
97 order_by: [fragment("? <=> now()::date", o.inserted_at)]
98 )
99 end
100
101 defp query_with(q, :rum, search_query, :websearch) do
102 from([a, o] in q,
103 where:
104 fragment(
105 "? @@ websearch_to_tsquery(?)",
106 o.fts_content,
107 ^search_query
108 ),
109 order_by: [fragment("? <=> now()::date", o.inserted_at)]
110 )
111 end
112
113 defp maybe_restrict_local(q, user) do
114 limit = Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
115
116 case {limit, user} do
117 {:all, _} -> restrict_local(q)
118 {:unauthenticated, %User{}} -> q
119 {:unauthenticated, _} -> restrict_local(q)
120 {false, _} -> q
121 end
122 end
123
124 defp restrict_local(q), do: where(q, local: true)
125
126 defp maybe_fetch(activities, user, search_query) do
127 with true <- Regex.match?(~r/https?:/, search_query),
128 {:ok, object} <- Fetcher.fetch_object_from_id(search_query),
129 %Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
130 true <- Visibility.visible_for_user?(activity, user) do
131 [activity | activities]
132 else
133 _ -> activities
134 end
135 end
136 end