2019-06-19 15:57:44 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-02 05:08:45 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-06-19 15:57:44 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.ActivityPub.MRF.AntiLinkSpamPolicy do
|
|
|
|
alias Pleroma.User
|
|
|
|
|
2019-08-13 21:52:54 +00:00
|
|
|
@behaviour Pleroma.Web.ActivityPub.MRF
|
|
|
|
|
2019-06-19 15:57:44 +00:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
# has the user successfully posted before?
|
2019-06-21 22:27:14 +00:00
|
|
|
defp old_user?(%User{} = u) do
|
2019-10-16 18:59:21 +00:00
|
|
|
u.note_count > 0 || u.follower_count > 0
|
2019-06-19 15:57:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# does the post contain links?
|
|
|
|
defp contains_links?(%{"content" => content} = _object) do
|
|
|
|
content
|
2020-02-15 22:55:26 +00:00
|
|
|
|> Floki.parse_fragment!()
|
2019-06-19 15:57:44 +00:00
|
|
|
|> Floki.filter_out("a.mention,a.hashtag,a[rel~=\"tag\"],a.zrl")
|
|
|
|
|> Floki.attribute("a", "href")
|
|
|
|
|> length() > 0
|
|
|
|
end
|
|
|
|
|
2019-06-19 16:33:49 +00:00
|
|
|
defp contains_links?(_), do: false
|
|
|
|
|
2019-08-13 21:52:54 +00:00
|
|
|
@impl true
|
2019-06-19 15:57:44 +00:00
|
|
|
def filter(%{"type" => "Create", "actor" => actor, "object" => object} = message) do
|
2020-06-25 10:40:39 +00:00
|
|
|
with {:ok, %User{local: false} = u} <- User.get_or_fetch_by_ap_id(actor),
|
2019-06-19 15:57:44 +00:00
|
|
|
{:contains_links, true} <- {:contains_links, contains_links?(object)},
|
2019-06-21 22:27:14 +00:00
|
|
|
{:old_user, true} <- {:old_user, old_user?(u)} do
|
2019-06-19 15:57:44 +00:00
|
|
|
{:ok, message}
|
|
|
|
else
|
2020-06-25 10:40:39 +00:00
|
|
|
{:ok, %User{local: true}} ->
|
|
|
|
{:ok, message}
|
|
|
|
|
2019-06-19 15:57:44 +00:00
|
|
|
{:contains_links, false} ->
|
|
|
|
{:ok, message}
|
|
|
|
|
2019-06-21 22:27:14 +00:00
|
|
|
{:old_user, false} ->
|
2020-07-13 13:47:13 +00:00
|
|
|
{:reject, "[AntiLinkSpamPolicy] User has no posts nor followers"}
|
2019-06-19 15:57:44 +00:00
|
|
|
|
|
|
|
{:error, _} ->
|
2020-07-13 13:47:13 +00:00
|
|
|
{:reject, "[AntiLinkSpamPolicy] Failed to get or fetch user by ap_id"}
|
2019-06-19 15:57:44 +00:00
|
|
|
|
|
|
|
e ->
|
2020-07-13 13:47:13 +00:00
|
|
|
{:reject, "[AntiLinkSpamPolicy] Unhandled error #{inspect(e)}"}
|
2019-06-19 15:57:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# in all other cases, pass through
|
|
|
|
def filter(message), do: {:ok, message}
|
2019-08-13 21:52:54 +00:00
|
|
|
|
|
|
|
@impl true
|
2019-08-13 22:36:24 +00:00
|
|
|
def describe, do: {:ok, %{}}
|
2019-06-19 15:57:44 +00:00
|
|
|
end
|