make it like actually work or something

This commit is contained in:
Micha Albert 2024-07-28 23:23:28 +00:00
parent b5ec309ccb
commit 60c7f70fe5

View file

@ -9,16 +9,37 @@ import uvicorn
from dotenv import load_dotenv from dotenv import load_dotenv
from fastapi import FastAPI, Request, Response from fastapi import FastAPI, Request, Response
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from fastapi_utils.tasks import repeat_every
from prisma import Prisma from prisma import Prisma
from slack_bolt.adapter.fastapi.async_handler import AsyncSlackRequestHandler from slack_bolt.adapter.fastapi.async_handler import AsyncSlackRequestHandler
from slack_bolt.async_app import AsyncAck, AsyncApp from slack_bolt.async_app import AsyncAck, AsyncApp
import asyncio
load_dotenv() load_dotenv()
async def update_active():
global active_stream
async with httpx.AsyncClient() as client:
streams = (await client.get("http://localhost:9997/v3/paths/list")).json()[
"items"
]
active_streams = []
for stream in streams:
if stream["ready"]:
active_streams.append(stream)
active_stream = choice(active_streams)["name"]
async def init_active_update_task():
global active_stream
while True:
asyncio.create_task(update_active())
await asyncio.sleep(5 * 60)
@asynccontextmanager @asynccontextmanager
async def lifespan(app: FastAPI): async def lifespan(app: FastAPI):
asyncio.create_task(init_active_update_task())
await db.connect() await db.connect()
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
for stream in await db.stream.find_many(): for stream in await db.stream.find_many():
@ -26,8 +47,8 @@ async def lifespan(app: FastAPI):
"http://127.0.0.1:9997/v3/config/paths/add/" + stream.key, "http://127.0.0.1:9997/v3/config/paths/add/" + stream.key,
json={"name": stream.key}, json={"name": stream.key},
) )
yield
await db.disconnect() await db.disconnect()
yield
api = FastAPI(lifespan=lifespan) api = FastAPI(lifespan=lifespan)
@ -41,7 +62,6 @@ api.add_middleware(
db = Prisma() db = Prisma()
bolt = AsyncApp( bolt = AsyncApp(
token=os.environ["SLACK_TOKEN"], signing_secret=os.environ["SLACK_SIGNING_SECRET"] token=os.environ["SLACK_TOKEN"], signing_secret=os.environ["SLACK_SIGNING_SECRET"]
) )
@ -71,6 +91,7 @@ async def get_user_by_id(user_id: str):
@api.get("/api/v1/active_stream") @api.get("/api/v1/active_stream")
async def get_active_stream(): async def get_active_stream():
global active_stream
return active_stream return active_stream
@ -156,6 +177,7 @@ async def approve(ack, body):
channel=sumbitter_convo["channel"]["id"], channel=sumbitter_convo["channel"]["id"],
text=f"Your application has been approved! Your stream key is `{new_stream.key}`. Keep this safe and do not share it with anyone!", text=f"Your application has been approved! Your stream key is `{new_stream.key}`. Keep this safe and do not share it with anyone!",
) )
await db.disconnect()
@bolt.view("apply") @bolt.view("apply")
@ -184,8 +206,8 @@ async def handle_application_submission(ack, body):
users=os.environ["ADMIN_SLACK_ID"], return_im=True users=os.environ["ADMIN_SLACK_ID"], return_im=True
) )
will_behave = False will_behave = False
boxes = body['view']['state']['values']['c+wGI']['checkboxes']['selected_options'] boxes = body["view"]["state"]["values"]["c+wGI"]["checkboxes"]["selected_options"]
if len(boxes) == 1 and boxes[0]["value"] == 'value-1': if len(boxes) == 1 and boxes[0]["value"] == "value-1":
will_behave = True will_behave = True
await bolt.client.chat_postMessage( await bolt.client.chat_postMessage(
channel=admin_convo["channel"]["id"], channel=admin_convo["channel"]["id"],
@ -392,22 +414,11 @@ async def slack_event_endpoint(req: Request):
return await bolt_handler.handle(req) return await bolt_handler.handle(req)
@repeat_every(seconds=5 * 60, wait_first=True) # @api.on_event("startup")
async def change_active_stream(): # @repeat_every(seconds=5, wait_first=False,raise_exceptions=True)
global active_stream # async def change_active_stream() -> None:
streams = [] # print('e')
await db.connect() # global active_stream
for stream in await db.stream.find_many(): # with httpx.AsyncClient as client:
streams.append(stream.id) # streams = (await client.get("http://localhost:9997/v3/paths/list")).json["items"]
if len(streams) == 0: # active_stream = choice(streams)["name"]
return
if active_stream not in streams:
active_stream = None
if active_stream is None:
active_stream = choice(streams)
else:
if streams.index(active_stream) + 1 == len(streams):
active_stream = streams[0]
else:
active_stream = streams[streams.index(active_stream) + 1]
bolt.client.chat_postMessage(channel="C07ERCGG989", text=f":partyparrot_wave1::partyparrot_wave2::partyparrot_wave3::partyparrot_wave4::partyparrot_wave5::partyparrot_wave6::partyparrot_wave7: Hey <@{(await db.stream.find_first(where={'id': active_stream})).user.slack_id}>, you're in focus right now! Remember to talk us through what you're doing!") # type: ignore