def standup_send(token, channel_id, message): """ Send a message to get buffered in the standup queue, assuming a standup is currently active. Parameters: token (str): Authorisation hash for user channel_id (int): Channels' channel ID message (str): Message from userz Returns: {} (dict): Empty dictionary """ if not isinstance(token, str): raise AccessError('Token is invalid') if auth.check_token_exists(token): auth_user = get_user_id(token) else: raise AccessError("User does not exist.") if not valid_channel_id(token, channel_id): raise InputError('Channel ID is not a valid channel') if len(message) > 1000: raise InputError("The message is longer than 1000 characters") channel_data = get_channel(channel_id) if channel_data["is_active"] is False: raise InputError( 'An active standup is not currently running in this channel') auth_id = get_user_id(token) if not verify_user_member(channel_id, auth_id): raise AccessError('Authorised user is not a member of the channel') usr = get_user_profile(auth_user) handle = usr["handle_str"] global msg msg += f"{handle}: {message}\n" return {}
def standup_start(token, channel_id, length): """ For a given channel, start the standup period whereby for the next "length" seconds if someone calls "standup_send" with a message, it is buffered during the X second window then at the end of the X second window a message will be added to the message queue in the channel from the user who started the standup. X is an integer that denotes the number of seconds that the standup occurs for. Parameters: token (str): Authorisation hash for user channel_id (int): Channels' channel ID length (int): Number of seconds Returns: time_finish (int): Time standup finishes - Unix timestamp """ if not isinstance(token, str): raise AccessError('Token is invalid') if not auth.check_token_exists(token): raise AccessError("User does not exist.") if not valid_channel_id(token, channel_id): raise InputError('Channel ID is not a valid channel') channel_data = get_channel(channel_id) if channel_data["is_active"]: raise InputError( 'An active standup is currently running in this channel') else: channel_data["is_active"] = True current_time = int(math.floor(time.time())) time_finish = current_time + length channel_data["time_finish"] = time_finish t = threading.Timer(length, reset_and_delay_messages, [channel_data, token]) t.start() return {'time_finish': time_finish}
def user_profile(token, u_id): """For a valid user, returns information about their u_id, email, first name, last name and handle """ if not isinstance(token, str): raise AccessError('Token is invalid') if not check_uid_exists(u_id): raise InputError("User does not exist") if not auth.check_token_exists(token): raise AccessError("Token is invalid.") # check if token matches u_id beforehand # if not check_matching_token_and_id(token, u_id): # raise InputError("Token is not matching") #step 2: return email, first name, last name and handle if valid return { "user": dict(list(data["users"][u_id].items())[:6]), }
def message_remove(token, message_id): """ Given a message_id for a message, this message is removed from the channel. Parameters: token (str): Authorisation hash for user message_id (int): Messages' message ID Returns: {} (dict): Empty dictionary """ message_list = list( filter(lambda msg: msg["message_id"] == message_id, data["messages"])) if not isinstance(token, str): raise AccessError('Token is invalid') if len(message_list) == 0: raise InputError("Message does not exist") message_list = message_list[0] channel_id = message_list["channel_id"] channel_data = get_channel(channel_id) if auth.check_token_exists(token): auth_user = get_user_id(token) else: raise AccessError("User does not exist.") list_of_auth_users = data["permissions"][0]["users"] is_owner = auth_user in channel_data["owner_members"] + list_of_auth_users is_auth = get_token_auth(token, message_id) if not is_owner and not is_auth: raise AccessError("You are not authorised to remove this message") data["messages"].remove(message_list) return {}
def message_edit(token, message_id, message): """ Given a message, update it's text with new text. If the new message is an empty string, the message is deleted. Parameters: token (str): Authorisation hash for user message_id (int): Messages' message ID message (str): Message from user Returns: {} (dict): Empty dictionary """ message_list = list( filter(lambda msg: msg["message_id"] == message_id, data["messages"])) if not isinstance(token, str): raise AccessError('Token is invalid') if len(message_list) == 0: raise InputError("Message does not exist") message_list = message_list[0] channel_id = message_list["channel_id"] channel_data = get_channel(channel_id) if auth.check_token_exists(token): auth_user = get_user_id(token) else: raise AccessError("User does not exist.") list_of_auth_users = data["permissions"][0]["users"] is_owner = auth_user in channel_data["owner_members"] + list_of_auth_users is_auth = get_token_auth(token, message_id) if not is_owner and not is_auth: raise AccessError("You are not authorised to edit this message") message_list["message"] = message return {}
def channels_listall(token): """ Provies a list of all channels and their associated details. Parameters: token (str): Authorisation hash for user Returns: data['channels'] (dict): A dictionary containing a list of channels """ if not isinstance(token, (bytes, str)): raise AccessError('Token is invalid') if not auth.check_token_exists(token): raise AccessError('Token does not exist') # dict(itertools.islice(data["channels"][0].items(), 2)) # channels_list = data['channels'].copy() # Need case for owner of flockr channels_list = list( map(lambda d: dict(itertools.islice(d.items(), 2)), data["channels"])) return {"channels": channels_list}
def message_react(token, message_id, react_id): """ Given a message within a channel the authorised user is part of, add a "react" to that particular message. Parameters: token (str): Authorisation hash for user message_id (int): Messages' message ID react_id (int): Reacts' react ID Returns: {} (dict): Empty dictionary """ if not isinstance(token, str): raise AccessError('Token is invalid') # Input Errors: # Raise Input Error if message_id is invalid message_list = message_reaction_checks(message_id, react_id) # 2. React_id is not a valid react --> only 1 if react_id != 1: raise InputError("Not a valid react") if auth.check_token_exists(token): auth_user = get_user_id(token) else: raise AccessError("User does not exist.") list_of_reactors = message_list[0]["reacts"][0]["u_ids"] for user in list_of_reactors: if auth_user == user: raise InputError("This message already contains a valid react") message_list[0]["reacts"][0]["u_ids"].append(auth_user) if message_list[0]["reacts"][0]["react_id"] == 0: message_list[0]["reacts"][0]["react_id"] = react_id message_list = list( filter(lambda msg: msg["message_id"] == message_id, data["messages"])) # if message_list[0]["u_id"] in message_list[0]["reacts"][0]["u_ids"]: # message_list[0]["reacts"][0]["is_this_user_reacted"] = True return {}
def message_unreact(token, message_id, react_id): """ Given a message within a channel the authorised user is part of, remove a "react" to that particular message. Parameters: token (str): Authorisation hash for user message_id (int): Messages' message ID react_id (int): Reacts' react ID Returns: {} (dict): Empty dictionary """ if not isinstance(token, str): raise AccessError('Token is invalid') # ESSENTIALLY SAME AS UNREACT BUT CHANGING VALID REACT TO 0 INSTEAD OF 1 message_list = message_reaction_checks(message_id, react_id) # 2. React_id is not a valid react --> only 0 if react_id != 1: raise InputError("Not a valid react") if auth.check_token_exists(token): auth_user = get_user_id(token) else: raise AccessError("User does not exist.") if message_list[0]["reacts"][0]["react_id"] == 0: raise InputError("Message does not contain an active react") list_of_reactors = message_list[0]["reacts"][0]["u_ids"] if auth_user not in list_of_reactors: raise InputError("User has not reacted to this message") message_list[0]["reacts"][0]["u_ids"].remove(auth_user) message_list = list( filter(lambda msg: msg["message_id"] == message_id, data["messages"])) if message_list[0]["reacts"][0]["u_ids"] == []: message_list[0]["reacts"][0]["react_id"] = 0 # if message_list[0]["u_id"] not in message_list[0]["reacts"][0]["u_ids"]: # message_list[0]["reacts"][0]["is_this_user_reacted"] = False return {}
def user_profile_setemail(token, email): """Update the authorised user's email address""" # Check token is valid. if not isinstance(token, str): raise AccessError('Token is invalid') if not auth.check_token_exists(token): raise AccessError("Invalid token") # Check valid email. if not auth.check_valid_email(email): raise InputError("This is not a valid email") # Check email exists. if auth.check_element_exists(search_type="email", element=email): raise InputError("This email already exists") # Retrieve user_id user_id = get_user_id(token) # Retrieve user. user = get_user_profile(user_id) # Set new email. user["email"] = email return {}
def message_pin(token, message_id): """ Given a message within a channel, mark it as "pinned" to be given special display treatment by the frontend. Parameters: token (str): Authorisation hash for user message_id (int): Messages' message ID Returns: {} (dict): Empty dictionary """ if not isinstance(token, str): raise AccessError('Token is invalid') # Input Errors: # 1. Message_id is not a valid message message_list = list(filter(lambda msg: msg["message_id"] == message_id, \ data["messages"])) if len(message_list) == 0: raise InputError("Message does not exist") # 2. Message ID is already pinned if message_list[0]["is_pinned"] is True: raise InputError("Message already pinned") if auth.check_token_exists(token): auth_user = get_user_id(token) else: raise AccessError("User does not exist.") # Access Errors: # 1. Unauthorised user channel_id = message_list[0]["channel_id"] channel_data = get_channel(channel_id) if auth_user not in channel_data["owner_members"]: raise AccessError("User is not authorised") message_list[0]["is_pinned"] = True return {}
def user_uploadphoto_http(): """Uploads photo for the user's profile""" if request.is_json: req_data = request.get_json() token = req_data["token"] if not check_token_exists(token): return "Invalid Token passed", 400 image_response = requests.get(req_data["img_url"], stream=True) if image_response.status_code == 200: # File Type response = requests.get(req_data["img_url"]) filetype = response.headers["Content-Type"].split("image/")[-1] # File Type must be jpg if filetype != 'jpeg': return "Image uploaded is not a JPG", 400 img = Image.open(image_response.raw) x1 = int(req_data["x_start"]) y1 = int(req_data["y_start"]) x2 = int(req_data["x_end"]) y2 = int(req_data["y_end"]) if x2 - x1 >= 0 and y2 - y1 >= 0 \ and x1 >= 0 and y1 >= 0 \ and x2 <= img.width \ and y2 <= img.height: img = img.crop((x1, y1, x2, y2)) min_bound = 10 letters_and_digits = ascii_letters + digits # Randomly generates an image filename. img_name = "".join( random.choice(letters_and_digits) for c in range(min_bound)) file_collection = database.data["file_names"] if len(file_collection) > 0: while any( list( map(lambda i: i["file_name"] == img_name, file_collection))): img_name = "".join( random.choice(letters_and_digits) for c in range(random.randint(min_bound, 20))) file_collection.append({"file_name": img_name}) # req_data["img_url"] user_id = get_user_id(token) user_profile = get_user_profile(user_id) url_root = request.url_root # print(type(url_root)) # print(url_root) user_profile[ "profile_img_url"] = url_root + "imgurl/" + img_name + ".jpg" # user_profile["profile_img_url"] = "http://localhost:5000/" + "imgurl/" + img_name + ".jpg" cur_path = os.getcwd() img.save(f"{cur_path}/src/static/profiles/{img_name}.jpg") return {}, 200 else: return "any of x_start, y_start, x_end y_end, \ are not within the dimensions of the image at the URL.", 404 else: return "img_url returns an HTTP status other than 200.", 400 return {}, 200 return "JSON not received", 400