Пример #1
0
        public void Should_not_have_error_when_message_is_specified()
        {
            var model = new SendPrivateMessageModel();

            model.Message = "some comment";
            _validator.ShouldNotHaveValidationErrorFor(x => x.Message, model);
        }
Пример #2
0
        /// <summary>
        /// Prepare the send private message model
        /// </summary>
        /// <param name="customerTo">Customer, recipient of the message</param>
        /// <param name="replyToPM">Private message, pass if reply to a previous message is need</param>
        /// <returns>Send private message model</returns>
        public virtual SendPrivateMessageModel PrepareSendPrivateMessageModel(Customer customerTo, PrivateMessage replyToPM)
        {
            if (customerTo == null)
            {
                throw new ArgumentNullException(nameof(customerTo));
            }

            var model = new SendPrivateMessageModel
            {
                ToCustomerId          = customerTo.Id,
                CustomerToName        = _customerService.FormatUserName(customerTo),
                AllowViewingToProfile = _customerSettings.AllowViewingProfiles && !customerTo.IsGuest()
            };

            if (replyToPM == null)
            {
                return(model);
            }

            if (replyToPM.ToCustomerId == _workContext.CurrentCustomer.Id ||
                replyToPM.FromCustomerId == _workContext.CurrentCustomer.Id)
            {
                model.ReplyToMessageId = replyToPM.Id;
                model.Subject          = $"Re: {replyToPM.Subject}";
            }

            return(model);
        }
        public void ShouldNotHaveErrorWhenMessageIsSpecified()
        {
            var model = new SendPrivateMessageModel
            {
                Message = "some comment"
            };

            _validator.ShouldNotHaveValidationErrorFor(x => x.Message, model);
        }
Пример #4
0
        public void Should_not_have_error_when_subject_is_specified()
        {
            var model = new SendPrivateMessageModel
            {
                Subject = "some comment"
            };

            _validator.ShouldNotHaveValidationErrorFor(x => x.Subject, model);
        }
Пример #5
0
        public void Should_have_error_when_message_is_null_or_empty()
        {
            var model = new SendPrivateMessageModel();

            model.Message = null;
            _validator.ShouldHaveValidationErrorFor(x => x.Message, model);
            model.Message = "";
            _validator.ShouldHaveValidationErrorFor(x => x.Message, model);
        }
Пример #6
0
        public void ShouldNotHaveErrorWhenSubjectIsSpecified()
        {
            var model = new SendPrivateMessageModel
            {
                Subject = "some comment"
            };

            _validator.TestValidate(model).ShouldNotHaveValidationErrorFor(x => x.Subject);
        }
        public void ShouldHaveErrorWhenMessageIsNullOrEmpty()
        {
            var model = new SendPrivateMessageModel
            {
                Message = null
            };

            _validator.ShouldHaveValidationErrorFor(x => x.Message, model);
            model.Message = string.Empty;
            _validator.ShouldHaveValidationErrorFor(x => x.Message, model);
        }
        public void ShouldHaveErrorWhenSubjectIsNullOrEmpty()
        {
            var model = new SendPrivateMessageModel
            {
                Subject = null
            };

            _validator.ShouldHaveValidationErrorFor(x => x.Subject, model);
            model.Subject = string.Empty;
            _validator.ShouldHaveValidationErrorFor(x => x.Subject, model);
        }
