def errorcheck_removeowner_v1(token, channel_id, u_id): # assumes the user must be in the channel before being added as an owner # if not, raise an input error data_structure = token_check(token) if is_valid_u_id(u_id) == False: raise InputError("Invalid u_id") if is_valid_channel_id(channel_id) == False: raise InputError("Invalid channel_id") if is_user_channel_membertype(channel_id, u_id, CHANNEL_MEMBER) == False: raise InputError( "user with user id u_id is not already a member of the channel") if is_user_channel_membertype(channel_id, u_id, CHANNEL_OWNER) == False: raise InputError( "user with user id u_id is not already a channel owner") if is_user_channel_membertype( channel_id, int(data_structure["u_id"]), CHANNEL_OWNER) == False and find_auth_user_permission_id( int(data_structure["u_id"])) == 2: raise AccessError( "auth_user is not an owner of the channel or an owner of **Dreams**" ) if is_user_only_owner(channel_id) == True: raise InputError("user to be removed is the only owner in the channel")
def errorcheck_channel_join_v2(data_structure, data, channel_id): if is_valid_channel_id(channel_id) == False: raise InputError(description="Channel ID is not a valid channel") if find_channel( data, channel_id)['is_public'] == False and find_auth_user_permission_id( int(data_structure["u_id"])) == MEMBER: raise AccessError( description="Channel ID refers to a channel that is private")
def message_sendlater_v1(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 Parameters: token - token of the sender channel_id - channel ID of the message to be sent at message - message to be sent later time_sent - time message will be sent at Returns { message_id } InputError when: Channel ID is not a valid channel Message is more than 1000 characters Time sent is a time in the past AccessError when: The authorised user has not joined the channel they are trying to post to """ # Check token if token_check(token): pass # Check if channel ID is valid if is_valid_channel_id(channel_id) is False: raise InputError("Invalid Channel ID") # Make sure authorised user is a member of the channel if is_auth_user_memberof_channel_id(token, channel_id) is False: raise AccessError(description="User is not a member of this channel") # InputError raised when empty message is sent if message == "": raise InputError(description="Cannot send an empty message") # Ensure message being sent is no more than 1000 characters if len(message) > 1000: raise InputError(description="Message is more than 1000 characters") # Find time now as a timestamp time_create_date = datetime.now().replace(microsecond=0) time_created = time_create_date.timestamp() # Check if time_sent is valid if time_sent < time_created: raise InputError(description='Time sent is invalid') # Find the difference in time between now and time_sent time_to_be_sent = time_sent - time_created # The sleep() function suspends execution of the current thread for a given number of seconds. time.sleep(time_to_be_sent) return message_send_v2(token, channel_id, message)
def errorcheck_channel_invite_v2(token, channel_id, u_id): token_check(token) if is_valid_channel_id(channel_id) == False: raise InputError( description="Channel_id does not refer to a valid channel") if is_valid_u_id(u_id) == False: raise InputError(description="u_id does not refer to a valid user") if is_auth_user_memberof_channel_id(token, channel_id) == False: raise AccessError( description= "the authorised user is not already a member of the channel")
def errorcheck_addowner_v1(token, channel_id, u_id): """ Checks the error cases for add_owner Arguments: token - jwt encoded data structure that stores auth_user info channel_id (int) - channel to be added into u_id (int) - user to be checked Exceptions: Refer to addowner function Return values: None """ data_structure = token_check(token) if is_valid_u_id(u_id) == False: raise InputError("u_id does not refer to a valid user") if is_valid_channel_id(channel_id) == False: raise InputError("Channel ID is not a valid channel") if is_user_channel_membertype(channel_id, u_id, CHANNEL_MEMBER) == False: raise InputError( "user with user id u_id is not already a member of the channel") if is_user_channel_membertype(channel_id, u_id, CHANNEL_OWNER) == True: raise InputError( "user with user id u_id is already an owner of the channel") if is_user_channel_membertype( channel_id, int(data_structure["u_id"]), CHANNEL_OWNER) == False and find_auth_user_permission_id( int(data_structure["u_id"])) == 2: raise AccessError( "the authorised user is not an owner of the Dreams or an owner of this channel" )
def errorcheck_channel_details_v2(token, channel_id): """ Checks the error cases for channel_details_v2 Arguments: token (string) - jwt encoded data structure of auth_user with session_id channel_id (int) - the channel to check for the u_id Exceptions: Refer to channel_details function Returns Value: N/A """ token_check(token) if is_valid_channel_id(channel_id) == False: raise InputError(description="Channel ID is not a valid channel") if is_auth_user_in_channel(token, channel_id) == False: raise AccessError( description= "Authorised user is not a member of channel with channel_id")
def message_share_v1(token, og_message_id, message, channel_id, dm_id): """ og_message_id is the original message. channel_id is the channel that the message is being shared to, and is -1 if it is being sent to a DM. dm_id is the DM that the message is being shared to, and is -1 if it is being sent to a channel. message is the optional message in addition to the shared message, and will be an empty string "" if no message is given Parameters: token - token of the user sharing message og_message_id - message id of the original message message - optional message to be commented with shared message channel_id - channel_id of the channel msg is being shared to (default -1) dm_id - dm_id of the dm group that message is being sent to (default -1) Returns: { shared_message-id } InputError when: Optional message is more than 1000 characters (Assumption) AccessError when: Invalid token The authorised user has not joined the channel or DM they are trying to share the message to """ time_stamp = datetime.now().replace(microsecond=0).timestamp() with open("data.json") as json_file: data = load(json_file) # Decode token if token is valid user = token_check(token) optional_msg = message # Input Error when optional message is more than 1000 chars if len(optional_msg) > 1000: raise InputError( description="Your optional message is more than 1000 characters") # Let the channel/dm not being shared to be -1 not_shared = -1 # If channel_id and dm_id are both given # user should give either a channel_id or dm_id if channel_id is not not_shared and dm_id is not not_shared: raise InputError( description= "Both channel_id and dm_id is given, choose one or the other") # Both channel_id and dm_id are given as -1 elif channel_id is not_shared and dm_id is not_shared: raise InputError( description="Both channel_id and dm_id are given as -1") # ----SHARING TO CHANNEL---- # If channel_id is given and dm_id is -1 elif channel_id is not not_shared and dm_id is not_shared: user_stats_update(data, user["u_id"], stat_types.MSG_SEND, time_stamp) dreams_stats_update(data, stat_types.MSG_SEND, time_stamp) # Check if channel_id given is valid if is_valid_channel_id(channel_id) is False: raise InputError(description="Channel id not found") # Check if the authorised user is a member of the channel if is_auth_user_memberof_channel_id(token, channel_id) is False: raise AccessError( description="Authorised use is not a member of the channel") msg_found = False # Loop through channel data to look for message_id for ch in data["channels"]: # Loop through messages of channel for msg in ch["messages"]: # if og_message_id is found if msg["message_id"] == og_message_id: msg_found = True break # The message wasn"t found in channel # Loop through the DM messages and check if it is there for dm in data["dms"]: # Loop through messages of dms for dm_message in dm["messages"]: # If message_id is found if dm_message["message_id"] == og_message_id: msg_found = True # If the message is found, assign string of shared message if msg_found: og_message = msg["message"] else: raise InputError(description="message id could not be found") # Find the channel for channel id given channel = find_channel(data, channel_id) # Loop through to the channel given and return specified channel dictionary all_msgs = channel["messages"] # Keep track of last message id last_msg_id = data.get("last_message_id", 0) message_id = last_msg_id + 1 # Update data data["last_message_id"] = message_id # unix timestamp time_create_date = datetime.now().replace(microsecond=0) time_created = time_create_date.timestamp() # New message struct new_message = { "message_id": message_id, "u_id": user["u_id"], # E.g. Hi guys, look at this ----|funny cat meme|---- "message": optional_msg + " " + "----|" + og_message + "|----", "time_created": time_created, 'reacts': [{ 'react_id': 0, 'u_ids': [], 'is_this_user_reacted': False }], 'react_history': [], 'is_pinned': False, } # Append the new message to the end of the list all_msgs.append(new_message) # ----SHARING TO DM---- # If channel is -1 and dm_id is given elif channel_id is not_shared and dm_id is not not_shared: user_stats_update(data, user["u_id"], stat_types.MSG_SEND, time_stamp) dreams_stats_update(data, stat_types.MSG_SEND, time_stamp) # Check if dm_id given is valid is_dm_exist = False for dm_data in data["dms"]: if dm_data["dm_id"] == dm_id: # Assign dm dictionary of given dm_id dm = dm_data is_dm_exist = True break if is_dm_exist is False: raise InputError("DM ID is not a valid DM") # Check if user is a member of dm group is_auth_user_member = False if user["u_id"] in dm["members"]: is_auth_user_member = True if is_auth_user_member is False: raise AccessError( description= "Authorised user is not a member of this DM with dm_id") msg_found = False # Loop through channel data to look for message_id for ch in data["channels"]: # Loop through messages of channel for msg in ch["messages"]: # if og_message_id is found if msg["message_id"] == og_message_id: msg_found = True # The message wasn"t found in channel # Loop through the DM messages and check if it is there for dm in data["dms"]: # Loop through messages of dms for dm_message in dm["messages"]: # If message_id is found if dm_message["message_id"] == og_message_id: msg_found = True break # If the message is found, assign string of shared message if msg_found: og_message = dm_message["message"] else: raise InputError(description="message id could not be found") # Keep track of last message id last_msg_id = data["last_message_id"] message_id = last_msg_id + 1 # Update data data["last_message_id"] = message_id # unix timestamp time_create_date = datetime.now().replace(microsecond=0) time_created = time_create_date.timestamp() new_message = { "message_id": message_id, "u_id": user["u_id"], "message": message + " " + "----|" + og_message + "|----", "time_created": time_created, 'reacts': [{ 'react_id': 0, 'u_ids': [], 'is_this_user_reacted': False }], 'react_history': [], 'is_pinned': False, } all_msgs = dm["messages"] # Append the new message to the end of the list all_msgs.append(new_message) with open("data.json", "w") as json_file: dump(data, json_file, indent=4) return {"shared_message_id": message_id}
def message_send_v2(token, channel_id, message): """ Send a message from authorised_user to the channel specified by channel_id. Note: Each message should have it's own unique ID. I.E. No messages should share an ID with another message, even if that other message is in a different channel. InputError when: Message is more than 1000 characters. Channel ID is not valid. AccessError when: The authorised user has not joined the channel they are trying to post to. Invalid token is passed. Parameters: token - token of the user, channel_id - channel id in which message is being sent to, message - message that is being sent Returns: { message_id } """ time_created = datetime.now().replace(microsecond=0).timestamp() with open("data.json") as json_file: data = load(json_file) # Check token is valid, if so, assign variable to decode u_id user = token_check(token) # Check channel ID given is valid if is_valid_channel_id(channel_id) is False: raise InputError("Invalid Channel ID") # Make sure authorised user is a member of the channel if is_auth_user_memberof_channel_id(token, channel_id) is False: raise AccessError(description="User is not a member of this channel") # InputError raised when empty message is sent if message == "": raise InputError(description="Cannot send an empty message") # Ensure message being sent is no more than 1000 characters if len(message) > 1000: raise InputError(description="Message is more than 1000 characters") # Loop through to the channel given and return specified channel dictionary for channel in data["channels"]: if channel["channel_id"] == channel_id: found_channel = channel all_msgs = found_channel["messages"] # Keep track of last message id last_msg_id = data.get("last_message_id", 0) message_id = last_msg_id + 1 # Update data data["last_message_id"] = message_id # unix timestamp user_stats_update(data, user["u_id"], stat_types.MSG_SEND, time_created) dreams_stats_update(data, stat_types.MSG_SEND, time_created) new_message = { "message_id": message_id, "u_id": int(user["u_id"]), "message": message, "time_created": time_created, "reacts": [{ 'react_id': 0, 'u_ids': [], 'is_this_user_reacted': False }], "react_history": [], "is_pinned": False, } # Append the new message to the end of the list all_msgs.append(new_message) with open("data.json", "w") as json_file: dump(data, json_file, indent=4) if message == "/u start": unscramble_play(word_banks.DEFAULT, found_channel["channel_id"]) elif message == "/u simple": unscramble_play(word_banks.SIMPLE, found_channel["channel_id"]) elif message == "/u leaderboard": unscramble_leaderboard(found_channel["channel_id"]) # i.e. the message is one singular word elif len(message.split(" ")) == 1: unscramble_listen(int(user["u_id"]), found_channel["channel_id"], message) return { "message_id": message_id, }
def channel_messages_v2(token, channel_id, start): # Given a Channel with ID channel_id that the authorised user is part of, return up to 50 # messages between index "start" and "start + 50". Message with index 0 is the most recent # message in the channel. This function returns a new index "end" which is the value of "start + 50", # or, if this function has returned the least recent messages in the channel, returns -1 # in "end" to indicate there are no more messages to load after this return. ''' Parameters: (token, channel_id, start) Return Type: {'messages', 'start': 0, 'end': 50 } 'messages': [ { 'message_id': int, 'u_id': int, 'message': string, 'time_created': datetime, 'channel_id': int, } ] InputError when: Channel ID is not a valid channel. Start is greater than the total number of messages in the channel. AccessError when: Authorised user is not a member of channel with channel_id. ''' with open("data.json") as json_file: data = load(json_file) # Check if token is valid and decode it user = token_check(token) # Access the channel dictionary for given channel id and # Check if channel id given is valid if is_valid_channel_id(channel_id) is False: raise InputError("Invalid channel ID") # Check if the user is a member of the channel if is_auth_user_memberof_channel_id(token, channel_id) is False: raise AccessError(description="User is not a member of this channel") # Find the channel given channel_get = find_channel(data, channel_id) # Reverse the list to receive newest to oldest messages all_msgs = channel_get['messages'] all_msgs.reverse() # If start index given is less than 0 or start value is greater # than the number of total messages in the channel if start < 0 or start > len(all_msgs): raise InputError( description= 'Invalid start value/ Start value is greater than existing total messages' ) # Create a list of messages to be returned return_messages = [] # No messages in specified channel if all_msgs == []: return {'messages': [], 'start': start, 'end': -1} end = -1 # Total number of messages in channel is more than 50 if len(all_msgs) > (start + 50): return_messages = all_msgs[start:start + 50] end = start + 50 else: return_messages = all_msgs[start:] for msgs in return_messages: for reactions in msgs['reacts']: reactions['is_this_user_reacted'] = user['u_id'] in reactions[ 'u_ids'] with open("data.json", "w") as json_file: dump(data, json_file, indent=4) return {'messages': return_messages, 'start': start, 'end': end}
def errorcheck_leave_v2(token, channel_id): if is_valid_channel_id(channel_id) is False: raise InputError('Channel ID is not valid') if is_auth_user_memberof_channel_id(token, channel_id) is False: raise AccessError('User is not a member of channel')