From 68ae04e72a6e72c8b73d5bcf7f366a2cb8146236 Mon Sep 17 00:00:00 2001 From: SouthFox Date: Sat, 29 Jul 2023 20:49:12 +0800 Subject: [PATCH] [lint] main.py --- app/main.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/app/main.py b/app/main.py index 9c1854d..03ac834 100644 --- a/app/main.py +++ b/app/main.py @@ -31,21 +31,25 @@ from app.boxes import _send_create from app.orgpython import to_html - def _check_0rtt_early_data(request: Request) -> None: """Disable TLS1.3 0-RTT requests for non-GET.""" - if request.headers.get("Early-Data", None) == "1" and request.method != "GET": + if request.headers.get("Early-Data", None) == "1" \ + and request.method != "GET": raise fastapi.HTTPException(status_code=425, detail="Too early") + app = FastAPI( - docs_url=None, redoc_url=None, dependencies=[Depends(_check_0rtt_early_data)] + docs_url=None, redoc_url=None, + dependencies=[Depends(_check_0rtt_early_data)] ) logger.remove() logger.add(sys.stdout, level="DEBUG" if DEBUG else "INFO") logger.add("output.log", level="DEBUG") -class ActivityPubResponse(JSONResponse): #pylint: disable=too-few-public-methods + +# pylint: disable=too-few-public-methods +class ActivityPubResponse(JSONResponse): """Simple wrap JSONresponse return ActivityPub response.""" media_type = "application/activity+json" @@ -78,9 +82,9 @@ async def index( @app.post("/inbox") async def inbox( - request: Request, - db_session: AsyncSession = Depends(get_db_session), - httpsig_checker = Depends(precheck.inbox_prechecker), + request: Request, + db_session: AsyncSession = Depends(get_db_session), + httpsig_checker=Depends(precheck.inbox_prechecker), ) -> Response: """ActivityPub inbox endpoint.""" payload = await request.json() @@ -111,13 +115,19 @@ async def outbox( logger.info(content) post_token = request.headers.get("Authorization") - if POST_TOKEN is None \ - and POST_TOKEN != post_token.split(" ")[1]: # type: ignore + def _check_post_token() -> bool: + if post_token is None: + return False + if POST_TOKEN != post_token.split(" ")[1]: + return False + + return True + + if not _check_post_token(): logger.warning("Non-valid post token!") return Response(status_code=406) logger.info("True token") - content = to_html(content).replace("\n", "") await _send_create( @@ -205,7 +215,7 @@ async def nodeinfo(): }, "protocols": ["activitypub"], "services": {"inbound": [], "outbound": []}, - "usage": {"users": {"total": 1}, "localPosts": 0}, #TODO + "usage": {"users": {"total": 1}, "localPosts": 0}, # TODO "openRegistrations": False, "metadata": {}, },