コード例 #1
0
        /// <summary>
        /// Prepare the private message model
        /// </summary>
        /// <param name="pm">Private message</param>
        /// <returns>Private message model</returns>
        public virtual PrivateMessageModel PreparePrivateMessageModel(PrivateMessage pm)
        {
            if (pm == null)
            {
                throw new ArgumentNullException(nameof(pm));
            }

            var fromCustomer = _customerService.GetCustomerById(pm.FromCustomerId);
            var toCustomer   = _customerService.GetCustomerById(pm.ToCustomerId);

            var model = new PrivateMessageModel
            {
                Id                      = pm.Id,
                FromCustomerId          = pm.FromCustomerId,
                CustomerFromName        = _customerService.FormatUsername(fromCustomer),
                AllowViewingFromProfile = _customerSettings.AllowViewingProfiles && !_customerService.IsGuest(fromCustomer),
                ToCustomerId            = pm.ToCustomerId,
                CustomerToName          = _customerService.FormatUsername(toCustomer),
                AllowViewingToProfile   = _customerSettings.AllowViewingProfiles && !_customerService.IsGuest(toCustomer),
                Subject                 = pm.Subject,
                Message                 = _forumService.FormatPrivateMessageText(pm),
                CreatedOn               = _dateTimeHelper.ConvertToUserTime(pm.CreatedOnUtc, DateTimeKind.Utc),
                IsRead                  = pm.IsRead,
            };

            return(model);
        }
コード例 #2
0
        public virtual async Task <IActionResult> ViewPM(string privateMessageId)
        {
            if (!_forumSettings.AllowPrivateMessages)
            {
                return(RedirectToRoute("HomePage"));
            }

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

            var pm = await _forumService.GetPrivateMessageById(privateMessageId);

            if (pm != null)
            {
                if (pm.ToCustomerId != _workContext.CurrentCustomer.Id && pm.FromCustomerId != _workContext.CurrentCustomer.Id)
                {
                    return(RedirectToRoute("PrivateMessages"));
                }

                if (!pm.IsRead && pm.ToCustomerId == _workContext.CurrentCustomer.Id)
                {
                    pm.IsRead = true;
                    await _forumService.UpdatePrivateMessage(pm);
                }
            }
            else
            {
                return(RedirectToRoute("PrivateMessages"));
            }

            var fromCustomer = await _customerService.GetCustomerById(pm.FromCustomerId);

            var toCustomer = await _customerService.GetCustomerById(pm.ToCustomerId);


            var model = new PrivateMessageModel
            {
                Id                      = pm.Id,
                FromCustomerId          = fromCustomer.Id,
                CustomerFromName        = fromCustomer.FormatUserName(_customerSettings.CustomerNameFormat),
                AllowViewingFromProfile = _customerSettings.AllowViewingProfiles && fromCustomer != null && !fromCustomer.IsGuest(),
                ToCustomerId            = toCustomer.Id,
                CustomerToName          = toCustomer.FormatUserName(_customerSettings.CustomerNameFormat),
                AllowViewingToProfile   = _customerSettings.AllowViewingProfiles && toCustomer != null && !toCustomer.IsGuest(),
                Subject                 = pm.Subject,
                Message                 = pm.FormatPrivateMessageText(),
                CreatedOn               = _dateTimeHelper.ConvertToUserTime(pm.CreatedOnUtc, DateTimeKind.Utc),
                IsRead                  = pm.IsRead,
            };

            return(View(model));
        }
コード例 #3
0
        public ActionResult View(int id /* privateMessageId */)
        {
            if (!AllowPrivateMessages())
            {
                return(HttpNotFound());
            }

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

            var pm = _forumService.GetPrivateMessageById(id);

            if (pm != null)
            {
                if (pm.ToCustomerId != _workContext.CurrentCustomer.Id && pm.FromCustomerId != _workContext.CurrentCustomer.Id)
                {
                    return(RedirectToRoute("PrivateMessages"));
                }

                if (!pm.IsRead && pm.ToCustomerId == _workContext.CurrentCustomer.Id)
                {
                    pm.IsRead = true;
                    _forumService.UpdatePrivateMessage(pm);
                }
            }
            else
            {
                return(RedirectToRoute("PrivateMessages"));
            }

            var model = new PrivateMessageModel()
            {
                Id                      = pm.Id,
                FromCustomerId          = pm.FromCustomer.Id,
                CustomerFromName        = pm.FromCustomer.FormatUserName(),
                AllowViewingFromProfile = _customerSettings.AllowViewingProfiles && pm.FromCustomer != null && !pm.FromCustomer.IsGuest(),
                ToCustomerId            = pm.ToCustomer.Id,
                CustomerToName          = pm.ToCustomer.FormatUserName(),
                AllowViewingToProfile   = _customerSettings.AllowViewingProfiles && pm.ToCustomer != null && !pm.ToCustomer.IsGuest(),
                Subject                 = pm.Subject,
                Message                 = pm.FormatPrivateMessageText(),
                CreatedOn               = _dateTimeHelper.ConvertToUserTime(pm.CreatedOnUtc, DateTimeKind.Utc),
                IsRead                  = pm.IsRead,
            };

            return(View(model));
        }
