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