def message_remove(token, message_id): """ Given a message_id for a message, this message is removed from the chxnel """ # Validate token if not validator.is_valid_token(token): raise AccessError(INVALID_TOKEN) if not isinstance(message_id, int): message_id = int(message_id) # Convert token to u_id u_id = jwt_handler.decode_token(token) # Check u_id is authorised user = database.get_user_by_id(u_id) message = database.get_message_by_id(message_id) if message is None: raise ValueError(MESSAGE_REMOVE_NOT_EXIST) channel_id = message["channel_id"] channel = database.get_channel_by_id(channel_id) # if sender of message if u_id == message["u_id"] or\ user["permission"] in [OWNER, ADMIN] or\ u_id in channel["owner_ids"]: # Delete message from database database.delete_message_by_id(message_id) # Delete message from channel channel["messages"].remove(message_id) database.update_channel_by_id(channel_id, channel) return {} raise AccessError(MESSAGE_EDIT_NO_AUTH)
def channel_removeowner(token, channel_id, u_id): """Remove user with user id u_id an owner of this channel""" # Convert channel_id and u_id to integer channel_id = int(channel_id) u_id = int(u_id) # Validate token, channel and user if not validator.is_valid_token(token): raise AccessError(INVALID_TOKEN) if not validator.is_valid_channel(channel_id): raise ValueError(INVALID_CHANNEL) if not validator.is_valid_user(u_id): raise ValueError(INVALID_USER) # Locate channel and user in database channel = database.get_channel_by_id(channel_id) auth_user_id = jwt_handler.decode_token(token) auth_user = database.get_user_by_id(auth_user_id) owner_list = channel["owner_ids"] if auth_user["permission"] not in [OWNER, ADMIN] or\ auth_user_id not in channel["owner_ids"]: raise AccessError(CHANNEL_DEL_OWNER_NO_AUTH) # Check that the added_user is currently an owner if u_id not in owner_list: raise ValueError(CHANNEL_NOT_OWNER) # Remove user from owner_ids owner_list.remove(u_id) database.update_channel_by_id(channel_id, { "owner_ids": owner_list }) 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""" # Validate token and channel ID if not validator.is_valid_token(token): raise AccessError(INVALID_TOKEN) if not validator.is_valid_channel(channel_id): raise ValueError(INVALID_CHANNEL) # Convert token to u_id u_id = jwt_handler.decode_token(token) user = database.get_user_by_id(u_id) # Locate channel in database channel = database.get_channel_by_id(channel_id) # If channel is private if not channel["is_public"] and user["permission"] == MEMBER: # If the user is not an admin/owner raise AccessError(CHANNEL_CANT_VIEW_DETAIL) # Add user to authorised users in channel auth_list = channel["auth_ids"] if u_id not in auth_list: auth_list.append(u_id) database.update_channel_by_id(channel_id, { "auth_ids": auth_list }) else: raise ValueError(CHANNEL_ALREADY_JOINED) return {}
def channel_leave(token, channel_id): """Given a channel ID, the user removed as a member of this channel""" # Validate token and channel ID if not validator.is_valid_token(token): raise AccessError(INVALID_TOKEN) if not validator.is_valid_channel(channel_id): raise ValueError(INVALID_CHANNEL) # Convert token to u_id u_id = jwt_handler.decode_token(token) # Locate channel channel = database.get_channel_by_id(channel_id) # Validate user is in channel auth_list = channel["auth_ids"] if u_id not in auth_list: raise AccessError(NOT_IN_CHANNEL) # Remove user from auth_list auth_list.remove(u_id) database.update_channel_by_id(channel_id, { "auth_ids": auth_list }) # If owner, remove from owner_list owner_list = channel["owner_ids"] if u_id in owner_list: owner_list.remove(u_id) database.update_channel_by_id(channel_id, { "owner_ids": owner_list }) 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""" # Convert channel_id and u_id to integer channel_id = int(channel_id) u_id = int(u_id) # Validate token, channel and user if not validator.is_valid_token(token): raise AccessError(INVALID_TOKEN) if not validator.is_valid_channel(channel_id): raise ValueError(INVALID_CHANNEL) if not validator.is_valid_user(u_id): raise ValueError(INVALID_USER) # Find invitor invitor_id = jwt_handler.decode_token(token) # Locate channel in database channel = database.get_channel_by_id(channel_id) # Check that the invitor is authorised auth_list = channel["auth_ids"] if invitor_id not in auth_list: raise AccessError(CHANNEL_INV_NO_AUTH) if u_id in auth_list: raise ValueError(CHANNEL_ALREADY_JOINED) auth_list.append(u_id) database.update_channel_by_id(channel_id, { "auth_ids": auth_list }) return {}
def message_sendlater(token, channel_id, message, time_sent): """ Send a message from authorised_user to the channel specified by channel_id """ # Validate data if not validator.is_valid_token(token): raise AccessError(INVALID_TOKEN) if not validator.is_valid_message(message): raise ValueError(INVALID_MESSAGE) # Convert token to u_id u_id = jwt_handler.decode_token(token) # Check channel exists channel = database.get_channel_by_id(channel_id) if channel is None: raise ValueError(INVALID_CHANNEL) # Read time_sent time_to_send = json_time_translator.timestamp_to_datetime(time_sent) # Check time_to_send is not in the past if time_to_send <= datetime.utcnow(): raise ValueError(MESSAGE_TIME_INVALID) if u_id not in channel["auth_ids"]: raise AccessError(MESSAGE_SEND_NO_AUTH) # Create message message_id = database.add_message({ "u_id": u_id, "message": message, "channel_id": int(channel_id), "time_created": json_time_translator.datetime_to_json(time_to_send), "is_pinned": False, "reacts": [] }) # Insert into channel's list of messages in order of time message_id_list = channel["messages"] inserted = False for i in reversed(range(len(message_id_list))): json_time = database.get_message_by_id( message_id_list[i])["time_created"] timestamp = json_time_translator.json_to_timestamp(json_time) if timestamp <= int(float(time_sent)): message_id_list.insert(i + 1, message_id) inserted = True break if inserted is False: message_id_list.insert(0, message_id) database.update_channel_by_id(channel_id, {"messages": message_id_list}) return {"message_id": message_id}
def channel_addowner(token, channel_id, u_id): """Make user with user id u_id an owner of this channel""" # Convert channel_id and u_id to integer channel_id = int(channel_id) u_id = int(u_id) # Validate token, channel and user if not validator.is_valid_token(token): raise AccessError(INVALID_TOKEN) if not validator.is_valid_channel(channel_id): raise ValueError(INVALID_CHANNEL) if not validator.is_valid_user(u_id): raise ValueError(INVALID_USER) # Locate channel and user in database channel = database.get_channel_by_id(channel_id) auth_user_id = jwt_handler.decode_token(token) auth_user = database.get_user_by_id(auth_user_id) # Check auth_user permissions if auth_user["permission"] not in [OWNER, ADMIN] or\ auth_user_id not in channel["owner_ids"]: raise AccessError(CHANNEL_ADD_OWNER_NO_AUTH) # Add user to auth_ids auth_list = channel["auth_ids"] if u_id not in auth_list: auth_list.append(u_id) database.update_channel_by_id(channel_id, { "auth_ids": auth_list }) # Add user to owner_ids owner_list = channel["owner_ids"] if u_id in owner_list: raise ValueError(CHANNEL_ALREADY_OWNER) owner_list.append(u_id) database.update_channel_by_id(channel_id, { "owner_ids": owner_list }) return {}