Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

133 рядки
3.6KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Activity.Search do
  5. alias Pleroma.Activity
  6. alias Pleroma.Object.Fetcher
  7. alias Pleroma.Pagination
  8. alias Pleroma.User
  9. alias Pleroma.Web.ActivityPub.Visibility
  10. require Pleroma.Constants
  11. import Ecto.Query
  12. def search(user, search_query, options \\ []) do
  13. index_type = if Pleroma.Config.get([:database, :rum_enabled]), do: :rum, else: :gin
  14. limit = Enum.min([Keyword.get(options, :limit), 40])
  15. offset = Keyword.get(options, :offset, 0)
  16. author = Keyword.get(options, :author)
  17. search_function =
  18. if :persistent_term.get({Pleroma.Repo, :postgres_version}) >= 11 do
  19. :websearch
  20. else
  21. :plain
  22. end
  23. Activity
  24. |> Activity.with_preloaded_object()
  25. |> Activity.restrict_deactivated_users()
  26. |> restrict_public()
  27. |> query_with(index_type, search_query, search_function)
  28. |> maybe_restrict_local(user)
  29. |> maybe_restrict_author(author)
  30. |> maybe_restrict_blocked(user)
  31. |> Pagination.fetch_paginated(
  32. %{"offset" => offset, "limit" => limit, "skip_order" => index_type == :rum},
  33. :offset
  34. )
  35. |> maybe_fetch(user, search_query)
  36. end
  37. def maybe_restrict_author(query, %User{} = author) do
  38. Activity.Queries.by_author(query, author)
  39. end
  40. def maybe_restrict_author(query, _), do: query
  41. def maybe_restrict_blocked(query, %User{} = user) do
  42. Activity.Queries.exclude_authors(query, User.blocked_users_ap_ids(user))
  43. end
  44. def maybe_restrict_blocked(query, _), do: query
  45. defp restrict_public(q) do
  46. from([a, o] in q,
  47. where: fragment("?->>'type' = 'Create'", a.data),
  48. where: ^Pleroma.Constants.as_public() in a.recipients
  49. )
  50. end
  51. defp query_with(q, :gin, search_query, :plain) do
  52. from([a, o] in q,
  53. where:
  54. fragment(
  55. "to_tsvector('english', ?->>'content') @@ plainto_tsquery('english', ?)",
  56. o.data,
  57. ^search_query
  58. )
  59. )
  60. end
  61. defp query_with(q, :gin, search_query, :websearch) do
  62. from([a, o] in q,
  63. where:
  64. fragment(
  65. "to_tsvector('english', ?->>'content') @@ websearch_to_tsquery('english', ?)",
  66. o.data,
  67. ^search_query
  68. )
  69. )
  70. end
  71. defp query_with(q, :rum, search_query, :plain) do
  72. from([a, o] in q,
  73. where:
  74. fragment(
  75. "? @@ plainto_tsquery('english', ?)",
  76. o.fts_content,
  77. ^search_query
  78. ),
  79. order_by: [fragment("? <=> now()::date", o.inserted_at)]
  80. )
  81. end
  82. defp query_with(q, :rum, search_query, :websearch) do
  83. from([a, o] in q,
  84. where:
  85. fragment(
  86. "? @@ websearch_to_tsquery('english', ?)",
  87. o.fts_content,
  88. ^search_query
  89. ),
  90. order_by: [fragment("? <=> now()::date", o.inserted_at)]
  91. )
  92. end
  93. defp maybe_restrict_local(q, user) do
  94. limit = Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
  95. case {limit, user} do
  96. {:all, _} -> restrict_local(q)
  97. {:unauthenticated, %User{}} -> q
  98. {:unauthenticated, _} -> restrict_local(q)
  99. {false, _} -> q
  100. end
  101. end
  102. defp restrict_local(q), do: where(q, local: true)
  103. defp maybe_fetch(activities, user, search_query) do
  104. with true <- Regex.match?(~r/https?:/, search_query),
  105. {:ok, object} <- Fetcher.fetch_object_from_id(search_query),
  106. %Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
  107. true <- Visibility.visible_for_user?(activity, user) do
  108. [activity | activities]
  109. else
  110. _ -> activities
  111. end
  112. end
  113. end