logo

pleroma

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

ensure_re_prepended.ex (1556B)


  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.MRF.EnsureRePrepended do
  5. alias Pleroma.Object
  6. @moduledoc "Ensure a re: is prepended on replies to a post with a Subject"
  7. @behaviour Pleroma.Web.ActivityPub.MRF.Policy
  8. @reply_prefix Regex.compile!("^re:[[:space:]]*", [:caseless])
  9. def history_awareness, do: :auto
  10. def filter_by_summary(
  11. %{data: %{"summary" => parent_summary}} = _in_reply_to,
  12. %{"summary" => child_summary} = child
  13. )
  14. when not is_nil(child_summary) and byte_size(child_summary) > 0 and
  15. not is_nil(parent_summary) and byte_size(parent_summary) > 0 do
  16. if (child_summary == parent_summary and not Regex.match?(@reply_prefix, child_summary)) or
  17. (Regex.match?(@reply_prefix, parent_summary) &&
  18. Regex.replace(@reply_prefix, parent_summary, "") == child_summary) do
  19. Map.put(child, "summary", "re: " <> child_summary)
  20. else
  21. child
  22. end
  23. end
  24. def filter_by_summary(_in_reply_to, child), do: child
  25. def filter(%{"type" => type, "object" => child_object} = object)
  26. when type in ["Create", "Update"] and is_map(child_object) do
  27. child =
  28. child_object["inReplyTo"]
  29. |> Object.normalize(fetch: false)
  30. |> filter_by_summary(child_object)
  31. object = Map.put(object, "object", child)
  32. {:ok, object}
  33. end
  34. def filter(object), do: {:ok, object}
  35. def describe, do: {:ok, %{}}
  36. end