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