예제 #1
0
        public async Task RemoveChat(Shared.DTO.Conversations.Chat chat, string whoRemoves)
        {
            var conversation = await conversationRepository.GetByIdAsync(chat.Id);

            if (conversation == null)
            {
                throw new InvalidDataException("Wrong conversation to remove.");
            }

            //secure chats may be removed by any of participants.

            var userRole = await rolesRepository.GetByIdAsync(conversation.Id, whoRemoves);

            if (!conversation.IsSecure)
            {
                if (conversation.IsGroup)
                {
                    if (userRole.RoleId != ChatRole.Creator)
                    {
                        throw new InvalidDataException("Only creator can remove group.");
                    }
                }
            }

            var attachmentsToDelete = await messagesService.GetAllAttachments(conversation.Id, whoRemoves);

            if (attachmentsToDelete != null && attachmentsToDelete.Any())
            {
                filesService.DeleteFiles(attachmentsToDelete
                                         .Select(x => x.AttachmentInfo.ContentUrl)
                                         .ToList());
            }

            //this removes all messages and users-chats links
            await conversationRepository.DeleteAsync(conversation);

            await unitOfWork.Commit();
        }