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_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_addowner(token, channel_id, u_id): '''Add ownership rights to a 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') for channel in database['channels']: if channel['channel_id'] == channel_id: if u_id in channel['owner_members']: raise ValueError('User already owner of this channel') if db.get_from_token('u_id', token, database) 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'].append(u_id) db.save_DB(database) return {}
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_messages(token, channel_id, start): '''Load and return all messages from a channel''' if not Token.isValid(token): raise AccessError("Not a valid token!") database = db.load_DB() channel_id = int(channel_id) start = int(start) if channel_id not in db.get_all_channels(database): raise ValueError('Channel ID is not a valid channel') if database['messages']: if int(start) > len([message for message in database['messages'] \ if message['in_channel'] == channel_id]): raise ValueError('start is greater than or equal to the total\ number of messages in the channel') u_id = db.get_from_token("u_id", token, database) if database['messages']: messages = [ msg for msg in database['messages'] if msg['in_channel'] == channel_id ] messages = sorted(messages, key=itemgetter('time_created'), reverse=True) end = start + 50 fmessages = [] for message in messages: d = {} d['message_id'] = message['message_id'] d['u_id'] = message['u_id'] d['message'] = message['message'] d['time_created'] = message['time_created'] d['is_pinned'] = message['is_pinned'] reacts = [] for react in message['reacts']: d2 = {} d2['react_id'] = react['react_id'] d2['u_ids'] = react['u_ids'] d2['is_this_user_reacted'] = db.get_user_reacted(database, \ u_id, message['message_id'], react['react_id']) reacts.append(d2) d['reacts'] = reacts fmessages.append(d) if len(messages) < 50: end = -1 else: fmessages = [] end = -1 return dumps({'messages': fmessages, 'start': start, 'end': end})
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']] })