Skip to content

Commit

Permalink
Only add Notifications + Achievements for human users
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Apr 6, 2024
1 parent 488c13f commit b55ef76
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 47 deletions.
44 changes: 31 additions & 13 deletions MyApp.ServiceInterface/App/CreateAnswerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ await db.InsertAsync(new StatTotals
var post = await db.SingleByIdAsync<Post>(postId);
if (post?.CreatedBy != null)
{
if (post.CreatedBy != answer.CreatedBy)
// Notify Post Author of new Answer
if (post.CreatedBy != answer.CreatedBy && appConfig.IsHuman(post.CreatedBy))
{
await db.InsertAsync(new Notification
{
Expand All @@ -50,11 +51,12 @@ await db.InsertAsync(new Notification
appConfig.IncrUnreadNotificationsFor(post.CreatedBy);
}

// Notify any User Mentions in Answer
if (!string.IsNullOrEmpty(answer.Body))
{
var cleanBody = answer.Body.StripHtml().Trim();
var userNameMentions = cleanBody.FindUserNameMentions()
.Where(x => x != post.CreatedBy && x != answer.CreatedBy).ToList();
.Where(x => x != post.CreatedBy && x != answer.CreatedBy && appConfig.IsHuman(x)).ToList();
if (userNameMentions.Count > 0)
{
var existingUsers = await db.SelectAsync(db.From<ApplicationUser>()
Expand All @@ -66,20 +68,36 @@ await db.InsertAsync(new Notification
if (firstMentionPos < 0) continue;

var startPos = Math.Max(0, firstMentionPos - 50);
await db.InsertAsync(new Notification
if (appConfig.IsHuman(existingUser.UserName))
{
UserName = existingUser.UserName!,
Type = NotificationType.AnswerMention,
RefId = $"{postId}",
PostId = postId,
CreatedDate = answer.CreationDate,
Summary = cleanBody.SubstringWithEllipsis(startPos, 100),
RefUserName = answer.CreatedBy,
});
appConfig.IncrUnreadNotificationsFor(existingUser.UserName!);
await db.InsertAsync(new Notification
{
UserName = existingUser.UserName!,
Type = NotificationType.AnswerMention,
RefId = $"{postId}",
PostId = postId,
CreatedDate = answer.CreationDate,
Summary = cleanBody.SubstringWithEllipsis(startPos, 100),
RefUserName = answer.CreatedBy,
});
appConfig.IncrUnreadNotificationsFor(existingUser.UserName!);
}
}
}
}
}

if (appConfig.IsHuman(answer.CreatedBy))
{
await db.InsertAsync(new Achievement
{
UserName = answer.CreatedBy,
Type = AchievementType.NewAnswer,
RefId = refId,
PostId = postId,
Score = 1,
CreatedDate = DateTime.UtcNow,
});
}
}
}
}
38 changes: 27 additions & 11 deletions MyApp.ServiceInterface/App/CreatePostCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ await db.InsertAsync(new StatTotals
{
var cleanBody = body.StripHtml().Trim();
var userNameMentions = cleanBody.FindUserNameMentions()
.Where(x => x != createdBy).ToList();
.Where(x => x != createdBy && appConfig.IsHuman(x)).ToList();
if (userNameMentions.Count > 0)
{
var existingUsers = await db.SelectAsync(db.From<ApplicationUser>()
Expand All @@ -59,19 +59,35 @@ await db.InsertAsync(new StatTotals
if (firstMentionPos < 0) continue;

var startPos = Math.Max(0, firstMentionPos - 50);
await db.InsertAsync(new Notification
if (appConfig.IsHuman(existingUser.UserName))
{
UserName = existingUser.UserName!,
Type = NotificationType.QuestionMention,
RefId = $"{post.Id}",
PostId = post.Id,
CreatedDate = post.CreationDate,
Summary = cleanBody.SubstringWithEllipsis(startPos, 100),
RefUserName = createdBy,
});
appConfig.IncrUnreadNotificationsFor(existingUser.UserName!);
await db.InsertAsync(new Notification
{
UserName = existingUser.UserName!,
Type = NotificationType.QuestionMention,
RefId = $"{post.Id}",
PostId = post.Id,
CreatedDate = post.CreationDate,
Summary = cleanBody.SubstringWithEllipsis(startPos, 100),
RefUserName = createdBy,
});
appConfig.IncrUnreadNotificationsFor(existingUser.UserName!);
}
}
}
}

if (appConfig.IsHuman(post.CreatedBy))
{
await db.InsertAsync(new Achievement
{
UserName = post.CreatedBy!,
Type = AchievementType.NewQuestion,
RefId = $"{post.Id}",
PostId = post.Id,
Score = 1,
CreatedDate = DateTime.UtcNow,
});
}
}
}
9 changes: 5 additions & 4 deletions MyApp.ServiceInterface/App/CreatePostVotesCommand.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Data;
using MyApp.Data;
using MyApp.ServiceModel;
using ServiceStack.Messaging;
using ServiceStack.OrmLite;

namespace MyApp.ServiceInterface.App;

