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