niming_backend/utils/misc.py

32 lines
851 B
Python
Raw Normal View History

2024-11-19 21:22:01 +08:00
from flask import jsonify, Response
2024-11-25 21:51:50 +08:00
from protobuf_files import niming_pb2
2024-11-19 21:22:01 +08:00
def error(message:str) -> Response:
2024-11-25 21:51:50 +08:00
return jsonify({"error":message})
def error_proto(message:str) -> Response:
proto = niming_pb2.PostResponse()
proto.status = niming_pb2.PostStatus.Failed
proto.hash = ""
proto.id = 0
proto.failed_message = message
return proto.SerializeToString()
def internal_json2protobuf(original:list|dict) -> bytes:
if isinstance(original, dict):
original = [original]
res = niming_pb2.FetchResponse()
for o in original:
ob = niming_pb2.FetchResponse.Message()
ob.id = o["id"]
ob.content = o["content"]
if o["reference"]:
ob.ref = o["reference"]
ob.files_id.extend(o["files"])
res.posts.append(ob)
return res.SerializeToString()