public class CreatePostVotesCommand(IDbConnection db, IMessageProducer mqClient) : IExecuteCommandAsync<Vote>
public class CreatePostVotesCommand(AppConfig appConfig, IDbConnection db, IMessageProducer mqClient) : IExecuteCommandAsync<Vote>
{
public async Task ExecuteAsync(Vote vote)
{
Expand All @@ -31,12 +32,12 @@ await db.ExecuteNonQueryAsync(
{
await db.InsertAsync(vote);

if (vote.RefUserName != null)
if (appConfig.IsHuman(vote.RefUserName))
{
await db.InsertAsync(new Achievement
{
UserName = vote.RefUserName,
RefUserName = vote.UserName,
UserName = vote.RefUserName!, // User who's Q or A was voted on
RefUserName = vote.UserName, // User who voted
PostId = vote.PostId,
RefId = vote.RefId,
Type = vote.Score > 0 ? voteUp : voteDown,
Expand Down
28 changes: 16 additions & 12 deletions MyApp.ServiceInterface/App/NewCommentCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task ExecuteAsync(NewComment request)
var cleanBody = comment.Body.StripHtml().Trim();
var createdDate = DateTimeOffset.FromUnixTimeMilliseconds(comment.Created).DateTime;

if (createdBy != null && createdBy != comment.CreatedBy)
if (createdBy != null && createdBy != comment.CreatedBy && appConfig.IsHuman(createdBy))
{
await db.InsertAsync(new Notification
{
Expand All @@ -41,7 +41,8 @@ await db.InsertAsync(new Notification
}

var userNameMentions = cleanBody.FindUserNameMentions()
.Where(x => x != createdBy && x != comment.CreatedBy).ToList();
.Where(x => x != createdBy && x != comment.CreatedBy && appConfig.IsHuman(x))
.ToList();
if (userNameMentions.Count > 0)
{
var existingUsers = await db.SelectAsync(db.From<ApplicationUser>()
Expand All @@ -53,17 +54,20 @@ await db.InsertAsync(new Notification
if (firstMentionPos < 0) continue;

var startPos = Math.Max(0, firstMentionPos - 50);
await db.InsertAsync(new Notification
if (appConfig.IsHuman(existingUser.UserName))
{
UserName = existingUser.UserName!,
Type = NotificationType.CommentMention,
RefId = commentRefId,
PostId = postId,
CreatedDate = createdDate,
Summary = cleanBody.SubstringWithEllipsis(startPos, 100),
RefUserName = comment.CreatedBy,
});
appConfig.IncrUnreadNotificationsFor(existingUser.UserName!);
await db.InsertAsync(new Notification
{
UserName = existingUser.UserName!,
Type = NotificationType.CommentMention,
RefId = commentRefId,
PostId = postId,
CreatedDate = createdDate,
Summary = cleanBody.SubstringWithEllipsis(startPos, 100),
RefUserName = comment.CreatedBy,
});
appConfig.IncrUnreadNotificationsFor(existingUser.UserName!);
}
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions MyApp.ServiceInterface/BackgroundMqServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public async Task Any(DiskTasks request)
public async Task ExecuteAsync<T>(IExecuteCommandAsync<T> command, T request) where T : class
{
var commandName = command.GetType().Name;
var sw = Stopwatch.StartNew();
try
{
var sw = Stopwatch.StartNew();
await command.ExecuteAsync(request);
log.LogDebug("{Command} took {ElapsedMilliseconds}ms to execute", commandName, sw.ElapsedMilliseconds);
#if DEBUG
Expand All @@ -61,10 +61,12 @@ public async Task ExecuteAsync<T>(IExecuteCommandAsync<T> command, T request) wh
}
catch (Exception e)
{
log.LogError(e, "{Command} failed: {Message}", commandName, e.Message);
log.LogError(e, "{Command}({Request}) failed: {Message}", commandName, request.ToJsv(), e.Message);
#if DEBUG
appConfig.CommandResults.Add(new() {
Name = commandName,
Ms = sw.ElapsedMilliseconds,
Request = request,
Error = e.Message,
});
#endif
Expand All @@ -76,7 +78,7 @@ public async Task ExecuteAsync<T>(IExecuteCommandAsync<T> command, T request) wh
public async Task Any(DbWrites request)
{
if (request.CreatePostVote != null)
await ExecuteAsync(new CreatePostVotesCommand(Db, MessageProducer), request.CreatePostVote);
await ExecuteAsync(new CreatePostVotesCommand(appConfig, Db, MessageProducer), request.CreatePostVote);

if (request.CreatePost != null)
await ExecuteAsync(new CreatePostCommand(GetLogger<CreatePostCommand>(), appConfig, Db), request.CreatePost);
Expand Down
3 changes: 3 additions & 0 deletions MyApp.ServiceInterface/Data/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,14 @@ public bool HasUnreadAchievements(string? userName)
}

public List<CommandResult> CommandResults { get; set; } = [];

public bool IsHuman(string? userName) => userName != null && GetModelUser(userName) == null;
}

public class CommandResult
{
public string Name { get; set; }
public long? Ms { get; set; }
public object Request { get; set; }
public string? Error { get; set; }
}
10 changes: 6 additions & 4 deletions MyApp.ServiceModel/Posts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,12 @@ public class Notification
public enum AchievementType
{
Unknown = 0,
AnswerUpVote = 1,
AnswerDownVote = 2,
QuestionUpVote = 3,
QuestionDownVote = 4,
NewAnswer = 1,
AnswerUpVote = 2,
AnswerDownVote = 3,
NewQuestion = 4,
QuestionUpVote = 5,
QuestionDownVote = 6,
}

public class Achievement
Expand Down

0 comments on commit b55ef76

Please sign in to comment.