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