public ActionResult Conversation(ConversationViewModel model) { try { //get conversation data ConversationModel conversation = new ConversationModel(model.ConversationID); if (!ModelState.IsValid) { model.Messages = conversation.Messages; return(View(model)); } //Add message to conversation conversation.AddMessage(model.NewMessageText, model.File); //initiate view model data model = new ConversationViewModel(conversation); return(RedirectToAction("Conversation", "Conversation", model.ConversationID)); } catch (Exception e) { return(RedirectToAction("HandledCodeError", "ErrorHandler", new { exception = e.ToString() })); } }
public void Receiver() { try { string conversationId = ""; string id = ""; bool notifyContactPersons = false; string emailId = ""; string emailAddress = ""; while (_client.Connected) { byte response = _reader.ReadByte(); switch (response) { case MessageDictionary.NewNotification: _writer.Write(MessageDictionary.OK); List <string> referenceFields = new List <string>(); string notificationId = _reader.ReadString(); string notificationText = _reader.ReadString(); DateTime notificationDate = DateTime.ParseExact(_reader.ReadString(), "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture); int referenceFieldCount = _reader.ReadInt32(); for (int i = 0; i < referenceFieldCount; i++) { referenceFields.Add(_reader.ReadString()); } if (_reader.Read() == MessageDictionary.EndOfMessage) { switch (_reader.ReadByte()) { #region NewMessage case MessageDictionary.NewMessage: id = _reader.ReadString(); conversationId = _reader.ReadString(); string authorId = _reader.ReadString(); string authorFrom = _reader.ReadString(); string initialDateString = _reader.ReadString(); byte[] previewData = ReceiveFile(); List <string> attachmentIds = new List <string>(); int attachmentCount = _reader.ReadInt32(); for (int j = 0; j < attachmentCount; j++) { attachmentIds.Add(_reader.ReadString()); } MemoryStream stream = new MemoryStream(previewData); BitmapImage previewImage = new BitmapImage(); previewImage.BeginInit(); previewImage.StreamSource = stream; previewImage.EndInit(); previewImage.Freeze(); byte type = _reader.ReadByte(); DateTime initialDate = DateTime.ParseExact(initialDateString, "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture); ConversationMessageModel message = new ConversationMessageModel(id, conversationId, authorId, authorFrom, initialDate, true, previewImage, false, attachmentIds); switch (type) { case MessageDictionary.MessageTypeEmail: string subject = _reader.ReadString(); string content = _reader.ReadString(); ConversationEmailMessageModel emailMessage = new ConversationEmailMessageModel(message, subject, content); message = emailMessage; break; case MessageDictionary.MessageTypePhoneCall: string recipientPhoneNumber = _reader.ReadString(); string description = _reader.ReadString(); bool answered = _reader.ReadBoolean(); ConversationPhoneMessageModel phoneMessage = new ConversationPhoneMessageModel(message, recipientPhoneNumber, description, answered); message = phoneMessage; break; } NewConversationMessageArrived?.Invoke(this, new NewConversationMessageArrivedEventArgs() { Message = message, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region RenameConversation case MessageDictionary.RenameConversation: conversationId = _reader.ReadString(); string oldName = _reader.ReadString(); string newName = _reader.ReadString(); ConversationRenamed?.Invoke(this, new ConversationRenamedEventArgs() { ConversationId = conversationId, OldName = oldName, NewName = newName, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region RenameFile case MessageDictionary.RenameFile: conversationId = _reader.ReadString(); oldName = _reader.ReadString(); newName = _reader.ReadString(); FileRenamed?.Invoke(this, new FileRenamedEventArgs() { FileId = conversationId, OldName = oldName, NewName = newName, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region AddConversationMembers case MessageDictionary.AddConversationMembers: conversationId = _reader.ReadString(); string personId = _reader.ReadString(); string personColor = _reader.ReadString(); ConversationMembersAdded?.Invoke(this, new ConversationMembersAddedEventArgs() { ConversationId = conversationId, PersonId = personId, PersonColor = personColor, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region RemoveConversationMember case MessageDictionary.RemoveConversationMember: conversationId = _reader.ReadString(); personId = _reader.ReadString(); ConversationMemberRemoved?.Invoke(this, new ConversationMemberRemovedEventArgs() { ConversationId = conversationId, PersonId = personId, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region ConversationSettingsChanged case MessageDictionary.ChangeConversationSettings: conversationId = _reader.ReadString(); notifyContactPersons = _reader.ReadBoolean(); ConversationSettingsChanged?.Invoke(this, new ConversationSettingsChangedEventArgs() { ConversationId = conversationId, NotifyContactPersons = notifyContactPersons, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region NewCompany case MessageDictionary.NewCompany: string companyId = _reader.ReadString(); string companyName = _reader.ReadString(); NewCompanyArrived?.Invoke(this, new NewCompanyEventArgs() { Company = new CompanyModel(companyId, companyName), Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region RenameCompany case MessageDictionary.RenameCompany: companyId = _reader.ReadString(); newName = _reader.ReadString(); CompanyRenamed?.Invoke(this, new CompanyRenamedEventArgs() { CompanyId = companyId, NewName = newName, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region RemoveCompanyEvent case MessageDictionary.RemoveCompany: companyId = _reader.ReadString(); CompanyRemoved?.Invoke(this, new CompanyRemovedEventArgs() { CompanyId = companyId, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region UpdatePersonDetails case MessageDictionary.UpdatePersonDetails: string PersonId = _reader.ReadString(); string PersonFirstName = _reader.ReadString(); string PersonLastName = _reader.ReadString(); string PersonGenderCode = _reader.ReadString(); string PersonCompanyId = _reader.ReadString(); Gender PersonGender = (Gender)Convert.ToInt32(PersonGenderCode); PersonModel contactPerson = new PersonModel(PersonId, PersonFirstName, PersonLastName, PersonGender, PersonCompanyId, true); int emailAddressCount = _reader.ReadInt32(); for (int j = 0; j < emailAddressCount; j++) { emailId = _reader.ReadString(); string emailName = _reader.ReadString(); emailAddress = _reader.ReadString(); EmailAddressModel emailAddressModel = new EmailAddressModel(emailId, emailAddress, emailName); contactPerson.EmailAddresses.Add(emailAddressModel); } int phoneNumberCount = _reader.ReadInt32(); for (int j = 0; j < phoneNumberCount; j++) { string phoneNumberId = _reader.ReadString(); string phoneName = _reader.ReadString(); string phoneNumber = _reader.ReadString(); PhoneNumberModel phoneNumberModel = new PhoneNumberModel(phoneNumberId, phoneNumber, phoneName); contactPerson.PhoneNumbers.Add(phoneNumberModel); } ContactDetailsUpdated?.Invoke(this, new ContactDetailsUpdatedEventArgs() { NewData = contactPerson, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region NewExternalContact case MessageDictionary.NewExternalContact: _writer.Write(MessageDictionary.OK); PersonId = _reader.ReadString(); PersonFirstName = _reader.ReadString(); PersonLastName = _reader.ReadString(); PersonGenderCode = _reader.ReadString(); PersonCompanyId = _reader.ReadString(); PersonGender = (Gender)Convert.ToInt32(PersonGenderCode); contactPerson = new PersonModel(PersonId, PersonFirstName, PersonLastName, PersonGender, PersonCompanyId, true); emailAddressCount = _reader.ReadInt32(); for (int j = 0; j < emailAddressCount; j++) { emailId = _reader.ReadString(); string emailName = _reader.ReadString(); emailAddress = _reader.ReadString(); EmailAddressModel emailAddressModel = new EmailAddressModel(emailId, emailAddress, emailName); contactPerson.EmailAddresses.Add(emailAddressModel); } phoneNumberCount = _reader.ReadInt32(); for (int j = 0; j < phoneNumberCount; j++) { string phoneNumberId = _reader.ReadString(); string phoneName = _reader.ReadString(); string phoneNumber = _reader.ReadString(); PhoneNumberModel phoneNumberModel = new PhoneNumberModel(phoneNumberId, phoneNumber, phoneName); contactPerson.PhoneNumbers.Add(phoneNumberModel); } NewExternalContact?.Invoke(this, new NewExternalContactEventArgs() { NewData = contactPerson, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region NewExternalContact case MessageDictionary.RemoveExternalContact: personId = _reader.ReadString(); ExternalContactRemoved?.Invoke(this, new ExternalContactRemovedEventArgs() { PersonId = personId, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion #region RemoveConversation case MessageDictionary.RemoveConversation: _writer.Write(MessageDictionary.OK); conversationId = _reader.ReadString(); ConversationRemoved?.Invoke(this, new ConversationRemovedEventArgs() { ConversationId = conversationId, Notification = new NotificationModel(notificationId, notificationText, referenceFields, notificationDate, false) }); break; #endregion } } break; #region NewFiles case MessageDictionary.NewFiles: List <FileModel> files = new List <FileModel>(); files.Clear(); _writer.Write(MessageDictionary.OK); int fileCount = _reader.ReadInt32(); for (int i = 0; i < fileCount; i++) { conversationId = _reader.ReadString(); id = _reader.ReadString(); string name = _reader.ReadString(); string contentType = _reader.ReadString(); long size = _reader.ReadInt64(); string dateAdded = _reader.ReadString(); FileModel file = new FileModel(id, name, new ContentType(contentType), size, DateTime.ParseExact(dateAdded, "dd-MM-yyyy", CultureInfo.InvariantCulture)) { ConversationId = conversationId }; files.Add(file); } NewFilesArrived?.Invoke(this, new NewFilesArrivedEventArgs() { Files = files }); break; #endregion #region NewConversation case MessageDictionary.NewConversation: _writer.Write(MessageDictionary.OK); conversationId = _reader.ReadString(); string conversationName = _reader.ReadString(); string visibleId = _reader.ReadString(); string dateCreatedString = _reader.ReadString(); notifyContactPersons = _reader.ReadBoolean(); DateTime dateCreated = DateTime.ParseExact(dateCreatedString, "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture); int memberCount = _reader.ReadInt32(); List <string> memberIds = new List <string>(); List <string> memberColors = new List <string>(); for (int j = 0; j < memberCount; j++) { string memberId = _reader.ReadString(); string memberColor = _reader.ReadString(); memberIds.Add(memberId); memberColors.Add(memberColor); } List <ConversationMessageModel> messages = new List <ConversationMessageModel>(); if (_reader.Read() == MessageDictionary.OK) { int messageCount = _reader.ReadInt32(); for (int i = 0; i < messageCount; i++) { id = _reader.ReadString(); string authorId = _reader.ReadString(); string authorFrom = _reader.ReadString(); string initialDateString = _reader.ReadString(); Boolean received = _reader.ReadBoolean(); byte[] previewData = ReceiveFile(); List <string> attachmentIds = new List <string>(); int attachmentCount = _reader.ReadInt32(); for (int j = 0; j < attachmentCount; j++) { attachmentIds.Add(_reader.ReadString()); } MemoryStream stream = new MemoryStream(previewData); BitmapImage previewImage = new BitmapImage(); previewImage.BeginInit(); previewImage.StreamSource = stream; previewImage.EndInit(); previewImage.Freeze(); DateTime initialDate = DateTime.ParseExact(initialDateString, "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture); ConversationMessageModel message = new ConversationMessageModel(id, conversationId, authorId, authorFrom, initialDate, received, previewImage, false, attachmentIds); byte type = _reader.ReadByte(); if (type == MessageDictionary.MessageTypeEmail) { string subject = _reader.ReadString(); string content = _reader.ReadString(); ConversationEmailMessageModel emailMessage = new ConversationEmailMessageModel(message, subject, content); message = emailMessage; } else if (type == MessageDictionary.MessageTypePhoneCall) { string recipientPhoneNumber = _reader.ReadString(); string callDescription = _reader.ReadString(); bool callAnswered = _reader.ReadBoolean(); ConversationPhoneMessageModel phoneMessage = new ConversationPhoneMessageModel(message, recipientPhoneNumber, callDescription, callAnswered); message = phoneMessage; } if (message != null) { messages.Add(message); } } if (_reader.Read() == MessageDictionary.EndOfMessage) { ConversationModel conversation = new ConversationModel(conversationId, conversationName, visibleId, dateCreated, memberIds, memberColors, notifyContactPersons); messages.ForEach(obj => conversation.AddMessage(obj)); NewConversationArrived?.Invoke(this, new NewConversationArrivedEventArgs() { Conversation = conversation }); } } break; #endregion #region RemoveConversation case MessageDictionary.RemoveConversation: _writer.Write(MessageDictionary.OK); conversationId = _reader.ReadString(); ConversationRemoved?.Invoke(this, new ConversationRemovedEventArgs() { ConversationId = conversationId }); break; #endregion #region NewEmailAddress case MessageDictionary.AddEmailAddress: _writer.Write(MessageDictionary.OK); emailId = _reader.ReadString(); emailAddress = _reader.ReadString(); string login = _reader.ReadString(); string imapHost = _reader.ReadString(); int imapPort = _reader.ReadInt32(); bool imapUseSel = _reader.ReadBoolean(); string smtpHost = _reader.ReadString(); int smtpPort = _reader.ReadInt32(); bool smtpUseSsl = _reader.ReadBoolean(); string addressName = _reader.ReadString(); NewEmailAddress?.Invoke(this, new NewEmailAddressEventArgs() { Id = emailId, Login = login, Address = emailAddress, ImapHost = imapHost, ImapPort = imapPort, ImapUseSsl = imapUseSel, SmtpHost = smtpHost, SmtpPort = smtpPort, SmtpUseSsl = smtpUseSsl, Name = addressName }); break; #endregion } } } catch (Exception ex) { MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace); ErrorHelper.LogError(ex); Logout(); } }