Merge branch 'legal-boilerplate' into 'develop'
[akkoma] / lib / pleroma / web / activity_pub / mrf / ensure_re_prepended.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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 @behaviour Pleroma.Web.ActivityPub.MRF
9
10 @reply_prefix Regex.compile!("^re:[[:space:]]*", [:caseless])
11 def filter_by_summary(
12 %{"summary" => parent_summary} = _parent,
13 %{"summary" => child_summary} = child
14 )
15 when not is_nil(child_summary) and byte_size(child_summary) > 0 and
16 not is_nil(parent_summary) and byte_size(parent_summary) > 0 do
17 if (child_summary == parent_summary and not Regex.match?(@reply_prefix, child_summary)) or
18 (Regex.match?(@reply_prefix, parent_summary) &&
19 Regex.replace(@reply_prefix, parent_summary, "") == child_summary) do
20 Map.put(child, "summary", "re: " <> child_summary)
21 else
22 child
23 end
24 end
25
26 def filter_by_summary(_parent, child), do: child
27
28 def filter(%{"type" => activity_type} = object) when activity_type == "Create" do
29 child = object["object"]
30 in_reply_to = Object.normalize(child["inReplyTo"])
31
32 child =
33 if(in_reply_to,
34 do: filter_by_summary(in_reply_to.data, child),
35 else: child
36 )
37
38 object = Map.put(object, "object", child)
39
40 {:ok, object}
41 end
42
43 def filter(object), do: {:ok, object}
44 end