diff --git a/Project-Unite/Controllers/ForumController.cs b/Project-Unite/Controllers/ForumController.cs index 9e3275a..f64bcfb 100644 --- a/Project-Unite/Controllers/ForumController.cs +++ b/Project-Unite/Controllers/ForumController.cs @@ -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 }); diff --git a/Project-Unite/Controllers/ProfilesController.cs b/Project-Unite/Controllers/ProfilesController.cs index 4e5e436..9a1073e 100644 --- a/Project-Unite/Controllers/ProfilesController.cs +++ b/Project-Unite/Controllers/ProfilesController.cs @@ -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()) }); } diff --git a/Project-Unite/NotificationDaemon.cs b/Project-Unite/NotificationDaemon.cs new file mode 100644 index 0000000..b8081e2 --- /dev/null +++ b/Project-Unite/NotificationDaemon.cs @@ -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(); + } + + + } +} \ No newline at end of file diff --git a/Project-Unite/Project-Unite.csproj b/Project-Unite/Project-Unite.csproj index f54b154..b45abe5 100644 --- a/Project-Unite/Project-Unite.csproj +++ b/Project-Unite/Project-Unite.csproj @@ -411,6 +411,7 @@ +