extend reject MRF to check if originating instance is blocked
[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.Policy
10
11 @reply_prefix Regex.compile!("^re:[[:space:]]*", [:caseless])
12
13 def history_awareness, do: :auto
14
15 def filter_by_summary(
16 %{data: %{"summary" => parent_summary}} = _in_reply_to,
17 %{"summary" => child_summary} = child
18 )
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)
25 else
26 child
27 end
28 end
29
30 def filter_by_summary(_in_reply_to, child), do: child
31
32 def filter(%{"type" => type, "object" => child_object} = object)
33 when type in ["Create", "Update"] and is_map(child_object) do
34 child =
35 child_object["inReplyTo"]
36 |> Object.normalize(fetch: false)
37 |> filter_by_summary(child_object)
38
39 object = Map.put(object, "object", child)
40
41 {:ok, object}
42 end
43
44 def filter(object), do: {:ok, object}
45
46 def describe, do: {:ok, %{}}
47 end