implement anti link spam MRF
[akkoma] / lib / pleroma / web / activity_pub / mrf / anti_link_spam_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.AntiLinkSpamPolicy do
6 alias Pleroma.User
7
8 require Logger
9
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
13 end
14
15 # does the post contain links?
16 defp contains_links?(%{"content" => content} = _object) do
17 content
18 |> Floki.filter_out("a.mention,a.hashtag,a[rel~=\"tag\"],a.zrl")
19 |> Floki.attribute("a", "href")
20 |> length() > 0
21 end
22
23 def filter(%{"type" => "Create", "actor" => actor, "object" => object} = message) do
24 with {:ok, %User{} = u} <- User.get_or_fetch_by_ap_id(actor),
25 {:contains_links, true} <- {:contains_links, contains_links?(object)},
26 {:posted_before, true} <- {:posted_before, user_has_posted_before?(u)} do
27 {:ok, message}
28 else
29 {:contains_links, false} ->
30 {:ok, message}
31
32 {:posted_before, false} ->
33 {:reject, nil}
34
35 {:error, _} ->
36 {:reject, nil}
37
38 e ->
39 Logger.warn("[MRF anti-link-spam] WTF: unhandled error #{inspect(e)}")
40 {:reject, nil}
41 end
42 end
43
44 # in all other cases, pass through
45 def filter(message), do: {:ok, message}
46 end