niming_backend/utils/misc.py
2024-11-26 01:17:44 +00:00

44 lines
1.2 KiB
Python

from flask import jsonify, Response
from protobuf_files import niming_pb2
def error(message:str) -> Response:
return jsonify({"error":message})
def error_proto(type:str, message:str) -> Response:
if type == "post":
proto = niming_pb2.PostResponse()
proto.hash = ""
proto.id = 0
elif type == "fetch":
proto = niming_pb2.FetchResponse()
proto.status = niming_pb2.Status.Failed
proto.failed_message = message
return proto.SerializeToString()
def internal_json2protobuf(original:list|dict) -> bytes:
if isinstance(original, dict):
original = [original]
res = niming_pb2.FetchResponse(status = niming_pb2.Status.Success)
for o in original:
ob = niming_pb2.FetchResponse.Message(
id = o["id"],
content = o["content"],
igid = o["igid"],
mark = o["mark"],
files_id = o["files_id"]
)
if None not in o["comments"]:
ob.comments_id.extend(o["comments"])
if o["reference"]:
ob.ref = o["reference"]
if "ip" in o:
ob.ip = o["ip"]
if "hash" in o:
ob.hash = o["hash"]
res.posts.append(ob)
return res.SerializeToString()