def dm_leave_v1(token, dm_id): ''' Summary: Given a DM ID, the user is removed as a member of this DM Args: token (string): A user session token dm_id (int): A dm_id number Returns: a empty dictionary Raises: InputError when: dm_id is not a valid DM AccessError when: Authorised user is not a member of DM with dm_id ''' global users, channels, dms assert check_token(token) auth_user = user_from_token(token) if valid_dm(dm_id) == False: raise InputError(f"dm_id is not a valid DM") current_dm = find_dm(dm_id) #find the current dm if auth_user['u_id'] not in current_dm['members']: raise AccessError(f"Authorised user is not a member of DM with dm_id") # Remove dm from user's list of dms auth_user['dms'].remove(dm_id) update_user_dm_stats(auth_user['u_id']) # Remove user from dm's members list current_dm['members'].remove(auth_user['u_id']) return {}
def search_v2(token, query_str): """Summary Given a query string, return a collection of messages in all of the channels/DMs that the user has joined that match the query Args: token (string): A user session token query_str (string): a string for search message Returns: Dictionary: { messages } ({ message_id, u_id, message, time_created }) Contains the imformation of messages Raises: InputError: query_str is above 1000 characters """ query_str = query_str.lower() # InputError: query_str is above 1000 characters if len(query_str) > 1000: raise InputError("query_str is above 1000 characters") check_token(token) auth_user_id = user_from_token(token)['u_id'] output = [] # search all the messages for message in messages: user_is_member = False # if this message is send to the channel, find the channel if message['channel_id'] != -1: channel = find_channel(message['channel_id']) user_is_member = (auth_user_id in channel['all_members'] and query_str in message['message'].lower()) # if this message is send to the dm, find the dm else: dm = find_dm(message['dm_id']) user_is_member = (auth_user_id in dm['members'] and query_str in message['message'].lower()) # list all the detail of this messagen, and append it to the putput if user_is_member: react_dict = { 'react_id': 1, 'u_ids': message['reacts'], 'is_this_user_reacted': auth_user_id in message['reacts'] } message_detail = { 'message_id': message['message_id'], 'u_id': message['author_id'], 'message': message['message'], 'time_created': message['time_created'], 'reacts': [react_dict], 'is_pinned': message['is_pinned'] } output.append(message_detail) return {'messages': output}
def sendlaterdm_handler(message_id, time_sent): global messages, future_messages time_sent_dt = datetime.utcfromtimestamp(time_sent) now_dt = datetime.utcnow() duration = (time_sent_dt - now_dt).total_seconds() sleep(duration) for msg in list(future_messages): if msg['message_id'] == message_id: messages.append(msg) target_dm = find_dm(msg['dm_id']) target_dm['messages'].append(message_id) future_messages.remove(msg)
def message_pin_v1(token, message_id): '''Summary Given a message within a channel or DM, mark it as "pinned" to be given special display treatment by the frontend Args: token (string): A user session token message_id (int): A message_id of the message Returns: A empty dictionary Raises: Raises: InputError when: message_id is not a valid message Message with ID message_id is already pinned AccessError when: The authorised user is not a member of the channel The authorised user is not a member of the DM that the message is within The authorised user is not an owner of the channel or DM ''' check_token(token) auth_user = user_from_token(token) if valid_message(message_id) == False: raise InputError("message_id is not a valid message") message = get_message_from_message_id(message_id) if message['is_pinned'] == True: raise InputError("Message with ID message_id is already pinned") if message['channel_id'] == -1: #this means this is a dm message current_dm = find_dm(message['dm_id']) if auth_user['u_id'] not in current_dm['members']: raise AccessError("The authorised user is not a member of the DM") if auth_user['u_id'] != current_dm[ 'creator']: # this error will get raised twice is this ok? / this is kinda redundent because if u have to be the owner to pin whats the point of checking for members? raise AccessError("The authorised user is not an owner of the DM") message['is_pinned'] = True if message['dm_id'] == -1: # this means this a channel message current_channel = find_channel(message['channel_id']) if auth_user['u_id'] not in current_channel['all_members']: raise AccessError( "The authorised user is not a member of the channel") if auth_user['u_id'] not in current_channel['owner_members']: raise AccessError( "The authorised user is not an owner of the channel") message['is_pinned'] = True return {}
def message_unpin_v1(token, message_id): ''' Summary: Given a message within a channel or DM, remove it's mark as unpinned Args: token (string): A user session token message_id (int): A message_id number Returns: empty dictionary Raises: InputError when: message_id is not a valid message Message with ID message_id is already unpinned AccessError when: The authorised user is not a member of the channel or DM that the message is within The authorised user is not an owner of the channel or DM ''' check_token(token) auth_user = user_from_token(token) if valid_message(message_id) == False: raise InputError("message_id is not a valid message") message = get_message_from_message_id(message_id) if message['is_pinned'] == False: raise InputError("Message with ID message_id is already unpinned") if message['channel_id'] == -1: #this means this is a dm message current_dm = find_dm(message['dm_id']) if auth_user['u_id'] not in current_dm['members']: raise AccessError("The authorised user is not a member of the DM") if auth_user['u_id'] != current_dm['creator']: raise AccessError("The authorised user is not an owner of the DM") message['is_pinned'] = False if message['dm_id'] == -1: # this means this a channel message current_channel = find_channel(message['channel_id']) if auth_user['u_id'] not in current_channel['all_members']: raise AccessError( "The authorised user is not a member of the channel") if auth_user['u_id'] not in current_channel['owner_members']: raise AccessError( "The authorised user is not an owner of the channel") message['is_pinned'] = False return {}
def message_sendlaterdm_v1(token, dm_id, message, time_sent): """Summary Send a message from authorised_user to the dm specified by dm_id automatically at a specified time in the future Args: token (string): A user session token dm_id (int): A dm id number which the message will be sented to message (string): The text with which the user wishes to sent to dm time_sent (int): the time of when this message will be sent Returns: Dictionary: { message_id } the new message Raises: AccessError: the authorised user has not joined the dm they are trying to post to InputError: dm ID is not a valid dm Message is more than 1000 characters Time sent is a time in the past """ # current_time = current_unix_timestamp() # time_intervel = time_sent - current_time # # InputError: Time sent is a time in the past # if time_intervel < 0: # raise InputError("Time sent is a time in the past") # time.sleep(time_intervel) # message_id = message_senddm_v1(token, dm_id, message) # return { 'message_id': message_id } global users, dms, next_message_id, messages assert check_token(token) # InputError: dm ID is not a valid dm if valid_dm(dm_id) == False: raise InputError(f"DM ID {dm_id} is not a valid dm") # InputError: Message is more than 1000 characters if len(message) > 1000: raise InputError(f"Message {message} is more than 1000 characters") current_time = current_unix_timestamp() time_intervel = time_sent - current_time # InputError: Time sent is a time in the past if time_intervel < 0: raise InputError("Time sent is a time in the past") auth_user = user_from_token(token) current_dm = find_dm(dm_id) #current dm with the given dm id if auth_user['u_id'] not in current_dm['members']: raise AccessError( f"the authorised user has not joined the dm they are trying to post to" ) message_id = next_message_id['id'] next_message_id['id'] += 1 future_message = {} future_message['message_id'] = message_id #message_id future_message['dm_id'] = dm_id future_message[ 'channel_id'] = -1 #this is a dm message not dm so for all dm's give -1 future_message['author_id'] = auth_user['u_id'] future_message['message'] = message future_message['reacts'] = [] future_message['time_created'] = time_sent future_message['is_pinned'] = False future_messages.append(future_message) return { 'message_id': future_message['message_id'] #message_id }