Create Question: Add context field to create
[akkoma] / lib / pleroma / web / activity_pub / mrf / ensure_re_prepended.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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) do
31 child =
32 child_object["inReplyTo"]
33 |> Object.normalize(child_object["inReplyTo"])
34 |> filter_by_summary(child_object)
35
36 object = Map.put(object, "object", child)
37
38 {:ok, object}
39 end
40
41 def filter(object), do: {:ok, object}
42
43 def describe, do: {:ok, %{}}
44 end