def edit_msg_react_in_list(msg, uid, method): """Interate the messages list by its id, return the message after edit.""" # get the channels channels = data.return_channels() messages = data.return_messages() # modify in channel for i in channels: if i['channel_id'] == msg['channel_id']: for temp in i['message']: if temp['message_id'] == msg['message_id']: if method == 'add': temp['reacts'][0]["u_ids"].append(int(uid)) elif method == 'delete': temp['reacts'][0]["u_ids"].remove(int(uid)) # modify in msg.json for temp in messages: if temp['message_id'] == msg['message_id']: if method == 'add': temp['reacts'][0]["u_ids"].append(int(uid)) elif method == 'delete': temp['reacts'][0]["u_ids"].remove(int(uid)) # add it to memory data.replace_channels(channels) data.replace_messages(messages)
def find_message(msg_id): """Interate the messages list by its id, return the message we need.""" return_message = None for i in data.return_messages(): if i['message_id'] == msg_id: return_message = i break return return_message
def delete_msg_in_list(msg): """Interate the messages list by its id, return the message we need.""" # get the channels channels = data.return_channels() messages = data.return_messages() # deleting message from memory for i in channels: if i['channel_id'] == msg['channel_id']: i['message'].remove(msg) messages.remove(msg) # add it to memory data.replace_channels(channels) data.replace_messages(messages)
def change_msg_pin(msg, sign): """Interate the messages list by its id, return the message after edit.""" # get the channels channels = data.return_channels() messages = data.return_messages() # deleting message from memory for i in channels: if i['channel_id'] == msg['channel_id']: for temp in i['message']: if temp['message_id'] == msg['message_id']: temp['is_pinned'] = sign for temp in messages: if temp['message_id'] == msg['message_id']: temp['is_pinned'] = sign # add it to memory data.replace_channels(channels) data.replace_messages(messages)
def edit_msg_in_list(msg, text): """Interate the messages list by its id, return the message after edit.""" # get the channels channels = data.return_channels() messages = data.return_messages() # deleting message from memory for i in channels: if i['channel_id'] == msg['channel_id']: for temp in i['message']: if temp['message_id'] == msg['message_id']: temp['message'] = text for temp in messages: if temp['message_id'] == msg['message_id']: temp['message'] = text # add it to memory data.replace_channels(channels) data.replace_messages(messages)
def search(token, query_str): """Search the message with the specific query_str.""" # check that token exists user = owner_from_token(token) id_from = user.get('u_id') mes_list = [] chan_list = [] channels = data.return_channels() for i in channels: for j in i['all_members']: if id_from == j['u_id']: chan_list.append(i['channel_id']) # make the query string to all_lower # this will make query case insensitive query_str = query_str.lower() # make query string to ignore whitespace query_str = "".join(query_str.split()) messages = data.return_messages() for i in messages: if i['channel_id'] in chan_list: # focus on the channels which is joinned by the user # make the message lowercase and ignoring whitespace working_msg = "".join(i['message'].lower().split()) if query_str in working_msg: added_message = { "message_id": i['message_id'], "u_id": i['u_id'], "message": i['message'], "time_created": i['time_created'], 'reacts': i['reacts'], 'is_pinned': i['is_pinned'], } mes_list.append(added_message) for msg in mes_list: msg['reacts'][0]['is_this_user_reacted'] = False if id_from in msg['reacts'][0]['u_ids']: msg['reacts'][0]['is_this_user_reacted'] = True return {'messages': mes_list}
def message_sendlater(token, channel_id, message, time_sent): """Send a message from authorised_user to the channel specified by channel_id automatically at a specified time in the future. Args: token: the token of the people who edit it. channel_id: the channel which is the target of message. message: the new message. time_sent: when the msg would be sent RETURNS: return { 'message_id': new_msg_id, } THEREFORE, TEST EVERYTHING BELOW: 1. InputError - Channel ID is not a valid channel - Message is more than 1000 characters - Time sent is a time in the past 2. AccessError - when the authorised user has not joined the channel they are trying to post to. """ # InputError 1: invalid token. auth_id = token_into_user_id(token) if auth_id == -1: raise InputError(description='invalid token.') # InputError 2: Message is more than 1000 characters. if len(message) > 1000: raise InputError(description='Message is more than 1000 characters.') # AccessError 3: invalid channel_id. channel_got = find_channel(channel_id) if channel_got is None: raise AccessError(description='invalid channel_id.') # AccessError 4: if the auth not in channel. if not find_one_in_channel(channel_got, auth_id): raise AccessError(description='auth not in channel') #Input error 5: the time is in the past # record the time rightnow now = datetime.utcnow() timestamp = int(now.replace(tzinfo=timezone.utc).timestamp()) if (time_sent < timestamp): raise InputError(description='The time is in the past') # Case 5: no error, add the message new_msg_id = 1 if len(data.return_messages()) != 0: new_msg_id = data.return_messages()[0]['message_id'] + 1 # create the message struct return_message = { 'message_id': new_msg_id, 'channel_id': channel_id, 'u_id': auth_id, 'message': message, 'time_created': time_sent, 'reacts': [{ 'react_id': 1, 'u_ids': [], 'is_this_user_reacted': False }], 'is_pinned': False } # insert the message in the top of messages in the channel. time.sleep(time_sent - timestamp) adding_message(return_message, channel_id) return { 'message_id': new_msg_id, }
def message_send(token, channel_id, message): """Send a message from authorised_user to the channel specified by channel_id. Args: token: the token of the sender. channel_id: the channel which is the target of message. message: the message we send. RETURNS: {message_id} THEREFORE, TEST EVERYTHING BELOW: 1. inputError - Message is more than 1000 characters. 2. accessError - The authorised user has not joined the channel they are trying to post to. - Cannot find the channel_id. """ # InputError 1: invalid token. auth_id = token_into_user_id(token) if auth_id == -1: raise InputError(description='invalid token.') # InputError 2: Message is more than 1000 characters. if len(message) > 1000: raise InputError(description='Message is more than 1000 characters.') # AccessError 3: invalid channel_id. channel_got = find_channel(channel_id) if channel_got is None: raise AccessError(description='invalid channel_id.') # AccessError 4: if the auth not in channel. if not find_one_in_channel(channel_got, auth_id): raise AccessError(description='auth not in channel') # Case 5: no error, add the message new_msg_id = 1 if len(data.return_messages()) != 0: new_msg_id = data.return_messages()[0]['message_id'] + 1 # record the time rightnow now = datetime.utcnow() timestamp = int(now.replace(tzinfo=timezone.utc).timestamp()) # create the message struct return_message = { 'message_id': new_msg_id, 'channel_id': channel_id, 'u_id': auth_id, 'message': message, 'time_created': timestamp, 'reacts': [{ 'react_id': 1, 'u_ids': [], 'is_this_user_reacted': False }], 'is_pinned': False } # insert the message in the top of messages in the channel. adding_message(return_message, channel_id) return { 'message_id': new_msg_id, }