def react_message(token, message_id, react_id): """ React message with message_id with react_id """ data = get_data() user = get_user_from_token(data, token) message = get_msg_from_msg_id(data, message_id) if message is None or not user_in_channel_with_msg(data, user['u_id'], message): raise ValueError('Message is not in the channel') if not valid_react_id(react_id): raise ValueError('Invalid react id') if message_contains_react(message, user['u_id']): raise ValueError('Message already has that react') react = get_react_from_react_id(message, react_id) # if there are no current reacts, create a new data structure # and store the information. Otherwise, add only the user id if react is None: react_info = generate_react_info(react_id, user['u_id']) message['reacts'].append(react_info) else: react['u_ids'].append(user['u_id']) pickle_data(data) return {}
def edit_message(token, message_id, message_str): """ Edit message with message_id with new message If message is empty, delete the message """ if len(message_str) == 0: return remove_message(token, message_id) data = get_data() og_message = get_msg_from_msg_id(data, message_id) user = get_user_from_token(data, token) channel = get_channel(data, og_message['channel_id']) # check if user did not send original message and user is admin/owner if (not user_sent_message(user['u_id'], og_message) and not user_is_admin_or_owner(user, channel)): raise AccessError("User did not send message or not enough privileges") # edit the original message with the new message og_message['message'] = message_str pickle_data(data) return {}
def remove_message(token, message_id): """ Remove message with message_id from channel """ data = get_data() user = get_user_from_token(data, token) message = get_msg_from_msg_id(data, message_id) if message is None: raise ValueError('Message no longer exists') channel = get_channel(data, message['channel_id']) if not user_sent_message(user['u_id'], message) and not user_is_admin_or_owner( user, channel): raise AccessError("User did not send message or not enough privileges") # remove the message object and the message_id from the 'database' data['messages'].remove(message) channel['message_ids'].remove(message_id) pickle_data(data) return {}
def unreact_message(token, message_id, react_id): """ Unreact message with message_id with react_id """ data = get_data() user = get_user_from_token(data, token) message = get_msg_from_msg_id(data, message_id) if message is None or not user_in_channel_with_msg(data, user['u_id'], message): raise ValueError('Message is not in the channel') if not valid_react_id(react_id): raise ValueError('Invalid react id') if not message_contains_react(message, user['u_id']): raise ValueError('Message does not have that react') # get the react object and delete the user u_id react = get_react_from_react_id(message, react_id) react['u_ids'].remove(user['u_id']) react['is_this_user_reacted'] = False pickle_data(data) return {}
def unpin_message(token, message_id): """ Unpin message with message_id """ data = get_data() user = get_user_from_token(data, token) message = get_msg_from_msg_id(data, message_id) if message is None: raise ValueError("Invalid message_id") channel = get_channel(data, message['channel_id']) if not perm_id_is_admin_owner(user['perm_id']): raise ValueError("User is not an admin or owner") if not message['is_pinned']: raise ValueError("Message is not pinned") if not user_in_channel(user['u_id'], channel): raise AccessError( "User not part of channel that the message is within") # set the pin flag on the message to False message['is_pinned'] = False pickle_data(data) return {}