コード例 #1
0
        /// <summary>
        /// Add a private message
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public PrivateMessage Add(PrivateMessage message)
        {
            // This is the message that the other user sees
            message = SanitizeMessage(message);
            message.DateSent = DateTime.UtcNow;
            message.IsSentMessage = false;
            var origMessage = ContextPerRequest.Db.PrivateMessage.Add(message);

            // We create a sent message that sits in the users sent folder, this is 
            // so that if the receiver deletes the message - The sender still has a record of it.
            var sentMessage = new PrivateMessage
            {
                IsSentMessage = true,
                DateSent = message.DateSent,
                Message = message.Message,
                Subject = message.Subject,
                MemberFromId = message.MemberFromId,
                MemberToId = message.MemberToId
            };
            ContextPerRequest.Db.PrivateMessage.Add(sentMessage);

            // Return the main message
            return origMessage;
        }
コード例 #2
0
 /// <summary>
 /// Delete a private message
 /// </summary>
 /// <param name="message"></param>
 public void DeleteMessage(PrivateMessage message)
 {
     ContextPerRequest.Db.PrivateMessage.Remove(message);
 }
コード例 #3
0
 public PrivateMessage SanitizeMessage(PrivateMessage privateMessage)
 {
     privateMessage.Message = AppHelpers.GetSafeHtml(privateMessage.Message);
     privateMessage.Subject = AppHelpers.SafePlainText(privateMessage.Subject);
     return privateMessage;
 }
コード例 #4
0
        public ActionResult Create(CreatePrivateMessageViewModel createPrivateMessageViewModel)
        {
            if (!Settings.AllowPrivateMessages || CurrentMember.DisablePrivateMessages)
            {
                return ErrorToHomePage(Lang("Errors.GenericMessage"));
            }
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                if (ModelState.IsValid)
                {
                    var userTo = createPrivateMessageViewModel.UserToUsername;

                    // first check they are not trying to message themself!
                    if (!string.Equals(userTo, CurrentMember.UserName, StringComparison.CurrentCultureIgnoreCase))
                    {
                        // Map the view model to message
                        var privateMessage = new PrivateMessage
                        {
                            MemberFrom = CurrentMember,
                            MemberFromId = CurrentMember.Id,
                            Subject = createPrivateMessageViewModel.Subject,
                            Message = createPrivateMessageViewModel.Message,
                        };
                        // now get the user its being sent to
                        var memberTo = ServiceFactory.MemberService.Get(userTo);

                        // check the member
                        if (memberTo != null)
                        {

                            // Check in box size
                            // First check sender
                            var receiverCount = ServiceFactory.PrivateMessageService.GetAllReceivedByUser(memberTo.Id).Count;
                            if (receiverCount > Settings.PrivateMessageInboxSize)
                            {
                                ModelState.AddModelError(string.Empty, string.Format(Lang("PM.ReceivedItemsOverCapcity"), memberTo.UserName));
                            }
                            else
                            {
                                // Good to go send the message!
                                privateMessage.MemberTo = memberTo;
                                privateMessage.MemberToId = memberTo.Id;
                                ServiceFactory.PrivateMessageService.Add(privateMessage);

                                try
                                {
                                    ShowMessage(new GenericMessageViewModel
                                    {
                                        Message = Lang("PM.MessageSent"),
                                        MessageType = GenericMessages.Success
                                    });

                                    unitOfWork.Commit();

                                    // Finally send an email to the user so they know they have a new private message
                                    // As long as they have not had notifications disabled
                                    if (memberTo.DisableEmailNotifications != true)
                                    {
                                        var email = new Email
                                        {
                                            EmailFrom = Settings.NotificationReplyEmailAddress,
                                            EmailTo = memberTo.Email,
                                            Subject = Lang("PM.NewPrivateMessageSubject"),
                                            NameTo = memberTo.UserName
                                        };

                                        var sb = new StringBuilder();
                                        sb.AppendFormat("<p>{0}</p>", string.Format(Lang("PM.NewPrivateMessageBody"), CurrentMember.UserName));
                                        email.Body = ServiceFactory.EmailService.EmailTemplate(email.NameTo, sb.ToString());
                                        ServiceFactory.EmailService.SendMail(email);
                                    }

                                    return Redirect(Urls.GenerateUrl(Urls.UrlType.MessageInbox));
                                }
                                catch (Exception ex)
                                {
                                    unitOfWork.Rollback();
                                    LogError(ex);
                                    ModelState.AddModelError(string.Empty, Lang("Errors.GenericMessage"));
                                }
                            }
                        }
                        else
                        {
                            // Error send back to user
                            ModelState.AddModelError(string.Empty, Lang("PM.UnableFindMember"));
                        }
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, Lang("PM.TalkToSelf"));
                    }
                }
                ShowModelErrors();
                return Redirect(Urls.GenerateUrl(Urls.UrlType.MessageCreate));
            }
        }