def channel_removeowner(token, channel_id, u_id): '''Remove ownership rights from user of a channel''' if not Token.isValid(token): raise AccessError("Not a valid token!") channel_id = int(channel_id) u_id = int(u_id) database = db.load_DB() if channel_id not in db.get_all_channels(database): raise ValueError('Channel (based on ID) does not exist') tok_u_id = db.get_from_token('u_id', token, database) channel = db.get_channel_by_id(database, channel_id) user = [user for user in database['users'] if user['u_id'] == tok_u_id][0] for channel in database['channels']: if channel['channel_id'] == channel_id: if u_id not in channel['owner_members']: raise ValueError('User not owner of this channel') if tok_u_id not in channel['owner_members'] and \ db.get_from_token('permission_id', token, database) == 3: raise AccessError('authorised user is not\ an owner of the slackr, or an owner of this channel') channel['owner_members'].remove(u_id) db.save_DB(database) return {}
def channel_invite(token, channel_id, u_id): '''Adds existing user to a new channel''' channel_id = int(channel_id) u_id = int(u_id) # Raise error if invalid token if not Token.isValid(token): raise AccessError('Invalid Token.') database = db.load_DB() # Check if valid channel_id if channel_id not in db.get_all_channels(database): raise ValueError('Invalid Channel ID.') # Check if authorised user is a member of the channel channel = db.get_channel_by_id(database, channel_id) if not db.get_from_token('u_id', token, database) in channel['members']: raise AccessError('You are not a member of this channel so \ you cannot invite others to join.') # Check if valid user ID for user to be adde if u_id not in db.get_all_users(database): raise AccessError('Invalid user id.') if u_id in channel['members']: raise AccessError('User already part of the channel') database['channels'].remove(channel) channel['members'].append(u_id) database['channels'].append(channel) db.save_DB(database) return dumps({})
def channel_join(token, channel_id): '''Join existing user to a new channel''' if not Token.isValid(token): raise AccessError("Not a valid token!") channel_id = int(channel_id) database = db.load_DB() u_id = db.get_from_token('u_id', token, database) if channel_id not in db.get_all_channels(database): raise ValueError('Channel (based on ID) does not exist') channel = db.get_channel_by_id(database, channel_id) if not channel['is_public']: if db.get_from_token('permission_id', token, database) not in [1, 2]: raise AccessError('channel_id refers to a channel that is private \ (when the authorised user is not an admin)') for channel in database['channels']: if channel['channel_id'] == channel_id: if not u_id in channel['members']: channel['members'].append(u_id) db.save_DB(database) return {}
def channel_leave(token, channel_id): '''Remove existing user from a channel''' if not Token.isValid(token): raise AccessError("Not a valid token!") channel_id = int(channel_id) database = db.load_DB() if channel_id not in db.get_all_channels(database): raise ValueError('Channel (based on ID) does not exist') u_id = db.get_from_token('u_id', token, database) channel = db.get_channel_by_id(database, channel_id) for channel in database['channels']: if channel['channel_id'] == channel_id: if u_id in channel['members']: channel['members'].remove(u_id) db.save_DB(database) return {}
def channel_details(token, channel_id): '''Gets the details of the channel''' if not Token.isValid(token): raise AccessError("Not a valid token!") database = db.load_DB() channel_id = int(channel_id) if channel_id not in db.get_all_channels(database): raise ValueError('Channel ID is not a valid channel') channel = db.get_channel_by_id(database, channel_id) if db.get_from_token("u_id", token, database) not in channel['members']: raise AccessError('Not authorised to view this channel') return dumps({ "name" : channel['name'], "all_members" : [user for user in database["users"] if user['u_id'] in channel['members']], "owner_members" : [user for user in database["users"] if \ user['u_id'] in channel['owner_members']] })