1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Search.DatabaseSearch do
8 alias Pleroma.Object.Fetcher
9 alias Pleroma.Pagination
11 alias Pleroma.Web.ActivityPub.Visibility
13 require Pleroma.Constants
17 @behaviour Pleroma.Search.SearchBackend
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)
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},
39 |> maybe_fetch(user, search_query)
41 _ -> maybe_fetch([], user, search_query)
46 def add_to_index(_activity), do: :ok
49 def remove_from_index(_object), do: :ok
51 def maybe_restrict_author(query, %User{} = author) do
52 Activity.Queries.by_author(query, author)
55 def maybe_restrict_author(query, _), do: query
57 def maybe_restrict_blocked(query, %User{} = user) do
58 Activity.Queries.exclude_authors(query, User.blocked_users_ap_ids(user))
61 def maybe_restrict_blocked(query, _), do: query
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()
70 where: fragment("?->>'type' = 'Create'", a.data),
71 where: fragment("? && ?", ^intended_recipients, a.recipients)
75 defp restrict_public(q, _user) do
77 where: fragment("?->>'type' = 'Create'", a.data),
78 where: ^Pleroma.Constants.as_public() in a.recipients
82 defp query_with(q, :gin, search_query, :plain) do
84 Ecto.Adapters.SQL.query!(
86 "select current_setting('default_text_search_config')::regconfig::oid;"
92 "to_tsvector(?::oid::regconfig, ?->>'content') @@ plainto_tsquery(?)",
100 defp query_with(q, :gin, search_query, :websearch) do
102 Ecto.Adapters.SQL.query!(
104 "select current_setting('default_text_search_config')::regconfig::oid;"
110 "to_tsvector(?::oid::regconfig, ?->>'content') @@ websearch_to_tsquery(?)",
118 defp query_with(q, :rum, search_query, :plain) do
122 "? @@ plainto_tsquery(?)",
126 order_by: [fragment("? <=> now()::date", o.inserted_at)]
130 defp query_with(q, :rum, search_query, :websearch) do
134 "? @@ websearch_to_tsquery(?)",
138 order_by: [fragment("? <=> now()::date", o.inserted_at)]
142 def maybe_restrict_local(q, user) do
143 limit = Config.get([:instance, :limit_to_local_content], :unauthenticated)
145 case {limit, user} do
146 {:all, _} -> restrict_local(q)
147 {:unauthenticated, %User{}} -> q
148 {:unauthenticated, _} -> restrict_local(q)
153 defp restrict_local(q), do: where(q, local: true)
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]