Deprecate Pleroma.Web.base_url/0
[akkoma] / lib / pleroma / web / activity_pub / mrf / no_empty_policy.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.NoEmptyPolicy do
6 @moduledoc "Filter local activities which have no content"
7 @behaviour Pleroma.Web.ActivityPub.MRF
8
9 alias Pleroma.Web.Endpoint
10
11 @impl true
12 def filter(%{"actor" => actor} = object) do
13 with true <- is_local?(actor),
14 true <- is_note?(object),
15 false <- has_attachment?(object),
16 true <- only_mentions?(object) do
17 {:reject, "[NoEmptyPolicy]"}
18 else
19 _ ->
20 {:ok, object}
21 end
22 end
23
24 def filter(object), do: {:ok, object}
25
26 defp is_local?(actor) do
27 if actor |> String.starts_with?("#{Endpoint.url()}") do
28 true
29 else
30 false
31 end
32 end
33
34 defp has_attachment?(%{
35 "type" => "Create",
36 "object" => %{"type" => "Note", "attachment" => attachments}
37 })
38 when length(attachments) > 0,
39 do: true
40
41 defp has_attachment?(_), do: false
42
43 defp only_mentions?(%{"type" => "Create", "object" => %{"type" => "Note", "source" => source}}) do
44 non_mentions =
45 source |> String.split() |> Enum.filter(&(not String.starts_with?(&1, "@"))) |> length
46
47 if non_mentions > 0 do
48 false
49 else
50 true
51 end
52 end
53
54 defp only_mentions?(_), do: false
55
56 defp is_note?(%{"type" => "Create", "object" => %{"type" => "Note"}}), do: true
57 defp is_note?(_), do: false
58
59 @impl true
60 def describe, do: {:ok, %{}}
61 end