Example #1
0
        public async Task BanDialog(string userToBanId, string whoAccessedId)
        {
            if (userToBanId == whoAccessedId)
            {
                throw new InvalidDataException("Can't ban yourself.");
            }

            var bannedBy = await usersRepository.GetByIdAsync(whoAccessedId);

            var banned = await usersRepository.GetByIdAsync(userToBanId);

            if (banned == null || bannedBy == null)
            {
                throw new KeyNotFoundException("Wrong id of person to ban.");
            }

            try
            {
                await UsersBansRepository.AddAsync(UsersBansDatamodel.Create(banned, bannedBy));
            }
            catch
            {
                throw new InvalidDataException("User is already banned.");
            }

            UsersConversationDataModel dialog;

            if ((dialog = await usersConversationsRepository.GetDialog(userToBanId, whoAccessedId)) != null)
            {
                await ConversationsBansRepository.AddAsync(ConversationsBansDataModel.Create(banned, dialog.Conversation));
            }

            await unitOfWork.Commit();
        }
Example #2
0
        public async Task <bool> IsBannedFromConversation(int conversationId, string Who)
        {
            var who = await usersRepository.GetByIdAsync(Who);

            if (who == null)
            {
                throw new InvalidDataException("Wrong id of a person to check.");
            }

            return((await ConversationsBansRepository.GetByIdAsync(Who, conversationId)) != null);
        }
Example #3
0
        public async Task UnbanUserFromConversation(int conversationId, string userToUnbanId, string whoAccessedId)
        {
            if (userToUnbanId == whoAccessedId)
            {
                throw new InvalidDataException("Can't unban yourself.");
            }

            var conversation = await ConversationRepository.GetByIdAsync(conversationId);

            var banned = await usersRepository.GetByIdAsync(userToUnbanId);

            var userRole = await rolesRepository.GetByIdAsync(conversationId, whoAccessedId);

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

            if (banned == null)
            {
                throw new InvalidDataException("Wrong user to unban id was provided.");
            }

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

            try
            {
                var entry = await ConversationsBansRepository.GetByIdAsync(userToUnbanId, conversationId);

                await ConversationsBansRepository.DeleteAsync(entry);

                await unitOfWork.Commit();
            }
            catch
            {
                throw new InvalidDataException("Wrong conversation id was provided.");
            }
        }
Example #4
0
        public async Task UnbanDialog(string userId, string whoUnbans)
        {
            try
            {
                await UsersBansRepository.DeleteAsync(await UsersBansRepository.GetByIdAsync(userId, whoUnbans));
            }
            catch
            {
                throw new InvalidDataException("Wrong id of a person to unban.");
            }

            UsersConversationDataModel dialog;

            if ((dialog = await usersConversationsRepository.GetDialog(userId, whoUnbans)) != null)
            {
                var entry = await ConversationsBansRepository.GetByIdAsync(userId, dialog.Conversation.Id);

                await ConversationsBansRepository.DeleteAsync(entry);
            }

            await unitOfWork.Commit();
        }