Esempio n. 1
0
        public async Task <string> DeleteConversation(string id)
        {
            Conversation exist = await conversationRepository.GetByIdAsync(ObjectId.Parse(id));

            if (exist != null)
            {
                //delete conversation
                await conversationRepository.DeleteAsync(ObjectId.Parse(id));

                //Delete notification group
                FilterDefinition <ClientGroup> finder = Builders <ClientGroup> .Filter.Eq("name", id);

                ClientGroup clientGroup = await clientGroupRepository.FindAsync(finder);

                if (clientGroup != null)
                {
                    await clientGroupRepository.DeleteAsync(clientGroup.Id);
                }

                return("Xóa cuộc trò chuyện thành công");
            }
            else
            {
                throw new Exception("Đã có lỗi xảy ra");
            }
        }
        /// <summary>
        ///     Should not be used frequently as it's quiet expensive.
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="whoRemovedId"></param>
        /// <param name="chatId"></param>
        /// <param name="IsSelf"></param>
        /// <returns></returns>
        public async Task RemoveUserFromConversation(string userId, string whoRemovedId, int chatId, bool IsSelf)
        {
            var conversation = await conversationRepository.GetByIdAsync(chatId);

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

            var userConversation = await usersConversationsRepository.GetByIdAsync(userId, chatId);

            if (userConversation == null)
            {
                throw new InvalidDataException("User is not a part of this conversation.");
            }

            var userRole = await rolesRepository.GetByIdAsync(chatId, whoRemovedId);

            if (!IsSelf)
            {
                if (userRole.RoleId != ChatRole.Moderator && userRole.RoleId != ChatRole.Creator)
                {
                    throw new InvalidDataException("Only creator / moderator can remove users in group.");
                }
            }

            await usersConversationsRepository.DeleteAsync(userConversation);

            if ((await usersConversationsRepository.CountAsync(new GetParticipantsSpec(conversation.Id)))
                .Equals(0))
            {
                await conversationRepository.DeleteAsync(conversation);
            }

            await rolesRepository.DeleteAsync(await rolesRepository.GetByIdAsync(chatId, userId));

            await unitOfWork.Commit();
        }