MRF: create MRF.Policy behaviour separate from MRF module
[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 Activity
30 |> Activity.with_preloaded_object()
31 |> Activity.restrict_deactivated_users()
32 |> restrict_public()
33 |> query_with(index_type, search_query, search_function)
34 |> maybe_restrict_local(user)
35 |> maybe_restrict_author(author)
36 |> maybe_restrict_blocked(user)
37 |> Pagination.fetch_paginated(
38 %{"offset" => offset, "limit" => limit, "skip_order" => index_type == :rum},
39 :offset
40 )
41 |> maybe_fetch(user, search_query)
42 end
43
44 def maybe_restrict_author(query, %User{} = author) do
45 Activity.Queries.by_author(query, author)
46 end
47
48 def maybe_restrict_author(query, _), do: query
49
50 def maybe_restrict_blocked(query, %User{} = user) do
51 Activity.Queries.exclude_authors(query, User.blocked_users_ap_ids(user))
52 end
53
54 def maybe_restrict_blocked(query, _), do: query
55
56 defp restrict_public(q) do
57 from([a, o] in q,
58 where: fragment("?->>'type' = 'Create'", a.data),
59 where: ^Pleroma.Constants.as_public() in a.recipients
60 )
61 end
62
63 defp query_with(q, :gin, search_query, :plain) do
64 from([a, o] in q,
65 where:
66 fragment(
67 "to_tsvector(?->>'content') @@ plainto_tsquery(?)",
68 o.data,
69 ^search_query
70 )
71 )
72 end
73
74 defp query_with(q, :gin, search_query, :websearch) do
75 from([a, o] in q,
76 where:
77 fragment(
78 "to_tsvector(?->>'content') @@ websearch_to_tsquery(?)",
79 o.data,
80 ^search_query
81 )
82 )
83 end
84
85 defp query_with(q, :rum, search_query, :plain) do
86 from([a, o] in q,
87 where:
88 fragment(
89 "? @@ plainto_tsquery(?)",
90 o.fts_content,
91 ^search_query
92 ),
93 order_by: [fragment("? <=> now()::date", o.inserted_at)]
94 )
95 end
96
97 defp query_with(q, :rum, search_query, :websearch) do
98 from([a, o] in q,
99 where:
100 fragment(
101 "? @@ websearch_to_tsquery(?)",
102 o.fts_content,
103 ^search_query
104 ),
105 order_by: [fragment("? <=> now()::date", o.inserted_at)]
106 )
107 end
108
109 defp maybe_restrict_local(q, user) do
110 limit = Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
111
112 case {limit, user} do
113 {:all, _} -> restrict_local(q)
114 {:unauthenticated, %User{}} -> q
115 {:unauthenticated, _} -> restrict_local(q)
116 {false, _} -> q
117 end
118 end
119
120 defp restrict_local(q), do: where(q, local: true)
121
122 defp maybe_fetch(activities, user, search_query) do
123 with true <- Regex.match?(~r/https?:/, search_query),
124 {:ok, object} <- Fetcher.fetch_object_from_id(search_query),
125 %Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
126 true <- Visibility.visible_for_user?(activity, user) do
127 [activity | activities]
128 else
129 _ -> activities
130 end
131 end
132 end