First
[anni] / lib / pleroma / activity / search.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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(user)
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, user) when not is_nil(user) do
61     intended_recipients = [
62       Pleroma.Constants.as_public(),
63       Pleroma.Web.ActivityPub.Utils.as_local_public()
64     ]
65
66     from([a, o] in q,
67       where: fragment("?->>'type' = 'Create'", a.data),
68       where: fragment("? && ?", ^intended_recipients, a.recipients)
69     )
70   end
71
72   defp restrict_public(q, _user) do
73     from([a, o] in q,
74       where: fragment("?->>'type' = 'Create'", a.data),
75       where: ^Pleroma.Constants.as_public() in a.recipients
76     )
77   end
78
79   defp query_with(q, :gin, search_query, :plain) do
80     %{rows: [[tsc]]} =
81       Ecto.Adapters.SQL.query!(
82         Pleroma.Repo,
83         "select current_setting('default_text_search_config')::regconfig::oid;"
84       )
85
86     from([a, o] in q,
87       where:
88         fragment(
89           "to_tsvector(?::oid::regconfig, ?->>'content') @@ plainto_tsquery(?)",
90           ^tsc,
91           o.data,
92           ^search_query
93         )
94     )
95   end
96
97   defp query_with(q, :gin, search_query, :websearch) do
98     %{rows: [[tsc]]} =
99       Ecto.Adapters.SQL.query!(
100         Pleroma.Repo,
101         "select current_setting('default_text_search_config')::regconfig::oid;"
102       )
103
104     from([a, o] in q,
105       where:
106         fragment(
107           "to_tsvector(?::oid::regconfig, ?->>'content') @@ websearch_to_tsquery(?)",
108           ^tsc,
109           o.data,
110           ^search_query
111         )
112     )
113   end
114
115   defp query_with(q, :rum, search_query, :plain) do
116     from([a, o] in q,
117       where:
118         fragment(
119           "? @@ plainto_tsquery(?)",
120           o.fts_content,
121           ^search_query
122         ),
123       order_by: [fragment("? <=> now()::date", o.inserted_at)]
124     )
125   end
126
127   defp query_with(q, :rum, search_query, :websearch) do
128     from([a, o] in q,
129       where:
130         fragment(
131           "? @@ websearch_to_tsquery(?)",
132           o.fts_content,
133           ^search_query
134         ),
135       order_by: [fragment("? <=> now()::date", o.inserted_at)]
136     )
137   end
138
139   defp maybe_restrict_local(q, user) do
140     limit = Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
141
142     case {limit, user} do
143       {:all, _} -> restrict_local(q)
144       {:unauthenticated, %User{}} -> q
145       {:unauthenticated, _} -> restrict_local(q)
146       {false, _} -> q
147     end
148   end
149
150   defp restrict_local(q), do: where(q, local: true)
151
152   defp maybe_fetch(activities, user, search_query) do
153     with true <- Regex.match?(~r/https?:/, search_query),
154          {:ok, object} <- Fetcher.fetch_object_from_id(search_query),
155          %Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
156          true <- Visibility.visible_for_user?(activity, user) do
157       [activity | activities]
158     else
159       _ -> activities
160     end
161   end
162 end