// a method to send a private message to a user, invoked by other methods public static bool SendPrivateMessage(string sender, string recipient, string subject, string body) { using (var db = new whoaverseEntities()) { try { var privateMessage = new Privatemessage { Sender = sender, Recipient = recipient, Timestamp = DateTime.Now, Subject = subject, Body = body, Status = true, Markedasunread = true }; db.Privatemessages.Add(privateMessage); db.SaveChanges(); return(true); } catch (Exception) { return(false); } } }
public async Task <ActionResult> Compose([Bind(Include = "Id,Recipient,Subject,Body")] Privatemessage privateMessage) { if (!ModelState.IsValid) { return(View()); } if (privateMessage.Recipient == null || privateMessage.Subject == null || privateMessage.Body == null) { return(RedirectToAction("Sent", "Messaging")); } if (Karma.CommentKarma(User.Identity.Name) < 100) { bool isCaptchaValid = await ReCaptchaUtility.Validate(Request); if (!isCaptchaValid) { ModelState.AddModelError(string.Empty, "Incorrect recaptcha answer."); return(View()); } } // check if recipient exists if (Voat.Utilities.UserHelper.UserExists(privateMessage.Recipient) && !Voat.Utilities.UserHelper.IsUserGloballyBanned(User.Identity.Name)) { // send the submission privateMessage.Timestamp = DateTime.Now; privateMessage.Sender = User.Identity.Name; privateMessage.Status = true; if (Voat.Utilities.UserHelper.IsUserGloballyBanned(User.Identity.Name)) { return(RedirectToAction("Sent", "Messaging")); } _db.Privatemessages.Add(privateMessage); try { await _db.SaveChangesAsync(); // get count of unread notifications int unreadNotifications = Voat.Utilities.UserHelper.UnreadTotalNotificationsCount(privateMessage.Recipient); // send SignalR realtime notification to recipient var hubContext = GlobalHost.ConnectionManager.GetHubContext <MessagingHub>(); hubContext.Clients.User(privateMessage.Recipient).setNotificationsPending(unreadNotifications); } catch (Exception) { return(View("~/Views/Errors/DbNotResponding.cshtml")); } } else { ModelState.AddModelError(string.Empty, "Sorry, there is no recipient with that username."); return(View()); } return(RedirectToAction("Sent", "Messaging")); }
public async Task <ActionResult> SendPrivateMessage([Bind(Include = "Id,Recipient,Subject,Body")] Privatemessage privateMessage) { if (!ModelState.IsValid) { return(new HttpStatusCodeResult(HttpStatusCode.Forbidden)); } if (privateMessage.Recipient == null || privateMessage.Subject == null || privateMessage.Body == null) { return(new HttpStatusCodeResult(HttpStatusCode.OK)); } // check if recipient exists if (Utils.User.UserExists(privateMessage.Recipient)) { // send the message privateMessage.Timestamp = DateTime.Now; privateMessage.Sender = User.Identity.Name; privateMessage.Status = true; if (Utils.User.IsUserBanned(User.Identity.Name)) { return(new HttpStatusCodeResult(HttpStatusCode.OK)); } _db.Privatemessages.Add(privateMessage); try { await _db.SaveChangesAsync(); // get count of unread notifications int unreadNotifications = Utils.User.UnreadTotalNotificationsCount(privateMessage.Recipient); // send SignalR realtime notification to recipient var hubContext = GlobalHost.ConnectionManager.GetHubContext <MessagingHub>(); hubContext.Clients.User(privateMessage.Recipient).setNotificationsPending(unreadNotifications); } catch (Exception) { return(RedirectToAction("HeavyLoad", "Error")); } } else { return(new HttpStatusCodeResult(HttpStatusCode.Forbidden)); } return(new HttpStatusCodeResult(HttpStatusCode.OK)); }
public async Task <ActionResult> Compose([Bind(Include = "Id,Recipient,Subject,Body")] Privatemessage privateMessage) { if (!ModelState.IsValid) { return(View()); } if (privateMessage.Recipient == null || privateMessage.Subject == null || privateMessage.Body == null) { return(RedirectToAction("Sent", "Messaging")); } // check if recipient exists if (Utils.User.UserExists(privateMessage.Recipient) && !Utils.User.IsUserGloballyBanned(User.Identity.Name)) { // send the message privateMessage.Timestamp = DateTime.Now; privateMessage.Sender = User.Identity.Name; privateMessage.Status = true; if (Utils.User.IsUserGloballyBanned(User.Identity.Name)) { return(RedirectToAction("Sent", "Messaging")); } _db.Privatemessages.Add(privateMessage); try { await _db.SaveChangesAsync(); // get count of unread notifications int unreadNotifications = Utils.User.UnreadTotalNotificationsCount(privateMessage.Recipient); // send SignalR realtime notification to recipient var hubContext = GlobalHost.ConnectionManager.GetHubContext <MessagingHub>(); hubContext.Clients.User(privateMessage.Recipient).setNotificationsPending(unreadNotifications); } catch (Exception) { return(RedirectToAction("HeavyLoad", "Error")); } } else { ModelState.AddModelError(string.Empty, "Sorry, there is no recipient with that username."); return(View()); } return(RedirectToAction("Sent", "Messaging")); }