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