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