Optimize slack door open
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Madis Mägi 2024-10-31 20:02:08 +02:00
parent 17648a840f
commit 7b6921e44d

View File

@ -1,6 +1,7 @@
import os import os
import re import re
import const import const
import threading
from datetime import datetime, timedelta from datetime import datetime, timedelta
from functools import wraps from functools import wraps
from pymongo import MongoClient from pymongo import MongoClient
@ -61,7 +62,6 @@ def get_group_cards():
@page_api.route("/api/slack/doorboy", methods=['POST']) @page_api.route("/api/slack/doorboy", methods=['POST'])
def view_slack_doorboy(): def view_slack_doorboy():
print(request.data)
if request.form.get("token") != const.SLACK_VERIFICATION_TOKEN: if request.form.get("token") != const.SLACK_VERIFICATION_TOKEN:
return "Invalid token was supplied" return "Invalid token was supplied"
if request.form.get("channel_id") not in ("C01CWPF5H8W", "CDL9H8Q9W"): if request.form.get("channel_id") not in ("C01CWPF5H8W", "CDL9H8Q9W"):
@ -79,7 +79,6 @@ def view_slack_doorboy():
return "Invalid command was supplied" return "Invalid command was supplied"
member = None member = None
print(g.users)
for user in g.users: for user in g.users:
if user.slack_id == request.form.get("user_id"): if user.slack_id == request.form.get("user_id"):
member = user member = user
@ -94,25 +93,29 @@ def view_slack_doorboy():
if door == "outsidedoors": if door == "outsidedoors":
doors = ["backdoor", "frontdoor", "grounddoor"] doors = ["backdoor", "frontdoor", "grounddoor"]
for d in doors:
db.eventlog.insert_one({
"method": "slack",
"approved": approved,
"duration": 5,
"component": "doorboy",
"type": "open-door",
"door": d,
"member_id": member.username,
"member": member.display_name,
"timestamp": datetime.utcnow(),
})
status = "Permitted" if approved else "Denied" status = "Permitted" if approved else "Denied"
subject = member.display_name subject = member.display_name
msg = "%s %s door access for %s via Slack bot" % (status, door, subject)
slack_post(msg, "doorboy") threading.Thread(target=handle_slack_door_event, args=(doors, approved, member, door, status, subject)).start()
if approved: if approved:
return "Opening %s for %s" % (door, subject) return "Opening %s for %s" % (door, subject)
else: else:
return "Permission denied" return "Permission denied"
def handle_slack_door_event(doors, approved, member, door, status, subject):
for d in doors:
db.eventlog.insert_one({
"method": "slack",
"approved": approved,
"duration": 5,
"component": "doorboy",
"type": "open-door",
"door": d,
"member_id": member.username,
"member": member.display_name,
"timestamp": datetime.utcnow(),
})
msg = "%s %s door access for %s via Slack bot" % (status, door, subject)
slack_post(msg, "doorboy")