1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.NoEmptyPolicy do
6 @moduledoc "Filter local activities which have no content"
7 @behaviour Pleroma.Web.ActivityPub.MRF.Policy
9 alias Pleroma.Web.Endpoint
12 def filter(%{"actor" => actor} = object) do
13 with true <- local?(actor),
14 true <- eligible_type?(object),
15 true <- note?(object),
16 false <- has_attachment?(object),
17 true <- only_mentions?(object) do
18 {:reject, "[NoEmptyPolicy]"}
25 def filter(object), do: {:ok, object}
28 if actor |> String.starts_with?("#{Endpoint.url()}") do
35 defp has_attachment?(%{
36 "object" => %{"type" => "Note", "attachment" => attachments}
38 when length(attachments) > 0,
41 defp has_attachment?(_), do: false
43 defp only_mentions?(%{"object" => %{"type" => "Note", "source" => source}}) do
46 %{"content" => text} -> text
51 source |> String.split() |> Enum.filter(&(not String.starts_with?(&1, "@"))) |> length
53 if non_mentions > 0 do
60 defp only_mentions?(_), do: false
62 defp note?(%{"object" => %{"type" => "Note"}}), do: true
63 defp note?(_), do: false
65 defp eligible_type?(%{"type" => type}) when type in ["Create", "Update"], do: true
66 defp eligible_type?(_), do: false
69 def describe, do: {:ok, %{}}