Skip to content

Commit

Permalink
Add real time updates for review and revisions (#8925)
Browse files Browse the repository at this point in the history
  • Loading branch information
chidozieononiwu authored Sep 4, 2024
1 parent d8e8f8d commit fc3bda5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using System.Collections.Generic;
using APIViewWeb.Models;
using System.Linq;
using APIViewWeb.Hubs;
using Microsoft.AspNetCore.SignalR;

namespace APIViewWeb.LeanControllers
{
Expand All @@ -20,15 +22,17 @@ public class APIRevisionsController : BaseApiController
private readonly IReviewManager _reviewManager;
private readonly INotificationManager _notificationManager;
private readonly IPullRequestManager _pullRequestManager;
private readonly IHubContext<SignalRHub> _signalRHubContext;

public APIRevisionsController(ILogger<APIRevisionsController> logger,
IReviewManager reviewManager, IPullRequestManager pullRequestManager,
IAPIRevisionsManager apiRevisionsManager, INotificationManager notificationManager)
IAPIRevisionsManager apiRevisionsManager, INotificationManager notificationManager, IHubContext<SignalRHub> signalRHub)
{
_logger = logger;
_apiRevisionsManager = apiRevisionsManager;
_reviewManager = reviewManager;
_notificationManager = notificationManager;
_signalRHubContext = signalRHub;
_pullRequestManager = pullRequestManager;
}

Expand Down Expand Up @@ -111,8 +115,10 @@ public async Task<ActionResult<APIRevisionListItemModel>> ToggleReviewApprovalAs
(var updateReview, var apiRevision) = await _apiRevisionsManager.ToggleAPIRevisionApprovalAsync(User, reviewId, apiRevisionId);
if (updateReview)
{
await _reviewManager.ToggleReviewApprovalAsync(User, reviewId, apiRevisionId);
var updatedReview = await _reviewManager.ToggleReviewApprovalAsync(User, reviewId, apiRevisionId);
await _signalRHubContext.Clients.All.SendAsync("ReviewUpdated", updatedReview);
}
await _signalRHubContext.Clients.All.SendAsync("APIRevisionUpdated", apiRevision);
return new LeanJsonResult(apiRevision, StatusCodes.Status200OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public async Task<ActionResult<APIRevisionListItemModel>> CreateReviewAsync([Fro
public async Task<ActionResult> ToggleReviewApprovalAsync(string reviewId, string apiRevisionId)
{
var updatedReview = await _reviewManager.ToggleReviewApprovalAsync(User, reviewId, apiRevisionId);
await _signalRHubContext.Clients.All.SendAsync("ReviewUpdated", updatedReview);
return new LeanJsonResult(updatedReview, StatusCodes.Status200OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { CodePanelData, CodePanelRowData, CodePanelRowDatatype } from 'src/app/_
import { UserProfile } from 'src/app/_models/userProfile';
import { ReviewPageWorkerMessageDirective } from 'src/app/_models/insertCodePanelRowDataMessage';
import { CommentItemModel } from 'src/app/_models/commentItemModel';
import { SignalRService } from 'src/app/_services/signal-r/signal-r.service';

@Component({
selector: 'app-review-page',
Expand Down Expand Up @@ -72,7 +73,7 @@ export class ReviewPageComponent implements OnInit {

constructor(private route: ActivatedRoute, private router: Router, private apiRevisionsService: RevisionsService,
private reviewsService: ReviewsService, private workerService: WorkerService, private changeDetectorRef: ChangeDetectorRef,
private userProfileService: UserProfileService, private commentsService: CommentsService) {}
private userProfileService: UserProfileService, private commentsService: CommentsService, private signalRService: SignalRService) {}

ngOnInit() {
this.userProfileService.getUserProfile().subscribe(
Expand Down Expand Up @@ -106,6 +107,8 @@ export class ReviewPageComponent implements OnInit {
this.loadAPIRevisions(0, this.apiRevisionPageSize);
this.loadComments();
this.createSideMenu();
this.handleRealTimeReviewUpdates();
this.handleRealTimeAPIRevisionUpdates();
}

createSideMenu() {
Expand Down Expand Up @@ -449,6 +452,33 @@ export class ReviewPageComponent implements OnInit {
this.codePanelComponent.scrollToNode(undefined, value);
}

handleRealTimeReviewUpdates() {
this.signalRService.onReviewUpdates().pipe(takeUntil(this.destroy$)).subscribe({
next: (updatedReview: Review) => {
if (updatedReview.id === this.reviewId) {
this.review = updatedReview;
}
}
});
}

handleRealTimeAPIRevisionUpdates() {
this.signalRService.onAPIRevisionUpdates().pipe(takeUntil(this.destroy$)).subscribe({
next: (updatedAPIRevision: APIRevision) => {
if (updatedAPIRevision.reviewId === this.reviewId) {
const apiRevisionIndex = this.apiRevisions.findIndex(x => x.id === updatedAPIRevision.id);
if (apiRevisionIndex > -1) {
this.apiRevisions[apiRevisionIndex] = updatedAPIRevision;
}

if (updatedAPIRevision.id === this.activeApiRevisionId) {
this.activeAPIRevision = updatedAPIRevision;
}
}
}
});
}

checkForFatalDiagnostics() {
for (const rowData of this.codePanelRowData) {
if (rowData.diagnostics && rowData.diagnostics.level === 'fatal') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Injectable } from '@angular/core';
import * as signalR from '@microsoft/signalr';
import { ConfigService } from '../config/config.service';
import { CommentItemModel, } from 'src/app/_models/commentItemModel';
import { Observable, Subject } from 'rxjs';
import { CommentThreadUpdateAction, CommentUpdatesDto } from 'src/app/_dtos/commentThreadUpdateDto';
import { CommentUpdatesDto } from 'src/app/_dtos/commentThreadUpdateDto';
import { Review } from 'src/app/_models/review';
import { APIRevision } from 'src/app/_models/revision';

@Injectable({
providedIn: 'root'
})
export class SignalRService {
private connection : signalR.HubConnection;
private commentUpdates: Subject<CommentUpdatesDto> = new Subject<CommentUpdatesDto>();
private reviewUpdates: Subject<Review> = new Subject<Review>();
private apiRevisionUpdates: Subject<APIRevision> = new Subject<APIRevision>();

constructor(private configService: ConfigService) {
this.connection = new signalR.HubConnectionBuilder()
Expand Down Expand Up @@ -38,6 +41,8 @@ export class SignalRService {
})
this.handleConnectionId();
this.handleCommentUpdates();
this.handleReviewUpdates();
this.handleAPIRevisionUpdates();
}

handleConnectionId() {
Expand All @@ -52,10 +57,30 @@ export class SignalRService {
});
}

handleReviewUpdates() {
this.connection.on("ReviewUpdated", (updatedReview: Review) => {
this.reviewUpdates.next(updatedReview);
});
}

handleAPIRevisionUpdates() {
this.connection.on("APIRevisionUpdated", (updatedAPIRevision: APIRevision) => {
this.apiRevisionUpdates.next(updatedAPIRevision);
});
}

onCommentUpdates() : Observable<CommentUpdatesDto> {
return this.commentUpdates.asObservable();
}

onReviewUpdates() : Observable<Review> {
return this.reviewUpdates.asObservable();
}

onAPIRevisionUpdates() : Observable<APIRevision> {
return this.apiRevisionUpdates.asObservable();
}

pushCommentUpdates(commentUpdates: CommentUpdatesDto) : void {
if (this.connection && this.connection.state === signalR.HubConnectionState.Connected) {
this.connection.invoke("PushCommentUpdates", commentUpdates);
Expand Down

0 comments on commit fc3bda5

Please sign in to comment.