niming_backend/utils/misc.py

32 lines
909 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 internal_json2protobuf(original:list|dict) -> bytes:
if isinstance(original, dict):
original = [original]
2024-11-26 09:17:44 +08:00
res = niming_pb2.FetchResponse(status = niming_pb2.Status.Success)
2024-11-25 21:51:50 +08:00
for o in original:
2024-11-26 09:17:44 +08:00
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"]:
2024-11-25 21:51:50 +08:00
ob.ref = o["reference"]
2024-11-26 09:17:44 +08:00
if "ip" in o:
ob.ip = o["ip"]
if "hash" in o:
ob.hash = o["hash"]
2024-11-25 21:51:50 +08:00
res.posts.append(ob)
return res.SerializeToString()