def channel_removeowner(token, channel_id, u_id): """ Remove user with user ID 'u_id' as an owner of this channel. Parameters: token (str): Authorisation hash for the user channel_id (int): Channels' channel ID u_id (int): Users' user ID Returns: {} (dict): Empty dictionary """ if not isinstance(token, str): raise AccessError('Token is invalid') if not valid_channel_id(token, channel_id): raise InputError('Channel ID is not a valid channel') owner_id = get_user_id(token) channel_data = get_channel(channel_id) if not u_id in channel_data["owner_members"]: raise InputError('User is not an owner of the channel') list_of_auth_users = data["permissions"][0]["users"] if owner_id not in channel_data["owner_members"] + list_of_auth_users: raise AccessError( 'User is not an owner of the channel or an owner of the flockr') channel_data["owner_members"].remove(u_id) channel_data["all_members"].append(u_id) channel_data = get_channel(channel_id) return {}
def channel_join(token, channel_id): """ Given a channel ID of a channel that the authorised user can join, adds them to the channel. Parameters: token (str): Authorisation hash for the user channel_id (int): Channels' channel ID Returns: {} (dict): Empty dictionary """ if not isinstance(token, str): raise AccessError('Token is invalid') # Check Token exists. if not valid_channel_id(token, channel_id): raise InputError('Channel ID is not a valid channel') u_id = get_user_id(token) channel_data = get_channel(channel_id) if verify_user_member(channel_id, u_id): raise InputError("User already exists in channel.") list_of_auth_users = data["permissions"][0]["users"] # If channel not public and user not authorised. if not channel_data["public"] and (u_id not in list_of_auth_users): raise AccessError("Channel is private and user is not a global owner") channel_data["all_members"].append(u_id) return {}
def message_unpin(token, message_id): """ Given a pinned message within a channel, remove its mark as pinned. 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 False: raise InputError("Message already unpinned") 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"] = False return {}
def channel_leave(token, channel_id): """ Given a channel ID, the user is removed as a member of this channel. Parameters: token (str): Authorisation hash for the user channel_id (int): Channels' channel ID Returns: {} (dict): Empty dictionary """ if not isinstance(token, str): raise AccessError('Token is invalid') if not valid_channel_id(token, channel_id): raise InputError('Channel ID is not a valid channel') channel_data = get_channel(channel_id) user_id = get_user_id(token) result_access = user_id in channel_data["all_members"] + channel_data["owner_members"] and \ channel_data["channel_id"] == channel_id if not result_access: raise AccessError('User is not a member of the channel') # If member of channel wants to leave we remove it. Otherwise we remove a owner member. if user_id in channel_data["all_members"]: channel_data["all_members"].remove(user_id) else: # Remove user_id for owner. channel_data["owner_members"].remove(user_id) return {}
def standup_active(token, channel_id): """ For a given channel, return whether a standup is active in it, and what time the standup finishes. If no standup is active, then time_finish returns None. Parameters: token (str): Authorisation hash for user channel_id (int): Channels' channel ID Returns: is_active (bool): Whether or not a standup is active 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) return { 'is_active': channel_data["is_active"], 'time_finish': channel_data["time_finish"], }
def message_send(token, channel_id, message): #add error checking for valid channel id? """ Send a message from authorised_user to the channel specified by channel_id. Parameters: token (str): Authorisation hash for user channel_id (int): Channels' channel ID message (str): Message from user Returns: {} (dict): Empty dictionary """ if not isinstance(token, str): raise AccessError('Token is invalid') if len(message) > 1000: raise InputError("The message is longer than 1000 characters") auth_user = get_user_id(token) channel_data = get_channel(channel_id) is_auth = auth_user not in channel_data["owner_members"] + channel_data[ "all_members"] if is_auth: raise AccessError('User is not a member of the channel') max_msg_id = len(data["messages"]) current_date = datetime.today() react_list = [] data["messages"].append({ "message_id": max_msg_id + 1, "u_id": get_user_id(token), "message": message, "time_created": int(time.mktime(current_date.timetuple())), "channel_id": channel_id, "reacts": [{ 'react_id': 0, 'u_ids': react_list, }], "is_pinned": False }) return {"message_id": max_msg_id + 1}
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 channel_invite(token, channel_id, u_id): """ Invites a user with ID 'u_id' to join a channel with ID 'channel_id'. Once invited, the user is added to the channel immediately. Parameters: token (str): Authorisation hash for user channel_id (int): Channels' channel ID u_id (int): Users' user ID Returns: {} (dict): Empty dictionary """ #step 0: error testing if not isinstance(token, str): raise AccessError('Token is invalid') if not valid_channel_id(token, channel_id): raise InputError('Channel ID is not a valid channel') # Check user id exists. if not check_uid_exists(u_id): raise InputError('User ID does not refer to a valid user') # Check auth user exists. # if check_user_exists(token): 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') channel_data = get_channel(channel_id) # Check for user exists. if u_id in channel_data["all_members"] + channel_data["owner_members"]: raise InputError("User already exists!") channel_data["all_members"].append(u_id) return {}
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 channel_details(token, channel_id): """ Given a channel with ID 'channel_id' that the authorised user is a part of, provide basic details about the channel. Parameters: token (str): Authorisation hash for the user channel_id (int): Channels' channel ID Returns: {} (dict): Empty dictionary """ #step 0: error testing if not isinstance(token, str): raise AccessError('Token is invalid') if not valid_channel_id(token, channel_id): raise InputError('Channel ID is not a valid channel') channel_data = get_channel(channel_id) auth_user = get_user_id(token) if auth_user not in channel_data["owner_members"] + channel_data[ "all_members"]: raise AccessError('User is not a member of the channel') owners_list = list(map(list_members, channel_data["owner_members"])) members_list = owners_list + list( map(list_members, channel_data["all_members"])) return { "name": channel_data["name"], "owner_members": owners_list, "all_members": members_list, # ensure that profile_img_url is also returned }