tiny refactor /longpoltiny and /longpolll

This commit is contained in:
2025-08-07 20:44:23 +03:00
parent 829da1f55f
commit 55ca235946

View File

@@ -11,13 +11,13 @@ import os
app = Sanic(__name__) app = Sanic(__name__)
monitor(app).expose_endpoint() monitor(app).expose_endpoint()
INVENTORY_API_KEY = os.environ["INVENTORY_API_KEY"] DOORBOY_SECRET_FLOOR = os.environ["DOORBOY_SECRET_FLOOR"] # API key for godoor controllers authenticating for k-space:floor
DOORBOY_SECRET_FLOOR = os.environ["DOORBOY_SECRET_FLOOR"] DOORBOY_SECRET_WORKSHOP = os.environ["DOORBOY_SECRET_WORKSHOP"] # API key for godoor controllers authenticating for k-space:workshop
DOORBOY_SECRET_WORKSHOP = os.environ["DOORBOY_SECRET_WORKSHOP"]
CARD_URI = os.environ["CARD_URI"]
FLOOR_ACCESS_GROUP = os.environ["FLOOR_ACCESS_GROUP"] FLOOR_ACCESS_GROUP = os.environ["FLOOR_ACCESS_GROUP"]
WORKSHOP_ACCESS_GROUP = os.environ["WORKSHOP_ACCESS_GROUP"] WORKSHOP_ACCESS_GROUP = os.environ["WORKSHOP_ACCESS_GROUP"]
MONGO_URI = os.environ["MONGO_URI"] MONGO_URI = os.environ["MONGO_URI"]
INVENTORY_API_KEY = os.environ["INVENTORY_API_KEY"] # asshole forwards to inventory-app, instead of using mongo directly
CARD_URI = os.environ["CARD_URI"]
SWIPE_URI = os.environ["SWIPE_URI"] SWIPE_URI = os.environ["SWIPE_URI"]
assert len(DOORBOY_SECRET_FLOOR) >= 10 assert len(DOORBOY_SECRET_FLOOR) >= 10
@@ -70,10 +70,7 @@ async def view_open_door_events(request):
results = await app.ctx.db.eventlog.find({ results = await app.ctx.db.eventlog.find({
"component": "doorboy", "component": "doorboy",
"type": "open-door", "type": "open-door",
"$or": [ "approved": True,
{ "approved": True },
{ "success": True },
],
"$or": [ "$or": [
{ "type": "open-door" }, { "type": "open-door" },
{ "event": "card-swiped" }, { "event": "card-swiped" },
@@ -106,7 +103,8 @@ async def view_longpoll(request):
key = request.headers.get("KEY") key = request.headers.get("KEY")
if not key or key not in [DOORBOY_SECRET_FLOOR, DOORBOY_SECRET_WORKSHOP]: if not key or key not in [DOORBOY_SECRET_FLOOR, DOORBOY_SECRET_WORKSHOP]:
return text("Invalid token") return text("Invalid token")
# authenticate
response = await request.respond(content_type="text/event-stream") response = await request.respond(content_type="text/event-stream")
await response.send("data: response-generator-started\n\n") await response.send("data: response-generator-started\n\n")
pipeline = [ pipeline = [
@@ -120,19 +118,26 @@ async def view_longpoll(request):
async with app.ctx.db.eventlog.watch(pipeline) as stream: async with app.ctx.db.eventlog.watch(pipeline) as stream:
await response.send("data: watch-stream-opened\n\n") await response.send("data: watch-stream-opened\n\n")
async for event in stream: async for event in stream:
if event["fullDocument"].get("type") == "open-door" and event["fullDocument"].get("approved", False): ev = event["fullDocument"]
await response.send("data: %s\n\n" % if ev["approved"] != "true":
event["fullDocument"]["door"]) continue
except pymongo.errors.PyMongoError as e: if ev["type"] == "token":
continue
response.send("data: %s\n\n" % ev["door"])
except PyMongoError as e:
print(e) print(e)
await response.send("data: response-generator-ended\n\n") await response.send("data: response-generator-ended\n\n")
return return
@app.post("/swipe") @app.post("/swipe")
async def forward_swipe(request): async def forward_swipe(request):
# authenticate
key = request.headers.get("KEY") key = request.headers.get("KEY")
if not key or key not in [DOORBOY_SECRET_FLOOR, DOORBOY_SECRET_WORKSHOP]: if not key or key not in [DOORBOY_SECRET_FLOOR, DOORBOY_SECRET_WORKSHOP]:
return text("Invalid token", status=401) return text("Invalid token", status=401)
# authorize
data = request.json data = request.json
doors = set() doors = set()
if key == DOORBOY_SECRET_FLOOR: if key == DOORBOY_SECRET_FLOOR: