1 # Pleroma: A lightweight social networking server
2 # Copyright © 2019 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
12 # has the user successfully posted before?
13 defp old_user?(%User{} = u) do
14 u.note_count > 0 || u.follower_count > 0
17 # does the post contain links?
18 defp contains_links?(%{"content" => content} = _object) do
20 |> Floki.parse_fragment!()
21 |> Floki.filter_out("a.mention,a.hashtag,a[rel~=\"tag\"],a.zrl")
22 |> Floki.attribute("a", "href")
26 defp contains_links?(_), do: false
29 def filter(%{"type" => "Create", "actor" => actor, "object" => object} = message) do
30 with {:ok, %User{} = u} <- User.get_or_fetch_by_ap_id(actor),
31 {:contains_links, true} <- {:contains_links, contains_links?(object)},
32 {:old_user, true} <- {:old_user, old_user?(u)} do
35 {:contains_links, false} ->
45 Logger.warn("[MRF anti-link-spam] WTF: unhandled error #{inspect(e)}")
50 # in all other cases, pass through
51 def filter(message), do: {:ok, message}
54 def describe, do: {:ok, %{}}