def test_get_channel_by_channel_id(): ''' Ensures get_channel_by_channel_id returns correct channel id ''' # Initialisation global_var.initialise_all() # Creating a user user = auth.auth_register("*****@*****.**", "pass123", "Raydon", "Smith") token = user["token"] # Creating first channel channel_1 = channel_functions.channels_create(token, "Channel 1", True) channel_1_id = channel_1["channel_id"] # Creating second channel channel_2 = channel_functions.channels_create(token, "Channel 2", True) channel_2_id = channel_2["channel_id"] # Ensuring that first channel has id assert helpers.get_channel_by_channel_id(channel_1_id).id == 0 # Ensuring that second channel has id assert helpers.get_channel_by_channel_id(channel_2_id).id == 1 # Checking no such channel_id assert helpers.get_channel_by_channel_id(-1) is None
def message_send(token, channel_id, message): ''' Send a message from authorised_user to the channel specified by channel_id ''' channel = get_channel_by_channel_id(channel_id) user = get_user_by_token(token) # Channel_id does not refer to a valid channel if channel is None: raise ValueError("Invalid Channel ID") # Message is not of appropriate length if valid_message(message): raise ValueError("Message length too long") # User has not joined the channel if not channel.is_member(user.u_id): raise AccessError("Authorised user is not a member of the channel") message_object = global_var.Message(user.u_id, message, channel_id) # Append message to channel list channel.add_message(message_object) return { "message_id": message_object.id }
def message_sendlater(token, channel_id, message, time_sent): ''' Sends a message from authorised_user to the channel specified by channel_id automatically at a specified time in the future ''' channel = get_channel_by_channel_id(channel_id) user = get_user_by_token(token) # Channel_id does not refer to a valid channel if channel is None: raise ValueError("Invalid Channel ID") # Message is not of appropriate length if valid_message(message): raise ValueError("Message length too long") # Time to be sent is in the past if time_sent < datetime.datetime.now().timestamp(): raise ValueError("Time sent was in the past") # User has not joined the channel if not channel.is_member(user.u_id): raise AccessError("Authorised user is not a member of the channel") # create new message object and update send time message_object = global_var.Message(user.u_id, message, channel_id) message_object.time_created = time_sent time_diff = time_sent - datetime.datetime.now().timestamp() Timer(time_diff, channel.add_message, args=[message_object]).start() return { "message_id": message_object.id }
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 ''' channel = get_channel_by_channel_id(channel_id) user = get_user_by_token(token) if channel is None: raise ValueError("Channel Does Not Exist") if channel.standup_running() is not False: raise ValueError("Standup Already Running") if not channel.is_member(user.u_id): raise AccessError("Cannot Access Channel") # Start standup and after length seconds end the standup time = datetime.datetime.now() + datetime.timedelta(seconds=length) channel.start_standup(time.timestamp()) Timer(length, channel.end_standup, args=[token]).start() return {"time_finish" : time.timestamp()}
def channel_addowner(token, channel_id, u_id): ''' make an user an owner of the channel ''' channel = get_channel_by_channel_id(channel_id) # channel_id does not refer to a valid channel if channel is None: raise ValueError("Channel does not exist") # user is already an owner of the channel if channel.is_owner(u_id): raise ValueError("User is already an owner of the channel") # User does not have permission to add owner if not channel.is_owner(decode_token(token)) and \ not token_is_admin(token) and \ not token_is_owner(token): raise AccessError("User is not an owner of the channel or slackrowner") # User is added as owner channel.add_owner(u_id) return {}
def channel_messages(token, channel_id, start): ''' Returns up to 50 messages between index "start" and "start + 50". Message with 0 index is the most recent message end = start + 50 If function has returned the least recent messages in the channel returns -1 in "end" to indicate there are no more msgs to load ''' channel = get_channel_by_channel_id(channel_id) u_id = decode_token(token) # channel_id does not refer to a valid channel if channel is None: raise ValueError("Channel does not exist") # user is not a member of the channel if not channel.is_member(u_id): raise AccessError("Authorised user is not a member of the channel") # If the start is greater than the number of messages in the channel given if start > len(channel.messages): raise ValueError("Start index is invalid") messages = [] for i in range(MAX_MESSAGES): # End index of -1 to indicate there are no more messages to load if start + i >= len(channel.messages): return {"messages": messages, "start": start, "end": -1} # Gets message object message = channel.messages[start + i] # Add information regarding if user has reacted to this message reacts = message.reacts for react in reacts: react["is_this_user_reacted"] = \ message.user_has_reacted(u_id, react['react_id']) # Append message dictionary into messages list messages.append({ "message_id": message.id, "u_id": message.sender, "message": message.message, "time_created": message.time_created, "reacts": reacts, "is_pinned": message.is_pinned, }) # Check which if start + MAX_MESSAGES >= len(channel.messages): end = -1 else: end = start + MAX_MESSAGES return {"messages": messages, "start": start, "end": end}
def channel_leave(token, channel_id): ''' Given a channel ID, the user is removed as a member of the channel ''' channel = get_channel_by_channel_id(channel_id) user = get_user_by_token(token) # channel_id does not refer to a valid channel if channel is None: raise ValueError("Channel does not exist") # User is removed as a member of the channel channel.remove_user(user.u_id) channel.remove_owner(user.u_id) return {}
def standup_send(token, channel_id, message): ''' Sending a message to get buffered in the standup queue, assuming a standup is currently active ''' channel = get_channel_by_channel_id(channel_id) user = get_user_by_token(token) if channel is None: raise ValueError("Channel Does Not Exist") if len(message) > MAX_MESSAGE_LENGTH: raise ValueError("Message Too Long") if not channel.is_member(user.u_id): raise AccessError("Cannot Access Channel") if channel.standup_running() is False: raise ValueError("Not Currently In Standup") channel.add_standup_message(token, message) return {}
def channel_join(token, channel_id): ''' Given a channel_id of a channel that the authorised user can join adds them to that channel ''' channel = get_channel_by_channel_id(channel_id) user = get_user_by_token(token) # channel_id does not refer to a valid channel if channel is None: raise ValueError("Channel does not exist") # User does not have permission to join channel without invite if not channel.is_public and \ not token_is_admin(token) and \ not token_is_owner(token): raise AccessError("Channel is private and user is not admin") # User is added to channel channel.add_user(user.u_id) return {}
def channel_invite(token, channel_id, u_id): ''' Invites a user to join a channel ''' channel = get_channel_by_channel_id(channel_id) # Channel_id does not refer to a valid channel if channel is None: raise ValueError("Channel does not exist") # Inviting user is not a member of the channel if not channel.is_member(decode_token(token)): raise AccessError("Authorised user is not a member of the channel") # u_id is not a user if not get_user_by_u_id(u_id): raise ValueError("Invalid User ID") # User is added as a member channel.add_user(u_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 ''' channel = get_channel_by_channel_id(channel_id) # Checking if channel exists if channel is None: raise ValueError("Channel Does Not Exist") # If standup is active if channel.standup_running(): return { "is_active": True, "time_finish": channel.get_standup_end() } # Standup is not active return { "is_active": False, "time_finish": None }
def channel_details(token, channel_id): ''' Provide basic details about the channel ''' channel = get_channel_by_channel_id(channel_id) # channel_id does not refer to a valid channel if channel is None: raise ValueError("Channel does not exist") # user is not a member of the channel if not channel.is_member(decode_token(token)): raise AccessError("Authorised user is not a member of the channel") # Get details regarding the owners and members of the channel owners = channel.get_owners_details() members = channel.get_members_details() # Returns the channel details corresponding to that channel id channel_detail = {"name": channel.name, "owner_members": owners, \ "all_members": members} return channel_detail
def channel_removeowner(token, channel_id, u_id): ''' Remove user with user id u_id an owner of this channel ''' channel = get_channel_by_channel_id(channel_id) # channel_id does not refer to a valid channel if channel is None: raise ValueError("Channel does not exist") # user is not an owner of the channel if not channel.is_owner(u_id): raise ValueError("User id is not an owner of the channel") # If the user trying to remove owner does not have permission if not channel.is_owner(decode_token(token)) and \ not token_is_admin(token) and \ not token_is_owner(token): raise AccessError("User is not an owner of the channel or slackrowner") channel.remove_owner(u_id) return {}