niming_backend/utils/misc.py

38 lines
1.1 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(message:str) -> bytes:
return niming_pb2.PostResponse(
status = niming_pb2.Status.Failed,
failed_message = message
).SerializeToString()
def internal_json2protobuf(original:list|dict) -> bytes:
if isinstance(original, dict):
original = [original]
res = niming_pb2.FetchResponse()
for o in original:
# drop null object
newo = {}
for oc in o:
if o[oc] is not None:
newo[oc] = o[oc]
o = newo
ob = niming_pb2.FetchResponse.Message()
if "id" in o: ob.id = o["id"]
if "content" in o: ob.content = o["content"]
if "igid" in o: ob.igid = o["igid"]
if "files_hash" in o: ob.files_hash.extend(o["files_hash"])
if "comments_hash" in o: ob.comments_hash.extend(o["comments_hash"])
res.posts.append(ob)
return res.SerializeToString()