Esempio n. 1
0
    def get_user_ban_status(self, room_id: str, user_id: str) -> dict:
        channel_id = self.channel_for_room(room_id)
        global_ban = self.redis.hget(RedisKeys.banned_users(), user_id)
        channel_ban = self.redis.hget(RedisKeys.banned_users_channel(channel_id), user_id)
        room_ban = self.redis.hget(RedisKeys.banned_users(room_id), user_id)

        global_timestamp = ''
        channel_timestamp = ''
        room_timestamp = ''

        if global_ban is not None:
            global_ban = str(global_ban, 'utf-8')
            global_timestamp = global_ban.split('|', 2)[1]
        if channel_ban is not None:
            channel_ban = str(channel_ban, 'utf-8')
            channel_timestamp = channel_ban.split('|', 2)[1]
        if room_ban is not None:
            room_ban = str(room_ban, 'utf-8')
            room_timestamp = room_ban.split('|', 2)[1]

        return {
            'global': global_timestamp,
            'channel': channel_timestamp,
            'room': room_timestamp
        }
Esempio n. 2
0
    def is_banned_from_room(self, room_id: str, user_id: str) -> (bool, Union[str, None]):
        ban = self.redis.hget(RedisKeys.banned_users(room_id), user_id)
        is_banned, time = self._is_banned(ban)
        if not is_banned:
            return False, None

        now = datetime.utcnow()
        end = datetime.fromtimestamp(float(time))
        if now > end:
            self.redis.hdel(RedisKeys.banned_users(), user_id)
            return False, None
        return True, time
Esempio n. 3
0
 def ban_user_global(self, user_id: str, ban_timestamp: str, ban_duration: str, reason: str=None, banner_id: str=None):
     user_name = ''
     try:
         user_name = self.get_user_name(user_id)
     except NoSuchUserException:
         pass
     self.redis.hset(RedisKeys.banned_users(), user_id, '%s|%s|%s' % (ban_duration, ban_timestamp, user_name))
Esempio n. 4
0
    def ban_user(self, past=False):
        if past:
            bantime = datetime.datetime.utcnow() - datetime.timedelta(
                0, 240)  # 4 minutes ago
        else:
            bantime = datetime.datetime.utcnow() + datetime.timedelta(
                0, 240)  # 4 minutes left

        bantime = bantime.strftime('%s')
        environ.env.db.redis.hset(RedisKeys.banned_users(), BaseTest.USER_ID,
                                  'asdf|%s' % bantime)
Esempio n. 5
0
    def ban_user(self, past=False):
        if past:
            bantime = datetime.datetime.utcnow() - datetime.timedelta(
                0, 240)  # 4 minutes ago
        else:
            bantime = datetime.datetime.utcnow() + datetime.timedelta(
                0, 240)  # 4 minutes left

        bantime = str(bantime.timestamp()).split('.')[0]
        self.env.db.redis.hset(RedisKeys.banned_users(), BaseTest.USER_ID,
                               'asdf|%s' % bantime)
Esempio n. 6
0
        def for_local_ban(_user_id, _room_id, _ban_info):
            ban_duration, ban_timestamp, username = _ban_info.split('|', 2)
            if datetime.fromtimestamp(int(ban_timestamp)) < now:
                self.redis.hdel(RedisKeys.banned_users(_room_id), _user_id)
                return

            output[_user_id] = {
                'name': b64e(self.get_user_name(_user_id)),
                'duration': ban_duration,
                'timestamp': datetime.fromtimestamp(int(ban_timestamp)).strftime(ConfigKeys.DEFAULT_DATE_FORMAT)
            }
Esempio n. 7
0
    def ban_user_room(self, user_id: str, ban_timestamp: str, ban_duration: str, room_id: str, reason: str=None, banner_id: str=None):
        try:
            self.channel_for_room(room_id)
        except NoChannelFoundException:
            raise NoSuchRoomException(room_id)

        user_name = ''
        try:
            user_name = self.get_user_name(user_id)
        except NoSuchUserException:
            pass
        self.redis.hset(RedisKeys.banned_users(room_id), user_id, '%s|%s|%s' % (ban_duration, ban_timestamp, user_name))
Esempio n. 8
0
    def test_get_global_ban_timestamp_after_expired(self):
        timestamp = str(
            int((datetime.utcnow() + timedelta(seconds=5 * 60)).timestamp()))
        duration = '5m'
        self.cache.set_global_ban_timestamp(CacheRedisTest.USER_ID, duration,
                                            timestamp,
                                            CacheRedisTest.USER_NAME)
        _dur, _time, _name = self.cache.get_global_ban_timestamp(
            CacheRedisTest.USER_ID)
        self.assertEqual(duration, _dur)
        self.assertEqual(timestamp, _time)
        self.assertEqual(CacheRedisTest.USER_NAME, _name)

        key = RedisKeys.banned_users()
        cache_key = '%s-%s' % (key, CacheRedisTest.USER_ID)
        self.cache._del(cache_key)

        _dur, _time, _name = self.cache.get_global_ban_timestamp(
            CacheRedisTest.USER_ID)
        self.assertEqual(duration, _dur)
        self.assertEqual(timestamp, _time)
        self.assertEqual(CacheRedisTest.USER_NAME, _name)
