def message_by_id_soft_delete(message_id) -> Response: """ Soft delete an existing message with a given unique ID. :param message_id: The unique identifier for a team/group message. :return: A response object for the DELETE API request. """ existing_message: Message = MessageDao.get_message_by_id( message_id=message_id) if existing_message is None: response = jsonify({ 'self': f'/v2/messages/soft/{message_id}', 'deleted': False, 'error': 'there is no existing message with this id' }) response.status_code = 400 return response if existing_message.deleted: response = jsonify({ 'self': f'/v2/messages/soft/{message_id}', 'deleted': False, 'error': 'this message is already soft deleted' }) response.status_code = 400 return response # Update the message model to reflect the soft delete existing_message.deleted = True existing_message.deleted_date = datetime.now() existing_message.deleted_app = 'saints-xctf-api' existing_message.modified_date = datetime.now() existing_message.modified_app = 'saints-xctf-api' is_deleted: bool = MessageDao.soft_delete_message(existing_message) if is_deleted: response = jsonify({ 'self': f'/v2/messages/soft/{message_id}', 'deleted': True, }) response.status_code = 204 return response else: response = jsonify({ 'self': f'/v2/messages/soft/{message_id}', 'deleted': False, 'error': 'failed to soft delete the message' }) response.status_code = 500 return response
def messages_get() -> Response: """ Retrieve all the messages in the database. :return: A response object for the GET API request. """ messages = MessageDao.get_messages() if messages is None: response = jsonify({ 'self': '/v2/messages', 'messages': None, 'error': 'an unexpected error occurred retrieving messages' }) response.status_code = 500 return response else: message_dicts = [] for message in messages: message_dict = MessageData(message).__dict__ if message_dict.get('time') is not None: message_dict['time'] = str(message_dict['time']) message_dicts.append(message_dict) response = jsonify({'self': '/v2/messages', 'messages': message_dicts}) response.status_code = 200 return response
def message_by_id_get(message_id) -> Response: """ Get a single message based on a unique ID. :param message_id: The unique identifier for a team/group message. :return: A response object for the GET API request. """ message: Message = MessageDao.get_message_by_id(message_id) if message is not None: message_dict = MessageData(message).__dict__ if message_dict.get('time') is not None: message_dict['time'] = str(message_dict['time']) response = jsonify({ 'self': f'/v2/messages/{message_id}', 'message': message_dict }) response.status_code = 200 return response else: response = jsonify({ 'self': f'/v2/messages/{message_id}', 'message': None, 'error': 'there is no message with this identifier' }) response.status_code = 400 return response
def message_by_id_delete(message_id) -> Response: """ Hard delete an existing message with a given unique ID. :param message_id: The unique identifier for a team/group message. :return: A response object for the DELETE API request. """ is_deleted = MessageDao.delete_message_by_id(message_id=message_id) if is_deleted: response = jsonify({ 'self': f'/v2/messages/{message_id}', 'deleted': True, }) response.status_code = 204 return response else: response = jsonify({ 'self': f'/v2/messages/{message_id}', 'deleted': False, 'error': 'failed to delete the message' }) response.status_code = 500 return response
def messages_by_id_put(message_id) -> Response: """ Update an existing message with a given unique ID. :param message_id: The unique identifier for a team/group message. :return: A response object for the PUT API request. """ old_message = MessageDao.get_message_by_id(message_id=message_id) if old_message is None: response = jsonify({ 'self': f'/v2/messages/{message_id}', 'updated': False, 'message': None, 'error': 'there is no existing message with this id' }) response.status_code = 400 return response message_data: dict = request.get_json() new_message = Message(message_data) if old_message != new_message: new_message.modified_date = datetime.now() new_message.modified_app = 'saints-xctf-api' is_updated = MessageDao.update_message(new_message) if is_updated: updated_message: Message = MessageDao.get_message_by_id( message_id=new_message.message_id) message_dict = MessageData(updated_message).__dict__ if message_dict.get('time') is not None: message_dict['time'] = str(message_dict['time']) response = jsonify({ 'self': f'/v2/messages/{message_id}', 'updated': True, 'message': message_dict }) response.status_code = 200 return response else: response = jsonify({ 'self': f'/v2/messages/{message_id}', 'updated': False, 'message': None, 'error': 'the message failed to update' }) response.status_code = 500 return response else: response = jsonify({ 'self': f'/v2/messages/{message_id}', 'updated': False, 'message': None, 'error': 'the message submitted is equal to the existing message with the same id' }) response.status_code = 400 return response
def message_post() -> Response: """ Create a new message in a group or team. :return: A response object for the POST API request. """ message_data: dict = request.get_json() if message_data is None: response = jsonify({ 'self': f'/v2/messages', 'added': False, 'message': None, 'error': "the request body isn't populated" }) response.status_code = 400 return response message_to_add = Message(message_data) if None in [ message_to_add.username, message_to_add.first, message_to_add.last, message_to_add.group_name, message_to_add.time ]: response = jsonify({ 'self': f'/v2/messages', 'added': False, 'message': None, 'error': "'username', 'first', 'last', 'group_name', and 'time' are required fields" }) response.status_code = 400 return response message_to_add.created_date = datetime.now() message_to_add.created_app = 'saints-xctf-api' message_to_add.created_user = None message_to_add.modified_date = None message_to_add.modified_app = None message_to_add.modified_user = None message_to_add.deleted_date = None message_to_add.deleted_app = None message_to_add.deleted_user = None message_to_add.deleted = False message_added_successfully = MessageDao.add_message( new_message=message_to_add) if message_added_successfully: message_added = MessageDao.get_message_by_id( message_id=message_to_add.message_id) message_dict = MessageData(message_added).__dict__ if message_dict.get('time') is not None: message_dict['time'] = str(message_dict['time']) response = jsonify({ 'self': '/v2/messages', 'added': True, 'message': message_dict }) response.status_code = 200 return response else: response = jsonify({ 'self': '/v2/messages', 'added': False, 'message': None, 'error': 'failed to create a new message' }) response.status_code = 500 return response
def message_feed_get(filter_by, bucket, limit, offset): """ Get a list of messages based on certain filters. :param filter_by: The filtering mechanism for the messages. You can filter by group (group_name). :param bucket: The bucket to filter by (the group name) :param limit: The maximum number of messages to return :param offset: The number of messages to skip from the result of this filter before returning :return: A response object for the GET API request. """ limit = int(limit) offset = int(offset) if filter_by == 'group' or filter_by == 'groups' or filter_by == 'groupname': messages: ResultProxy = MessageDao.get_message_feed(group_name=bucket, limit=limit, offset=offset) else: messages = None # Generate MessageFeed API URLs self_url = f'/v2/message_feed/{filter_by}/{bucket}/{limit}/{offset}' prev_offset = offset - limit if prev_offset >= 0: prev_url = f'/v2/message_feed/{filter_by}/{bucket}/{limit}/{prev_offset}' else: prev_url = None if messages is None or messages.rowcount == 0: next_url = None response = jsonify({ 'self': self_url, 'next': next_url, 'prev': prev_url, 'messages': None, 'error': 'no messages found in this feed' }) response.status_code = 500 return response else: message_list = [] for message in messages: message_list.append({ 'message_id': message.message_id, 'username': message.username, 'first': message.first, 'last': message.last, 'group_name': message.group_name, 'time': str(message.time), 'content': message.content, 'deleted': message.deleted }) next_url = f'/v2/message_feed/{filter_by}/{bucket}/{limit}/{offset + limit}' response = jsonify({ 'self': self_url, 'next': next_url, 'prev': prev_url, 'messages': message_list }) response.status_code = 200 return response