aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/SignalRHub
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-05-15 16:01:21 +0800
committercrupest <crupest@outlook.com>2021-05-15 16:01:21 +0800
commite9017498a0bd87e2a5ae457c00f9ddee35809c29 (patch)
tree3658ceb6200f0feaf1d2a95d5d9990aba9de9fbd /BackEnd/Timeline/SignalRHub
parentd5c70793f203dccf96219b89c2da8e5432617e17 (diff)
downloadtimeline-e9017498a0bd87e2a5ae457c00f9ddee35809c29.tar.gz
timeline-e9017498a0bd87e2a5ae457c00f9ddee35809c29.tar.bz2
timeline-e9017498a0bd87e2a5ae457c00f9ddee35809c29.zip
feat: Timeline post change notification with signalr.
Diffstat (limited to 'BackEnd/Timeline/SignalRHub')
-rw-r--r--BackEnd/Timeline/SignalRHub/ITimelineClient.cs9
-rw-r--r--BackEnd/Timeline/SignalRHub/TimelineHub.cs58
2 files changed, 67 insertions, 0 deletions
diff --git a/BackEnd/Timeline/SignalRHub/ITimelineClient.cs b/BackEnd/Timeline/SignalRHub/ITimelineClient.cs
new file mode 100644
index 00000000..0d1be093
--- /dev/null
+++ b/BackEnd/Timeline/SignalRHub/ITimelineClient.cs
@@ -0,0 +1,9 @@
+using System.Threading.Tasks;
+
+namespace Timeline.SignalRHub
+{
+ public interface ITimelineClient
+ {
+ Task OnTimelinePostChanged(string timelineName);
+ }
+}
diff --git a/BackEnd/Timeline/SignalRHub/TimelineHub.cs b/BackEnd/Timeline/SignalRHub/TimelineHub.cs
new file mode 100644
index 00000000..2ad7bd66
--- /dev/null
+++ b/BackEnd/Timeline/SignalRHub/TimelineHub.cs
@@ -0,0 +1,58 @@
+using Microsoft.AspNetCore.SignalR;
+using Microsoft.Extensions.Logging;
+using System;
+using System.Threading.Tasks;
+using Timeline.Auth;
+using Timeline.Services;
+using Timeline.Services.Timeline;
+using Timeline.Services.User;
+
+namespace Timeline.SignalRHub
+{
+ public class TimelineHub : Hub<ITimelineClient>
+ {
+ private readonly ILogger<TimelineHub> _logger;
+ private readonly ITimelineService _timelineService;
+
+ public TimelineHub(ILogger<TimelineHub> logger, ITimelineService timelineService)
+ {
+ _logger = logger;
+ _timelineService = timelineService;
+ }
+
+ public static string GenerateTimelinePostChangeListeningGroupName(string timelineName)
+ {
+ return $"timeline-post-change-{timelineName}";
+ }
+
+ public async Task SubscribeTimelinePostChange(string timelineName)
+ {
+ try
+ {
+ var timelineId = await _timelineService.GetTimelineIdByNameAsync(timelineName);
+ var user = Context.User;
+ if (!user.HasPermission(UserPermission.AllTimelineManagement) && !await _timelineService.HasReadPermissionAsync(timelineId, user.GetOptionalUserId()))
+ throw new HubException("Forbidden.");
+
+ var group = GenerateTimelinePostChangeListeningGroupName(timelineName);
+ await Groups.AddToGroupAsync(Context.ConnectionId, group);
+ _logger.LogInformation("Add connection {0} to group {1}", Context.ConnectionId, group);
+ }
+ catch (ArgumentException)
+ {
+ throw new HubException("Timeline name is illegal.");
+ }
+ catch (EntityNotExistException)
+ {
+ throw new HubException("Timeline not exist.");
+ }
+ }
+
+ public async Task UnsubscribeTimelinePostChange(string timelineName)
+ {
+ var group = GenerateTimelinePostChangeListeningGroupName(timelineName);
+ await Groups.RemoveFromGroupAsync(Context.ConnectionId, group);
+ _logger.LogInformation("Remove connection {0} from group {1}", Context.ConnectionId, group);
+ }
+ }
+}