From e9017498a0bd87e2a5ae457c00f9ddee35809c29 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 15 May 2021 16:01:21 +0800 Subject: feat: Timeline post change notification with signalr. --- BackEnd/Timeline/Controllers/TimelinePostController.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'BackEnd/Timeline/Controllers/TimelinePostController.cs') diff --git a/BackEnd/Timeline/Controllers/TimelinePostController.cs b/BackEnd/Timeline/Controllers/TimelinePostController.cs index da45cbea..f00a689c 100644 --- a/BackEnd/Timeline/Controllers/TimelinePostController.cs +++ b/BackEnd/Timeline/Controllers/TimelinePostController.cs @@ -1,6 +1,8 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -13,6 +15,7 @@ using Timeline.Models.Validation; using Timeline.Services.Mapper; using Timeline.Services.Timeline; using Timeline.Services.User; +using Timeline.SignalRHub; namespace Timeline.Controllers { @@ -24,6 +27,8 @@ namespace Timeline.Controllers [ProducesErrorResponseType(typeof(CommonResponse))] public class TimelinePostController : MyControllerBase { + private readonly ILogger _logger; + private readonly ITimelineService _timelineService; private readonly ITimelinePostService _postService; @@ -31,12 +36,16 @@ namespace Timeline.Controllers private readonly MarkdownProcessor _markdownProcessor; - public TimelinePostController(ITimelineService timelineService, ITimelinePostService timelinePostService, IGenericMapper mapper, MarkdownProcessor markdownProcessor) + private readonly IHubContext _timelineHubContext; + + public TimelinePostController(ILogger logger, ITimelineService timelineService, ITimelinePostService timelinePostService, IGenericMapper mapper, MarkdownProcessor markdownProcessor, IHubContext timelineHubContext) { + _logger = logger; _timelineService = timelineService; _postService = timelinePostService; _mapper = mapper; _markdownProcessor = markdownProcessor; + _timelineHubContext = timelineHubContext; } private bool UserHasAllTimelineManagementPermission => UserHasPermission(UserPermission.AllTimelineManagement); @@ -207,6 +216,11 @@ namespace Timeline.Controllers try { var post = await _postService.CreatePostAsync(timelineId, userId, createRequest); + + var group = TimelineHub.GenerateTimelinePostChangeListeningGroupName(timeline); + await _timelineHubContext.Clients.Group(group).SendAsync(nameof(ITimelineClient.OnTimelinePostChanged), timeline); + _logger.LogInformation("Notify group {0} of timeline post change.", group); + var result = await Map(post); return result; } -- cgit v1.2.3