Skip to content

Commit

Permalink
Add same behavior to human answers vs llm answers
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed May 3, 2024
1 parent 2fb30e8 commit cfafd1c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 27 deletions.
21 changes: 1 addition & 20 deletions MyApp.ServiceInterface/AiServerServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,7 @@ public async Task Any(CreateAnswerCallback request)
}
});

// Only add notifications for answers older than 1hr
var post = await Db.SingleByIdAsync<Post>(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
{
Expand Down
26 changes: 26 additions & 0 deletions MyApp.ServiceInterface/Data/DbExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Data;
using MyApp.ServiceModel;
using ServiceStack;
using ServiceStack.Messaging;
using ServiceStack.OrmLite;

namespace MyApp.Data;
Expand Down Expand Up @@ -101,4 +102,29 @@ public static async Task<bool> IsWatchingPostAsync(this IDbConnection db, string

public static async Task<bool> IsWatchingTagAsync(this IDbConnection db, string userName, string tag) =>
await db.ExistsAsync(db.From<WatchTag>().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<Post>(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,
},
});
}
}
}

}
40 changes: 33 additions & 7 deletions MyApp.ServiceInterface/QuestionServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,52 @@ public async Task<object> 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();
}
Expand Down
3 changes: 3 additions & 0 deletions MyApp.ServiceModel/Posts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@ public class AnswerQuestion : IPost, IReturn<AnswerQuestionResponse>

[Input(Type="hidden")]
public string? RefId { get; set; }

[Input(Type="hidden")]
public string? RefUrn { get; set; }
}
public class AnswerQuestionResponse
{
Expand Down

0 comments on commit cfafd1c

Please sign in to comment.