1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.EnsureRePrepended do
8 @moduledoc "Ensure a re: is prepended on replies to a post with a Subject"
9 @behaviour Pleroma.Web.ActivityPub.MRF.Policy
11 @reply_prefix Regex.compile!("^re:[[:space:]]*", [:caseless])
13 def history_awareness, do: :auto
15 def filter_by_summary(
16 %{data: %{"summary" => parent_summary}} = _in_reply_to,
17 %{"summary" => child_summary} = child
19 when not is_nil(child_summary) and byte_size(child_summary) > 0 and
20 not is_nil(parent_summary) and byte_size(parent_summary) > 0 do
21 if (child_summary == parent_summary and not Regex.match?(@reply_prefix, child_summary)) or
22 (Regex.match?(@reply_prefix, parent_summary) &&
23 Regex.replace(@reply_prefix, parent_summary, "") == child_summary) do
24 Map.put(child, "summary", "re: " <> child_summary)
30 def filter_by_summary(_in_reply_to, child), do: child
32 def filter(%{"type" => type, "object" => child_object} = object)
33 when type in ["Create", "Update"] and is_map(child_object) do
35 child_object["inReplyTo"]
36 |> Object.normalize(fetch: false)
37 |> filter_by_summary(child_object)
39 object = Map.put(object, "object", child)
44 def filter(object), do: {:ok, object}
46 def describe, do: {:ok, %{}}