Skip to content

Commit

Permalink
Add RealTime Comment Updates (#8868)
Browse files Browse the repository at this point in the history
* Add Signal R package

* Add SignalR Service

* Push Comment Updates to All clients from controller

* Hookup SignalR ClientSide

* Two way comment push

* Add Real Time Comment Updates
  • Loading branch information
chidozieononiwu authored Aug 28, 2024
1 parent 5835f21 commit 3051445
Show file tree
Hide file tree
Showing 21 changed files with 600 additions and 137 deletions.
43 changes: 43 additions & 0 deletions src/dotnet/APIView/APIViewWeb/DTOs/CommentUpdatesDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Text.Json.Serialization;
using APIViewWeb.LeanModels;

namespace APIViewWeb.DTOs
{
public enum CommentThreadUpdateAction
{
CommentCreated = 0,
CommentTextUpdate,
CommentResolved,
CommentUnResolved,
CommentUpVoteToggled,
CommentDeleted
}

public class CommentUpdatesDto
{
[JsonPropertyName("commentThreadUpdateAction")]
public CommentThreadUpdateAction CommentThreadUpdateAction { get; set; }
[JsonPropertyName("nodeId")]
public string NodeId { get; set; }
[JsonPropertyName("nodeIdHashed")]
public string NodeIdHashed { get; set; }
[JsonPropertyName("reviewId")]
public string ReviewId { get; set; }
[JsonPropertyName("revisionId")]
public string RevisionId { get; set; }
[JsonPropertyName("commentId")]
public string CommentId { get; set; }
[JsonPropertyName("elementId")]
public string ElementId { get; set; }
[JsonPropertyName("commentText")]
public string CommentText { get; set; }
[JsonPropertyName("comment")]
public CommentItemModel Comment { get; set; }
[JsonPropertyName("resolvedBy")]
public string ResolvedBy { get; set; }
[JsonPropertyName("associatedRowPositionInGroup")]
public int? AssociatedRowPositionInGroup { get; set; }
[JsonPropertyName("allowAnyOneToResolve")]
public bool? AllowAnyOneToResolve { get; set; }
}
}
8 changes: 8 additions & 0 deletions src/dotnet/APIView/APIViewWeb/Helpers/APIHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public PagedList(IEnumerable<T> items, int noOfItemsRead, int totalCount, int pa
public class LeanJsonResult : JsonResult
{
private readonly int _statusCode;
private readonly string _locationUrl;

private static readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions
{
Expand All @@ -88,6 +89,12 @@ public LeanJsonResult(object value, int statusCode) : base(value)
_statusCode = statusCode;
}

public LeanJsonResult(object value, int statusCode, string locationUrl) : base(value)
{
_statusCode = statusCode;
_locationUrl = locationUrl;
}

public override async Task ExecuteResultAsync(ActionContext context)
{
if (context == null)
Expand All @@ -99,6 +106,7 @@ public override async Task ExecuteResultAsync(ActionContext context)

response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json";
response.StatusCode = _statusCode;
response.Headers["Location"] = _locationUrl;

var serializedValue = JsonSerializer.Serialize(Value, _serializerOptions);
await response.WriteAsync(serializedValue);
Expand Down
13 changes: 11 additions & 2 deletions src/dotnet/APIView/APIViewWeb/Hubs/SignalRHub.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Threading.Tasks;
using APIViewWeb.DTOs;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;

namespace APIViewWeb.Hubs
{
Expand All @@ -26,5 +26,14 @@ public async Task PushComment(string reviewId, string elementId, string partialV
await Clients.Others.SendAsync("ReceiveComment", reviewId, elementId, partialViewResult);
}
}
}

/// <summary>
/// Endpoint Consumed by Client SPA
/// </summary>
/// <returns></returns>
public async Task PushCommentUpdates(CommentUpdatesDto commentUpdatesDto)
{
await Clients.All.SendAsync("ReceiveCommentUpdates", commentUpdatesDto);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using APIViewWeb.Helpers;
using APIViewWeb.Hubs;
using APIViewWeb.LeanModels;
using APIViewWeb.Managers;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
Expand All @@ -19,8 +21,8 @@ public class CommentsController : BaseApiController
private readonly IReviewManager _reviewManager;
private readonly INotificationManager _notificationManager;

public CommentsController(ILogger<CommentsController> logger,
ICommentsManager commentManager, IReviewManager reviewManager, INotificationManager notificationManager)
public CommentsController(ILogger<CommentsController> logger, ICommentsManager commentManager,
IReviewManager reviewManager, INotificationManager notificationManager)
{
_logger = logger;
_commentsManager = commentManager;
Expand Down Expand Up @@ -126,7 +128,7 @@ public async Task<ActionResult> CreateCommentAsync(
{
await _notificationManager.SubscribeAsync(review, User);
}
return CreatedAtAction("GetComments", new { reviewId = reviewId }, comment);
return new LeanJsonResult(comment, StatusCodes.Status201Created, Url.Action("GetComments", new { reviewId = reviewId }));
}

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions src/dotnet/APIView/APIViewWeb/LeanModels/ChangeHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace APIViewWeb.LeanModels
{
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))]
[JsonConverter(typeof(StringEnumConverter))]
public enum AICommentChangeAction
{
Expand All @@ -12,6 +13,7 @@ public enum AICommentChangeAction
Modified
}

[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))]
[JsonConverter(typeof(StringEnumConverter))]
public enum ReviewChangeAction
{
Expand All @@ -24,6 +26,7 @@ public enum ReviewChangeAction
UnDeleted
}

[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))]
[JsonConverter(typeof(StringEnumConverter))]
public enum APIRevisionChangeAction
{
Expand All @@ -34,6 +37,7 @@ public enum APIRevisionChangeAction
UnDeleted
}

[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))]
[JsonConverter(typeof(StringEnumConverter))]
public enum CommentChangeAction
{
Expand Down
2 changes: 2 additions & 0 deletions src/dotnet/APIView/APIViewWeb/LeanModels/CommentItemModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace APIViewWeb.LeanModels
{
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))]
[JsonConverter(typeof(StringEnumConverter))]
public enum CommentType
{
Expand All @@ -14,6 +15,7 @@ public enum CommentType

public class CommentItemModel
{
[System.Text.Json.Serialization.JsonPropertyName("id")]
[JsonProperty("id")]
public string Id { get; set; } = IdHelper.GenerateId();
public string ReviewId { get; set; }
Expand Down
Loading

0 comments on commit 3051445

Please sign in to comment.