Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.5KB

  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.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 filter_by_summary(
  10. %{data: %{"summary" => parent_summary}} = _in_reply_to,
  11. %{"summary" => child_summary} = child
  12. )
  13. when not is_nil(child_summary) and byte_size(child_summary) > 0 and
  14. not is_nil(parent_summary) and byte_size(parent_summary) > 0 do
  15. if (child_summary == parent_summary and not Regex.match?(@reply_prefix, child_summary)) or
  16. (Regex.match?(@reply_prefix, parent_summary) &&
  17. Regex.replace(@reply_prefix, parent_summary, "") == child_summary) do
  18. Map.put(child, "summary", "re: " <> child_summary)
  19. else
  20. child
  21. end
  22. end
  23. def filter_by_summary(_in_reply_to, child), do: child
  24. def filter(%{"type" => "Create", "object" => child_object} = object)
  25. when is_map(child_object) do
  26. child =
  27. child_object["inReplyTo"]
  28. |> Object.normalize(fetch: false)
  29. |> filter_by_summary(child_object)
  30. object = Map.put(object, "object", child)
  31. {:ok, object}
  32. end
  33. def filter(object), do: {:ok, object}
  34. def describe, do: {:ok, %{}}
  35. end