50 lines
1.6 KiB
Python
50 lines
1.6 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(role:str, otype:str, original:list) -> bytes:
|
|
if otype == 'a':
|
|
if role == "admin": rtype = niming_pb2.AdminFetchPostResponse
|
|
else: rtype = niming_pb2.FetchPostResponse
|
|
elif otype == 'c':
|
|
if role == "admin": rtype = niming_pb2.AdminFetchCommentResponse
|
|
else: rtype = niming_pb2.FetchCommentResponse
|
|
|
|
res = rtype()
|
|
|
|
for o in original:
|
|
# drop null object
|
|
o = {k:v for k, v in o.items() if v is not None}
|
|
|
|
if otype == "a":
|
|
ob = rtype.Message(id=o["id"])
|
|
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"])
|
|
# admin
|
|
if "mark" in o: ob.mark = o["mark"]
|
|
if "hash" in o: ob.hash = o["hash"]
|
|
if "ip" in o: ob.ip = o["ip"]
|
|
elif otype == "c":
|
|
ob = rtype.Message(sha1=o["sha1"])
|
|
# admin
|
|
if "mark" in o: ob.mark = o["mark"]
|
|
if "ip" in o: ob.ip = o["ip"]
|
|
if "hash" in o: ob.hash = o["hash"]
|
|
|
|
ob.content = o["content"]
|
|
|
|
res.posts.append(ob)
|
|
return res.SerializeToString()
|