def message_unpin(token, message_id): """ Given a message within a channel, mark it as "pinned" to be given special display treatment by the frontend. """ # Check that token is valid check_token(token) # Check that the message is valid channel_id = get_channel_id_from_message(message_id) if channel_id is None: raise ValueError("Invalid message") # Check the user permissions user = get_user_from_token(token) if user["permission_id"] == 3: raise ValueError("The authorised user is not an admin") # Check if the messages is not pinned message = get_info_about_message(message_id) if message["is_pinned"] is False: raise ValueError("Message with ID message_id is already unpinned") # Check if the user is inside the channel if is_member(user["u_id"], channel_id) is False: raise AccessError("User is not a member of the channel") # unpin the message message["is_pinned"] = False save() return {}
def message_unreact(token, message_id, react_id): # check validity of inputs check_token(token) check_react_id(react_id) # get the channel id & check if message is valid channel_id = get_channel_id_from_message(message_id) if channel_id is None: raise ValueError("Invalid message") # get the user id & check if the user is in the channel u_id = get_user_from_token(token)["u_id"] if not is_member(u_id, channel_id): raise ValueError("User is not part of the channel") # get the msg msg = get_info_about_message(message_id) for reacts in msg["reacts"]: # find the react id if reacts["react_id"] == react_id: # have to do it this way because we redefined ValueError if u_id in reacts["u_ids"]: reacts["u_ids"].remove(u_id) save() else: raise ValueError("User has not reacted to the message") return {}
def message_react(token, message_id, react_id): # check validity of inputs check_token(token) check_react_id(react_id) # get the channel id & check if message is valid channel_id = get_channel_id_from_message(message_id) if channel_id is None: raise ValueError("Invalid message") # get the user id & check if the user is in the channel u_id = get_user_from_token(token)["u_id"] if not is_member(u_id, channel_id): raise ValueError("User is not part of the channel") # get the msg msg = get_info_about_message(message_id) for reacts in msg["reacts"]: # find the react id if reacts["react_id"] == react_id: # check if the user did the same reacts already for user_id in reacts["u_ids"]: if user_id == u_id: raise ValueError( "User already reacted to the message with the same react" ) reacts["u_ids"].append(u_id) save() break return {}
def message_remove(token, message_id): # check that token is valid check_token(token) # get info about user and message channel_id = get_channel_id_from_message(message_id) message_info = get_info_about_message(message_id) u_id = get_user_from_token(token)["u_id"] # check that message_id is valid if channel_id is None: raise ValueError("Invalid message") # check that the authorised user is the one who sent the message # or is an admin/owner of the channel or slackr if message_info["u_id"] != u_id: if is_owner(u_id, channel_id) is False or get_user_from_id( u_id)["permission_id"] == 3: raise AccessError("User is not authorised to remove this message") # since all values are correct, remove the message for channel in data["channels"]: if channel["channel_id"] == channel_id: for message in channel["messages"]: if message["message_id"] == message_info["message_id"]: channel["messages"].remove(message) break save() return {}
def test_channel_messages(): helper.reset_data() # Create users for testing user1 = helper.token_account_1() user1_token = user1["token"] user2 = helper.token_account_2() user2_token = user2["token"] # Create a new channel for testing channel_id = helper.channelid_public(user1_token) # Leave the channel in case user2 is in the new channel stub.channel_leave(user2_token, channel_id) # Setting the start & end start = 0 # Check that error is raised if an invalid token is passed in with pytest.raises(validation_helper.AccessError): stub.channel_messages("fake token", channel_id, start) # Start is a invalid number with pytest.raises(validation_helper.ValueError): stub.channel_messages(user1_token, channel_id, -1) # Channel doesn't exist with pytest.raises(validation_helper.ValueError): stub.channel_messages(user1_token, -1, start) # Index of start greater then messages in channel with pytest.raises(validation_helper.ValueError): stub.channel_messages(user1_token, channel_id, 1000) # user2 is not a member with pytest.raises(validation_helper.AccessError): stub.channel_messages(user2_token, channel_id, start) # valid call message_id = helper.get_valid_message(user1_token, channel_id) message = get_info_helper.get_info_about_message(message_id) assert stub.channel_messages(user1_token, channel_id, start) == {"messages": [message], "start": 0, "end": -1} # now test it with >50 messages sent in the channel i = 0 while i < 70: stub.message_send(user1_token, channel_id, "Hello") i += 1 messages = stub.channel_messages(user1_token, channel_id, 10) assert messages["start"] == 10 assert messages["end"] == 60 assert len(messages["messages"]) == 50
def message_edit(token, message_id, message): # check that token is valid check_token(token) # check that message is valid if len(message) > 1000: raise ValueError("Message can't be more than 1000 characters") # get info about user and message channel_id = get_channel_id_from_message(message_id) message_info = get_info_about_message(message_id) u_id = get_user_from_token(token)["u_id"] # check that message_id is valid if channel_id is None: raise ValueError("Invalid message") # check that the authorised user is the one who sent the message # or is an admin/owner of the channel or slackr if message_info["u_id"] != u_id: if is_owner(u_id, channel_id) is False or get_user_from_id( u_id)["permission_id"] == 3: raise AccessError("User is not authorised to remove this message") # check if the message is empty string remove the message if message == "": message_remove(token, message_id) # since all values are correct, edit the message for channel in data["channels"]: if channel["channel_id"] == channel_id: for msg in channel["messages"]: if msg["message_id"] == message_info["message_id"]: msg["message"] = message break save() return {}