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 }
def ban_user_channel(self, user_id: str, ban_timestamp: str, ban_duration: str, channel_id: 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_channel(channel_id), user_id, '%s|%s|%s' % (ban_duration, ban_timestamp, user_name))
def for_local_ban_channel(_user_id, _channel_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_channel(_channel_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) }
def is_banned_from_channel(self, channel_id: str, user_id: str) -> (bool, Union[str, None]): ban = self.redis.hget(RedisKeys.banned_users_channel(channel_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
def get_banned_users_for_channel(self, channel_id): now = datetime.utcnow() output = dict() def for_local_ban_channel(_user_id, _channel_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_channel(_channel_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) } channel_bans = self.redis.hgetall(RedisKeys.banned_users_channel(channel_id)) for user_id, ban_info in channel_bans.items(): user_id = str(user_id, 'utf-8') ban_info = str(ban_info, 'utf-8') for_local_ban_channel(user_id, channel_id, ban_info) return output
def get_channel_ban_timestamp(self, channel_id: str, user_id: str) -> str: key = RedisKeys.banned_users_channel(channel_id) return self._get_ban_timestamp(key, user_id)
def set_channel_ban_timestamp(self, channel_id: str, user_id: str, duration: str, timestamp: str, username: str) -> None: key = RedisKeys.banned_users_channel(channel_id) self._set_ban_timestamp(key, user_id, '%s|%s|%s' % (duration, timestamp, username))
def get_channel_ban_timestamp(self, channel_id: str, user_id: str) -> (str, str, str): ban = self.redis.hget(RedisKeys.banned_users_channel(channel_id), user_id) return self._get_ban_timestamp(ban)
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
def remove_channel(self, channel_id: str) -> None: self.redis.hdel(RedisKeys.channels(), channel_id) self.redis.hdel(RedisKeys.banned_users_channel(channel_id)) self.redis.hdel(RedisKeys.channel_acl(channel_id))
def remove_channel_ban(self, channel_id: str, user_id: str) -> str: self.redis.hdel(RedisKeys.banned_users_channel(channel_id), user_id)