From 4834b181710efb0cdb96545de0b6b1caf69745d6 Mon Sep 17 00:00:00 2001 From: Justin Britton Date: Thu, 11 Mar 2021 14:34:57 -0500 Subject: [PATCH 1/4] backend signalr changes for collab editor --- src/TopoMojo.Abstractions/Models/Document.cs | 13 ++++++++ .../Controllers/DocumentController.cs | 30 +++++++++++++++++-- src/TopoMojo.Web/Controllers/Hubs/TopoHub.cs | 19 +++++++++++- 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 src/TopoMojo.Abstractions/Models/Document.cs diff --git a/src/TopoMojo.Abstractions/Models/Document.cs b/src/TopoMojo.Abstractions/Models/Document.cs new file mode 100644 index 00000000..a5f7d549 --- /dev/null +++ b/src/TopoMojo.Abstractions/Models/Document.cs @@ -0,0 +1,13 @@ +// Copyright 2020 Carnegie Mellon University. All Rights Reserved. +// Released under a 3 Clause BSD-style license. See LICENSE.md in the project root for license information. + +using System; + +namespace TopoMojo.Models +{ + public class Document + { + public string Text { get; set; } + public string WhenSaved { get; set; } + } +} diff --git a/src/TopoMojo.Web/Controllers/DocumentController.cs b/src/TopoMojo.Web/Controllers/DocumentController.cs index 904084ed..43a87779 100644 --- a/src/TopoMojo.Web/Controllers/DocumentController.cs +++ b/src/TopoMojo.Web/Controllers/DocumentController.cs @@ -8,9 +8,11 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Logging; using TopoMojo.Abstractions; using TopoMojo.Extensions; +using TopoMojo.Models; using TopoMojo.Services; using TopoMojo.Web.Models; @@ -24,24 +26,28 @@ public DocumentController( ILogger logger, IIdentityResolver identityResolver, WorkspaceService workspaceService, - FileUploadOptions uploadOptions + FileUploadOptions uploadOptions, + IHubContext hub ) : base(logger, identityResolver) { _uploadOptions = uploadOptions; _workspaceService = workspaceService; + _hub = hub; } private readonly WorkspaceService _workspaceService; private readonly FileUploadOptions _uploadOptions; + private readonly IHubContext _hub; /// /// Save markdown as document. /// /// Workspace Id /// Markdown text + /// Cause of save was automatic from user typing /// [HttpPut("api/document/{id}")] - public async Task Save(string id, [FromBody]string text) + public async Task Save(string id, [FromBody]string text, bool fromTyping = false) { if (!await _workspaceService.CanEdit(id)) return Forbid(); @@ -49,8 +55,10 @@ public async Task Save(string id, [FromBody]string text) string path = BuildPath(); path = System.IO.Path.Combine(path, id + ".md"); - System.IO.File.WriteAllText(path, text); + + if (fromTyping) + SendBroadcast(id, "saved", text); return Ok(); } @@ -139,6 +147,22 @@ private string BuildPath(params string[] segments) return path; } + private void SendBroadcast(string roomId, string action, string text) + { + _hub.Clients + .Group(roomId) + .DocumentEvent( + new BroadcastEvent( + User, + "DOCUMENT." + action.ToUpper(), + new Document { + Text = text, + WhenSaved = DateTime.UtcNow.ToString("u") + } + ) + ); + } + } } diff --git a/src/TopoMojo.Web/Controllers/Hubs/TopoHub.cs b/src/TopoMojo.Web/Controllers/Hubs/TopoHub.cs index c5548bd2..e6f8cb4b 100644 --- a/src/TopoMojo.Web/Controllers/Hubs/TopoHub.cs +++ b/src/TopoMojo.Web/Controllers/Hubs/TopoHub.cs @@ -84,6 +84,21 @@ public Task TemplateMessage(string action, Template model){ return Clients.OthersInGroup(model.WorkspaceGlobalId).TemplateEvent(new BroadcastEvent