public void Can_save_and_load_privatemessage() { var store = GetTestStore(); var storeFromDb = SaveAndLoadEntity(store); storeFromDb.ShouldNotBeNull(); var customer1 = GetTestCustomer(); var customer1FromDb = SaveAndLoadEntity(customer1); customer1FromDb.ShouldNotBeNull(); var customer2 = GetTestCustomer(); var customer2FromDb = SaveAndLoadEntity(customer2); customer2FromDb.ShouldNotBeNull(); var privateMessage = new PrivateMessage { Subject = "Private Message 1 Subject", Text = "Private Message 1 Text", IsDeletedByAuthor = false, IsDeletedByRecipient = false, IsRead = false, CreatedOnUtc = DateTime.UtcNow, FromCustomerId = customer1FromDb.Id, ToCustomerId = customer2FromDb.Id, StoreId = store.Id, }; var fromDb = SaveAndLoadEntity(privateMessage); fromDb.ShouldNotBeNull(); fromDb.Subject.ShouldEqual("Private Message 1 Subject"); fromDb.Text.ShouldEqual("Private Message 1 Text"); fromDb.IsDeletedByAuthor.ShouldBeFalse(); fromDb.IsDeletedByRecipient.ShouldBeFalse(); fromDb.IsRead.ShouldBeFalse(); }
/// <summary> /// Sends a private message notification /// </summary> /// <param name="privateMessage">Private message</param> /// <param name="languageId">Message language identifier</param> /// <returns>Queued email identifier</returns> public int SendPrivateMessageNotification(PrivateMessage privateMessage, int languageId) { if (privateMessage == null) { throw new ArgumentNullException("privateMessage"); } var store = _storeService.GetStoreById(privateMessage.StoreId) ?? _storeContext.CurrentStore; var messageTemplate = GetLocalizedActiveMessageTemplate("Customer.NewPM", languageId, store.Id); if (messageTemplate == null ) { return 0; } //tokens var tokens = new List<Token>(); _messageTokenProvider.AddStoreTokens(tokens, store); _messageTokenProvider.AddPrivateMessageTokens(tokens, privateMessage); //event notification _eventPublisher.MessageTokensAdded(messageTemplate, tokens); var emailAccount = GetEmailAccountOfMessageTemplate(messageTemplate, languageId); var toEmail = privateMessage.ToCustomer.Email; var toName = privateMessage.ToCustomer.GetFullName(); return SendNotification(messageTemplate, emailAccount, languageId, tokens, toEmail, toName); }
public ActionResult SendPm(CustomerModel model) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageCustomers)) return AccessDeniedView(); var customer = _customerService.GetCustomerById(model.Id); if (customer == null) //No customer found with the specified id return RedirectToAction("List"); try { if (!_forumSettings.AllowPrivateMessages) throw new NasException("Private messages are disabled"); if (customer.IsGuest()) throw new NasException("Customer should be registered"); if (String.IsNullOrWhiteSpace(model.SendPm.Subject)) throw new NasException("PM subject is empty"); if (String.IsNullOrWhiteSpace(model.SendPm.Message)) throw new NasException("PM message is empty"); var privateMessage = new PrivateMessage { StoreId = _storeContext.CurrentStore.Id, ToCustomerId = customer.Id, FromCustomerId = _workContext.CurrentCustomer.Id, Subject = model.SendPm.Subject, Text = model.SendPm.Message, IsDeletedByAuthor = false, IsDeletedByRecipient = false, IsRead = false, CreatedOnUtc = DateTime.UtcNow }; _forumService.InsertPrivateMessage(privateMessage); SuccessNotification(_localizationService.GetResource("Admin.Customers.Customers.SendPM.Sent")); } catch (Exception exc) { ErrorNotification(exc.Message); } return RedirectToAction("Edit", new { id = customer.Id }); }
public virtual void AddPrivateMessageTokens(IList<Token> tokens, PrivateMessage privateMessage) { tokens.Add(new Token("PrivateMessage.Subject", privateMessage.Subject)); tokens.Add(new Token("PrivateMessage.Text", privateMessage.FormatPrivateMessageText(), true)); //event notification _eventPublisher.EntityTokensAdded(privateMessage, tokens); }
public ActionResult SendPM(SendPrivateMessageModel model) { if (!_forumSettings.AllowPrivateMessages) { return RedirectToRoute("HomePage"); } 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); }
/// <summary> /// Inserts a private message /// </summary> /// <param name="privateMessage">Private message</param> public virtual void InsertPrivateMessage(PrivateMessage privateMessage) { if (privateMessage == null) { throw new ArgumentNullException("privateMessage"); } _forumPrivateMessageRepository.Insert(privateMessage); //event notification _eventPublisher.EntityInserted(privateMessage); var customerTo = _customerService.GetCustomerById(privateMessage.ToCustomerId); if (customerTo == null) { throw new NasException("Recipient could not be loaded"); } //UI notification _genericAttributeService.SaveAttribute(customerTo, SystemCustomerAttributeNames.NotifiedAboutNewPrivateMessages, false, privateMessage.StoreId); //Email notification if (_forumSettings.NotifyAboutPrivateMessages) { _workflowMessageService.SendPrivateMessageNotification(privateMessage, _workContext.WorkingLanguage.Id); } }
/// <summary> /// Deletes a private message /// </summary> /// <param name="privateMessage">Private message</param> public virtual void DeletePrivateMessage(PrivateMessage privateMessage) { if (privateMessage == null) { throw new ArgumentNullException("privateMessage"); } _forumPrivateMessageRepository.Delete(privateMessage); //event notification _eventPublisher.EntityDeleted(privateMessage); }
/// <summary> /// Updates the private message /// </summary> /// <param name="privateMessage">Private message</param> public virtual void UpdatePrivateMessage(PrivateMessage privateMessage) { if (privateMessage == null) throw new ArgumentNullException("privateMessage"); if (privateMessage.IsDeletedByAuthor && privateMessage.IsDeletedByRecipient) { _forumPrivateMessageRepository.Delete(privateMessage); //event notification _eventPublisher.EntityDeleted(privateMessage); } else { _forumPrivateMessageRepository.Update(privateMessage); //event notification _eventPublisher.EntityUpdated(privateMessage); } }