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. ''' channel = get_channel_by_message_id(message_id) message_obj = get_message_by_message_id(message_id) user = get_user_by_token(token) # message_id does not refer to an existing message if not valid_message_id(message_id): raise ValueError("Message does not exist") # message is not of appropriate length if valid_message(message): raise ValueError("Message length too long") # User does not have permission to edit message if not message_obj.user_sent_message(user.u_id) and \ not token_is_admin(token) and \ not token_is_owner(token) and \ not channel.is_owner(user.u_id): raise AccessError("User does not have permission") # Edit channel message if not message.strip(): # If empty message, delete channel = get_channel_by_message_id(message_id) channel.remove_message(message_id) else: # Otherwise, edit message message_obj.edit_message(message) return {}
def message_unpin(token, message_id): ''' Given a message within a channel, remove it's mark as unpinned ''' channel = get_channel_by_message_id(message_id) message_obj = get_message_by_message_id(message_id) user = get_user_by_token(token) # Message_id does not refer to an existing message if not valid_message_id(message_id): raise ValueError("Message does not exist") # Message_id is already unpinned if not message_obj.is_pinned: raise ValueError("Message is currently unpinned") # User is not a member of the channel if not channel.is_member(user.u_id): raise AccessError("Authorised user is not a member of the channel") # User is not an owner of the channel if not channel.is_owner(user.u_id) and \ not token_is_admin(token) and \ not token_is_owner(token): raise ValueError("User is not an admin") # Unpinning message message_obj.unpin_message() 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 ''' channel = get_channel_by_message_id(message_id) message_obj = get_message_by_message_id(message_id) user = get_user_by_token(token) # Message_id does not refer to an existing message if not valid_message_id(message_id): raise ValueError("Message does not exist") # Message_id is already pinned if message_obj.is_pinned: raise ValueError("Message is currently pinned") # User is not a member of the channel if not channel.is_member(user.u_id): raise AccessError("Authorised user is not a member of the channel") # User is not an owner of the channel if not channel.is_owner(user.u_id) and \ not token_is_admin(token) and \ not token_is_owner(token): raise ValueError("User is not an admin") # Pin message message_obj.pin_message() 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 ''' channel = get_channel_by_message_id(message_id) message_obj = get_message_by_message_id(message_id) user = get_user_by_token(token) # Message_id does not refer to an existing message if not valid_message_id(message_id): raise ValueError("Message does not exist") # User is not a member of the channel if not channel.is_member(user.u_id): raise AccessError("Authorised user is not a member of the channel") # React_id does not refer to a valid react if not valid_react_id(react_id): raise ValueError("Invalid React ID") # Message already has an react id by the given user if not message_obj.user_has_reacted(user.u_id, react_id): raise ValueError("Message does not contain an active react") # Removing react from message message_obj.remove_react(user.u_id, react_id) return {}
def test_get_channel_by_message_id(): ''' Ensures get_channel_by_message_id returns the correct channel ''' # Initalise global_var.initialise_all() # Creating a user user = auth.auth_register("*****@*****.**", "pass123", "Raydon", "Smith") token = user["token"] # Create a channel channel = channel_functions.channels_create(token, "Channel 1", True) channel_id = channel["channel_id"] # Sending one message in channel assert message_functions.message_send(token, channel_id, "Hello Everyone")\ == {"message_id" : 0} # Check message obtain from first message assert helpers.get_channel_by_message_id(0).id == channel_id # Checking no such message_id assert helpers.get_channel_by_message_id(-1) is None
def message_remove(token, message_id): ''' Given a message ID, the message is removed ''' channel = get_channel_by_message_id(message_id) message = get_message_by_message_id(message_id) user = get_user_by_token(token) # Message_id does not refer to an existing message if not valid_message_id(message_id): raise ValueError("Message does not exist") # User does not have permission to remove message if not message.user_sent_message(user.u_id) and \ not token_is_admin(token) and \ not token_is_owner(token) and \ not channel.is_owner(user.u_id): raise AccessError("User does not have permission") # Removing message channel.remove_message(message_id) return {}