public string CreateChatMessage(string ChatMessage) { List <ErrorCodes> errors = new List <ErrorCodes>(); ErrorTypes errorType = ErrorTypes.Ok; ChatMessage chatMsg; string errorMsg = String.Empty; try { chatMsg = BsonSerializer.Deserialize <ChatMessage>(ChatMessage); } catch (Exception e) { errors.Add(ErrorCodes.CouldNotParseJsonToClass); return(new Notification(null, ErrorTypes.Error, errors).ToJson()); } if (!_rr.DoesRoomExist(chatMsg.RoomId)) { errors.Add(ErrorCodes.RoomDoesNotExist); return(new Notification(null, ErrorTypes.Error, errors).ToJson()); } //Assign date to ChatMessage chatMsg.Timestamp = TimeHelper.timeSinceEpoch(); //assign ID to room chatMsg.Id = ObjectId.GenerateNewId(DateTime.Now).ToString(); try { _cr.AddChatMessage(chatMsg); } catch (Exception e) { errors.Add(ErrorCodes.CouldNotGetChatMessages); return(new Notification(null, ErrorTypes.Error, errors).ToJson()); } try { _irabbitPublisher.publishString("CreateChatMessage", chatMsg.ToJson()); } catch (Exception e) { errors.Add(ErrorCodes.RoomDoesNotExist); errorType = ErrorTypes.Complicated; } return(new Notification(null, errorType, errors).ToJson()); }
public async Task <ChatMessageModel> AddChatMessage(AddMessageModel request, Guid personUid) { var chatEntity = await _chatRepository.GetChat(request.ChatUid); var personEntity = await _personRepository.GetPerson(personUid); var chatMessageUid = Guid.NewGuid(); var date = DateTime.UtcNow; var messageEntity = new ChatMessageEntity { ChatMessageUid = chatMessageUid, Content = request.Content, MessageTime = date, ChatId = chatEntity.ChatId, AuthorId = personEntity.PersonId }; await _chatRepository.AddChatMessage(messageEntity); var chatImageUids = new List <Guid>(); foreach (var image in request.Images) { var chatImageUid = await _imageLogic.SaveImage(image); await _chatRepository.SaveChatImage(chatMessageUid, new ChatImageContentEntity { ChatImageContentUid = chatImageUid }); chatImageUids.Add(chatImageUid); } await _chatRepository.AddLastReadChatMessage(chatEntity, personUid, messageEntity.ChatMessageId); await SendPushNotificationsToChatMembers(chatEntity, personEntity, request.Content); return(new ChatMessageModel { Images = chatImageUids, MessageContent = request.Content, MessageUid = chatMessageUid, PersonUid = personEntity.PersonUid, PersonName = personEntity.Name, MessageTime = date, PersonImageUid = personEntity.PersonImageContentEntity?.PersonImageContentUid }); }