Skip to content

Commit

Permalink
.NET8 + Piranha 11
Browse files Browse the repository at this point in the history
- Upgraded to .NET 8 and Piranha 11
- Default values for config
  • Loading branch information
jensbrak committed Mar 17, 2024
1 parent 9372f03 commit 88b4cfd
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 56 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ I wanted to see what it took to make a module with full Manager support, includi
In other words: You might have use for this module even if antispam/comments are not of interest - if you just want to understand how Manager support can be added to a custom Piranha module. There's no claim that this is the right way to do it, however.

# Dependencies
* `.NET 8`
* `Zon3.SpamDetector.Localization`
* `Microsoft.Extensions.Http` version 6
* `Microsoft.Extensions.Localization` version 6
* `Piranha` version 10
* `Piranha.Manager` version 10
* `Microsoft.Extensions.Http` version 8
* `Microsoft.Extensions.Localization` version 8
* `Piranha` version 11
* `Piranha.Manager` version 11
* Also: An Akismet API key

# Demo
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<AssemblyName>Zon3.SpamDetector.Localization</AssemblyName>
<RootNamespace>Zon3.SpamDetector.Localization</RootNamespace>
<VersionPrefix>1.4.0</VersionPrefix>
<VersionPrefix>1.5.0</VersionPrefix>

<Authors>Jens Bråkenhielm</Authors>
<Copyright>Copyright 2020-2022 (c) Jens Bråkenhielm</Copyright>
<Copyright>Copyright 2020-2024 (c) Jens Bråkenhielm</Copyright>
<Description>Piranha Module detecting comment spam using Akismet</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand All @@ -26,7 +26,7 @@


