/// <summary> /// Creates the notification. /// </summary> /// <param name="userid">The userid.</param> /// <param name="post">The post.</param> /// <param name="time">The time.</param> /// <returns></returns> public Notification CreateNotification(Guid userid, GlobalPostID post, DateTime time) { var db = Current.DB; var notification = new Notification(); notification.UserID = userid; notification.IsUnread = true; notification.GlobalPostID = post.GlobalPostID1; notification.Date = time; db.Notifications.InsertOnSubmit(notification); db.SubmitChanges(); InvalidateNotificationCache(userid); return(notification); }
public virtual ActionResult Compose(ComposeViewModel data) { var db = Current.DB; var currentTime = DateTime.Now; var message = new Message(); var conversation = new Conversation(); // Sender IDs message.SenderID = Current.UserID.Value; conversation.User1 = Current.UserID.Value; message.Date = currentTime; message.IsUnread = true; message.NumberInConvo = 1; conversation.LastMessageDate = currentTime; conversation.StartDate = currentTime; conversation.Subject = HtmlUtilities.Safe(data.Subject.TruncateWithEllipsis(100)); // Look up other user var otherUserObj = Membership.FindUsersByName(data.UserName)[data.UserName]; if (otherUserObj == null) { ModelState.AddModelError("UserName", "Invalid user"); return(View(data)); } var otherUser = (Guid)otherUserObj.ProviderUserKey; message.ReceipientID = otherUser; conversation.User2 = otherUser; // Handle markdown message.Markdown = HtmlUtilities.Sanitize(data.Markdown); message.Html = HtmlUtilities.Safe(HtmlUtilities.RawToCooked(message.Markdown)); // Submit try { db.Conversations.InsertOnSubmit(conversation); //An exception will be thrown here if there are invalid properties db.SubmitChanges(); message.ConversationID = conversation.ConversationID; db.Messages.InsertOnSubmit(message); db.SubmitChanges(); } catch (Exception ex) { // Yikes Elmah.ErrorSignal.FromCurrentContext().Raise(ex, Current.Context); return(RedirectToAction("InternalServerError", "Error")); } // Global Post: var gpostconversation = new GlobalPostID(); gpostconversation.UserID = Current.UserID.Value; gpostconversation.PostCategory = MagicCategoryStrings.Conversation; gpostconversation.SubmissionDate = currentTime; gpostconversation.SpecificPostID = conversation.ConversationID; db.GlobalPostIDs.InsertOnSubmit(gpostconversation); db.SubmitChanges(); conversation.GlobalPostID = gpostconversation.GlobalPostID1; db.SubmitChanges(); var gpostmessage = new GlobalPostID(); gpostmessage.UserID = Current.UserID.Value; gpostmessage.PostCategory = MagicCategoryStrings.Message; gpostmessage.SubmissionDate = currentTime; gpostmessage.SpecificPostID = message.MessageID; db.GlobalPostIDs.InsertOnSubmit(gpostmessage); db.SubmitChanges(); message.GlobalPostID = gpostmessage.GlobalPostID1; db.SubmitChanges(); // Notification: new NotificationsController().CreateNotification(otherUser, gpostconversation, currentTime); return(RedirectToAction("Thread", new { id = conversation.ConversationID })); }
public virtual ActionResult Reply(ThreadViewModel model) { if (!ModelState.IsValid) { return(ThreadWithModel(model.ThreadID, model)); } var db = Current.DB; var currentTime = DateTime.Now; var data = new Message(); data.SenderID = Current.UserID.Value; data.ConversationID = model.ThreadID; var thread = db.Conversations.Where(c => c.ConversationID == model.ThreadID).SingleOrDefault(); // Set receipient ID to the other user's ID if (Current.UserID.Value == thread.User1) { data.ReceipientID = thread.User2; } else // Current.UserID.Value == thread.User2 { data.ReceipientID = thread.User1; } data.Date = currentTime; thread.LastMessageDate = currentTime; data.NumberInConvo = thread.Messages.OrderByDescending(m => m.Date).First().NumberInConvo + 1; data.IsUnread = true; // Handle markdown data.Markdown = HtmlUtilities.Sanitize(model.Markdown); data.Html = HtmlUtilities.Safe(HtmlUtilities.RawToCooked(data.Markdown)); // Submit try { db.Messages.InsertOnSubmit(data); //An exception will be thrown here if there are invalid properties db.SubmitChanges(); } catch (Exception ex) { // Yikes Elmah.ErrorSignal.FromCurrentContext().Raise(ex, Current.Context); return(RedirectToAction("InternalServerError", "Error")); } // Global Post: var gpostmessage = new GlobalPostID(); gpostmessage.UserID = Current.UserID.Value; gpostmessage.PostCategory = MagicCategoryStrings.Message; gpostmessage.SubmissionDate = currentTime; gpostmessage.SpecificPostID = data.MessageID; db.GlobalPostIDs.InsertOnSubmit(gpostmessage); db.SubmitChanges(); data.GlobalPostID = gpostmessage.GlobalPostID1; db.SubmitChanges(); // Notification: new NotificationsController().CreateNotification(data.ReceipientID, gpostmessage, currentTime); return(RedirectToAction("Message", new { id = data.MessageID })); }