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.Activity.Search do
7 alias Pleroma.Object.Fetcher
8 alias Pleroma.Pagination
10 alias Pleroma.Web.ActivityPub.Visibility
12 require Pleroma.Constants
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)
23 if :persistent_term.get({Pleroma.Repo, :postgres_version}) >= 11 do
31 |> Activity.with_preloaded_object()
32 |> Activity.restrict_deactivated_users()
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},
42 |> maybe_fetch(user, search_query)
44 _ -> maybe_fetch([], user, search_query)
48 def maybe_restrict_author(query, %User{} = author) do
49 Activity.Queries.by_author(query, author)
52 def maybe_restrict_author(query, _), do: query
54 def maybe_restrict_blocked(query, %User{} = user) do
55 Activity.Queries.exclude_authors(query, User.blocked_users_ap_ids(user))
58 def maybe_restrict_blocked(query, _), do: query
60 defp restrict_public(q) do
62 where: fragment("?->>'type' = 'Create'", a.data),
63 where: ^Pleroma.Constants.as_public() in a.recipients
67 defp query_with(q, :gin, search_query, :plain) do
69 Ecto.Adapters.SQL.query!(
71 "select current_setting('default_text_search_config')::regconfig::oid;"
77 "to_tsvector(?::oid::regconfig, ?->>'content') @@ plainto_tsquery(?)",
85 defp query_with(q, :gin, search_query, :websearch) do
87 Ecto.Adapters.SQL.query!(
89 "select current_setting('default_text_search_config')::regconfig::oid;"
95 "to_tsvector(?::oid::regconfig, ?->>'content') @@ websearch_to_tsquery(?)",
103 defp query_with(q, :rum, search_query, :plain) do
107 "? @@ plainto_tsquery(?)",
111 order_by: [fragment("? <=> now()::date", o.inserted_at)]
115 defp query_with(q, :rum, search_query, :websearch) do
119 "? @@ websearch_to_tsquery(?)",
123 order_by: [fragment("? <=> now()::date", o.inserted_at)]
127 defp maybe_restrict_local(q, user) do
128 limit = Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
130 case {limit, user} do
131 {:all, _} -> restrict_local(q)
132 {:unauthenticated, %User{}} -> q
133 {:unauthenticated, _} -> restrict_local(q)
138 defp restrict_local(q), do: where(q, local: true)
140 defp maybe_fetch(activities, user, search_query) do
141 with true <- Regex.match?(~r/https?:/, search_query),
142 {:ok, object} <- Fetcher.fetch_object_from_id(search_query),
143 %Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
144 true <- Visibility.visible_for_user?(activity, user) do
145 [activity | activities]