def channels_create(token, name, is_public): ''' Creates a new channel ''' valid_token = False for user in users: if user['token'] == token: valid_token = True if valid_token and len(name) <= 20: new_channel = { 'channel_id': len(channels) + 1, 'name': name, 'is_public': is_public, 'creator': create_member(get_u_id(token)), 'owners': [create_member(get_u_id(token))], 'members': [create_member(get_u_id(token))], 'messages': [], 'is_standup': False, 'standup_finish': None, 'standup': '' } new_channel_copy = new_channel.copy() channels.append(new_channel_copy) saveChannelData(channels) return {'channel_id': new_channel_copy['channel_id']} if len(name) > 20: raise InputError('Invalid Name') return None
def channel_invite(token, channel_id, u_id): channels = getChannelData() ''' Tests channel_invite function ''' #retireving u_id from token from token authorised_u_id = get_u_id(token) #raising error if channel does not exist if not channel_exists_persist(channel_id): raise InputError('Invalid channel') #raising error is user does not exist if not user_exists_persist(u_id): raise InputError('Invalid user id') #raising error is user in now in channel if not user_in_channel_persist(authorised_u_id, channel_id): raise AccessError('User not a member of channel') #finding the correct channel then and appending new user for channel in channels: if channel['channel_id'] == int(channel_id): channel['members'].append(create_member(int(u_id))) saveChannelData(channels) return {}
def channel_join_p(token, channel_id): # pragma: no cover ''' adds user to channel ''' #Comment out below channels = getChannelData() channel_id = int(channel_id) u_id = get_u_id(token) #rasies error if channel does not exist if not channel_exists(channel_id): raise InputError('Invalid channel') #Check if channel is public or private public = False for channel in channels: if int(channel['channel_id']) == channel_id: if channel['is_public']: public = True #if channel is prive error is raised if not public: raise AccessError('Private Channel') #adds member to channel for channel in channels: if (int(channel['channel_id']) == channel_id and create_member(u_id) not in channel['members']): channel['members'].append(create_member(u_id)) #Comment out below saveChannelData(channels) return {}
def channel_leave(token, channel_id): ''' removes user from channel ''' authorised_u_id = get_u_id(token) channels = getChannelData() #raises error is channel does not exist if not channel_exists(channel_id): raise InputError('Invalid channel') #raises error is user is not a member of the channel if not user_in_channel(authorised_u_id, channel_id): raise AccessError('User not a member of channel') #removing member from channel for channel in channels: if channel['channel_id'] == channel_id: print("Channel_before:\n", channel['members']) for owner in channel['owners']: if owner['u_id'] == authorised_u_id: channel['owners'].remove(owner) for member in channel['members']: if member['u_id'] == authorised_u_id: channel['members'].remove(member) print("Channels_after:\n", channel['members']) saveChannelData(channels) return {}
def standup_send(token, channel_id, message): channels = getChannelData() users = getUserData() channel_id = int(channel_id) authorised_u_id = get_u_id(token) if not channel_exists_persist(channel_id): raise InputError('Invalid channel') if not user_in_channel_persist(authorised_u_id, channel_id): raise AccessError('User not a member of channel') if len(message) > 1000: raise InputError("Message too long") for channel in channels: if channel['channel_id'] == channel_id: if channel['is_standup'] == False: raise InputError("Channel not in standup") for user in users: if user['u_id'] == authorised_u_id: handle = user['handle_str'] new_message = handle + ": " + message + "\n" for channel in channels: if channel['channel_id'] == channel_id: channel['standup'] = channel['standup'] + new_message saveChannelData(channels) return {}
def standup_start(token, channel_id, length): channels = getChannelData() authorised_u_id = get_u_id(token) if not channel_exists_persist(channel_id): raise InputError('Invalid channel') if not user_in_channel_persist(authorised_u_id, channel_id): raise AccessError('User not a member of channel') for channel in channels: if channel['channel_id'] == channel_id: if channel['is_standup'] != False: raise InputError('Channel already in standup') thread = Thread(target=standup, args=(channel_id, token, length)) for channel in channels: if channel['channel_id'] == channel_id: channel['is_standup'] = True thread.start() finish_time = datetime.now() + timedelta(seconds=int(length)) unixstamp = time.mktime(finish_time.timetuple()) for channel in channels: if channel['channel_id'] == channel_id: channel['standup_finish'] = unixstamp saveChannelData(channels) return {'time_finish': unixstamp}
def standup_active(token, channel_id): channels = getChannelData() authorised_u_id = get_u_id(token) if not channel_exists_persist(channel_id): raise InputError('Invalid channel') if not user_in_channel_persist(authorised_u_id, channel_id): raise AccessError('User not a member of channel') for channel in channels: if channel['channel_id'] == channel_id: is_active = channel['is_standup'] time_finish = channel['standup_finish'] saveChannelData(channels) return {'is_active': is_active, 'time_finish': time_finish}
def standup(token, channel_id, length): # pragma: no cover time.sleep(length) channels = getChannelData() print("Standup start for " + str(length) + " seconds") for channel in channels: if channel['channel_id'] == int(channel_id): standup_message = ['standup'] message_send(token, channel_id, standup_message) for channel in channels: if channel['channel_id'] == int(channel_id): channel['is_standup'] == False channel['standup'] == '' channel['standup_finish'] == None saveChannelData(channels) print("Standup finish") return