public ActionResult Create(Guid?id, Guid?to)
        {
            // Check if private messages are enabled
            if (!SettingsService.GetSettings().EnablePrivateMessages || LoggedOnUser.DisablePrivateMessages == true)
            {
                return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage")));
            }

            // Check flood control
            var lastMessage = _privateMessageService.GetLastSentPrivateMessage(LoggedOnUser.Id);

            if (lastMessage != null && DateUtils.TimeDifferenceInMinutes(DateTime.UtcNow, lastMessage.DateSent) < SettingsService.GetSettings().PrivateMessageFloodControl)
            {
                return(ErrorToInbox(LocalizationService.GetResourceString("PM.SendingToQuickly")));
            }

            // Check outbox size of logged in user
            var senderCount = _privateMessageService.GetAllSentByUser(LoggedOnUser.Id).Count;

            if (senderCount > SettingsService.GetSettings().MaxPrivateMessagesPerMember)
            {
                return(ErrorToInbox(LocalizationService.GetResourceString("PM.SentItemsOverCapcity")));
            }
            if (senderCount > (SettingsService.GetSettings().MaxPrivateMessagesPerMember - SiteConstants.PrivateMessageWarningAmountLessThanAllowedSize))
            {
                // Send user a warning they are about to exceed
                var sb = new StringBuilder();
                sb.AppendFormat("<p>{0}</p>", LocalizationService.GetResourceString("PM.AboutToExceedInboxSizeBody"));
                var email = new Email
                {
                    EmailFrom = SettingsService.GetSettings().AdminEmailAddress,
                    EmailTo   = LoggedOnUser.Email,
                    NameTo    = LoggedOnUser.UserName,
                    Subject   = LocalizationService.GetResourceString("PM.AboutToExceedInboxSizeSubject")
                };
                email.Body = _emailService.EmailTemplate(email.NameTo, sb.ToString());
                _emailService.SendMail(email);
            }

            var viewModel = new CreatePrivateMessageViewModel();

            // add the username to the to box if available
            if (to != null)
            {
                var userTo = MembershipService.GetUser((Guid)to);
                viewModel.UserToUsername = userTo.UserName;
            }

            // See if this is a reply or not
            if (id != null)
            {
                var previousMessage = _privateMessageService.Get((Guid)id);
                // Its a reply, get the details
                viewModel.UserToUsername  = previousMessage.UserFrom.UserName;
                viewModel.Subject         = previousMessage.Subject;
                viewModel.PreviousMessage = previousMessage.Message;
            }
            return(View(viewModel));
        }
        public ActionResult Create(Guid to)
        {
            var viewModel = new CreatePrivateMessageViewModel
            {
                To = to
            };

            try
            {
                var loggedOnReadOnlyUser = User.GetMembershipUser(MembershipService);
                var loggedOnUsersRole    = loggedOnReadOnlyUser.GetRole(RoleService);

                var permissions = RoleService.GetPermissions(null, loggedOnUsersRole);
                var settings    = SettingsService.GetSettings();
                // Check if private messages are enabled
                if (!settings.EnablePrivateMessages || loggedOnReadOnlyUser.DisablePrivateMessages == true)
                {
                    return(Content(LocalizationService.GetResourceString("Errors.GenericMessage")));
                }

                // Check outbox size of logged in user
                var senderCount = _privateMessageService.GetAllSentByUser(loggedOnReadOnlyUser.Id).Count;
                if (senderCount > settings.MaxPrivateMessagesPerMember)
                {
                    return(Content(LocalizationService.GetResourceString("PM.SentItemsOverCapcity")));
                }
                if (senderCount > settings.MaxPrivateMessagesPerMember -
                    SiteConstants.Instance.PrivateMessageWarningAmountLessThanAllowedSize)
                {
                    // Send user a warning they are about to exceed
                    var sb = new StringBuilder();
                    sb.Append($"<p>{LocalizationService.GetResourceString("PM.AboutToExceedInboxSizeBody")}</p>");
                    var email = new Email
                    {
                        EmailTo = loggedOnReadOnlyUser.Email,
                        NameTo  = loggedOnReadOnlyUser.UserName,
                        Subject = LocalizationService.GetResourceString("PM.AboutToExceedInboxSizeSubject")
                    };
                    email.Body = _emailService.EmailTemplate(email.NameTo, sb.ToString());
                    _emailService.SendMail(email);
                }

                // Set editor permissions
                ViewBag.ImageUploadType = permissions[SiteConstants.Instance.PermissionInsertEditorImages].IsTicked
                    ? "forumimageinsert"
                    : "image";

                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                Context.RollBack();
                LoggingService.Error(ex);
            }

            return(PartialView(viewModel));
        }
        public ActionResult Create(int to)
        {
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                var viewModel = new CreatePrivateMessageViewModel
                {
                    To = to
                };

                try
                {
                    // Check if private messages are enabled
                    if (!SettingsService.GetSettings().EnablePrivateMessages || LoggedOnUser.DisablePrivateMessages == true)
                    {
                        return(Content(LocalizationService.GetResourceString("Errors.GenericMessage")));
                    }

                    // Check outbox size of logged in user
                    var senderCount = _privateMessageService.GetAllSentByUser(LoggedOnUser.Id).Count;
                    if (senderCount > SettingsService.GetSettings().MaxPrivateMessagesPerMember)
                    {
                        return(Content(LocalizationService.GetResourceString("PM.SentItemsOverCapcity")));
                    }
                    if (senderCount > (SettingsService.GetSettings().MaxPrivateMessagesPerMember - SiteConstants.PrivateMessageWarningAmountLessThanAllowedSize))
                    {
                        // Send user a warning they are about to exceed
                        var sb = new StringBuilder();
                        sb.AppendFormat("<p>{0}</p>", LocalizationService.GetResourceString("PM.AboutToExceedInboxSizeBody"));
                        var email = new Email
                        {
                            EmailTo = LoggedOnUser.Email,
                            NameTo  = LoggedOnUser.UserName,
                            Subject = LocalizationService.GetResourceString("PM.AboutToExceedInboxSizeSubject")
                        };
                        email.Body = _emailService.EmailTemplate(email.NameTo, sb.ToString());
                        _emailService.SendMail(email);
                    }

                    unitOfWork.Commit();
                }
                catch (Exception ex)
                {
                    unitOfWork.Rollback();
                    LoggingService.Error(ex);
                }

                return(PartialView(viewModel));
            }
        }
        public ActionResult Create(Guid?id, Guid?to)
        {
            // Check if private messages are enabled
            if (!SettingsService.GetSettings().EnablePrivateMessages || LoggedOnUser.DisablePrivateMessages == true)
            {
                return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage")));
            }

            // Check flood control
            var lastMessage = _privateMessageService.GetLastSentPrivateMessage(LoggedOnUser.Id);

            if (lastMessage != null && DateUtils.TimeDifferenceInMinutes(DateTime.UtcNow, lastMessage.DateSent) < SettingsService.GetSettings().PrivateMessageFloodControl)
            {
                return(ErrorToInbox(LocalizationService.GetResourceString("PM.SendingToQuickly")));
            }

            // Check outbox size
            var senderCount = _privateMessageService.GetAllSentByUser(LoggedOnUser.Id).Count;

            if (senderCount > SettingsService.GetSettings().MaxPrivateMessagesPerMember)
            {
                return(ErrorToInbox(LocalizationService.GetResourceString("PM.SentItemsOverCapcity")));
            }

            var viewModel = new CreatePrivateMessageViewModel();

            // add the username to the to box if available
            if (to != null)
            {
                var userTo = MembershipService.GetUser((Guid)to);
                viewModel.UserToUsername = userTo.UserName;
            }

            // See if this is a reply or not
            if (id != null)
            {
                var previousMessage = _privateMessageService.Get((Guid)id);
                // Its a reply, get the details
                viewModel.UserToUsername  = previousMessage.UserFrom.UserName;
                viewModel.Subject         = previousMessage.Subject;
                viewModel.PreviousMessage = previousMessage.Message;
            }
            return(View(viewModel));
        }