Merge branch 'develop' into stable
[akkoma] / lib / pleroma / search / database_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.Search.DatabaseSearch 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 @behaviour Pleroma.Search.SearchBackend
17
18 def search(user, search_query, options \\ []) do
19 index_type = if Pleroma.Config.get([:database, :rum_enabled]), do: :rum, else: :gin
20 limit = Enum.min([Keyword.get(options, :limit), 40])
21 offset = Keyword.get(options, :offset, 0)
22 author = Keyword.get(options, :author)
23
24 search_function =
25 if :persistent_term.get({Pleroma.Repo, :postgres_version}) >= 11 do
26 :websearch
27 else
28 :plain
29 end
30
31 try do
32 Activity
33 |> Activity.with_preloaded_object()
34 |> Activity.restrict_deactivated_users()
35 |> restrict_public()
36 |> query_with(index_type, search_query, search_function)
37 |> maybe_restrict_local(user)
38 |> maybe_restrict_author(author)
39 |> maybe_restrict_blocked(user)
40 |> Pagination.fetch_paginated(
41 %{"offset" => offset, "limit" => limit, "skip_order" => index_type == :rum},
42 :offset
43 )
44 |> maybe_fetch(user, search_query)
45 rescue
46 _ -> maybe_fetch([], user, search_query)
47 end
48 end
49
50 @impl true
51 def add_to_index(_activity), do: nil
52
53 @impl true
54 def remove_from_index(_object), do: nil
55
56 def maybe_restrict_author(query, %User{} = author) do
57 Activity.Queries.by_author(query, author)
58 end
59
60 def maybe_restrict_author(query, _), do: query
61
62 def maybe_restrict_blocked(query, %User{} = user) do
63 Activity.Queries.exclude_authors(query, User.blocked_users_ap_ids(user))
64 end
65
66 def maybe_restrict_blocked(query, _), do: query
67
68 def restrict_public(q) do
69 from([a, o] in q,
70 where: fragment("?->>'type' = 'Create'", a.data),
71 where: ^Pleroma.Constants.as_public() in a.recipients
72 )
73 end
74
75 defp query_with(q, :gin, search_query, :plain) do
76 %{rows: [[tsc]]} =
77 Ecto.Adapters.SQL.query!(
78 Pleroma.Repo,
79 "select current_setting('default_text_search_config')::regconfig::oid;"
80 )
81
82 from([a, o] in q,
83 where:
84 fragment(
85 "to_tsvector(?::oid::regconfig, ?->>'content') @@ plainto_tsquery(?)",
86 ^tsc,
87 o.data,
88 ^search_query
89 )
90 )
91 end
92
93 defp query_with(q, :gin, search_query, :websearch) do
94 %{rows: [[tsc]]} =
95 Ecto.Adapters.SQL.query!(
96 Pleroma.Repo,
97 "select current_setting('default_text_search_config')::regconfig::oid;"
98 )
99
100 from([a, o] in q,
101 where:
102 fragment(
103 "to_tsvector(?::oid::regconfig, ?->>'content') @@ websearch_to_tsquery(?)",
104 ^tsc,
105 o.data,
106 ^search_query
107 )
108 )
109 end
110
111 defp query_with(q, :rum, search_query, :plain) do
112 from([a, o] in q,
113 where:
114 fragment(
115 "? @@ plainto_tsquery(?)",
116 o.fts_content,
117 ^search_query
118 ),
119 order_by: [fragment("? <=> now()::date", o.inserted_at)]
120 )
121 end
122
123 defp query_with(q, :rum, search_query, :websearch) do
124 from([a, o] in q,
125 where:
126 fragment(
127 "? @@ websearch_to_tsquery(?)",
128 o.fts_content,
129 ^search_query
130 ),
131 order_by: [fragment("? <=> now()::date", o.inserted_at)]
132 )
133 end
134
135 def maybe_restrict_local(q, user) do
136 limit = Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
137
138 case {limit, user} do
139 {:all, _} -> restrict_local(q)
140 {:unauthenticated, %User{}} -> q
141 {:unauthenticated, _} -> restrict_local(q)
142 {false, _} -> q
143 end
144 end
145
146 defp restrict_local(q), do: where(q, local: true)
147
148 def maybe_fetch(activities, user, search_query) do
149 with true <- Regex.match?(~r/https?:/, search_query),
150 {:ok, object} <- Fetcher.fetch_object_from_id(search_query),
151 %Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
152 true <- Visibility.visible_for_user?(activity, user) do
153 [activity | activities]
154 else
155 _ -> activities
156 end
157 end
158 end