/// <summary> /// Create a new reply in an existing thread. /// Adding a reply to an existing thread will notify all subscribed and connected clients. /// </summary> /// <param name="parentId">Id of the parent comment.</param> /// <param name="replyText">Text of the reply.</param> public void NewReply(int parentId, string replyText) { User currentUser = db.Users.Find(CodeFirstSecurity.GetUserId(Context.User.Identity.Name)); Comment comment = new Comment(replyText, currentUser); Comment parentComment = db.Comments.Include("Comments").First(pc => pc.CommentId == parentId); if (parentComment.SubjectId != null) { comment.SubjectId = parentComment.SubjectId; } else { comment.SubjectId = parentComment.CommentId; } comment.ParentCommentId = parentComment.CommentId; Comment thread = db.Comments.Find(comment.SubjectId); thread.LastUpdatedTime = DateTime.UtcNow; parentComment.Comments.Add(comment); // For all subscribed users other than the current user, mark this comment as unread. List <Guid> subscribers = new List <Guid>(); foreach (Subscription subscriber in db.Subscriptions.Include("User").Where(x => x.Subject.CommentId == comment.SubjectId)) { subscribers.Add(subscriber.User.UserId); if (subscriber.User.UserId != currentUser.UserId) { db.UnreadItems.Add(new UnreadItem { User = subscriber.User, Comment = comment }); } } db.SaveChanges(); Message sendMessage = new Message() { text = Palaver2.Helpers.CustomHtmlHelpers.BuildComments(comment, null), parentId = parentId, commentId = comment.CommentId, userid = currentUser.UserId.ToString(), threadId = comment.SubjectId, authorName = comment.User.Username }; // Loop through the connections and if they're subscribed to this // thread then update them. foreach (String connectionId in userIdsByConnection.Keys) { if (subscribers.Contains(userIdsByConnection[connectionId])) { Clients.Client(connectionId).addReply(sendMessage); } } }
/// <summary> /// Unsubscribe the current user from the specified thread. /// </summary> /// <param name="threadId"></param> public void Unsubscribe(int threadId) { User currentUser = db.Users.Find(CodeFirstSecurity.GetUserId(Context.User.Identity.Name)); Subscription sub = db.Subscriptions.Where(x => x.Subject.CommentId == threadId && x.User.UserId == currentUser.UserId).Single(); db.Subscriptions.Remove(sub); db.SaveChanges(); }
public ActionResult GetThreads() { Guid userId = CodeFirstSecurity.GetUserId(HttpContext.User.Identity.Name); var comments = from c in db.Comments join s in db.Subscriptions on c.CommentId equals s.Subject.CommentId where !c.ParentCommentId.HasValue && s.User.UserId == userId orderby c.LastUpdatedTime descending select c; return(Content(Helpers.CustomHtmlHelpers.BuildThreads(comments))); }
public ActionResult GetThread(string threadId) { int intThreadId; Guid userId = CodeFirstSecurity.GetUserId(HttpContext.User.Identity.Name); var comments = from c in db.Comments join s in db.Subscriptions on c.CommentId equals s.Subject.CommentId where !c.ParentCommentId.HasValue && s.User.UserId == userId orderby c.LastUpdatedTime descending select c; ViewBag.Comments = comments; if (threadId != null && int.TryParse(threadId, out intThreadId)) { ViewBag.ThreadId = intThreadId; } return(View("Index")); }
/// <summary> /// Create a new thread. /// Creating a thread will subscribe all users to it and notify any connected clients. /// </summary> /// <param name="threadSubject">Text of the thread subject.</param> public void NewThread(string threadSubject) { User currentUser = db.Users.Find(CodeFirstSecurity.GetUserId(Context.User.Identity.Name)); Comment comment = new Comment(threadSubject, currentUser); db.Comments.Add(comment); db.SaveChanges(); // For new threads, subscribe everyone and add the first entry to everyone's // unread list. foreach (User uu in db.Users) { db.Subscriptions.Add(new Subscription { User = uu, Subject = comment }); if (uu.UserId != currentUser.UserId) { db.UnreadItems.Add(new UnreadItem { User = uu, Comment = comment }); } } comment.SubjectId = comment.CommentId; db.SaveChanges(); string html = Palaver2.Helpers.CustomHtmlHelpers.BuildThread(comment, null); Message sendMessage = new Message() { text = html, commentId = comment.CommentId, userid = currentUser.UserId.ToString(), threadId = comment.SubjectId, authorName = comment.User.Username }; Clients.All.addThread(sendMessage); }
public override Task OnReconnected() { userIdsByConnection.Add(Context.ConnectionId, db.Users.Find(CodeFirstSecurity.GetUserId(Context.User.Identity.Name)).UserId); System.Diagnostics.Debug.WriteLine("Reonnected ConnectionId: " + Context.ConnectionId); return(base.OnReconnected()); }