def get_messages(self, p_id, timestamp=None): """ p_id: project id timestamp: if provided, messages only after this time stamp is provided """ if timestamp is None: timestamp = datetime.datetime(1970, 1, 1) else: timestamp = datetime.datetime.strptime(timestamp, "%Y-%m-%d, %H:%M:%S") messages = Message.query \ .filter(Message.p_id == p_id) \ .filter(Message.reply_id.is_(None)) \ .filter(Message.created_at > timestamp) \ .all() message_list = [] for message in messages: replies_list = [] for reply in message.replies: reply_dict = get_message_dict(reply) replies_list.append(reply_dict) message_dict = get_message_dict(message) message_dict["replies"] = replies_list message_list.append(message_dict) return message_list
def handle_file_save(self, json_req): """ json_req: { "p_id": process id "content": content of the file "comment": comment for file-save, defaults to None } """ p_id = json_req['p_id'] content = json_req['content'] comment = json_req.get('comment', "") user = User.verify_auth_token(json_req['token']) perm = self.permission_check_emit(user.id, int(p_id)) # if permission is correct and file saved properly if perm and self.fm.save_file(int(p_id), content, user, comment): # send service message message_ = "[service message] saved changes" new_message = self.cm.add_message( user, message_, str(p_id), message_type=MessageType.SYSTEM_MESSAGE) new_message_dict = get_message_dict(new_message) socketio.emit('chat-message-client', json.dumps(new_message_dict), room=str(p_id)) # emit file-changed event to trigger reload of flight track socketio.emit('file-changed', json.dumps({ "p_id": p_id, "u_id": user.id }), room=str(p_id))
def handle_file_save(self, json_req): """ json_req: { "op_id": operation id "content": content of the file "comment": comment for file-save, defaults to None } """ op_id = json_req['op_id'] content = json_req['content'] comment = json_req.get('comment', "") user = User.verify_auth_token(json_req['token']) if user is not None: # when the socket connection is expired this in None and also on wrong tokens perm = self.permission_check_emit(user.id, int(op_id)) # if permission is correct and file saved properly if perm and self.fm.save_file(int(op_id), content, user, comment): # send service message message_ = f"[service message] **{user.username}** saved changes" new_message = self.cm.add_message(user, message_, str(op_id), message_type=MessageType.SYSTEM_MESSAGE) new_message_dict = get_message_dict(new_message) socketio.emit('chat-message-client', json.dumps(new_message_dict)) # emit file-changed event to trigger reload of flight track socketio.emit('file-changed', json.dumps({"op_id": op_id, "u_id": user.id})) else: logging.debug(f'login expired for {user.username}, state unauthorized!')
def message_attachment(): file_token = secrets.token_urlsafe(16) file = request.files['file'] op_id = request.form.get("op_id", None) message_type = MessageType(int(request.form.get("message_type"))) user = g.user # ToDo review users = fm.fetch_users_without_permission(int(op_id), user.id) if users is False: return jsonify({"success": False, "message": "Could not send message. No file uploaded."}) if file is not None: with fs.open_fs('/') as home_fs: file_dir = fs.path.join(APP.config['UPLOAD_FOLDER'], op_id) if not home_fs.exists(file_dir): home_fs.makedirs(file_dir) file_name, file_ext = file.filename.rsplit('.', 1) file_name = f'{file_name}-{time.strftime("%Y%m%dT%H%M%S")}-{file_token}.{file_ext}' file_name = secure_filename(file_name) file_path = fs.path.join(file_dir, file_name) file.save(file_path) static_dir = fs.path.basename(APP.config['UPLOAD_FOLDER']) static_file_path = fs.path.join(static_dir, op_id, file_name) new_message = cm.add_message(user, static_file_path, op_id, message_type) new_message_dict = get_message_dict(new_message) sockio.emit('chat-message-client', json.dumps(new_message_dict)) return jsonify({"success": True, "path": static_file_path}) return jsonify({"success": False, "message": "Could not send message. No file uploaded."})
def handle_message(self, _json): """ json is a dictionary version of data sent to back-end """ op_id = _json['op_id'] reply_id = int(_json["reply_id"]) user = User.verify_auth_token(_json['token']) if user is not None: perm = self.permission_check_emit(user.id, int(op_id)) if perm: new_message = self.cm.add_message(user, _json['message_text'], str(op_id), reply_id=reply_id) new_message_dict = get_message_dict(new_message) if reply_id == -1: socketio.emit('chat-message-client', json.dumps(new_message_dict)) else: socketio.emit('chat-message-reply-client', json.dumps(new_message_dict))
def test_get_message_dict(self): result = get_message_dict(Message()) assert result["message_type"] == MessageType.TEXT