Notification daemon works.

You now get notifications when someone you follow makes a post - or when
a user replies to a topic you made.
This commit is contained in:
Michael 2017-03-24 15:54:53 -04:00
parent c29c136621
commit cd73cc80d1
4 changed files with 65 additions and 1 deletions

View file

@ -223,6 +223,8 @@ public ActionResult PostReply(CreateTopicViewModel model)
post.PostedAt = DateTime.Now;
db.ForumPosts.Add(post);
db.SaveChanges();
NotificationDaemon.NotifyFollowers(User.Identity.GetUserId(), "New post from " + ACL.UserNameRaw(User.Identity.GetUserId()) + "!", ACL.UserNameRaw(User.Identity.GetUserId()) + " just posted a reply to " + topic.Subject + ".", Url.Action("ViewTopic", new { id = topic.Discriminator }));
NotificationDaemon.NotifyUser(User.Identity.GetUserId(), topic.AuthorId, "New post from " + ACL.UserNameRaw(User.Identity.GetUserId()) + "!", ACL.UserNameRaw(User.Identity.GetUserId()) + " just posted a reply to " + topic.Subject + ".", Url.Action("ViewTopic", new { id = topic.Discriminator }));
return RedirectToAction("ViewTopic", new { id = topic.Discriminator });
@ -292,6 +294,7 @@ public ActionResult CreateTopic(CreateTopicViewModel model)
db.ForumTopics.Add(topic);
db.ForumPosts.Add(post);
db.SaveChanges();
NotificationDaemon.NotifyFollowers(User.Identity.GetUserId(), "New topic started by " + ACL.UserNameRaw(User.Identity.GetUserId()) + "!", ACL.UserNameRaw(User.Identity.GetUserId()) + " just started a topic: " + topic.Subject + ".", Url.Action("ViewTopic", new { id = topic.Discriminator }));
return RedirectToAction("ViewTopic", new { id = topic.Discriminator });

View file

@ -142,7 +142,8 @@ public ActionResult LikePost(string id)
return RedirectToAction("ViewProfile", new { id = ACL.UserNameRaw(tid) });
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult PostContent(UserPost model)
{
var db = new ApplicationDbContext();
@ -151,6 +152,8 @@ public ActionResult PostContent(UserPost model)
model.UserId = User.Identity.GetUserId();
db.UserPosts.Add(model);
db.SaveChanges();
NotificationDaemon.NotifyFollowers(User.Identity.GetUserId(), "New post from " + ACL.UserNameRaw(User.Identity.GetUserId()) + "!", ACL.UserNameRaw(User.Identity.GetUserId()) + " just posted something on their profile.", Url.Action("ViewProfile", new { id = ACL.UserNameRaw(User.Identity.GetUserId()) }));
return RedirectToAction("ViewProfile", "Profiles", new { id = ACL.UserNameRaw(User.Identity.GetUserId()) });
}

View file

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Project_Unite.Models;
namespace Project_Unite
{
public static class NotificationDaemon
{
public static void NotifyFollowers(string uid, string title, string desc, string url)
{
var db = new ApplicationDbContext();
var user = db.Users.FirstOrDefault(x => x.Id == uid);
if (user == null)
throw new Exception("Cannot find user with ID " + uid + ".");
foreach(var follower in user.Followers)
{
NotifyUser(uid, follower.Follower, title, desc, url);
}
}
public static void NotifyEveryone(string uid, string title, string desc, string url)
{
var db = new ApplicationDbContext();
var user = db.Users.FirstOrDefault(x => x.Id == uid);
if (user == null)
throw new Exception("Cannot find user with ID " + uid + ".");
foreach (var usr in db.Users.Where(x=>x.Id!=uid).ToArray())
{
NotifyUser(uid, usr.Id, title, desc, url);
}
}
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);
if (user == null)
throw new Exception("Cannot find user with ID " + target + ".");
var note = new Notification
{
Id = Guid.NewGuid().ToString(),
UserId = target,
Title = title,
ActionUrl = url,
Description = desc,
AvatarUrl = user.AvatarUrl
};
db.Notifications.Add(note);
db.SaveChanges();
}
}
}

View file

@ -411,6 +411,7 @@
<Compile Include="Models\ManageViewModels.cs" />
<Compile Include="Models\Notification.cs" />
<Compile Include="Models\Role.cs" />
<Compile Include="NotificationDaemon.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Startup.cs" />
</ItemGroup>