def channel_join(token, channel_id): """ Given a channel_id of a channel that the authorised user can join, adds them to that channel Parameters: token(string): an authorisation hash channel_id(int): identifier for channel Returns: Nothing """ # Check valid token user_id = validation.check_valid_token(token) # Checks channel exists validation.check_valid_channel_id(channel_id) #Checks the person wasn"t already in the channel validation.check_is_not_existing_channel_member(user_id, channel_id) validation.check_channel_is_public(channel_id) #Checks the channel is public and adds the user to the members data.channel_add_member(channel_id, user_id) return {}
def standup_active(token, channel_id): """ Checks if there is a standup active on the channel Parameters: token(string): An authorisation hash channel_id(int): The channel_id of the channel that the standup is being checked Returns: is_active(boolean): If there is a standup active on the given channel time_finish(int(unix timestamp)): When the standup will be posted """ # Check that the token is valid user_input_id = validation.check_valid_token(token) # Check that the channel_id is valid validation.check_valid_channel_id(channel_id) # Check that user is in channel validation.check_user_in_channel(user_input_id, channel_id) # checks if the standup is running for the channel running = data.check_standup_running(channel_id) # Creates time_finish and if the standup is running puts the finish_time in it time_finish = None if running: time_finish = data.get_standup_timer_finish(channel_id) return {"is_active": running, "time_finish": time_finish}
def standup_send(token, channel_id, message): """ Adds a new message to the standup in a channel Parameters: token(string): An authorisation hash channel_id(int): The channel_id of the channel standup the message is being added too message(string): The message of the message being added Returns: Nothing """ # Check that the token is valid user_input_id = validation.check_valid_token(token) # Check that the channel_id is valid validation.check_valid_channel_id(channel_id) # Check that user is in channel validation.check_user_in_channel(user_input_id, channel_id) #Checks a standup is already running on this channel, raises error if it isn't validation.check_standup_running(channel_id) # Check that the message is valid validation.valid_message(message) # Will insert the message to the running standup data.add_message_standup(user_input_id, message, channel_id) return {}
def channel_invite(token, channel_id, u_id): """ Invites a user (with user id u_id) to join a channel with ID channel_id. Once invited the user is added to the channel immediately Parameters: token(string): an authorisation hash channel_id(int): identifier for channel u_id(int): Identifier for user Returns: Nothing """ # Check if token is valid inviter_uid = validation.check_valid_token(token) # Check if given valid channel_id validation.check_valid_channel_id(channel_id) # Check if is authorised(member of channel) validation.check_user_in_channel(inviter_uid, channel_id) # Check if given valid u_id validation.check_valid_u_id(u_id) # Check if invitee is already part of channel. If so,raise input error validation.check_is_not_existing_channel_member(u_id, channel_id) # Everything valid, Proceed with adding to channel data.channel_add_member(channel_id, u_id) return {}
def channel_messages(token, channel_id, start): """ Checks that given information (token, channel_id, u_id) is valid then 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(string): an authorisation hash channel_id(int): identifier for channel start(int): index of starting message Returns: (dict): {messages, start, end} """ # Check if valid token u_id = validation.check_valid_token(token) # Check if given valid channel_id validation.check_valid_channel_id(channel_id) # Check if user is authorised(member of channel) validation.check_user_in_channel(u_id, channel_id) # Proceed to getting messages messages_list = {"messages": [], "start": start, "end": start + 50} channel = data.get_channel_info(channel_id) # Checking if start value is greater than number of total messages # and if so, raise Input Error if len(channel["messages"]) < start: raise InputError(description="Start value is too high") for message in reversed( list(channel["messages"].keys())[start:start + 50]): for react in channel["messages"][message]["reacts"]: if u_id in react['u_ids']: react['is_this_user_reacted'] = True else: react['is_this_user_reacted'] = False messages_list["messages"].append(channel["messages"][message]) # Returns -1 when "end" is less than start + 50 and no more messages # to load if len(channel["messages"]) < start + 50: messages_list["end"] = -1 return messages_list
def test_check_valid_channel_id(): """ check_valid_channel_id returns None if channel exists check_valid_channel_id raises an InputError if channel doesn't exist """ with pytest.raises(InputError): assert validation.check_valid_channel_id(1) assert validation.check_valid_channel_id(2) assert validation.check_valid_channel_id("three") user = auth.auth_register("*****@*****.**", "Imeanttest", "thisfile", "righthere") _chan1 = channels.channels_create(user["token"], "ONE", True) _chan2 = channels.channels_create(user["token"], "TWO", True) assert validation.check_valid_channel_id(1) == None assert validation.check_valid_channel_id(2) == None other.clear()
def channel_details(token, channel_id): """ Checks that given information (token, channel_id, u_id) is valid then given a Channel with ID channel_id that the authorised user is part of, provide basic details about the channel Parameters: token(string): an authorisation hash channel_id(int): identifier for channel Returns: (dict): { name, owner_members, all_members } """ # Check if token is valid u_id = validation.check_valid_token(token) # Check if given valid channel_id validation.check_valid_channel_id(channel_id) # Check if user is authorised(member of channel) validation.check_user_in_channel(u_id, channel_id) # Everything valid, Proceed with getting details channel_info = {"name": "", "owner_members": [], "all_members": []} # Find channel and copy infomation into channel_details channel = data.get_channel_info(channel_id) channel_info["name"] = channel["name"] for u_id in channel["members"]: user = data.get_user_info(u_id) member = {} member["u_id"] = user["u_id"] member["name_first"] = user["name_first"] member["name_last"] = user["name_last"] member["name_last"] = user["name_last"] member["profile_img_url"] = user["profile_img_url"] channel_info["all_members"].append(member) for u_id in channel["owners"]: user = data.get_user_info(u_id) owner = {} owner["u_id"] = user["u_id"] owner["name_first"] = user["name_first"] owner["name_last"] = user["name_last"] owner["profile_img_url"] = user["profile_img_url"] channel_info["owner_members"].append(owner) return channel_info
def standup_start(token, channel_id, length): """ Starts the standup for the given channel Parameters: token(string): An authorisation hash channel_id(int): The channel_id of the channel the standup is being started for length(int): The delay before the standup should be posted Returns: time_finish(int(unix timestamp)): When the standup will be posted """ # Check that the token is valid user_input_id = validation.check_valid_token(token) # Check that the channel_id is valid validation.check_valid_channel_id(channel_id) # Check that user is in channel validation.check_user_in_channel(user_input_id, channel_id) # Check that the length is greater then 0 validation.check_length_valid(length) #Checks a standup isn't already running on this channel, raises error if it is validation.check_standup_not_running(channel_id) # Will begin the timer which will wait the length amount of time before calling the # function timer_class = threading.Timer(length, send_standup, [user_input_id, channel_id]) # Calculates when the standup will finish time_finish = get_time_finish(length) # Inserts the standup into the channel data.create_standup(channel_id, user_input_id, timer_class, time_finish) timer_class.start() return {"time_finish": time_finish}
def message_sendlater(token, channel_id, message, time_sent): """ Adds a new message to the messages in a channel at a set date Parameters: token(string): An authorisation hash channel_id(int): The channel_id of the channel the message is being added too message(string): The message of the message being added time_sent(int): The Unix timestamp of the date the message is to be sent Returns: message_id(int): An identifier for the new message """ # Check that the token is valid user_input_id = validation.check_valid_token(token) # Check that the message is valid. validation.valid_message(message) # Check that the channel_id is valid validation.check_valid_channel_id(channel_id) # Check that user is in channel validation.check_user_in_channel(user_input_id, channel_id) current_timestamp = round(datetime.datetime.now().timestamp()) delta = time_sent - current_timestamp if round(delta) <= 0: raise InputError(description="Can't send to the past") t = threading.Timer(delta, message_send(token, channel_id, message)) t.start() data.add_sendlater(t, time_sent) new_message_id = data.get_message_num() return { "message_id": new_message_id, }
def channel_leave(token, channel_id): """ Checks if user is in channel, then removes the user from the channel Parameters: token(string): An authorisation hash channel_id(int): Identifier for channels Returns: Empty dictionary """ # Check valid token u_id = validation.check_valid_token(token) # Check if given valid channel_id validation.check_valid_channel_id(channel_id) # Check if token is valid and user is authorised(member of channel) validation.check_user_in_channel(u_id, channel_id) # Everything valid, Proceed with leaving channel data.channel_remove_member(channel_id, u_id) return {}
def message_send(token, channel_id, message): """ Adds a new message to the messages in a channel Parameters: token(string): An authorisation hash channel_id(int): The channel_id of the channel the message is being added too message(string): The message of the message being added Returns: message_id(int): An identifier for the new message """ # Check that the token is valid user_input_id = validation.check_valid_token(token) # Check that the message is valid. validation.valid_message(message) # Check that the channel_id is valid validation.check_valid_channel_id(channel_id) # Check that user is in channel validation.check_user_in_channel(user_input_id, channel_id) new_message_id = data.make_message_id() new_message = {} new_message["message"] = message new_message["u_id"] = user_input_id new_message["time_created"] = datetime.datetime.now().replace().timestamp() new_message["message_id"] = new_message_id new_message["is_pinned"] = False new_message["reacts"] = [{ "react_id": 1, "u_ids": [] }, { "react_id": 2, "u_ids": [] }, { "react_id": 3, "u_ids": [] }, { "react_id": 4, "u_ids": [] }] # Check if message is calling weather API if validation.check_weather_call(message): new_message['message'] = weather.get_weather(message) # Check if message will start a hangman session if validation.check_start_hangman(channel_id, message): return hangman.start(user_input_id, channel_id, new_message, new_message_id) # Check if hangman is active and message is a guess if validation.check_if_hangman(channel_id, message): hangman.guess(user_input_id, channel_id, message) # Check if hangman should stop (/hangman stop) if validation.check_if_stop_message(message): return hangman.stop(user_input_id, channel_id) if message.startswith("/KAHIO/END"): new_message["message"] = kahio.kahio_end(user_input_id, channel_id) elif message.startswith("/KAHIO"): new_message["message"] = kahio.start_kahio(user_input_id, channel_id, message) elif data.check_kahio_running(channel_id): return kahio.kahio_guess(user_input_id, channel_id, new_message) data.add_message(new_message, channel_id) return { "message_id": new_message_id, }