From a8d0a7d886d864d142629eccf192355fca1cf2fa Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Thu, 21 Jul 2022 13:40:17 +0100 Subject: [PATCH] Implement more API functions --- api/views.py | 84 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 14 deletions(-) diff --git a/api/views.py b/api/views.py index 317a0d5..98f49bc 100644 --- a/api/views.py +++ b/api/views.py @@ -1,11 +1,12 @@ import functools -from json import dumps +from json import JSONDecodeError, dumps, loads from klein import Klein from twisted.web.server import Request import main from modules import userinfo +from utils.logging.log import warn def login_required(func): @@ -38,28 +39,83 @@ class API(object): app = Klein() - @app.route("/test/", methods=["GET"]) + @app.route("/", methods=["GET"]) @login_required def hello(self, request): return "Hello" - @app.route("/who//", methods=["GET"]) + @app.route("/who//", methods=["POST"]) @login_required def who(self, request, query): - result = userinfo.getWho(query) + try: + data = loads(request.content.read()) + except JSONDecodeError: + return "Invalid JSON" + if "query" not in data: + return "No query provided" + result = userinfo.getWho(data["query"]) # Expand the generator return dumps({k: [x for x in v] for k, v in result.items()}) - @app.route("/chans//", methods=["GET"]) + @app.route("/chans/", methods=["POST"]) @login_required - def chans(self, request, query): - query = query.split(",") - result = userinfo.getChans(query) - return dumps({k: [x for x in v] for k, v in result.items()}) + def chans(self, request): + try: + data = loads(request.content.read()) + except JSONDecodeError: + return "Invalid JSON" + if "net" not in data: + return "No net provided" + if "query" not in data: + return "No query provided" + if not data["query"]: + warn(f"No query provided: for chans {data}") + return dumps({}) + result = userinfo.getChansSingle(data["net"], data["query"]) + if not result: + return dumps({}) + return dumps({"chans": [x for x in result]}) - @app.route("/users//", methods=["GET"]) + @app.route("/users/", methods=["POST"]) @login_required - def users(self, request, query): - query = [f"#{x}" for x in query.split(",")] - result = userinfo.getUsers(query) - return dumps({k: [x for x in v] for k, v in result.items()}) + def users(self, request): + try: + data = loads(request.content.read()) + except JSONDecodeError: + return "Invalid JSON" + if "net" not in data: + return "No net provided" + if "query" not in data: + return "No query provided" + if not data["query"]: + warn(f"No query provided for users: {data}") + return dumps({}) + result = userinfo.getUsersSingle(data["net"], data["query"]) + if not result: + return dumps({}) + return dumps({"users": [x for x in result]}) + + @app.route("/online/", methods=["POST"]) + @login_required + def online(self, request): + try: + data = loads(request.content.read()) + except JSONDecodeError: + return "Invalid JSON" + if "net" not in data: + return "No net provided" + if "query" not in data: + return "No users provided" + if not data["query"]: + warn(f"No users provided: for online {data}") + return dumps({}) + net = data["net"] + usermap = {} + for user in data["query"]: + channels = userinfo.getChansSingle(net, [user]) + if channels: + usermap[user] = True + else: + usermap[user] = False + + return dumps(usermap)