From 6b9a0734578139e998f081ddb41c4738c8b7cbae Mon Sep 17 00:00:00 2001 From: csasq Date: Thu, 29 Aug 2024 18:05:07 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=20API=20=D1=81=D1=86=D0=B5=D0=BD=D0=B0=D1=80?= =?UTF-8?q?=D0=B8=D0=B5=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/main.py | 24 ++++++++++++++++++------ database.sql | 24 ++++++++++++------------ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/api/main.py b/api/main.py index f1a28e0..e64ad5f 100644 --- a/api/main.py +++ b/api/main.py @@ -62,7 +62,7 @@ scripts = [ ] -scripts_cm = ConnectionManager() +connection_manager = ConnectionManager() @api.middleware('http') @@ -123,8 +123,12 @@ async def _( 'time': time, 'messageNumber': message_number, 'isEnabled': is_enabled, - }, + } scripts.append(script) + asyncio.ensure_future(connection_manager.broadcast({ + 'action': 'insert', + 'target': script, + })) return Response( status_code=201, ) @@ -152,6 +156,10 @@ async def _( scripts[script_id]['time'] = time scripts[script_id]['messageNumber'] = message_number scripts[script_id]['isEnabled'] = is_enabled + asyncio.ensure_future(connection_manager.broadcast({ + 'action': 'update', + 'target': scripts[script_id], + })) return Response( status_code=201, ) @@ -163,22 +171,26 @@ async def _( async def _( script_id: int = Path(), ): + script = scripts[script_id].copy() scripts[script_id] = None + asyncio.ensure_future(connection_manager.broadcast({ + 'action': 'delete', + 'target': script, + })) return Response( status_code=204, ) @api.websocket( - path='/ws/scripts', + path='/ws', ) async def _( websocket: WebSocket, ): - await scripts_cm.connect(websocket) + await connection_manager.connect(websocket) try: - await scripts_cm.broadcast(scripts) while True: await websocket.receive_json() except WebSocketDisconnect: - scripts_cm.disconnect(websocket) + connection_manager.disconnect(websocket) diff --git a/database.sql b/database.sql index 2b11652..f4d51b4 100644 --- a/database.sql +++ b/database.sql @@ -15,6 +15,18 @@ create table users ( unique (telegram_user_id) ); +create table messages ( + id bigserial not null, + telegram_message_id bigint not null, + created_at timestamp default now() not null, + primary key (id), + unique (telegram_message_id) +); + +create table text_messages ( + text character varying (4096) not null +) inherits (messages); + create table poll_schemas ( id bigserial not null, owner_id bigint not null, @@ -36,18 +48,6 @@ create table poll_options ( unique (poll_schema_id, ordinal) ); -create table messages ( - id bigserial not null, - telegram_message_id bigint not null, - created_at timestamp default now() not null, - primary key (id), - unique (telegram_message_id) -); - -create table text_messages ( - text character varying (4096) not null -) inherits (messages); - create table poll_messages ( telegram_poll_id text not null, poll_schema_id bigint not null,