OpenSlides/openslides/core/websocket.py

114 lines
3.6 KiB
Python
Raw Normal View History

2018-10-26 15:37:29 +02:00
from typing import Any
from ..utils.constants import get_constants
from ..utils.websocket import (
BaseWebsocketClientMessage,
ProtocollAsyncJsonWebsocketConsumer,
get_element_data,
)
class NotifyWebsocketClientMessage(BaseWebsocketClientMessage):
"""
Websocket message from a client to send a message to other clients.
"""
2019-01-06 16:22:33 +01:00
identifier = "notify"
2018-10-26 15:37:29 +02:00
schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Notify elements.",
"description": "Elements that one client can send to one or many other clients.",
"type": "array",
"items": {
"type": "object",
"properties": {
2019-01-06 16:22:33 +01:00
"projectors": {"type": "array", "items": {"type": "integer"}},
"reply_channels": {"type": "array", "items": {"type": "string"}},
"users": {"type": "array", "items": {"type": "integer"}},
},
2018-10-26 15:37:29 +02:00
},
"minItems": 1,
}
2019-01-06 16:22:33 +01:00
async def receive_content(
self, consumer: "ProtocollAsyncJsonWebsocketConsumer", content: Any, id: str
) -> None:
2018-10-26 15:37:29 +02:00
await consumer.channel_layer.group_send(
"site",
{
"type": "send_notify",
"incomming": content,
"senderReplyChannelName": consumer.channel_name,
2019-01-06 16:22:33 +01:00
"senderUserId": consumer.scope["user"]["id"],
2018-10-26 15:37:29 +02:00
},
)
class ConstantsWebsocketClientMessage(BaseWebsocketClientMessage):
"""
The Client requests the constants.
"""
2019-01-06 16:22:33 +01:00
identifier = "constants"
2018-10-26 15:37:29 +02:00
content_required = False
2019-01-06 16:22:33 +01:00
async def receive_content(
self, consumer: "ProtocollAsyncJsonWebsocketConsumer", content: Any, id: str
) -> None:
2018-10-26 15:37:29 +02:00
# Return all constants to the client.
2019-01-06 16:22:33 +01:00
await consumer.send_json(
type="constants", content=get_constants(), in_response=id
)
2018-10-26 15:37:29 +02:00
class GetElementsWebsocketClientMessage(BaseWebsocketClientMessage):
"""
The Client request database elements.
"""
2019-01-06 16:22:33 +01:00
identifier = "getElements"
2018-10-26 15:37:29 +02:00
schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"titel": "getElement request",
"description": "Request from the client to server to get elements.",
"type": "object",
"properties": {
# change_id is not required
2019-01-06 16:22:33 +01:00
"change_id": {"type": "integer"}
2018-10-26 15:37:29 +02:00
},
}
2019-01-06 16:22:33 +01:00
async def receive_content(
self, consumer: "ProtocollAsyncJsonWebsocketConsumer", content: Any, id: str
) -> None:
requested_change_id = content.get("change_id", 0)
2018-10-26 15:37:29 +02:00
try:
2019-01-06 16:22:33 +01:00
element_data = await get_element_data(
consumer.scope["user"]["id"], requested_change_id
)
2018-10-26 15:37:29 +02:00
except ValueError as error:
2019-01-06 16:22:33 +01:00
await consumer.send_json(type="error", content=str(error), in_response=id)
2018-10-26 15:37:29 +02:00
else:
2019-01-06 16:22:33 +01:00
await consumer.send_json(
type="autoupdate", content=element_data, in_response=id
)
2018-10-26 15:37:29 +02:00
class AutoupdateWebsocketClientMessage(BaseWebsocketClientMessage):
"""
The Client turns autoupdate on or off.
"""
2019-01-06 16:22:33 +01:00
identifier = "autoupdate"
async def receive_content(
self, consumer: "ProtocollAsyncJsonWebsocketConsumer", content: Any, id: str
) -> None:
2018-10-26 15:37:29 +02:00
# Turn on or off the autoupdate for the client
if content: # accept any value, that can be interpreted as bool
2019-01-06 16:22:33 +01:00
await consumer.channel_layer.group_add("autoupdate", consumer.channel_name)
2018-10-26 15:37:29 +02:00
else:
2019-01-06 16:22:33 +01:00
await consumer.channel_layer.group_discard(
"autoupdate", consumer.channel_name
)