コード例 #4
0
        public ActionResult ViewPM(int privateMessageId)
        {
            if (!AllowPrivateMessages())
            {
                return(RedirectToAction("index", "home"));
            }

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

            var pm = _forumService.GetPrivateMessageById(privateMessageId);

            if (pm != null)
            {
                if (pm.ToCustomerId != _workContext.CurrentCustomer.Id && pm.FromCustomerId != _workContext.CurrentCustomer.Id)
                {
                    return(RedirectToAction("Index"));
                }

                if (!pm.IsRead && pm.ToCustomerId == _workContext.CurrentCustomer.Id)
                {
                    pm.IsRead = true;
                    _forumService.UpdatePrivateMessage(pm);
                }
            }
            else
            {
                return(RedirectToAction("Index"));
            }

            var model = new PrivateMessageModel()
            {
                Id                      = pm.Id,
                FromCustomerId          = pm.FromCustomer.Id,
                CustomerFromName        = pm.FromCustomer.FormatUserName(),
                AllowViewingFromProfile = _customerSettings.AllowViewingProfiles && pm.FromCustomer != null && !pm.FromCustomer.IsGuest(),
                ToCustomerId            = pm.ToCustomer.Id,
                CustomerToName          = pm.ToCustomer.FormatUserName(),
                AllowViewingToProfile   = _customerSettings.AllowViewingProfiles && pm.ToCustomer != null && !pm.ToCustomer.IsGuest(),
                Subject                 = pm.Subject,
                Message                 = pm.FormatPrivateMessageText(),
                CreatedOnUtc            = pm.CreatedOnUtc,
                IsRead                  = pm.IsRead,
            };

            return(View(model));
        }
コード例 #5
0
        public ActionResult SendPM(int toCustomerId, int?replyToMessageId)
        {
            if (!AllowPrivateMessages())
            {
                return(RedirectToAction("index", "home"));
            }

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

            var customerTo = _customerService.GetCustomerById(toCustomerId);

            if (customerTo == null)
            {
                return(RedirectToAction("Index"));
            }

            var model = new PrivateMessageModel();

            model.ToCustomerId   = toCustomerId;
            model.customerToName = customerTo.FormatUserName();

            if (replyToMessageId.HasValue)
            {
                var replyToPM = _forumService.GetPrivateMessageById(replyToMessageId.Value);
                if (replyToPM == null)
                {
                    return(RedirectToAction("Index"));
                }

                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(RedirectToAction("Index"));
                }
            }
            return(View(model));
        }
コード例 #6
0
        /// <summary>
        /// Prepare the private message model
        /// </summary>
        /// <param name="pm">Private message</param>
        /// <returns>Private message model</returns>
        public virtual PrivateMessageModel PreparePrivateMessageModel(PrivateMessage pm)
        {
            if (pm == null)
            {
                throw new ArgumentNullException("pm");
            }

            var model = new PrivateMessageModel
            {
                Id                      = pm.Id,
                FromCustomerId          = pm.FromCustomer.Id,
                CustomerFromName        = pm.FromCustomer.FormatUserName(),
                AllowViewingFromProfile = _customerSettings.AllowViewingProfiles && pm.FromCustomer != null && !pm.FromCustomer.IsGuest(),
                ToCustomerId            = pm.ToCustomer.Id,
                CustomerToName          = pm.ToCustomer.FormatUserName(),
                AllowViewingToProfile   = _customerSettings.AllowViewingProfiles && pm.ToCustomer != null && !pm.ToCustomer.IsGuest(),
                Subject                 = pm.Subject,
                Message                 = pm.FormatPrivateMessageText(),
                CreatedOn               = _dateTimeHelper.ConvertToUserTime(pm.CreatedOnUtc, DateTimeKind.Utc),
                IsRead                  = pm.IsRead,
            };

            return(model);
        }
コード例 #7
0
        public ActionResult SendPM(PrivateMessageModel model)
        {
            if (!AllowPrivateMessages())
            {
                return(RedirectToAction("index", "home"));
            }

            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(RedirectToAction("Index"));
                }
            }
            else
            {
                toCustomer = _customerService.GetCustomerById(model.ToCustomerId);
            }

            if (toCustomer == null || toCustomer.IsGuest())
            {
                return(RedirectToAction("Index"));
            }

            try
            {
                string subject = model.Subject;
                if (subject != null)
                {
                    subject = subject.Trim();
                }

                if (String.IsNullOrEmpty(subject))
                {
                    throw new NopException(_localizationService.GetResource("PrivateMessages.SubjectCannotBeEmpty"));
                }

                var maxSubjectLength = _forumSettings.PMSubjectMaxLength;
                if (maxSubjectLength > 0 && subject.Length > maxSubjectLength)
                {
                    subject = subject.Substring(0, maxSubjectLength);
                }

                var text = model.Message;
                if (text != null)
                {
                    text = text.Trim();
                }

                if (String.IsNullOrEmpty(text))
                {
                    throw new NopException(_localizationService.GetResource("PrivateMessages.MessageCannotBeEmpty"));
                }

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

                var nowUtc = DateTime.UtcNow;

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

                _forumService.InsertPrivateMessage(privateMessage);

                return(RedirectToAction("Index", new { tab = "sent" }));
            }
            catch (Exception ex)
            {
                model.PostError = ex.Message;
            }

            return(View(model));
        }
コード例 #8
0
        public PrivateMessageController(SharpWiredModel model) : base(model)
        {
            privateMessageModel = new PrivateMessageModel();

            model.Connected += OnConnected;
        }
コード例 #9
0
 public void AddPrivateMessage(PrivateMessageModel message)
 {
     Clients.Client(GetConnectionId(message.To.Username)).SendAsync("ReceivePrivateMessage", message);
 }