1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.AntiLinkSpamPolicy do
8 @behaviour Pleroma.Web.ActivityPub.MRF.Policy
13 def history_awareness, do: :auto
15 # has the user successfully posted before?
16 defp old_user?(%User{} = u) do
17 u.note_count > 0 || u.follower_count > 0
20 # does the post contain links?
21 defp contains_links?(%{"content" => content} = _object) do
23 |> Floki.parse_fragment!()
24 |> Floki.filter_out("a.mention,a.hashtag,a[rel~=\"tag\"],a.zrl")
25 |> Floki.attribute("a", "href")
29 defp contains_links?(_), do: false
32 def filter(%{"type" => type, "actor" => actor, "object" => object} = message)
33 when type in ["Create", "Update"] do
34 with {:ok, %User{local: false} = u} <- User.get_or_fetch_by_ap_id(actor),
35 {:contains_links, true} <- {:contains_links, contains_links?(object)},
36 {:old_user, true} <- {:old_user, old_user?(u)} do
39 {:ok, %User{local: true}} ->
42 {:contains_links, false} ->
46 {:reject, "[AntiLinkSpamPolicy] User has no posts nor followers"}
49 {:reject, "[AntiLinkSpamPolicy] Failed to get or fetch user by ap_id"}
52 {:reject, "[AntiLinkSpamPolicy] Unhandled error #{inspect(e)}"}
56 # in all other cases, pass through
57 def filter(message), do: {:ok, message}
60 def describe, do: {:ok, %{}}