Merge branch 'support/bugfix_272' into 'develop'
[akkoma] / lib / pleroma / web / activity_pub / mrf / ensure_re_prepended.ex
1 defmodule Pleroma.Web.ActivityPub.MRF.EnsureRePrepended do
2 alias Pleroma.Object
3
4 @behaviour Pleroma.Web.ActivityPub.MRF
5
6 @reply_prefix Regex.compile!("^re:[[:space:]]*", [:caseless])
7 def filter_by_summary(
8 %{"summary" => parent_summary} = _parent,
9 %{"summary" => child_summary} = child
10 )
11 when not is_nil(child_summary) and byte_size(child_summary) > 0 and
12 not is_nil(parent_summary) and byte_size(parent_summary) > 0 do
13 if (child_summary == parent_summary and not Regex.match?(@reply_prefix, child_summary)) or
14 (Regex.match?(@reply_prefix, parent_summary) &&
15 Regex.replace(@reply_prefix, parent_summary, "") == child_summary) do
16 Map.put(child, "summary", "re: " <> child_summary)
17 else
18 child
19 end
20 end
21
22 def filter_by_summary(_parent, child), do: child
23
24 def filter(%{"type" => activity_type} = object) when activity_type == "Create" do
25 child = object["object"]
26 in_reply_to = Object.normalize(child["inReplyTo"])
27
28 child =
29 if(in_reply_to,
30 do: filter_by_summary(in_reply_to.data, child),
31 else: child
32 )
33
34 object = Map.put(object, "object", child)
35
36 {:ok, object}
37 end
38
39 def filter(object), do: {:ok, object}
40 end