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

137 строки
3.7KB

  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. try do
  24. Activity
  25. |> Activity.with_preloaded_object()
  26. |> Activity.restrict_deactivated_users()
  27. |> restrict_public()
  28. |> query_with(index_type, search_query, search_function)
  29. |> maybe_restrict_local(user)
  30. |> maybe_restrict_author(author)
  31. |> maybe_restrict_blocked(user)
  32. |> Pagination.fetch_paginated(
  33. %{"offset" => offset, "limit" => limit, "skip_order" => index_type == :rum},
  34. :offset
  35. )
  36. |> maybe_fetch(user, search_query)
  37. rescue
  38. _ -> maybe_fetch([], user, search_query)
  39. end
  40. end
  41. def maybe_restrict_author(query, %User{} = author) do
  42. Activity.Queries.by_author(query, author)
  43. end
  44. def maybe_restrict_author(query, _), do: query
  45. def maybe_restrict_blocked(query, %User{} = user) do
  46. Activity.Queries.exclude_authors(query, User.blocked_users_ap_ids(user))
  47. end
  48. def maybe_restrict_blocked(query, _), do: query
  49. defp restrict_public(q) do
  50. from([a, o] in q,
  51. where: fragment("?->>'type' = 'Create'", a.data),
  52. where: ^Pleroma.Constants.as_public() in a.recipients
  53. )
  54. end
  55. defp query_with(q, :gin, search_query, :plain) do
  56. from([a, o] in q,
  57. where:
  58. fragment(
  59. "to_tsvector(?->>'content') @@ plainto_tsquery(?)",
  60. o.data,
  61. ^search_query
  62. )
  63. )
  64. end
  65. defp query_with(q, :gin, search_query, :websearch) do
  66. from([a, o] in q,
  67. where:
  68. fragment(
  69. "to_tsvector(?->>'content') @@ websearch_to_tsquery(?)",
  70. o.data,
  71. ^search_query
  72. )
  73. )
  74. end
  75. defp query_with(q, :rum, search_query, :plain) do
  76. from([a, o] in q,
  77. where:
  78. fragment(
  79. "? @@ plainto_tsquery(?)",
  80. o.fts_content,
  81. ^search_query
  82. ),
  83. order_by: [fragment("? <=> now()::date", o.inserted_at)]
  84. )
  85. end
  86. defp query_with(q, :rum, search_query, :websearch) do
  87. from([a, o] in q,
  88. where:
  89. fragment(
  90. "? @@ websearch_to_tsquery(?)",
  91. o.fts_content,
  92. ^search_query
  93. ),
  94. order_by: [fragment("? <=> now()::date", o.inserted_at)]
  95. )
  96. end
  97. defp maybe_restrict_local(q, user) do
  98. limit = Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
  99. case {limit, user} do
  100. {:all, _} -> restrict_local(q)
  101. {:unauthenticated, %User{}} -> q
  102. {:unauthenticated, _} -> restrict_local(q)
  103. {false, _} -> q
  104. end
  105. end
  106. defp restrict_local(q), do: where(q, local: true)
  107. defp maybe_fetch(activities, user, search_query) do
  108. with true <- Regex.match?(~r/https?:/, search_query),
  109. {:ok, object} <- Fetcher.fetch_object_from_id(search_query),
  110. %Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
  111. true <- Visibility.visible_for_user?(activity, user) do
  112. [activity | activities]
  113. else
  114. _ -> activities
  115. end
  116. end
  117. end