Skip to content

Commit

Permalink
update logics of spam checker
Browse files Browse the repository at this point in the history
  • Loading branch information
qihangnet committed Apr 5, 2017
1 parent c216611 commit 17a1407
Showing 1 changed file with 51 additions and 50 deletions.
101 changes: 51 additions & 50 deletions MZBlog.Core/Commands/Posts/NewCommentCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,72 @@
using MZBlog.Core.Documents;
using System;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;

namespace MZBlog.Core.Commands.Posts
{
public class NewCommentCommand
{
public NewCommentCommand()
{
Id = ObjectId.NewObjectId();
}
public class NewCommentCommand
{
public NewCommentCommand()
{
Id = ObjectId.NewObjectId();
}

public string Id { get; set; }
public string Id { get; set; }

public SpamShield SpamShield { get; set; }
public SpamShield SpamShield { get; set; }

public string PostId { get; set; }
public string PostId { get; set; }

[Required]
public string NickName { get; set; }
[Required]
public string NickName { get; set; }

[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }

public string SiteUrl { get; set; }
public string SiteUrl { get; set; }

[Required]
[MinLength(3)]
public string Content { get; set; }
[Required]
[MinLength(3)]
public string Content { get; set; }

public string IPAddress { get; set; }
}
public string IPAddress { get; set; }
}

public class NewCommentCommandInvoker : ICommandInvoker<NewCommentCommand, CommandResult>
{
private readonly DB.AutoBox _db;
private readonly ISpamShieldService _spamShield;
public class NewCommentCommandInvoker : ICommandInvoker<NewCommentCommand, CommandResult>
{
private readonly DB.AutoBox _db;
private readonly ISpamShieldService _spamShield;

public NewCommentCommandInvoker(DB.AutoBox db, ISpamShieldService spamShield)
{
_db = db;
_spamShield = spamShield;
}
public NewCommentCommandInvoker(DB.AutoBox db, ISpamShieldService spamShield)
{
_db = db;
_spamShield = spamShield;
}

public CommandResult Execute(NewCommentCommand command)
{
if (_spamShield.IsSpam(command.SpamShield))
{
return new CommandResult("You are a spam!");
}
public CommandResult Execute(NewCommentCommand command)
{
if (Regex.IsMatch(command.Email, @"smithg\d*@gmail.com") || _spamShield.IsSpam(command.SpamShield))
{
return new CommandResult("You are a spam!");
}

var comment = new BlogComment
{
Id = command.Id,
Email = command.Email,
NickName = command.NickName,
Content = command.Content,
IPAddress = command.IPAddress,
PostId = command.PostId,
SiteUrl = command.SiteUrl,
CreatedTime = DateTime.UtcNow
};
var comment = new BlogComment
{
Id = command.Id,
Email = command.Email,
NickName = command.NickName,
Content = command.Content,
IPAddress = command.IPAddress,
PostId = command.PostId,
SiteUrl = command.SiteUrl,
CreatedTime = DateTime.UtcNow
};

var result = _db.Insert(DBTableNames.BlogComments, comment);
var result = _db.Insert(DBTableNames.BlogComments, comment);

return CommandResult.SuccessResult;
}
}
return CommandResult.SuccessResult;
}
}
}

0 comments on commit 17a1407

Please sign in to comment.