Exemplo n.º 1
0
        public MessageDetail AddMessage(string senderId, string receiverId, MessageDetail message)
        {
            var conversation = _conversationRepository.FindPrivateConversationByReceiverId(senderId, receiverId);

            if (conversation == null)
            {
                conversation = new Conversation()
                {
                    Id        = ObjectId.GenerateNewId().ToString(),
                    Type      = "private",
                    Receivers = new List <string>()
                    {
                        message.FromUserId, receiverId
                    },
                    CreatedDate = DateTime.Now,
                    SeenIds     = new List <string>()
                    {
                        message.FromUserId
                    }
                };
                conversation = _conversationRepository.Add(conversation);
            }
            else
            {
                conversation.SeenIds = new List <string>()
                {
                    message.FromUserId
                };
                _conversationRepository.Update(conversation);
            }

            message.ConversationId = conversation.Id;

            return(_messageRepository.Add(message));
        }
Exemplo n.º 2
0
        public async Task CreateOne(string ownerId, string creatorId)
        {
            var isExisted = await CheckExistence(ownerId, creatorId);

            if (isExisted)
            {
                return;
            }
            var conversation = new Conversation()
            {
                UpdatedDate = DateTime.UtcNow,
                OwnerId     = ownerId,
                CreatorId   = creatorId,
            };
            var conversationForCreator = new Conversation()
            {
                UpdatedDate = DateTime.UtcNow,
                OwnerId     = creatorId,
                CreatorId   = ownerId,
            };
            await _conversationRepo.Add(conversation);

            await _conversationRepo.Add(conversationForCreator);
        }
        public async Task <ConversationViewModel> CreateConversationAsync(ConversationRequest request)
        {
            var conversation = new Models.Conversation()
            {
                Id = Guid.NewGuid()
            };

            var result = new ConversationViewModel()
            {
                Id = conversation.Id,
            };
            var currentUsername = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
            var currentUser     = await _userManager.FindByNameAsync(currentUsername);

            if (!request.IsAutoCreate)
            {
                conversation.Name = request.Name;
                result.Name       = request.Name;
            }
            else
            {
                var participant = await _userManager.FindByIdAsync(request.ParticipantIds.FirstOrDefault().ToString());

                result.Name = $"{participant.FirstName} {participant.LastName}";
            }

            _conversationRepository.Add(conversation);
            request.ParticipantIds.Add(currentUser.Id);
            foreach (var participantId in request.ParticipantIds)
            {
                var participant = new Participant()
                {
                    Id             = Guid.NewGuid(),
                    ConversationId = conversation.Id,
                    UserId         = participantId
                };
                _participantRepository.Add(participant);
            }

            await _unitOfWork.SaveChangeAsync();

            return(result);
        }
Exemplo n.º 4
0
        public void SendToReceiver(string senderId, string receiverId, string message)
        {
            var conversation = new Conversation()
            {
                Id        = ObjectId.GenerateNewId().ToString(),
                Type      = "private",
                Receivers = new List <string>()
                {
                    senderId, receiverId
                },
                SeenIds = new List <string>()
                {
                    senderId
                },
                CreatedDate = DateTime.Now
            };

            var messageObject = new MessageDetail()
            {
                Id             = ObjectId.GenerateNewId().ToString(),
                Content        = message,
                ConversationId = conversation.Id,
                FromUserId     = senderId,
                Time           = DateTime.Now
            };

            // Add conversation to db
            _conversationRepository.Add(conversation);
            _messageRepository.Add(messageObject);

            var users = _conversationRepository.GetAllUserInConversation(conversation.Id);

            foreach (var user in users)
            {
                if (user.Connections != null)
                {
                    Clients.Clients(user.Connections).SendAsync("clientMessageListener", conversation.Id, messageObject);
                }
            }
        }