Beispiel #1
0
        public async Task <IPagedList <Message> > GetMessagesThread(GetMessagesThreadPaginationRequest paginationRequest)
        {
            var currentUser = await profileService.GetCurrentUser();

            if (currentUser.Id == paginationRequest.RecipientId)
            {
                throw new EntityNotFoundException("Messages thread not found");
            }

            if (!SendMessageOnlyToFriendsSpecification.Create(paginationRequest.RecipientId).IsSatisfied(currentUser))
            {
                throw new NoPermissionsException("You can chat only with your friends");
            }

            var messages = await database.MessageRepository.GetMessagesThread(currentUser.Id, paginationRequest.RecipientId, (paginationRequest.PageNumber, paginationRequest.PageSize));

            messages = await MarkAsRead(currentUser.Id, paginationRequest.RecipientId, messages);

            return(messages);
        }
Beispiel #2
0
        public async Task <Message> Send(string recipientId, string text)
        {
            var sender = await profileService.GetCurrentUser();

            if (sender.Id == recipientId)
            {
                throw new NoPermissionsException("You cannot send message to yourself");
            }

            if (!SendMessageOnlyToFriendsSpecification.Create(recipientId).IsSatisfied(sender))
            {
                throw new NoPermissionsException("You can send message only to your friends");
            }

            var recipient = await GetRecipient(recipientId);

            var message = Message.Create(text);

            sender.MessagesSent.Add(message);
            recipient.MessagesReceived.Add(message);

            return(await database.Complete() ? message : null);
        }