Exemplo n.º 1
0
        public void BlockUser_ShouldReturnTrue_WhenUserIsBlocked()
        {
            // Arrange
            UserEntity user = new UserEntity()
            {
                Id = 1, Username = "******"
            };
            UserEntity blockedUser = new UserEntity()
            {
                Id = 2, Username = "******"
            };

            _userRepository.FindOne(i => true)
            .ReturnsForAnyArgs(x =>
            {
                var func = ((Expression <Func <UserEntity, bool> >)x[0]).Compile();

                if (func(user))
                {
                    return(user);
                }

                if (func(blockedUser))
                {
                    return(blockedUser);
                }

                return(null);
            });

            _userBlockRepository.FindOne(Arg.Any <Expression <Func <UserBlockEntity, bool> > >())
            .Returns((UserBlockEntity)null);

            _userBlockRepository.Insert(Arg.Any <UserBlockEntity>())
            .Returns(new UserBlockEntity()
            {
                Id = 1
            });

            _activityRepository.Insert(Arg.Any <ActivityEntity>())
            .Returns(new ActivityEntity()
            {
                Id = 1
            });

            // Act
            bool isBlocked = _userManager.BlockUser(user.Id, blockedUser.Username);

            // Assert
            Assert.That(isBlocked, Is.True);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 添加屏蔽对象
        /// </summary>
        /// <param name="userId">屏蔽人UserId</param>
        /// <param name="objectType">被屏蔽对象类型</param>
        /// <param name="objectId">被屏蔽对象Id</param>
        /// <param name="objectName">被屏蔽对象名称</param>
        public bool Create(long userId, int objectType, long objectId, string objectName)
        {
            //在Repository添加被屏蔽对象时,需要检查userId +  objectType + objectId 是否已存在,如果已存在则不添加
            UserBlockedObject userBlockedObject = new UserBlockedObject
            {
                ObjectId    = objectId,
                ObjectName  = objectName,
                ObjectType  = objectType,
                UserId      = userId,
                DateCreated = DateTime.UtcNow
            };

            EventBus <UserBlockedObject> .Instance().OnBefore(userBlockedObject, new CommonEventArgs(EventOperationType.Instance().Create()));

            if (IsBlocked(userId, objectType, objectId))
            {
                return(false);
            }
            object blockId = userBlockRepository.Insert(userBlockedObject) ?? 0;

            if (Convert.ToInt64(blockId) > 0)
            {
                EventBus <UserBlockedObject> .Instance().OnAfter(userBlockedObject, new CommonEventArgs(EventOperationType.Instance().Create()));

                return(true);
            }
            return(false);
        }
Exemplo n.º 3
0
        public bool BlockUser(int userId, string blockedUsername)
        {
            UserEntity user = _userRepository.FindOne(x => x.Id == userId);

            if (user == null)
            {
                return(false);
            }

            UserEntity blockedUser = _userRepository.FindOne(x => x.Username == blockedUsername);

            if (blockedUser == null)
            {
                return(false);
            }

            UserBlockEntity userBlock = _userBlockRepository.FindOne(x => x.UserId == user.Id && x.BlockedUserId == blockedUser.Id);

            if (userBlock != null)
            {
                return(true);
            }

            userBlock = new UserBlockEntity();

            userBlock.UserId        = user.Id;
            userBlock.BlockedUserId = blockedUser.Id;

            userBlock = _userBlockRepository.Insert(userBlock);

            bool isBlocked = userBlock.Id > 0;

            if (isBlocked)
            {
                AddActivity(userId, ActivityTypeEnum.BlockUser, $"Blocked {blockedUser.Username}.");
            }

            return(isBlocked);
        }