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
10 # has the user successfully posted before?
11 defp user_has_posted_before?(%User{} = u) do
12 u.info.note_count > 0 || u.info.follower_count > 0
15 # does the post contain links?
16 defp contains_links?(%{"content" => content} = _object) do
18 |> Floki.filter_out("a.mention,a.hashtag,a[rel~=\"tag\"],a.zrl")
19 |> Floki.attribute("a", "href")
23 defp contains_links?(_), do: false
25 def filter(%{"type" => "Create", "actor" => actor, "object" => object} = message) do
26 with {:ok, %User{} = u} <- User.get_or_fetch_by_ap_id(actor),
27 {:contains_links, true} <- {:contains_links, contains_links?(object)},
28 {:posted_before, true} <- {:posted_before, user_has_posted_before?(u)} do
31 {:contains_links, false} ->
34 {:posted_before, false} ->
41 Logger.warn("[MRF anti-link-spam] WTF: unhandled error #{inspect(e)}")
46 # in all other cases, pass through
47 def filter(message), do: {:ok, message}