From cfafd1c38bfbb1a9c83915377c30ce654da21934 Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Sat, 4 May 2024 01:13:40 +0800 Subject: [PATCH] Add same behavior to human answers vs llm answers --- MyApp.ServiceInterface/AiServerServices.cs | 21 +---------- MyApp.ServiceInterface/Data/DbExtensions.cs | 26 ++++++++++++++ MyApp.ServiceInterface/QuestionServices.cs | 40 +++++++++++++++++---- MyApp.ServiceModel/Posts.cs | 3 ++ 4 files changed, 63 insertions(+), 27 deletions(-) diff --git a/MyApp.ServiceInterface/AiServerServices.cs b/MyApp.ServiceInterface/AiServerServices.cs index 3bf887e..8975fa1 100644 --- a/MyApp.ServiceInterface/AiServerServices.cs +++ b/MyApp.ServiceInterface/AiServerServices.cs @@ -45,26 +45,7 @@ public async Task Any(CreateAnswerCallback request) } }); - // Only add notifications for answers older than 1hr - var post = await Db.SingleByIdAsync(request.PostId); - if (post?.CreatedBy != null && DateTime.UtcNow - post.CreationDate > TimeSpan.FromHours(1)) - { - if (!string.IsNullOrEmpty(answer.Summary)) - { - MessageProducer.Publish(new DbWrites { - CreateNotification = new() - { - UserName = post.CreatedBy, - PostId = post.Id, - Type = NotificationType.NewAnswer, - CreatedDate = DateTime.UtcNow, - RefId = answer.RefId!, - Summary = answer.Summary, - RefUserName = answer.CreatedBy, - }, - }); - } - } + await Db.NotifyQuestionAuthorIfRequiredAsync(MessageProducer, answer); MessageProducer.Publish(new AiServerTasks { diff --git a/MyApp.ServiceInterface/Data/DbExtensions.cs b/MyApp.ServiceInterface/Data/DbExtensions.cs index 3693cc9..a32f37a 100644 --- a/MyApp.ServiceInterface/Data/DbExtensions.cs +++ b/MyApp.ServiceInterface/Data/DbExtensions.cs @@ -1,6 +1,7 @@ using System.Data; using MyApp.ServiceModel; using ServiceStack; +using ServiceStack.Messaging; using ServiceStack.OrmLite; namespace MyApp.Data; @@ -101,4 +102,29 @@ public static async Task IsWatchingPostAsync(this IDbConnection db, string public static async Task IsWatchingTagAsync(this IDbConnection db, string userName, string tag) => await db.ExistsAsync(db.From().Where(x => x.UserName == userName && x.Tag == tag)); + + public static async Task NotifyQuestionAuthorIfRequiredAsync(this IDbConnection db, IMessageProducer mq, Post answer) + { + // Only add notifications for answers older than 1hr + var post = await db.SingleByIdAsync(answer.ParentId); + if (post?.CreatedBy != null && DateTime.UtcNow - post.CreationDate > TimeSpan.FromHours(1)) + { + if (!string.IsNullOrEmpty(answer.Summary)) + { + mq.Publish(new DbWrites { + CreateNotification = new() + { + UserName = post.CreatedBy, + PostId = post.Id, + Type = NotificationType.NewAnswer, + CreatedDate = DateTime.UtcNow, + RefId = answer.RefId!, + Summary = answer.Summary, + RefUserName = answer.CreatedBy, + }, + }); + } + } + } + } diff --git a/MyApp.ServiceInterface/QuestionServices.cs b/MyApp.ServiceInterface/QuestionServices.cs index 7b7b13a..97bedf5 100644 --- a/MyApp.ServiceInterface/QuestionServices.cs +++ b/MyApp.ServiceInterface/QuestionServices.cs @@ -155,26 +155,52 @@ public async Task Any(AnswerQuestion request) { var userName = GetUserName(); var now = DateTime.UtcNow; - var post = new Post + var answer = new Post { ParentId = request.PostId, - Summary = request.Body.StripHtml().SubstringWithEllipsis(0,200), + Summary = request.Body.GenerateSummary(), CreationDate = now, CreatedBy = userName, LastActivityDate = now, Body = request.Body, - RefId = request.RefId, // Optional External Ref Id, not '{PostId}-{UserName}' + RefUrn = request.RefUrn, + RefId = $"{request.PostId}-{userName}" }; MessageProducer.Publish(new DbWrites { - CreateAnswer = post, + CreateAnswer = answer, AnswerAddedToPost = new() { Id = request.PostId}, }); - await questions.SaveHumanAnswerAsync(post); - rendererCache.DeleteCachedQuestionPostHtml(post.Id); + rendererCache.DeleteCachedQuestionPostHtml(answer.Id); + + await questions.SaveHumanAnswerAsync(answer); + + MessageProducer.Publish(new DbWrites + { + SaveStartingUpVotes = new() + { + Id = answer.RefId!, + PostId = request.PostId, + StartingUpVotes = 0, + CreatedBy = userName, + } + }); - answerNotifier.NotifyNewAnswer(request.PostId, post.CreatedBy); + answerNotifier.NotifyNewAnswer(request.PostId, answer.CreatedBy); + + var userId = Request.GetClaimsPrincipal().GetUserId(); + MessageProducer.Publish(new AiServerTasks + { + CreateRankAnswerTask = new CreateRankAnswerTask { + AnswerId = answer.RefId!, + UserId = userId!, + } + }); + + MessageProducer.Publish(new SearchTasks { + AddAnswerToIndex = answer.RefId + }); return new AnswerQuestionResponse(); } diff --git a/MyApp.ServiceModel/Posts.cs b/MyApp.ServiceModel/Posts.cs index 48e456d..bfd9201 100644 --- a/MyApp.ServiceModel/Posts.cs +++ b/MyApp.ServiceModel/Posts.cs @@ -373,6 +373,9 @@ public class AnswerQuestion : IPost, IReturn [Input(Type="hidden")] public string? RefId { get; set; } + + [Input(Type="hidden")] + public string? RefUrn { get; set; } } public class AnswerQuestionResponse {