def channel_leave(token,channel_id): channel_id = str(channel_id) channel_data = get_data() user_data = auth.get_data() u_id = helper.get_id(token,user_data) helper.check_channel(channel_id,channel_data) helper.check_access(u_id,channel_data,channel_id) storage.remove_member(u_id,channel_id)
def channel_invite(token,channel_id,u_id): u_id = str(u_id) channel_id = str(channel_id) data_channel = get_data() data_user = auth.get_data() helper.check_channel(channel_id,data_channel) helper.check_user(u_id,data_user) if data_user[u_id] not in data_channel[channel_id]['member']: helper.check_access(helper.get_id(token,data_user), data_channel, channel_id) storage.add_member(u_id, channel_id)
def channel_detail(token, channel_id): channel_data = get_data() data = storage.load_user_all() helper.check_channel(channel_id, channel_data) u_id = helper.get_id(token,data) helper.check_access(u_id,channel_data, channel_id) return { "name":channel_data[channel_id]['name'], "owner":channel_data[channel_id]['owner'], "members": channel_data[channel_id]['member'] }
def channel_removeowner(token, channel_id, u_id): channel_id = str(channel_id) channel_data = get_data() # Error check that channel_id refers to a valid channel helper.check_channel(channel_id, data) # Error check if the user is not an owner data_user = auth.get_data() if data_user[u_id] not in channel_data[channel_id]['owner']: raise error.InputError # Error if the authorised user is not already a member of the channel helper.check_access(token, channel_data, channel_id) storage.remove_owner(u_id, channel_id)
def channel_message(token,channel_id,start): channel_id = str(channel_id) user_data = auth.get_data() u_id = helper.get_id(token, user_data) channel_data = get_data() helper.check_channel(channel_id, channel_data) helper.check_access(u_id, channel_data, channel_id) index = start-1 message = channel_data[channel_id]['messages'] if start > len(message): raise error.InputError message_list = [i for i in message if message.index(i)>= index and message.index(i) <= 50+index] if len(message_list) < 50: end = -1 else: end = 50 return { 'messages': message_list, 'start': start, 'end': end }
def message_sendlater(token, channel_id, message, time_sent): data = storage.load_channel_all() user_data = auth.get_data() u_id = helper.get_id(token,user_data) helper.check_access(u_id,data,channel_id) u_id = helper.get_id(token, user_data) message_id = helper.channel_id() # Channel ID is not a valid channel if not helper.valid_channel_id(channel_id, data): raise InputError('Channel id is not a valid channel') # helper.check_access(u_id,data, channel_id) if len(message) > 1000: raise InputError("Message should be under 1000 characters.") now = datetime.now() current_time = datetime.timestamp(now) if time_sent < current_time: raise InputError('The time entered is a time in the past') message_data = { 'message_id': message_id, 'u_id':u_id, 'reacts': False, 'is_pinned': False, 'message_text': message, } time = time_sent - current_time timer = threading.Timer(time, storage.add_message(message_data, channel_id), \ [message_data, channel_id]) timer.start() return {'message_id': message_id}
def message_send(token,channel_id,message): data = storage.load_channel_all() user_data = auth.get_data() u_id = helper.get_id(token, user_data) helper.check_access(u_id,data,channel_id) message_id = helper.channel_id() # helper.check_access(u_id,data, channel_id) if len(message) > 1000: raise InputError("Message should be under 1000 characters.") time_created = helper.get_current_time_as_unix_timestamp() message_data = { 'message_id': message_id, 'u_id': u_id, 'reacts': [], 'is_pinned': False, 'message_text': message, 'time_created': time_created, } storage.add_message(message_data, channel_id) # channel['messages_list'].prepend(message_data) if message == "/hangman": return -1 return {'message_id': message_id}