<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Localization" Version="6.0.4" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="8.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
29 changes: 20 additions & 9 deletions Zon3.SpamDetector/AkismetSpamDetectorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,45 @@ namespace Zon3.SpamDetector
{
public class AkismetSpamDetectorService : SpamDetectorService
{
private readonly string _piranhaVersion;
private readonly string _pluginVersion;

/// <inheritdoc cref="SpamDetectorService" select="param"/>
/// <summary>
/// The Akismet anti-spam service implementation of the <see cref="SpamDetectorService"/> core functionality.
/// Composes a request to Akismet with as much relevant information as possible provided and
/// translates a response from Akismet to a review result in form of a <see cref="CommentReview"/>.
/// </summary>
public AkismetSpamDetectorService(IApi piranha, SpamDetectorConfigService config, IHttpClientFactory httpClientFactory, ILoggerFactory loggerFactory) : base(piranha, config, httpClientFactory, loggerFactory)
public AkismetSpamDetectorService(IApi api, SpamDetectorConfigService config, IHttpClientFactory httpClientFactory, ILoggerFactory loggerFactory) : base(api, config, httpClientFactory, loggerFactory)
{
// Intentionally left empty
_piranhaVersion = Utils.GetAssemblyVersion(typeof(App).Assembly);
_pluginVersion = Utils.GetAssemblyVersion(typeof(SpamDetectorModule).Assembly);
}

/// <inheritdoc cref="SpamDetectorService"/>
protected override async Task<HttpRequestMessage> GetSpamRequestMessageAsync(Comment comment)
{
CommentId = comment.Id;
var userAgent = $"Piranha CMS/{_piranhaVersion} | SpamDetector/{_pluginVersion}";

var post = await Piranha.Posts.GetByIdAsync(comment.ContentId);
var page = await Piranha.Pages.GetByIdAsync(post?.BlogId ?? comment.ContentId);
var permalink = post != null ? post.Permalink : page.Permalink;
var permalinkFull = $"{ConfigModel.SiteUrl}{permalink}";
string permalink;
var post = await Api.Posts.GetByIdAsync(comment.ContentId);
if (post != null)
{
permalink = $"{ConfigModel.SiteUrl}{post.Permalink}";
}
else
{
var page = await Api.Pages.GetByIdAsync(post?.BlogId ?? comment.ContentId);
permalink = $"{ConfigModel.SiteUrl}{page.Permalink}";
}

var parameters = new Dictionary<string, string>
{
{"blog", HttpUtility.UrlEncode(ConfigModel.SiteUrl)},
{"user_ip", HttpUtility.UrlEncode(comment.IpAddress)},
{"user_agent", HttpUtility.UrlEncode(comment.UserAgent)},
{"user_agent", HttpUtility.UrlEncode(userAgent)}, // Ignore comment.UserAgent and use Akismet preferred format
{"referrer", HttpUtility.UrlEncode(string.Empty)}, // ???
{"permalink", HttpUtility.UrlEncode(permalinkFull)},
{"permalink", HttpUtility.UrlEncode(permalink)},
{"comment_type", HttpUtility.UrlEncode("comment")},
{"comment_author", HttpUtility.UrlEncode(comment.Author ?? string.Empty)},
{"comment_author_email", HttpUtility.UrlEncode(comment.Email ?? string.Empty)},
Expand Down
12 changes: 6 additions & 6 deletions Zon3.SpamDetector/Services/SpamDetectorConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace Zon3.SpamDetector.Services
/// </summary>
public class SpamDetectorConfigService
{
private readonly IApi _piranha;
private readonly IApi _api;

/// <summary>
/// Default constructor.
/// </summary>
/// <param name="piranha">The Piranha API</param>
public SpamDetectorConfigService(IApi piranha)
/// <param name="api">The Piranha API</param>
public SpamDetectorConfigService(IApi api)
{
_piranha = piranha;
_api = api;
}

/// <summary>
Expand All @@ -25,7 +25,7 @@ public SpamDetectorConfigService(IApi piranha)
/// <returns>The SpamDetectorService model</returns>
public SpamDetectorConfigModel Get()
{
using var config = new SpamDetectorConfig(_piranha);
using var config = new SpamDetectorConfig(_api);
return new SpamDetectorConfigModel
{
Enabled = config.Enabled,
Expand All @@ -44,7 +44,7 @@ public SpamDetectorConfigModel Get()
/// <param name="configModel">The SpamDetectorService model</param>
public void Save(SpamDetectorConfigModel configModel)
{
using var config = new SpamDetectorConfig(_piranha)
using var config = new SpamDetectorConfig(_api)
{
Enabled = configModel.Enabled,
IsTest = configModel.IsTest,
Expand Down
22 changes: 15 additions & 7 deletions Zon3.SpamDetector/SpamDetectorConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public class SpamDetectorConfig : IDisposable
public static readonly string KeySiteEncoding = $"{KeyPrefix}SiteEncoding";
public static readonly string KeyUserRole = $"{KeyPrefix}UserRole";

private static readonly bool DefaultValueEnabled = false;
private static readonly bool DefaultValueIsTest = true;
private static readonly string DefaultValueSpamApiUrl = "";
private static readonly string DefaultValueSiteUrl = "";
private static readonly string DefaultValueSiteLanguage = "en-US";
private static readonly string DefaultValueSiteEncoding = "UTF8";
private static readonly string DefaultValueUserRole = "guest";

public SpamDetectorConfig(IParamService paramService)
{
_service = paramService;
Expand All @@ -33,7 +41,7 @@ public bool Enabled
get
{
var param = _service.GetByKeyAsync(KeyEnabled).GetAwaiter().GetResult();
return param == null || Convert.ToBoolean(param.Value);
return param == null ? DefaultValueEnabled : Convert.ToBoolean(param.Value);
}
set
{
Expand All @@ -52,7 +60,7 @@ public bool IsTest
get
{
var param = _service.GetByKeyAsync(KeyIsTest).GetAwaiter().GetResult();
return param == null || Convert.ToBoolean(param.Value);
return param == null ? DefaultValueIsTest : Convert.ToBoolean(param.Value);
}
set
{
Expand All @@ -71,7 +79,7 @@ public string SpamApiUrl
get
{
var param = _service.GetByKeyAsync(KeySpamApiUrl).GetAwaiter().GetResult();
return param?.Value;
return param == null ? DefaultValueSpamApiUrl : param.Value;
}
set
{
Expand All @@ -90,7 +98,7 @@ public string SiteUrl
get
{
var param = _service.GetByKeyAsync(KeySiteUrl).GetAwaiter().GetResult();
return param?.Value;
return param == null ? DefaultValueSiteUrl : param.Value;
}
set
{
Expand All @@ -109,7 +117,7 @@ public string SiteLanguage
get
{
var param = _service.GetByKeyAsync(KeySiteLanguage).GetAwaiter().GetResult();
return param == null ? "en-US" : param.Value;
return param == null ? DefaultValueSiteLanguage : param.Value;
}
set
{
Expand All @@ -128,7 +136,7 @@ public string SiteEncoding
get
{
var param = _service.GetByKeyAsync(KeySiteEncoding).GetAwaiter().GetResult();
return param == null ? "UTF8" : param.Value;
return param == null ? DefaultValueSiteEncoding : param.Value;
}
set
{
Expand All @@ -147,7 +155,7 @@ public string UserRole
get
{
var param = _service.GetByKeyAsync(KeyUserRole).GetAwaiter().GetResult();
return param == null ? "guest" : param.Value;
return param == null ? DefaultValueUserRole: param.Value;
}
set
{
Expand Down
45 changes: 26 additions & 19 deletions Zon3.SpamDetector/SpamDetectorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,23 @@ namespace Zon3.SpamDetector
/// </summary>
public abstract class SpamDetectorService : ISpamDetectorService
{
protected IApi Piranha;
protected IApi Api;
protected ILogger Logger;
protected IHttpClientFactory HttpClientFactory;
protected SpamDetectorConfigModel ConfigModel;
protected Guid CommentId;

public bool Enabled => ConfigModel.Enabled;

/// <summary>
/// The default constructor.
/// </summary>
/// <param name="piranha">The Piranha API</param>
/// <param name="api">The Piranha API</param>
/// <param name="config">The configuration to use</param>
/// <param name="httpClientFactory">The factory to use to get http client for requests</param>
/// <param name="loggerFactory">The factory to use to get a logger</param>
protected SpamDetectorService(IApi piranha, SpamDetectorConfigService config, IHttpClientFactory httpClientFactory, ILoggerFactory loggerFactory)
protected SpamDetectorService(IApi api, SpamDetectorConfigService config, IHttpClientFactory httpClientFactory, ILoggerFactory loggerFactory)
{
Piranha = piranha;
Api = api;
HttpClientFactory = httpClientFactory;
ConfigModel = config.Get();

Expand All @@ -48,24 +47,34 @@ protected SpamDetectorService(IApi piranha, SpamDetectorConfigService config, IH

if (!ConfigModel.Enabled)
{
Logger.LogWarning("Module disabled by configuration: comments will not be reviewed/changed");
Logger.LogWarning("Module disabled: spam detection will not be performed");
}

if (ConfigModel.IsTest)
{
Logger.LogWarning("Module in test mode: spam detection results may be affected");
}

if (string.IsNullOrEmpty(ConfigModel.SpamApiUrl))
{
const string msg = "Module not configured properly, mandatory value missing: SpamApiUrl";
const string msg = "Module configuration error, mandatory value not set: SpamApiUrl";
Logger.LogError(msg);
throw new InvalidOperationException(msg);
}

if (string.IsNullOrEmpty(ConfigModel.SiteUrl))
{
Logger.LogWarning("Option SiteUrl is missing: results may be wrong");
}

if (ConfigModel.IsTest)
{
Logger.LogWarning("Option IsTest is true: no live requests will be made");
const string msg = "Module configuration error, mandatory value not set: SiteUrl";
if (ConfigModel.IsTest)
{
// Mandatory but in test mode so just warn
Logger.LogWarning(msg);
}
else
{
Logger.LogError(msg);
throw new InvalidOperationException(msg);
}
}
}

Expand All @@ -76,20 +85,18 @@ protected SpamDetectorService(IApi piranha, SpamDetectorConfigService config, IH
/// <returns>Interpreted and selected information about the result of the review</returns>
public async Task<CommentReview> ReviewAsync(Comment comment)
{
CommentId = comment.Id;

if (!Enabled)
{
return new CommentReview() { Approved = comment.IsApproved };
}

Logger.LogDebug($"Composing API request for comment '{CommentId}' by '{comment.Email}'");
Logger.LogDebug($"Composing API request for comment '{comment.Id}' by '{comment.Email}'");
var requestMessage = await GetSpamRequestMessageAsync(comment);
Logger.LogDebug("Sending API request for comment '{1}' from IP '{2}'...", CommentId, comment.IpAddress);
Logger.LogDebug("Sending API request for comment '{1}' from IP '{2}'...", comment.Id, comment.IpAddress);
var response = await HttpClientFactory.CreateClient().SendAsync(requestMessage);
Logger.LogDebug("Received API response for comment '{1}' = '{2}'", CommentId, response.Content);
Logger.LogDebug("Received API response for comment '{1}' = '{2}'", comment.Id, response.Content);
response.EnsureSuccessStatusCode();
Logger.LogDebug("Interpreting API response for comment '{1}' = '{2}'", CommentId, requestMessage);
Logger.LogDebug("Interpreting API response for comment '{1}' = '{2}'", comment.Id, requestMessage);
var review = await GetCommentReviewFromResponse(response);

return review;
Expand Down
14 changes: 7 additions & 7 deletions Zon3.SpamDetector/Zon3.SpamDetector.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>

<AssemblyName>Zon3.SpamDetector</AssemblyName>
<RootNamespace>Zon3.SpamDetector</RootNamespace>
<VersionPrefix>1.4.0</VersionPrefix>
<VersionPrefix>1.5.0</VersionPrefix>

<Authors>Jens Bråkenhielm</Authors>
<Copyright>Copyright 2020-2022 (c) Jens Bråkenhielm</Copyright>
<Copyright>Copyright 2020-2024 (c) Jens Bråkenhielm</Copyright>
<Description>Piranha Module detecting comment spam using Akismet</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand All @@ -28,10 +28,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="6.0.4" />
<PackageReference Include="Piranha" Version="10.1.0" />
<PackageReference Include="Piranha.Manager" Version="10.1.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="8.0.3" />
<PackageReference Include="Piranha" Version="11.0.0" />
<PackageReference Include="Piranha.Manager" Version="11.0.0" />

</ItemGroup>

Expand Down

0 comments on commit 88b4cfd

Please sign in to comment.