def subscribe(self, *args): """ call this to subscribe specific connection to new channels """ username = self.request.json_body.get('username') subscribe_to_channels = self.request.json_body.get('channels') if not subscribe_to_channels: self.request.response.status = 400 return {'error': "No channels specified"} user = USERS.get(username) if not user: self.request.response.status = 404 return {'error': "User doesn't exist"} with lock: user.last_active = datetime.utcnow() for channel_name in subscribe_to_channels: if channel_name not in user.allowed_channels: user.allowed_channels.append(channel_name) # create channels that didnt exist channel = CHANNELS.get(channel_name) if not channel: channel = Channel(channel_name) CHANNELS[channel_name] = channel else: channel.last_active = datetime.utcnow() return subscribe_to_channels
def connect(self): """ set user along with connection info """ username = self.request.json_body.get('username') def_status = self.request.registry.settings['status_codes']['online'] user_status = int(self.request.json_body.get('status', def_status)) conn_id = self.request.json_body.get('conn_id') subscribe_to_channels = self.request.json_body.get('channels') if username is None: self.request.response.status = 400 return {'error': "No username specified"} if not subscribe_to_channels: self.request.response.status = 400 return {'error': "No channels specified"} # everything is ok so lets add new user and channels with lock: if not username in USERS: user = User(username, def_status) USERS[username] = user else: user = USERS[username] user.last_active = datetime.utcnow() for channel_name in subscribe_to_channels: channel = CHANNELS.get(channel_name) if not channel: channel = Channel(channel_name) CHANNELS[channel_name] = channel else: channel.last_active = datetime.utcnow() user.allowed_channels.extend(subscribe_to_channels) log.info('connecting %s' % username) return {'status': user.status}