Проработан API сценариев

Этот коммит содержится в:
Глеб Иваницкий 2024-08-29 18:05:07 +03:00
родитель 3df0db1bee
Коммит 6b9a073457
2 изменённых файлов: 30 добавлений и 18 удалений

Просмотреть файл

@ -62,7 +62,7 @@ scripts = [
] ]
scripts_cm = ConnectionManager() connection_manager = ConnectionManager()
@api.middleware('http') @api.middleware('http')
@ -123,8 +123,12 @@ async def _(
'time': time, 'time': time,
'messageNumber': message_number, 'messageNumber': message_number,
'isEnabled': is_enabled, 'isEnabled': is_enabled,
}, }
scripts.append(script) scripts.append(script)
asyncio.ensure_future(connection_manager.broadcast({
'action': 'insert',
'target': script,
}))
return Response( return Response(
status_code=201, status_code=201,
) )
@ -152,6 +156,10 @@ async def _(
scripts[script_id]['time'] = time scripts[script_id]['time'] = time
scripts[script_id]['messageNumber'] = message_number scripts[script_id]['messageNumber'] = message_number
scripts[script_id]['isEnabled'] = is_enabled scripts[script_id]['isEnabled'] = is_enabled
asyncio.ensure_future(connection_manager.broadcast({
'action': 'update',
'target': scripts[script_id],
}))
return Response( return Response(
status_code=201, status_code=201,
) )
@ -163,22 +171,26 @@ async def _(
async def _( async def _(
script_id: int = Path(), script_id: int = Path(),
): ):
script = scripts[script_id].copy()
scripts[script_id] = None scripts[script_id] = None
asyncio.ensure_future(connection_manager.broadcast({
'action': 'delete',
'target': script,
}))
return Response( return Response(
status_code=204, status_code=204,
) )
@api.websocket( @api.websocket(
path='/ws/scripts', path='/ws',
) )
async def _( async def _(
websocket: WebSocket, websocket: WebSocket,
): ):
await scripts_cm.connect(websocket) await connection_manager.connect(websocket)
try: try:
await scripts_cm.broadcast(scripts)
while True: while True:
await websocket.receive_json() await websocket.receive_json()
except WebSocketDisconnect: except WebSocketDisconnect:
scripts_cm.disconnect(websocket) connection_manager.disconnect(websocket)

Просмотреть файл

@ -15,6 +15,18 @@ create table users (
unique (telegram_user_id) 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 ( create table poll_schemas (
id bigserial not null, id bigserial not null,
owner_id bigint not null, owner_id bigint not null,
@ -36,18 +48,6 @@ create table poll_options (
unique (poll_schema_id, ordinal) 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 ( create table poll_messages (
telegram_poll_id text not null, telegram_poll_id text not null,
poll_schema_id bigint not null, poll_schema_id bigint not null,