public async Task <PostConversationResponse> PostConversation(PostConversationRequest postConversationRequest, string conversationId)
        {
            using (_logger.BeginScope("{ConversationId}", conversationId))
            {
                PostMessageResponse message = new PostMessageResponse
                {
                    Id             = postConversationRequest.FirstMessage.Id,
                    Text           = postConversationRequest.FirstMessage.Text,
                    SenderUsername = postConversationRequest.FirstMessage.SenderUsername,
                    UnixTime       = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                };

                PostConversationResponse conversation = new PostConversationResponse
                {
                    Id = conversationId,
                    CreatedUnixTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                };
                var stopWatch = Stopwatch.StartNew();
                await _messageStore.AddMessage(message, conversationId);

                var fetchedConversation = await _conversationStore.AddConversation(conversation, postConversationRequest.Participants);

                _telemetryClient.TrackMetric("ConversationStore.AddConversation.Time", stopWatch.ElapsedMilliseconds);
                _telemetryClient.TrackEvent("ConversationAdded");
                return(fetchedConversation);
            }
        }
        public ConversationResponse CreateConversation(PostConversationRequest request)
        {
            _conversationRepository.Index(new Model.Conversation
            {
                Id             = request.ConversationId.ToString(),
                ApplicationId  = request.ApplicationId,
                CompanyId      = request.CompanyId,
                SenderId       = request.SenderId,
                RecipientId    = request.RecipientId,
                Subject        = request.Subject,
                JobId          = request.JobId,
                IsPrivate      = request.IsPrivate,
                IsDeleted      = false,
                CreateDate     = request.CreateDate,
                LastModifyDate = request.LastModifyDate
            });

            return(new ConversationResponse
            {
                Id = request.ConversationId,
                ApplicationId = request.ApplicationId,
                CompanyId = request.CompanyId,
                SenderId = request.SenderId,
                RecipientId = request.RecipientId,
                Subject = request.Subject,
                JobId = request.JobId,
                IsPrivate = request.IsPrivate,
                IsDeleted = false,
                CreateDate = request.CreateDate,
                LastModifyDate = request.LastModifyDate
            });
        }
        public ConversationResponse CreateConversation(PostConversationRequest request)
        {
            var queueManager = new ConversationQueueManager(new RabbitMQConversationQueueFactory());

            var conversationId = Guid.NewGuid();
            var creationDate   = DateTime.Now;

            queueManager.Enqueue(new ConversationQueueRequest
            {
                ConversationId = conversationId,
                ApplicationId  = request.ApplicationId,
                CompanyId      = request.CompanyId,
                SenderId       = request.SenderId,
                RecipientId    = request.RecipientId,
                JobId          = request.JobId,
                Subject        = request.Subject,
                IsPrivate      = request.IsPrivate,
                CreationDate   = creationDate
            });

            return(new ConversationResponse
            {
                Id = conversationId,
                SenderId = request.SenderId,
                RecipientId = request.RecipientId,
                Subject = request.Subject,
                CreateDate = creationDate
            });
        }
        public async Task PostGetConversationListAssertLimitTest(int paginationLimit)
        {
            Profile profile1 = CreateRandomProfile();
            await _chatServiceClient.AddProfile(profile1);

            PostConversationRequest[] conversationsarray = new PostConversationRequest[10];

            for (int index = 0; index < 10; index++)
            {
                Profile profile2 = CreateRandomProfile();
                await _chatServiceClient.AddProfile(profile2);

                conversationsarray[index] = CreateRandomPostConversationRequest(profile1.Username, profile2.Username);
            }

            for (int index = 0; index < 10; index++)
            {
                await _chatServiceClient.AddConversation(conversationsarray[index]);
            }

            GetConversationsResponse fetchedConversationList = await _chatServiceClient.GetConversationList(profile1.Username, paginationLimit, 0);

            int countConversationsInFetchedList = fetchedConversationList.Conversations.Length;

            Assert.Equal(paginationLimit, countConversationsInFetchedList);
        }
        public PostConversationRequest CreateRandomPostConversationRequest(PostMessageRequest message, string[] participants)
        {
            var conversation = new PostConversationRequest
            {
                Participants = participants,
                FirstMessage = message
            };

            return(conversation);
        }
        public PostConversationRequest CreateRandomPostConversationRequest()
        {
            string[] participants = { CreateRandomString(), CreateRandomString() };
            var      conversation = new PostConversationRequest
            {
                Participants = participants,
                FirstMessage = CreateRandomPostMessageRequest()
            };

            return(conversation);
        }
        public PostConversationRequest CreateRandomPostConversationRequest(string username1, string username2)
        {
            string[] participants = { username1, username2 };
            var      conversation = new PostConversationRequest
            {
                Participants = participants,
                FirstMessage = CreateRandomPostMessageRequest(username1)
            };

            return(conversation);
        }
Example #8
0
        public async Task <PostConversationResponse> AddConversation(PostConversationRequest conversation)
        {
            string json = JsonConvert.SerializeObject(conversation);
            HttpResponseMessage responseMessage = await _httpClient.PostAsync($"api/conversations", new StringContent(json, Encoding.UTF8,
                                                                                                                      "application/json"));

            await EnsureSuccessOrThrowConversationsException(responseMessage);

            string responseJson = await responseMessage.Content.ReadAsStringAsync();

            var fetchedConversation = JsonConvert.DeserializeObject <PostConversationResponse>(responseJson);

            return(fetchedConversation);
        }
 private bool ValidateConversation(PostConversationRequest conversation, out string error)
 {
     if (conversation.Participants.Length != 2)
     {
         error = "Conversatoin participants must include 2 usernames";
         return(false);
     }
     if (string.IsNullOrWhiteSpace(conversation.Participants[0]) || string.IsNullOrWhiteSpace(conversation.Participants[1]))// The client app shouldn't allow empty messages. We can choose to allow emtpy messages on our side.
     {
         error = "Both participants must not be empty";
         return(false);
     }
     error = "";
     return(true);
 }
        public async Task <IActionResult> PostConversation([FromBody] PostConversationRequest postConversationRequest)
        {
            if (!ValidateConversation(postConversationRequest, out string conversationFormatError))
            {
                return(BadRequest(conversationFormatError));
            }
            if (!ValidateMessage(postConversationRequest.FirstMessage, out string messageFormatError))
            {
                return(BadRequest(messageFormatError));
            }

            string conversationId = postConversationRequest.Participants[0];

            if (String.Compare(postConversationRequest.Participants[0], postConversationRequest.Participants[1]) == -1)
            {
                conversationId += $"_{postConversationRequest.Participants[1]}";
            }
            else
            {
                conversationId = $"{postConversationRequest.Participants[1]}_" + conversationId;
            }

            try
            {
                var conversation = await _conversationsService.PostConversation(postConversationRequest, conversationId);

                return(StatusCode(201, conversation));
            }
            catch (StorageErrorException e)
            {
                _logger.LogError(e, $"Failed to add conversation {conversationId} to storage");
                return(StatusCode(503, "The service is unavailable, please retry in few minutes"));
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Unknown exception occured while adding conversation {conversationId} to storage");
                return(StatusCode(500, "An internal server error occured, please reachout to support if this error persists"));
            }
        }
        public IActionResult Post(PostConversationRequest request)
        {
            var serviceResponse = _conversationService.CreateConversation(request);

            return(ApiResponse(serviceResponse));
        }