refactor(anti-spam): make anti-spam agnostic from Akismet
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
defmodule Mobilizon.Service.Akismet do
|
||||
defmodule Mobilizon.Service.AntiSpam.Akismet do
|
||||
@moduledoc """
|
||||
Validate user data
|
||||
"""
|
||||
@@ -9,12 +9,16 @@ defmodule Mobilizon.Service.Akismet do
|
||||
alias Mobilizon.Discussions.Comment
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Reports.Report
|
||||
alias Mobilizon.Service.AntiSpam.Provider
|
||||
alias Mobilizon.Users.User
|
||||
alias Mobilizon.Web.Endpoint
|
||||
require Logger
|
||||
|
||||
@behaviour Provider
|
||||
|
||||
@env Application.compile_env(:mobilizon, :env)
|
||||
|
||||
@impl Provider
|
||||
@spec check_user(String.t(), String.t(), String.t()) ::
|
||||
:ham | :spam | :discard | {:error, HTTPoison.Response.t()}
|
||||
def check_user(email, ip, user_agent) do
|
||||
@@ -27,6 +31,7 @@ defmodule Mobilizon.Service.Akismet do
|
||||
})
|
||||
end
|
||||
|
||||
@impl Provider
|
||||
@spec check_profile(String.t(), String.t(), String.t() | nil, String.t(), String.t()) ::
|
||||
:ham | :spam | :discard | {:error, HTTPoison.Response.t()}
|
||||
def check_profile(username, summary, email \\ nil, ip \\ "127.0.0.1", user_agent \\ nil) do
|
||||
@@ -41,6 +46,7 @@ defmodule Mobilizon.Service.Akismet do
|
||||
})
|
||||
end
|
||||
|
||||
@impl Provider
|
||||
@spec check_event(String.t(), String.t(), String.t() | nil, String.t(), String.t()) ::
|
||||
:ham | :spam | :discard | {:error, HTTPoison.Response.t()}
|
||||
def check_event(event_body, username, email \\ nil, ip \\ "127.0.0.1", user_agent \\ nil) do
|
||||
@@ -55,6 +61,7 @@ defmodule Mobilizon.Service.Akismet do
|
||||
})
|
||||
end
|
||||
|
||||
@impl Provider
|
||||
@spec check_comment(String.t(), String.t(), boolean(), String.t() | nil, String.t(), String.t()) ::
|
||||
:ham | :spam | :discard | {:error, HTTPoison.Response.t()}
|
||||
def check_comment(
|
||||
@@ -108,6 +115,7 @@ defmodule Mobilizon.Service.Akismet do
|
||||
Application.get_env(:mobilizon, __MODULE__) |> get_in([:key])
|
||||
end
|
||||
|
||||
@impl Provider
|
||||
def ready?, do: !is_nil(api_key())
|
||||
|
||||
@spec report_to_akismet_comment(Report.t()) :: AkismetComment.t() | {:error, atom()}
|
||||
17
lib/service/anti_spam/anti_spam.ex
Normal file
17
lib/service/anti_spam/anti_spam.ex
Normal file
@@ -0,0 +1,17 @@
|
||||
defmodule Mobilizon.Service.AntiSpam do
|
||||
@moduledoc """
|
||||
Module to load the service adapter defined inside the configuration.
|
||||
|
||||
See `Mobilizon.Service.AntiSpam.Provider`.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Returns the appropriate service adapter.
|
||||
|
||||
According to the config behind
|
||||
`config :mobilizon, Mobilizon.Service.AntiSpam,
|
||||
service: Mobilizon.Service.AntiSpam.Module`
|
||||
"""
|
||||
@spec service :: module
|
||||
def service, do: get_in(Application.get_env(:mobilizon, __MODULE__), [:service])
|
||||
end
|
||||
58
lib/service/anti_spam/provider.ex
Normal file
58
lib/service/anti_spam/provider.ex
Normal file
@@ -0,0 +1,58 @@
|
||||
defmodule Mobilizon.Service.AntiSpam.Provider do
|
||||
@moduledoc """
|
||||
Provider Behaviour for anti-spam detection.
|
||||
|
||||
## Supported backends
|
||||
|
||||
* `Mobilizon.Service.AntiSpam.Akismet` [🔗](https://akismet.com/)
|
||||
|
||||
"""
|
||||
|
||||
@type spam_result :: :ham | :spam | :discard
|
||||
@type result :: spam_result() | {:error, any()}
|
||||
|
||||
@doc """
|
||||
Make sure the provider is ready
|
||||
"""
|
||||
@callback ready?() :: boolean()
|
||||
|
||||
@doc """
|
||||
Check an user details
|
||||
"""
|
||||
@callback check_user(email :: String.t(), ip :: String.t(), user_agent :: String.t()) ::
|
||||
result()
|
||||
|
||||
@doc """
|
||||
Check a profile details
|
||||
"""
|
||||
@callback check_profile(
|
||||
username :: String.t(),
|
||||
summary :: String.t(),
|
||||
email :: String.t() | nil,
|
||||
ip :: String.t(),
|
||||
user_agent :: String.t() | nil
|
||||
) :: result()
|
||||
|
||||
@doc """
|
||||
Check an event details
|
||||
"""
|
||||
@callback check_event(
|
||||
event_body :: String.t(),
|
||||
username :: String.t(),
|
||||
email :: String.t() | nil,
|
||||
ip :: String.t(),
|
||||
user_agent :: String.t() | nil
|
||||
) :: result()
|
||||
|
||||
@doc """
|
||||
Check a comment details
|
||||
"""
|
||||
@callback check_comment(
|
||||
comment_body :: String.t(),
|
||||
username :: String.t(),
|
||||
is_reply? :: boolean(),
|
||||
email :: String.t() | nil,
|
||||
ip :: String.t(),
|
||||
user_agent :: String.t() | nil
|
||||
) :: result()
|
||||
end
|
||||
Reference in New Issue
Block a user