2019-05-31 03:22:13 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 13:38:50 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-05-31 03:22:13 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Activity.Search do
|
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.Object.Fetcher
|
2019-07-11 07:55:31 -06:00
|
|
|
alias Pleroma.Pagination
|
2019-05-31 03:22:13 -06:00
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.ActivityPub.Visibility
|
|
|
|
|
2019-07-28 20:43:19 -06:00
|
|
|
require Pleroma.Constants
|
|
|
|
|
2019-05-31 03:22:13 -06:00
|
|
|
import Ecto.Query
|
|
|
|
|
2019-07-11 07:55:31 -06:00
|
|
|
def search(user, search_query, options \\ []) do
|
2019-05-31 03:22:13 -06:00
|
|
|
index_type = if Pleroma.Config.get([:database, :rum_enabled]), do: :rum, else: :gin
|
2019-07-11 07:55:31 -06:00
|
|
|
limit = Enum.min([Keyword.get(options, :limit), 40])
|
|
|
|
offset = Keyword.get(options, :offset, 0)
|
|
|
|
author = Keyword.get(options, :author)
|
2019-05-31 03:22:13 -06:00
|
|
|
|
2020-11-20 08:26:43 -07:00
|
|
|
search_function =
|
2020-11-23 07:29:55 -07:00
|
|
|
if :persistent_term.get({Pleroma.Repo, :postgres_version}) >= 11 do
|
2020-11-20 08:26:43 -07:00
|
|
|
:websearch
|
|
|
|
else
|
|
|
|
:plain
|
|
|
|
end
|
2020-11-19 08:12:01 -07:00
|
|
|
|
2021-08-12 02:29:53 -06:00
|
|
|
try do
|
|
|
|
Activity
|
|
|
|
|> Activity.with_preloaded_object()
|
|
|
|
|> Activity.restrict_deactivated_users()
|
2022-05-05 08:44:34 -06:00
|
|
|
|> restrict_public(user)
|
2021-08-12 02:29:53 -06:00
|
|
|
|> query_with(index_type, search_query, search_function)
|
|
|
|
|> maybe_restrict_local(user)
|
|
|
|
|> maybe_restrict_author(author)
|
|
|
|
|> maybe_restrict_blocked(user)
|
|
|
|
|> Pagination.fetch_paginated(
|
|
|
|
%{"offset" => offset, "limit" => limit, "skip_order" => index_type == :rum},
|
|
|
|
:offset
|
|
|
|
)
|
|
|
|
|> maybe_fetch(user, search_query)
|
|
|
|
rescue
|
|
|
|
_ -> maybe_fetch([], user, search_query)
|
|
|
|
end
|
2019-05-31 03:22:13 -06:00
|
|
|
end
|
|
|
|
|
2019-07-11 07:55:31 -06:00
|
|
|
def maybe_restrict_author(query, %User{} = author) do
|
2020-01-20 11:47:44 -07:00
|
|
|
Activity.Queries.by_author(query, author)
|
2019-07-11 07:55:31 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def maybe_restrict_author(query, _), do: query
|
|
|
|
|
2020-01-20 11:47:44 -07:00
|
|
|
def maybe_restrict_blocked(query, %User{} = user) do
|
|
|
|
Activity.Queries.exclude_authors(query, User.blocked_users_ap_ids(user))
|
|
|
|
end
|
|
|
|
|
|
|
|
def maybe_restrict_blocked(query, _), do: query
|
|
|
|
|
2022-05-05 08:44:34 -06:00
|
|
|
defp restrict_public(q, user) when not is_nil(user) do
|
2022-05-05 09:18:18 -06:00
|
|
|
intended_recipients = [
|
|
|
|
Pleroma.Constants.as_public(),
|
|
|
|
Pleroma.Web.ActivityPub.Utils.as_local_public()
|
|
|
|
]
|
2022-05-05 08:44:34 -06:00
|
|
|
|
|
|
|
from([a, o] in q,
|
|
|
|
where: fragment("?->>'type' = 'Create'", a.data),
|
|
|
|
where: fragment("? && ?", ^intended_recipients, a.recipients)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp restrict_public(q, _user) do
|
2019-05-31 03:22:13 -06:00
|
|
|
from([a, o] in q,
|
|
|
|
where: fragment("?->>'type' = 'Create'", a.data),
|
2019-07-28 20:43:19 -06:00
|
|
|
where: ^Pleroma.Constants.as_public() in a.recipients
|
2019-05-31 03:22:13 -06:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-11-19 08:12:01 -07:00
|
|
|
defp query_with(q, :gin, search_query, :plain) do
|
2021-08-27 19:43:50 -06:00
|
|
|
%{rows: [[tsc]]} =
|
|
|
|
Ecto.Adapters.SQL.query!(
|
|
|
|
Pleroma.Repo,
|
|
|
|
"select current_setting('default_text_search_config')::regconfig::oid;"
|
|
|
|
)
|
|
|
|
|
2019-05-31 03:22:13 -06:00
|
|
|
from([a, o] in q,
|
|
|
|
where:
|
|
|
|
fragment(
|
2021-08-27 19:43:50 -06:00
|
|
|
"to_tsvector(?::oid::regconfig, ?->>'content') @@ plainto_tsquery(?)",
|
|
|
|
^tsc,
|
2019-05-31 03:22:13 -06:00
|
|
|
o.data,
|
|
|
|
^search_query
|
2019-06-12 03:22:56 -06:00
|
|
|
)
|
2019-05-31 03:22:13 -06:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-11-19 08:12:01 -07:00
|
|
|
defp query_with(q, :gin, search_query, :websearch) do
|
2021-08-27 19:43:50 -06:00
|
|
|
%{rows: [[tsc]]} =
|
|
|
|
Ecto.Adapters.SQL.query!(
|
|
|
|
Pleroma.Repo,
|
|
|
|
"select current_setting('default_text_search_config')::regconfig::oid;"
|
|
|
|
)
|
|
|
|
|
2020-11-19 08:12:01 -07:00
|
|
|
from([a, o] in q,
|
|
|
|
where:
|
|
|
|
fragment(
|
2021-08-27 19:43:50 -06:00
|
|
|
"to_tsvector(?::oid::regconfig, ?->>'content') @@ websearch_to_tsquery(?)",
|
|
|
|
^tsc,
|
2020-11-19 08:12:01 -07:00
|
|
|
o.data,
|
|
|
|
^search_query
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp query_with(q, :rum, search_query, :plain) do
|
2019-05-31 03:22:13 -06:00
|
|
|
from([a, o] in q,
|
|
|
|
where:
|
|
|
|
fragment(
|
2021-02-06 02:42:17 -07:00
|
|
|
"? @@ plainto_tsquery(?)",
|
2019-05-31 03:22:13 -06:00
|
|
|
o.fts_content,
|
|
|
|
^search_query
|
|
|
|
),
|
|
|
|
order_by: [fragment("? <=> now()::date", o.inserted_at)]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-11-19 08:12:01 -07:00
|
|
|
defp query_with(q, :rum, search_query, :websearch) do
|
|
|
|
from([a, o] in q,
|
|
|
|
where:
|
|
|
|
fragment(
|
2021-02-06 02:42:17 -07:00
|
|
|
"? @@ websearch_to_tsquery(?)",
|
2020-11-19 08:12:01 -07:00
|
|
|
o.fts_content,
|
|
|
|
^search_query
|
|
|
|
),
|
|
|
|
order_by: [fragment("? <=> now()::date", o.inserted_at)]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-06-11 08:25:53 -06:00
|
|
|
defp maybe_restrict_local(q, user) do
|
|
|
|
limit = Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
|
2019-05-31 03:22:13 -06:00
|
|
|
|
2019-06-11 08:25:53 -06:00
|
|
|
case {limit, user} do
|
|
|
|
{:all, _} -> restrict_local(q)
|
|
|
|
{:unauthenticated, %User{}} -> q
|
|
|
|
{:unauthenticated, _} -> restrict_local(q)
|
|
|
|
{false, _} -> q
|
2019-06-06 06:17:49 -06:00
|
|
|
end
|
|
|
|
end
|
2019-05-31 03:22:13 -06:00
|
|
|
|
2019-06-11 08:25:53 -06:00
|
|
|
defp restrict_local(q), do: where(q, local: true)
|
|
|
|
|
2019-05-31 03:22:13 -06:00
|
|
|
defp maybe_fetch(activities, user, search_query) do
|
|
|
|
with true <- Regex.match?(~r/https?:/, search_query),
|
|
|
|
{:ok, object} <- Fetcher.fetch_object_from_id(search_query),
|
|
|
|
%Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
|
|
|
|
true <- Visibility.visible_for_user?(activity, user) do
|
2019-12-09 10:45:04 -07:00
|
|
|
[activity | activities]
|
2019-05-31 03:22:13 -06:00
|
|
|
else
|
|
|
|
_ -> activities
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|