niming_backend/utils/misc.py
2024-11-26 09:38:28 +00:00

32 lines
909 B
Python

from flask import jsonify, Response
from protobuf_files import niming_pb2
def error(message:str) -> Response:
return jsonify({"error":message})
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()