Beispiel #1
0
        public async Task <Conversation> StartConversation(List <string> userIds)
        {
            userIds.Sort();
            var conversionHashCode = string.Join(string.Empty, userIds).GetHashCodeSimple();
            var conversation       = await conversationRepository.GetByHashCode(conversionHashCode);

            var users = userRepository.GetAll().Where(u => userIds.Contains(u.Id)).ToList();

            if (conversation != null)
            {
                return(conversation);
            }
            conversation = new Conversation
            {
                HashCode = conversionHashCode,
                Messages = new List <Message>()
            };

            conversation = await conversationRepository.Create(conversation);

            var conversationUsers = new List <ConversationUser>(users.Select(u => new ConversationUser
            {
                ConversationId = conversation.Id, UserId = u.Id
            }));

            await conversationUserRepository.CreateRange(conversationUsers);

            return(conversation);
        }
Beispiel #2
0
        public async Task CreateConversation(User sender, User recipient, Message newMessage)
        {
            UserConversation userConvo = await _conversationRepo.RetrieveUserConversation(sender.Id, recipient.Id);

            if (userConvo != null)
            {
                Conversation convo = await _conversationRepo.Retrieve(userConvo.ConversationsId);

                newMessage.Conversation = convo;
                await _messageRepo.Create(newMessage);

                return;
            }

            Conversation newConvo = new Conversation {
            };

            newConvo.UserConversations = new List <UserConversation> {
                new UserConversation {
                    Conversation = newConvo,
                    User         = sender
                },
                new UserConversation {
                    Conversation = newConvo,
                    User         = recipient
                }
            };

            newConvo.Messages = new List <Message> {
                newMessage
            };

            await _conversationRepo.Create(newConvo);
        }
        public void WriteMessage(string message)
        {
            Conversation conversation = new Conversation()
            {
                Message = message,
                Date    = DateTime.Now
            };

            conversationRepo.Create(conversation);
            conversationRepo.Save();
        }
Beispiel #4
0
        public ActionResult <ConversationReadDto> Create([FromBody] ConversationCreateDto request)
        {
            if (_userRepository.Get(request.CreatorId) == null)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, "User doesn't exist."));
            }

            if (request.CreatorId != request.ParticipantId && _userRepository.Get(request.ParticipantId) == null)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, "User doesn't exist."));
            }

            Conversation newEntity = _mapper.Map <Conversation>(request);

            newEntity = _conversationRepository.Create(newEntity);

            return(StatusCode(StatusCodes.Status201Created, _mapper.Map <ConversationReadDto>(newEntity)));
        }
        public async Task <string> CreateConversation(string creatorId, List <string> userIds)
        {
            var ids = new List <string>(userIds);

            ids.Add(creatorId);

            var list = String.Join('-', ids.OrderBy(x => x).ToArray()).Replace("-", "");

            var hash = CreateMD5(list);

            await conversations.Create(new Conversation()
            {
                Id        = hash,
                Members   = ids,
                CreatedBy = creatorId,
                CreatedAt = DateTime.Now
            });

            return(hash);
        }
Beispiel #6
0
        public ActionResult PostConversation([FromBody] ConversationCreateDto dto)
        {
            var entity = _repository.Create(dto);

            return(Ok(entity));
        }