logo

pleroma

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

topics.ex (2539B)


  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.Ir.Topics do
  5. alias Pleroma.Object
  6. alias Pleroma.Web.ActivityPub.Visibility
  7. def get_activity_topics(activity) do
  8. activity
  9. |> Object.normalize(fetch: false)
  10. |> generate_topics(activity)
  11. |> List.flatten()
  12. end
  13. defp generate_topics(%{data: %{"type" => "ChatMessage"}}, %{data: %{"type" => "Delete"}}) do
  14. ["user", "user:pleroma_chat"]
  15. end
  16. defp generate_topics(%{data: %{"type" => "ChatMessage"}}, %{data: %{"type" => "Create"}}) do
  17. []
  18. end
  19. defp generate_topics(%{data: %{"type" => "Answer"}}, _) do
  20. []
  21. end
  22. defp generate_topics(object, activity) do
  23. ["user", "list"] ++ visibility_tags(object, activity)
  24. end
  25. defp visibility_tags(object, %{data: %{"type" => type}} = activity) when type != "Announce" do
  26. case Visibility.get_visibility(activity) do
  27. "public" ->
  28. if activity.local do
  29. ["public", "public:local"]
  30. else
  31. ["public"]
  32. end
  33. |> item_creation_tags(object, activity)
  34. "local" ->
  35. ["public:local"]
  36. |> item_creation_tags(object, activity)
  37. "direct" ->
  38. ["direct"]
  39. _ ->
  40. []
  41. end
  42. end
  43. defp visibility_tags(_object, _activity) do
  44. []
  45. end
  46. defp item_creation_tags(tags, object, %{data: %{"type" => "Create"}} = activity) do
  47. tags ++
  48. remote_topics(activity) ++ hashtags_to_topics(object) ++ attachment_topics(object, activity)
  49. end
  50. defp item_creation_tags(tags, _, _) do
  51. tags
  52. end
  53. defp hashtags_to_topics(object) do
  54. object
  55. |> Object.hashtags()
  56. |> Enum.map(fn hashtag -> "hashtag:" <> hashtag end)
  57. end
  58. defp remote_topics(%{local: true}), do: []
  59. defp remote_topics(%{actor: actor}) when is_binary(actor),
  60. do: ["public:remote:" <> URI.parse(actor).host]
  61. defp remote_topics(_), do: []
  62. defp attachment_topics(%{data: %{"attachment" => []}}, _act), do: []
  63. defp attachment_topics(_object, %{local: true} = activity) do
  64. case Visibility.get_visibility(activity) do
  65. "public" ->
  66. ["public:media", "public:local:media"]
  67. "local" ->
  68. ["public:local:media"]
  69. _ ->
  70. []
  71. end
  72. end
  73. defp attachment_topics(_object, %{actor: actor}) when is_binary(actor),
  74. do: ["public:media", "public:remote:media:" <> URI.parse(actor).host]
  75. defp attachment_topics(_object, _act), do: ["public:media"]
  76. end