logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

search.ex (4518B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 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(user)
  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, user) when not is_nil(user) do
  50. intended_recipients = [
  51. Pleroma.Constants.as_public(),
  52. Pleroma.Web.ActivityPub.Utils.as_local_public()
  53. ]
  54. from([a, o] in q,
  55. where: fragment("?->>'type' = 'Create'", a.data),
  56. where: fragment("? && ?", ^intended_recipients, a.recipients)
  57. )
  58. end
  59. defp restrict_public(q, _user) do
  60. from([a, o] in q,
  61. where: fragment("?->>'type' = 'Create'", a.data),
  62. where: ^Pleroma.Constants.as_public() in a.recipients
  63. )
  64. end
  65. defp query_with(q, :gin, search_query, :plain) do
  66. %{rows: [[tsc]]} =
  67. Ecto.Adapters.SQL.query!(
  68. Pleroma.Repo,
  69. "select current_setting('default_text_search_config')::regconfig::oid;"
  70. )
  71. from([a, o] in q,
  72. where:
  73. fragment(
  74. "to_tsvector(?::oid::regconfig, ?->>'content') @@ plainto_tsquery(?)",
  75. ^tsc,
  76. o.data,
  77. ^search_query
  78. )
  79. )
  80. end
  81. defp query_with(q, :gin, search_query, :websearch) do
  82. %{rows: [[tsc]]} =
  83. Ecto.Adapters.SQL.query!(
  84. Pleroma.Repo,
  85. "select current_setting('default_text_search_config')::regconfig::oid;"
  86. )
  87. from([a, o] in q,
  88. where:
  89. fragment(
  90. "to_tsvector(?::oid::regconfig, ?->>'content') @@ websearch_to_tsquery(?)",
  91. ^tsc,
  92. o.data,
  93. ^search_query
  94. )
  95. )
  96. end
  97. defp query_with(q, :rum, search_query, :plain) do
  98. from([a, o] in q,
  99. where:
  100. fragment(
  101. "? @@ plainto_tsquery(?)",
  102. o.fts_content,
  103. ^search_query
  104. ),
  105. order_by: [fragment("? <=> now()::date", o.inserted_at)]
  106. )
  107. end
  108. defp query_with(q, :rum, search_query, :websearch) do
  109. from([a, o] in q,
  110. where:
  111. fragment(
  112. "? @@ websearch_to_tsquery(?)",
  113. o.fts_content,
  114. ^search_query
  115. ),
  116. order_by: [fragment("? <=> now()::date", o.inserted_at)]
  117. )
  118. end
  119. defp maybe_restrict_local(q, user) do
  120. limit = Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
  121. case {limit, user} do
  122. {:all, _} -> restrict_local(q)
  123. {:unauthenticated, %User{}} -> q
  124. {:unauthenticated, _} -> restrict_local(q)
  125. {false, _} -> q
  126. end
  127. end
  128. defp restrict_local(q), do: where(q, local: true)
  129. defp maybe_fetch(activities, user, search_query) do
  130. with true <- Regex.match?(~r/https?:/, search_query),
  131. {:ok, object} <- Fetcher.fetch_object_from_id(search_query),
  132. %Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
  133. true <- Visibility.visible_for_user?(activity, user) do
  134. [activity | activities]
  135. else
  136. _ -> activities
  137. end
  138. end
  139. end