Esempio n. 9
0
    def get_banned_users_for_room(self, room_id) -> dict:
        now = datetime.utcnow()
        output = dict()

        def for_local_ban(_user_id, _room_id, _ban_info):
            ban_duration, ban_timestamp, username = _ban_info.split('|', 2)
            if datetime.fromtimestamp(int(ban_timestamp)) < now:
                self.redis.hdel(RedisKeys.banned_users(_room_id), _user_id)
                return

            output[_user_id] = {
                'name': b64e(self.get_user_name(_user_id)),
                'duration': ban_duration,
                'timestamp': datetime.fromtimestamp(int(ban_timestamp)).strftime(ConfigKeys.DEFAULT_DATE_FORMAT)
            }

        local_bans = self.redis.hgetall(RedisKeys.banned_users(room_id))
        for user_id, ban_info in local_bans.items():
            user_id = str(user_id, 'utf-8')
            ban_info = str(ban_info, 'utf-8')
            for_local_ban(user_id, room_id, ban_info)

        return output
Esempio n. 10
0
File: redis.py Progetto: Xlzman/dino
 def get_room_ban_timestamp(self, room_id: str, user_id: str) -> str:
     key = RedisKeys.banned_users(room_id)
     return self._get_ban_timestamp(key, user_id)
Esempio n. 11
0
File: redis.py Progetto: Xlzman/dino
 def get_global_ban_timestamp(self, user_id: str) -> str:
     key = RedisKeys.banned_users()
     return self._get_ban_timestamp(key, user_id)
Esempio n. 12
0
File: redis.py Progetto: Xlzman/dino
 def set_room_ban_timestamp(self, room_id: str, user_id: str, duration: str,
                            timestamp: str, username: str) -> None:
     key = RedisKeys.banned_users(room_id)
     self._set_ban_timestamp(key, user_id,
                             '%s|%s|%s' % (duration, timestamp, username))
Esempio n. 13
0
 def setUp(self):
     super(RequestLoginTest, self).setUp()
     self.clear_session()
     environ.env.db.redis.hdel(RedisKeys.banned_users(), BaseTest.USER_ID)
Esempio n. 14
0
 def remove_global_ban(self, user_id: str) -> str:
     self.redis.hdel(RedisKeys.banned_users(), user_id)
Esempio n. 15
0
 def get_room_ban_timestamp(self, room_id: str, user_id: str) -> (str, str, str):
     ban = self.redis.hget(RedisKeys.banned_users(room_id), user_id)
     return self._get_ban_timestamp(ban)
Esempio n. 16
0
 def get_global_ban_timestamp(self, user_id: str) -> (str, str, str):
     ban = self.redis.hget(RedisKeys.banned_users(), user_id)
     return self._get_ban_timestamp(ban)
Esempio n. 17
0
 def remove_room_ban(self, room_id: str, user_id: str) -> str:
     self.redis.hdel(RedisKeys.banned_users(room_id), user_id)
Esempio n. 18
0
    def get_bans_for_user(self, user_id: str, session=None) -> dict:
        def _to_date(_timestamp):
            return datetime.fromtimestamp(int(_timestamp)).strftime(ConfigKeys.DEFAULT_DATE_FORMAT)

        now = datetime.utcnow()
        all_channels = self.redis.hgetall(RedisKeys.channels())
        channel_ids = list()
        room_ids = list()

        output = {
            'global': dict(),
            'room': dict(),
            'channel': dict()
        }

        for channel_id, _ in all_channels.items():
            channel_ids.append(str(channel_id, 'utf-8'))

        for channel_id in channel_ids:
            all_rooms = self.redis.hgetall(RedisKeys.rooms(channel_id))

            for room_id, _ in all_rooms.items():
                room_ids.append(str(room_id, 'utf-8'))

        for channel_id in channel_ids:
            r_key = RedisKeys.banned_users_channel(channel_id)
            if not self.redis.hexists(r_key, user_id):
                continue

            ban_info = self.redis.hget(RedisKeys, r_key)
            ban_duration, ban_timestamp, _ = ban_info.split('|', 2)
            if datetime.fromtimestamp(int(ban_timestamp)) < now:
                self.redis.hdel(r_key, user_id)
                continue

            output['channel'][channel_id] = {
                'name': b64e(self.get_channel_name(channel_id)),
                'duration': ban_duration,
                'timestamp': _to_date(ban_timestamp)
            }

        for room_id in room_ids:
            r_key = RedisKeys.banned_users(room_id)
            if not self.redis.hexists(r_key, user_id):
                continue

            ban_info = self.redis.hget(RedisKeys, r_key)
            ban_duration, ban_timestamp, _ = ban_info.split('|', 2)
            if datetime.fromtimestamp(int(ban_timestamp)) < now:
                self.redis.hdel(r_key, user_id)
                continue

            output['room'][room_id] = {
                'name': b64e(self.get_room_name(room_id)),
                'duration': ban_duration,
                'timestamp': _to_date(ban_timestamp)
            }

        r_key = RedisKeys.banned_users()
        if self.redis.hexists(r_key, user_id):
            ban_info = self.redis.hget(RedisKeys.banned_users(), user_id)
            ban_duration, ban_timestamp, _ = ban_info.split('|', 2)
            if datetime.fromtimestamp(int(ban_timestamp)) < now:
                self.redis.hdel(r_key, user_id)
            else:
                output['global'] = {
                    'duration': ban_duration,
                    'timestamp': _to_date(ban_timestamp)
                }
        return output