logo

pleroma

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

common_fixes.ex (3911B)


  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.Web.ActivityPub.ObjectValidators.CommonFixes do
  5. alias Pleroma.EctoType.ActivityPub.ObjectValidators
  6. alias Pleroma.Maps
  7. alias Pleroma.Object
  8. alias Pleroma.Object.Containment
  9. alias Pleroma.User
  10. alias Pleroma.Web.ActivityPub.Transmogrifier
  11. alias Pleroma.Web.ActivityPub.Utils
  12. require Pleroma.Constants
  13. def cast_and_filter_recipients(message, field, follower_collection, field_fallback \\ []) do
  14. {:ok, data} = ObjectValidators.Recipients.cast(message[field] || field_fallback)
  15. data =
  16. Enum.reject(data, fn x ->
  17. String.ends_with?(x, "/followers") and x != follower_collection
  18. end)
  19. Map.put(message, field, data)
  20. end
  21. def fix_object_defaults(data) do
  22. data = Maps.filter_empty_values(data)
  23. context =
  24. Utils.maybe_create_context(
  25. data["context"] || data["conversation"] || data["inReplyTo"] || data["id"]
  26. )
  27. %User{follower_address: follower_collection} = User.get_cached_by_ap_id(data["attributedTo"])
  28. data
  29. |> Map.put("context", context)
  30. |> cast_and_filter_recipients("to", follower_collection)
  31. |> cast_and_filter_recipients("cc", follower_collection)
  32. |> cast_and_filter_recipients("bto", follower_collection)
  33. |> cast_and_filter_recipients("bcc", follower_collection)
  34. |> Transmogrifier.fix_implicit_addressing(follower_collection)
  35. end
  36. def fix_activity_addressing(activity) do
  37. %User{follower_address: follower_collection} = User.get_cached_by_ap_id(activity["actor"])
  38. activity
  39. |> cast_and_filter_recipients("to", follower_collection)
  40. |> cast_and_filter_recipients("cc", follower_collection)
  41. |> cast_and_filter_recipients("bto", follower_collection)
  42. |> cast_and_filter_recipients("bcc", follower_collection)
  43. |> Transmogrifier.fix_implicit_addressing(follower_collection)
  44. end
  45. def fix_actor(data) do
  46. actor =
  47. data
  48. |> Map.put_new("actor", data["attributedTo"])
  49. |> Containment.get_actor()
  50. data
  51. |> Map.put("actor", actor)
  52. |> Map.put("attributedTo", actor)
  53. end
  54. def fix_activity_context(data, %Object{data: %{"context" => object_context}}) do
  55. data
  56. |> Map.put("context", object_context)
  57. end
  58. def fix_object_action_recipients(%{"actor" => actor} = data, %Object{data: %{"actor" => actor}}) do
  59. to = ((data["to"] || []) -- [actor]) |> Enum.uniq()
  60. Map.put(data, "to", to)
  61. end
  62. def fix_object_action_recipients(data, %Object{data: %{"actor" => actor}}) do
  63. to = ((data["to"] || []) ++ [actor]) |> Enum.uniq()
  64. Map.put(data, "to", to)
  65. end
  66. def fix_quote_url(%{"quoteUrl" => _quote_url} = data), do: data
  67. # Fedibird
  68. # https://github.com/fedibird/mastodon/commit/dbd7ae6cf58a92ec67c512296b4daaea0d01e6ac
  69. def fix_quote_url(%{"quoteUri" => quote_url} = data) do
  70. Map.put(data, "quoteUrl", quote_url)
  71. end
  72. # Old Fedibird (bug)
  73. # https://github.com/fedibird/mastodon/issues/9
  74. def fix_quote_url(%{"quoteURL" => quote_url} = data) do
  75. Map.put(data, "quoteUrl", quote_url)
  76. end
  77. # Misskey fallback
  78. def fix_quote_url(%{"_misskey_quote" => quote_url} = data) do
  79. Map.put(data, "quoteUrl", quote_url)
  80. end
  81. def fix_quote_url(%{"tag" => [_ | _] = tags} = data) do
  82. tag = Enum.find(tags, &object_link_tag?/1)
  83. if not is_nil(tag) do
  84. data
  85. |> Map.put("quoteUrl", tag["href"])
  86. else
  87. data
  88. end
  89. end
  90. def fix_quote_url(data), do: data
  91. # https://codeberg.org/fediverse/fep/src/branch/main/fep/e232/fep-e232.md
  92. def object_link_tag?(%{
  93. "type" => "Link",
  94. "mediaType" => media_type,
  95. "href" => href
  96. })
  97. when media_type in Pleroma.Constants.activity_json_mime_types() and is_binary(href) do
  98. true
  99. end
  100. def object_link_tag?(_), do: false
  101. end