MRF Policies: Return a {:reject, reason} instead of {:reject, nil}
[akkoma] / lib / pleroma / web / activity_pub / mrf / anti_link_spam_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 @behaviour Pleroma.Web.ActivityPub.MRF
9
10 require Logger
11
12 # has the user successfully posted before?
13 defp old_user?(%User{} = u) do
14 u.note_count > 0 || u.follower_count > 0
15 end
16
17 # does the post contain links?
18 defp contains_links?(%{"content" => content} = _object) do
19 content
20 |> Floki.parse_fragment!()
21 |> Floki.filter_out("a.mention,a.hashtag,a[rel~=\"tag\"],a.zrl")
22 |> Floki.attribute("a", "href")
23 |> length() > 0
24 end
25
26 defp contains_links?(_), do: false
27
28 @impl true
29 def filter(%{"type" => "Create", "actor" => actor, "object" => object} = message) do
30 with {:ok, %User{local: false} = 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
33 {:ok, message}
34 else
35 {:ok, %User{local: true}} ->
36 {:ok, message}
37
38 {:contains_links, false} ->
39 {:ok, message}
40
41 {:old_user, false} ->
42 {:reject, "[AntiLinkSpamPolicy] User has no posts nor followers"}
43
44 {:error, _} ->
45 {:reject, "[AntiLinkSpamPolicy] Failed to get or fetch user by ap_id"}
46
47 e ->
48 {:reject, "[AntiLinkSpamPolicy] Unhandled error #{inspect(e)}"}
49 end
50 end
51
52 # in all other cases, pass through
53 def filter(message), do: {:ok, message}
54
55 @impl true
56 def describe, do: {:ok, %{}}
57 end