SignalR notifications

This commit is contained in:
Michael 2017-04-17 08:30:58 -04:00
parent c018558559
commit a9b8092732
11 changed files with 3067 additions and 2 deletions

View file

@ -62,6 +62,15 @@ public static IHtmlString GetLatestUnread(this HtmlHelper hpr, string userName)
return hpr.Raw(builder.ToString());
}
internal static object NotificationCountRaw(string uid)
{
var db = new ApplicationDbContext();
var usr = db.Users.FirstOrDefault(x => x.Id == uid || x.UserName == uid);
if (usr == null)
return 0;
return usr.UnreadNotifications;
}
public static bool IsUnlisted(string topicId)
{
return new ApplicationDbContext().ForumTopics.FirstOrDefault(x => x.Id == topicId).IsUnlisted;

View file

@ -14,6 +14,9 @@ public partial class Startup
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
app.MapSignalR();
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

View file

@ -45,5 +45,11 @@ public ActionResult Skins()
var db = new ApplicationDbContext();
return Content(Serializer.Serialize(db.Skins.ToArray()));
}
[Authorize]
public ActionResult GetNotificationCount()
{
return Content(ACL.NotificationCountRaw(User.Identity.Name).ToString());
}
}
}

View file

@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.AspNet.SignalR;
using Project_Unite.Models;
namespace Project_Unite
@ -10,6 +12,13 @@ public static class NotificationDaemon
{
public static Action<Notification> OnBroadcast;
private static void SendMessage(string uid, string message)
{
GlobalHost
.ConnectionManager
.GetHubContext<NotificationHub>().Clients.User(uid).sendMessage(message);
}
public static void NotifyFollowers(string uid, string title, string desc, string url)
{
var db = new ApplicationDbContext();
@ -34,7 +43,28 @@ public static void NotifyEveryone(string uid, string title, string desc, string
}
}
public static void NotifyUser(string uid, string target, string title, string desc, string url)
private static string ComposeHtml(Notification note)
{
var builder = new StringBuilder();
builder.AppendLine("<a href=\"" + note.ActionUrl + "\">");
//Avatar holder start:
builder.AppendLine("<div style=\"width:64px;height:64px;display:inline-block;\">");
//Avatar
builder.AppendLine("<img src=\"" + note.AvatarUrl + "\" width=\"64\" height=\"64\"/>");
//Avatar holder end:
builder.AppendLine("</div>");
//Notification title.
builder.AppendLine("<p><strong>" + note.Title + "</strong><br/><br/>");
//Contents.
builder.AppendLine(note.Description + "</p>");
builder.AppendLine("</a>");
return builder.ToString();
}
public static void NotifyUser(string uid, string target, string title, string desc, string url)
{
var db = new ApplicationDbContext();
var user = db.Users.FirstOrDefault(x => x.Id == uid);
@ -53,6 +83,8 @@ public static void NotifyUser(string uid, string target, string title, string de
db.Notifications.Add(note);
db.SaveChanges();
SendMessage(target, ComposeHtml(note));
}

View file

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace Project_Unite
{
public class NotificationHub : Hub
{
public string Activate()
{
return "Monitor Activated";
}
}
}

View file

@ -101,6 +101,14 @@
<HintPath>..\packages\Microsoft.ApplicationInsights.2.0.0\lib\net45\Microsoft.ApplicationInsights.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.AspNet.SignalR.Core, Version=2.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.SignalR.Core.2.2.2-preview1\lib\net45\Microsoft.AspNet.SignalR.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.AspNet.SignalR.SystemWeb, Version=2.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.SignalR.SystemWeb.2.2.2-preview1\lib\net45\Microsoft.AspNet.SignalR.SystemWeb.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
<Private>True</Private>
@ -440,6 +448,7 @@
<Compile Include="Models\Skin.cs" />
<Compile Include="Models\WikiModels.cs" />
<Compile Include="NotificationDaemon.cs" />
<Compile Include="NotificationHub.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Startup.cs" />
</ItemGroup>
@ -464,6 +473,8 @@
<Content Include="Scripts\jquery-1.10.2.js" />
<Content Include="Scripts\jquery-1.10.2.min.js" />
<None Include="Scripts\jquery.validate-vsdoc.js" />
<Content Include="Scripts\jquery.signalR-2.2.2-preview1.js" />
<Content Include="Scripts\jquery.signalR-2.2.2-preview1.min.js" />
<Content Include="Scripts\jquery.validate.js" />
<Content Include="Scripts\jquery.validate.min.js" />
<Content Include="Scripts\jquery.validate.unobtrusive.js" />

Binary file not shown.

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

View file

@ -22,7 +22,7 @@
@Styles.Render("~/Content/bootstrap-theme.css")
@Styles.Render("~/Content/Site.css")
<link rel="stylesheet" href="https://cdn.rawgit.com/xcatliu/simplemde-theme-dark/master/dist/simplemde-theme-dark.min.css" />
@Scripts.Render("~/signalr/hubs")
@Scripts.Render("~/Scripts/simplemde.js")
@Scripts.Render("~/bundles/modernizr")
</head>
@ -238,6 +238,22 @@
<script type="text/javascript">
var mde = new SimpleMDE({forceSync: true});
mde.options.forceSync();
var notificationHub = $.connection.notificationHub;
$.connection.hub.start(function () {
notificationHub.activate(function (response) {
$("#notifications_body")
.find('ul')
.append($("<li></li>").html(response));
$.ajax({
url: "/API/GetNotificationCount",
success: function (result) {
$("#notification_count").html(result);
}
});
});
});
</script>
</body>

View file

@ -19,6 +19,10 @@
<package id="Microsoft.AspNet.Identity.Owin" version="2.2.1" targetFramework="net452" />
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.SignalR" version="2.2.2-preview1" targetFramework="net452" />
<package id="Microsoft.AspNet.SignalR.Core" version="2.2.2-preview1" targetFramework="net452" />
<package id="Microsoft.AspNet.SignalR.JS" version="2.2.2-preview1" targetFramework="net452" />
<package id="Microsoft.AspNet.SignalR.SystemWeb" version="2.2.2-preview1" targetFramework="net452" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net452" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" />