def message_unreact(token, message_id, react_id): ''' This function unreacts to a message ''' curr_user_id = token_to_user(token) message = get_msg_dict(message_id) channel = msg_to_channel(message_id) # check if the message_id is valid if message_id >= server_data.data["n_messages"]: raise ValueError("message_id is invalid") is_msg_removed(message_id) # check if the user is valid check_valid_user(curr_user_id) # check if the channel is valid check_valid_channel(channel["channel_id"]) # check if the user is in the channel if not check_channel_member(curr_user_id, channel["channel_id"]): raise AccessError("User is not a member of the channel") # check if the react_id is valid, only 1 in iteration 2 react_id_list = [1] if react_id not in react_id_list: raise ValueError("React ID is not valid") # check if the message already has the react_id # check if the user has reacted to that message present_flag = 0 for react_dict in message['reacts']: if react_dict['react_id'] == react_id: present_flag = 1 if curr_user_id in react_dict['u_ids']: # the current user has reacted react_dict['u_ids'].remove(curr_user_id) if len(react_dict['u_ids']) == 0: # remove react entirely if no more members del react_dict if present_flag == 0: raise ValueError("Message does not have that react") return {}
def message_react(token, message_id, react_id): ''' This function reacts to a message ''' curr_user_id = token_to_user(token) message = get_msg_dict(message_id) channel = msg_to_channel(message_id) # check if the message_id is valid if message_id >= server_data.data["n_messages"]: raise ValueError("message_id is invalid") is_msg_removed(message_id) # check if the user is valid check_valid_user(curr_user_id) # check if the channel is valid check_valid_channel(channel["channel_id"]) # check if the user is in the channel if not check_channel_member(curr_user_id, channel["channel_id"]): raise AccessError("User is not a member of the channel") # check if the react_id is valid, only 1 in iteration 2 react_id_list = [1] if react_id not in react_id_list: raise ValueError("React ID is not valid") # if the message already has the react id for react in message["reacts"]: if react['react_id'] == react_id: # check if the current user has already reacted if curr_user_id in react['u_ids']: raise ValueError("Message already reacted by current user") # react to the message! react['u_ids'].append(curr_user_id) return {} # otherwise, the message has not been reacted too message['reacts'].append({'react_id': react_id, 'u_ids': [curr_user_id]}) return {}
def message_edit(token, message_id, message): ''' The following function edits a message ''' curr_user_id = token_to_user(token) check_valid_user(curr_user_id) # if message too long / not a string is_valid_message(message) message_dict = get_msg_dict(message_id) # check if the message exists is_msg_removed(message_id) # Check if message is the same msg_str = (get_msg_dict(message_id))['message'] if msg_str == message: raise ValueError(f"Message is the same") channel = msg_to_channel(message_id) check_channel_member(curr_user_id, channel['channel_id']) if message_dict['u_id'] == curr_user_id: pass elif is_slackr_admin(curr_user_id): pass elif is_owner(curr_user_id, channel['channel_id']): pass else: raise AccessError('User does not have the right permission') if message == "": message_remove(token, message_id) else: message_dict['message'] = message return {}
def message_pin(token, message_id): ''' The following function pins a message ''' curr_user_id = token_to_user(token) message = get_msg_dict(message_id) channel = msg_to_channel(message_id) # check if the message_id is valid if message_id >= server_data.data["n_messages"]: raise ValueError("message_id is invalid") is_msg_removed(int(message_id)) # check if the user is valid check_valid_user(curr_user_id) # check if the channel is valid check_valid_channel(channel["channel_id"]) # check if the user is in the channel if not check_channel_member(curr_user_id, channel["channel_id"]): raise AccessError("User is not a member of the channel") # User must be a admin of either the channel or slackr check_permission = server_data.data["users"][curr_user_id]["permission"] check_isowner = is_owner(curr_user_id, channel["channel_id"]) if (not check_isowner) and check_permission == 3: raise ValueError("User is not authorised for this action") # check if the message is already pinned if message["is_pinned"]: raise ValueError("Message is already pinned") message["is_pinned"] = True return {}
def message_remove(token, message_id): ''' The following function removes a message @@ can change to remove completely ''' curr_user_id = token_to_user(token) check_valid_user(curr_user_id) message = get_msg_dict(message_id) # check if the message exists is_msg_removed(message_id) channel = msg_to_channel(message_id) check_channel_member(curr_user_id, channel['channel_id']) # if the message was sent by the user if message['u_id'] == curr_user_id: pass # if the current user is a owner of the channel elif is_slackr_admin(curr_user_id): pass elif is_owner(curr_user_id, channel['channel_id']): pass else: raise AccessError('User does not have the right permission') # decrease the channel_n_messages and n_messages channel["channel_n_messages"] -= 1 # deleting message for i in range(len(channel['messages'])): if channel['messages'][i]['message_id'] == int(message_id): del channel['messages'][i] break return {}