Skip to content
This repository has been archived by the owner on Aug 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #10 from cmu-sei/editor
Browse files Browse the repository at this point in the history
Backend SignalR changes for collaborative editor
  • Loading branch information
sei-jmattson committed May 13, 2021
2 parents e1f599e + 8ceeaaf commit 9dbcd4c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/TopoMojo.Abstractions/Models/Document.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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; }
public string Timestamp { get; set; }
}
}
28 changes: 26 additions & 2 deletions src/TopoMojo.Web/Controllers/DocumentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,15 +26,18 @@ public DocumentController(
ILogger<AdminController> logger,
IIdentityResolver identityResolver,
WorkspaceService workspaceService,
FileUploadOptions uploadOptions
FileUploadOptions uploadOptions,
IHubContext<TopologyHub, ITopoEvent> hub
) : base(logger, identityResolver)
{
_uploadOptions = uploadOptions;
_workspaceService = workspaceService;
_hub = hub;
}

private readonly WorkspaceService _workspaceService;
private readonly FileUploadOptions _uploadOptions;
private readonly IHubContext<TopologyHub, ITopoEvent> _hub;

/// <summary>
/// Save markdown as document.
Expand All @@ -49,8 +54,9 @@ public async Task<ActionResult> Save(string id, [FromBody]string text)
string path = BuildPath();

path = System.IO.Path.Combine(path, id + ".md");

System.IO.File.WriteAllText(path, text);

SendBroadcast($"{id}-doc", "saved", text);

return Ok();
}
Expand Down Expand Up @@ -139,6 +145,24 @@ private string BuildPath(params string[] segments)
return path;
}

private void SendBroadcast(string roomId, string action, string text)
{
DateTime currentDatetime = DateTime.UtcNow;
_hub.Clients
.Group(roomId)
.DocumentEvent(
new BroadcastEvent<Document>(
User,
"DOCUMENT." + action.ToUpper(),
new Document {
Text = text,
WhenSaved = currentDatetime.ToString("u"),
Timestamp = currentDatetime.ToString("ss.ffff")
}
)
);
}

}

}
19 changes: 18 additions & 1 deletion src/TopoMojo.Web/Controllers/Hubs/TopoHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ public Task TemplateMessage(string action, Template model){
return Clients.OthersInGroup(model.WorkspaceGlobalId).TemplateEvent(new BroadcastEvent<Template>(Context.User, action, model));
}

public Task Edited(string channelId, object textDiff)
{
return Clients.OthersInGroup(channelId).DocumentEvent(new BroadcastEvent<object>(Context.User, "DOCUMENT.UPDATED", textDiff));
}

public Task CursorChanged(string channelId, object positions)
{
return Clients.OthersInGroup(channelId).DocumentEvent(new BroadcastEvent<object>(Context.User, "DOCUMENT.CURSOR", positions));
}

public Task Editing(string channelId, bool val)
{
return Clients.OthersInGroup(channelId).DocumentEvent(new BroadcastEvent<Document>(Context.User, (val) ? "DOCUMENT.TYPING" : "DOCUMENT.IDLE", null));
}

}

public interface ITopoEvent
Expand All @@ -92,6 +107,8 @@ public interface ITopoEvent
Task TopoEvent(BroadcastEvent<Workspace> broadcastEvent);
Task TemplateEvent(BroadcastEvent<Template> broadcastEvent);
Task ChatEvent(BroadcastEvent<Message> broadcastEvent);
Task DocumentEvent(BroadcastEvent<object> broadcastEvent);
Task DocumentEvent(BroadcastEvent<Document> broadcastEvent);
Task VmEvent(BroadcastEvent<VmState> broadcastEvent);
Task PresenceEvent(BroadcastEvent broadcastEvent);
Task GameEvent(BroadcastEvent<GameState> broadcastEvent);
Expand Down Expand Up @@ -138,7 +155,7 @@ public static Actor AsActor(this System.Security.Principal.IPrincipal user)
return new Actor
{
Id = ((ClaimsPrincipal)user).FindFirstValue(JwtRegisteredClaimNames.Sub),
Name = user.Identity.Name
Name = ((ClaimsPrincipal)user).FindFirstValue("name")
};
}
}
Expand Down

0 comments on commit 9dbcd4c

Please sign in to comment.