Пример #9
0
        public void Should_have_error_when_subject_is_null_or_empty()
        {
            var model = new SendPrivateMessageModel
            {
                Subject = null
            };

            _validator.ShouldHaveValidationErrorFor(x => x.Subject, model);
            model.Subject = "";
            _validator.ShouldHaveValidationErrorFor(x => x.Subject, model);
        }
        public virtual async Task <IActionResult> SendPM(SendPrivateMessageModel model)
        {
            if (!_forumSettings.AllowPrivateMessages)
            {
                return(RedirectToRoute("Homepage"));
            }

            var customer = await _workContext.GetCurrentCustomerAsync();

            if (await _customerService.IsGuestAsync(customer))
            {
                return(Challenge());
            }

            Customer toCustomer;
            var      replyToPM = await _forumService.GetPrivateMessageByIdAsync(model.ReplyToMessageId);

            if (replyToPM != null)
            {
                //reply to a previous PM
                if (replyToPM.ToCustomerId == customer.Id || replyToPM.FromCustomerId == customer.Id)
                {
                    //Reply to already sent PM (by current customer) should not be sent to yourself
                    toCustomer = await _customerService.GetCustomerByIdAsync(replyToPM.FromCustomerId == customer.Id
                                                                             ?replyToPM.ToCustomerId
                                                                             : replyToPM.FromCustomerId);
                }
                else
                {
                    return(RedirectToRoute("PrivateMessages"));
                }
            }
            else
            {
                //first PM
                toCustomer = await _customerService.GetCustomerByIdAsync(model.ToCustomerId);
            }

            if (toCustomer == null || await _customerService.IsGuestAsync(toCustomer))
            {
                return(RedirectToRoute("PrivateMessages"));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var subject = model.Subject;
                    if (_forumSettings.PMSubjectMaxLength > 0 && subject.Length > _forumSettings.PMSubjectMaxLength)
                    {
                        subject = subject[0.._forumSettings.PMSubjectMaxLength];
        public ActionResult SendPM(int toCustomerId, int?replyToMessageId)
        {
            if (!_forumSettings.AllowPrivateMessages)
            {
                return(RedirectToRoute("HomePage"));
            }

            if (_workContext.CurrentCustomer.IsGuest())
            {
                return(new HttpUnauthorizedResult());
            }

            var customerTo = _customerService.GetCustomerById(toCustomerId);

            if (customerTo == null || customerTo.IsGuest())
            {
                return(RedirectToRoute("PrivateMessages"));
            }

            var model = new SendPrivateMessageModel();

            model.ToCustomerId          = customerTo.Id;
            model.CustomerToName        = customerTo.FormatUserName();
            model.AllowViewingToProfile = _customerSettings.AllowViewingProfiles && !customerTo.IsGuest();

            if (replyToMessageId.HasValue)
            {
                //reply to a previous PM
                var replyToPM = _forumService.GetPrivateMessageById(replyToMessageId.Value);
                if (replyToPM == null)
                {
                    return(RedirectToRoute("PrivateMessages"));
                }

                if (replyToPM.ToCustomerId == _workContext.CurrentCustomer.Id || replyToPM.FromCustomerId == _workContext.CurrentCustomer.Id)
                {
                    model.ReplyToMessageId = replyToPM.Id;
                    model.Subject          = string.Format("Re: {0}", replyToPM.Subject);
                }
                else
                {
                    return(RedirectToRoute("PrivateMessages"));
                }
            }
            return(View(model));
        }
Пример #12
0
        public virtual async Task <IActionResult> SendPM(string toCustomerId, string replyToMessageId)
        {
            if (!_forumSettings.AllowPrivateMessages)
            {
                return(RedirectToRoute("HomePage"));
            }

            if (_workContext.CurrentCustomer.IsGuest())
            {
                return(Challenge());
            }

            var customerTo = await _customerService.GetCustomerById(toCustomerId);

            if (customerTo == null || customerTo.IsGuest())
            {
                return(RedirectToRoute("PrivateMessages"));
            }

            var model = new SendPrivateMessageModel();

            model.ToCustomerId          = customerTo.Id;
            model.CustomerToName        = customerTo.FormatUserName(_customerSettings.CustomerNameFormat);
            model.AllowViewingToProfile = _customerSettings.AllowViewingProfiles && !customerTo.IsGuest();

            if (!String.IsNullOrEmpty(replyToMessageId))
            {
                var replyToPM = await _forumService.GetPrivateMessageById(replyToMessageId);

                if (replyToPM == null)
                {
                    return(RedirectToRoute("PrivateMessages"));
                }

                if (replyToPM.ToCustomerId == _workContext.CurrentCustomer.Id || replyToPM.FromCustomerId == _workContext.CurrentCustomer.Id)
                {
                    model.ReplyToMessageId = replyToPM.Id;
                    model.Subject          = string.Format("Re: {0}", replyToPM.Subject);
                }
                else
                {
                    return(RedirectToRoute("PrivateMessages"));
                }
            }
            return(View(model));
        }
Пример #13
0
        public async Task <IActionResult> SendPrivateMessage([FromBody] SendPrivateMessageModel sendPrivateMessageModel)
        {
            var currentUserId = GetUserId();

            var privateMessage = await _messageService.AddPrivateMessage(
                currentUserId,
                sendPrivateMessageModel.ReceiverId,
                sendPrivateMessageModel.Sended,
                sendPrivateMessageModel.Content);

            var todoSender = CreateMessageSendingTasks(
                _onlineUsers.GetChatHubConnections(currentUserId),
                ChatMessageViewModel.ForMessageSender(privateMessage),
                _hubContext);

            var todoReceiver = CreateMessageSendingTasks(
                _onlineUsers.GetChatHubConnections(privateMessage.ReceiverId),
                ChatMessageViewModel.ForMessageReceiver(privateMessage),
                _hubContext);

            await Task.WhenAll(todoSender.Concat(todoReceiver));

            return(Ok());
        }
        public ActionResult Send(SendPrivateMessageModel model)
        {
            if (!AllowPrivateMessages())
            {
                return(HttpNotFound());
            }

            if (_workContext.CurrentCustomer.IsGuest())
            {
                return(new HttpUnauthorizedResult());
            }

            Customer toCustomer = null;
            var      replyToPM  = _forumService.GetPrivateMessageById(model.ReplyToMessageId);

            if (replyToPM != null)
            {
                if (replyToPM.ToCustomerId == _workContext.CurrentCustomer.Id || replyToPM.FromCustomerId == _workContext.CurrentCustomer.Id)
                {
                    toCustomer = replyToPM.FromCustomer;
                }
                else
                {
                    return(RedirectToRoute("PrivateMessages"));
                }
            }
            else
            {
                toCustomer = _customerService.GetCustomerById(model.ToCustomerId);
            }

            if (toCustomer == null || toCustomer.IsGuest())
            {
                return(RedirectToRoute("PrivateMessages"));
            }
            model.ToCustomerId          = toCustomer.Id;
            model.CustomerToName        = toCustomer.FormatUserName();
            model.AllowViewingToProfile = _customerSettings.AllowViewingProfiles && !toCustomer.IsGuest();

            if (ModelState.IsValid)
            {
                try
                {
                    string subject = model.Subject;
                    if (_forumSettings.PMSubjectMaxLength > 0 && subject.Length > _forumSettings.PMSubjectMaxLength)
                    {
                        subject = subject.Substring(0, _forumSettings.PMSubjectMaxLength);
                    }

                    var text = model.Message;
                    if (_forumSettings.PMTextMaxLength > 0 && text.Length > _forumSettings.PMTextMaxLength)
                    {
                        text = text.Substring(0, _forumSettings.PMTextMaxLength);
                    }

                    var nowUtc = DateTime.UtcNow;

                    var privateMessage = new PrivateMessage
                    {
                        StoreId              = _storeContext.CurrentStore.Id,
                        ToCustomerId         = toCustomer.Id,
                        FromCustomerId       = _workContext.CurrentCustomer.Id,
                        Subject              = subject,
                        Text                 = text,
                        IsDeletedByAuthor    = false,
                        IsDeletedByRecipient = false,
                        IsRead               = false,
                        CreatedOnUtc         = nowUtc
                    };

                    _forumService.InsertPrivateMessage(privateMessage);

                    //activity log
                    _customerActivityService.InsertActivity("PublicStore.SendPM", _localizationService.GetResource("ActivityLog.PublicStore.SendPM"), toCustomer.Email);

                    return(RedirectToRoute("PrivateMessages", new { tab = "sent" }));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            return(View(model));
        }
        public virtual IActionResult SendPM(SendPrivateMessageModel model)
        {
            if (!_forumSettings.AllowPrivateMessages)
            {
                return(RedirectToRoute("Homepage"));
            }

            if (_workContext.CurrentCustomer.IsGuest())
            {
                return(Challenge());
            }

            Customer toCustomer;
            var      replyToPM = _forumService.GetPrivateMessageById(model.ReplyToMessageId);

            if (replyToPM != null)
            {
                //reply to a previous PM
                if (replyToPM.ToCustomerId == _workContext.CurrentCustomer.Id || replyToPM.FromCustomerId == _workContext.CurrentCustomer.Id)
                {
                    //Reply to already sent PM (by current customer) should not be sent to yourself
                    toCustomer = replyToPM.FromCustomerId == _workContext.CurrentCustomer.Id
                        ? replyToPM.ToCustomer
                        : replyToPM.FromCustomer;
                }
                else
                {
                    return(RedirectToRoute("PrivateMessages"));
                }
            }
            else
            {
                //first PM
                toCustomer = _customerService.GetCustomerById(model.ToCustomerId);
            }

            if (toCustomer == null || toCustomer.IsGuest())
            {
                return(RedirectToRoute("PrivateMessages"));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var subject = model.Subject;
                    if (_forumSettings.PMSubjectMaxLength > 0 && subject.Length > _forumSettings.PMSubjectMaxLength)
                    {
                        subject = subject.Substring(0, _forumSettings.PMSubjectMaxLength);
                    }

                    var text = model.Message;
                    if (_forumSettings.PMTextMaxLength > 0 && text.Length > _forumSettings.PMTextMaxLength)
                    {
                        text = text.Substring(0, _forumSettings.PMTextMaxLength);
                    }

                    var nowUtc = DateTime.UtcNow;

                    var privateMessage = new PrivateMessage
                    {
                        StoreId              = _storeContext.CurrentStore.Id,
                        ToCustomerId         = toCustomer.Id,
                        FromCustomerId       = _workContext.CurrentCustomer.Id,
                        Subject              = subject,
                        Text                 = text,
                        IsDeletedByAuthor    = false,
                        IsDeletedByRecipient = false,
                        IsRead               = false,
                        CreatedOnUtc         = nowUtc
                    };

                    _forumService.InsertPrivateMessage(privateMessage);

                    //activity log
                    _customerActivityService.InsertActivity("PublicStore.SendPM",
                                                            string.Format(_localizationService.GetResource("ActivityLog.PublicStore.SendPM"), toCustomer.Email), toCustomer);

                    return(RedirectToRoute("PrivateMessages", new { tab = "sent" }));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            model = _privateMessagesModelFactory.PrepareSendPrivateMessageModel(toCustomer, replyToPM);
            return(View(model));
        }