2019-05-06 13:00:40 +02:00
|
|
|
from typing import Any, Dict, Optional
|
2018-10-26 15:37:29 +02:00
|
|
|
|
2019-05-03 17:39:48 +02:00
|
|
|
from ..utils.auth import async_has_perm
|
2018-10-26 15:37:29 +02:00
|
|
|
from ..utils.constants import get_constants
|
2019-01-10 15:06:10 +01:00
|
|
|
from ..utils.projector import get_projector_data
|
2019-05-06 13:00:40 +02:00
|
|
|
from ..utils.stats import WebsocketLatencyLogger
|
2018-10-26 15:37:29 +02:00
|
|
|
from ..utils.websocket import (
|
2019-05-14 15:46:48 +02:00
|
|
|
WEBSOCKET_CHANGE_ID_TOO_HIGH,
|
|
|
|
WEBSOCKET_NOT_AUTHORIZED,
|
2018-10-26 15:37:29 +02:00
|
|
|
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#",
|
2018-10-17 18:04:06 +02:00
|
|
|
"title": "Notify element.",
|
|
|
|
"description": "Element that one client can send to one or many other clients.",
|
|
|
|
"type": "object",
|
|
|
|
"properties": {
|
|
|
|
"name": {"description": "The name of the notify message", "type": "string"},
|
|
|
|
"content": {"description": "The actual content of this message."},
|
|
|
|
"reply_channels": {
|
|
|
|
"description": "A list of channels to send this message to.",
|
|
|
|
"type": "array",
|
|
|
|
"items": {"type": "string"},
|
|
|
|
},
|
|
|
|
"users": {
|
|
|
|
"anyOf": [
|
|
|
|
{
|
|
|
|
"description": "A list of user ids to send this message to.",
|
|
|
|
"type": "array",
|
|
|
|
"items": {"type": "integer"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"description": "This flag indicates, that this message should be send to all users.",
|
|
|
|
"enum": [True],
|
|
|
|
},
|
|
|
|
]
|
2019-01-06 16:22:33 +01:00
|
|
|
},
|
2018-10-26 15:37:29 +02:00
|
|
|
},
|
2018-10-17 18:04:06 +02:00
|
|
|
"required": ["name", "content"],
|
2018-10-26 15:37:29 +02:00
|
|
|
}
|
2019-05-03 17:39:48 +02:00
|
|
|
# Define a required permission for a notify message here. If the emitting user does not
|
|
|
|
# have this permission, he will get an error message in response.
|
2019-07-30 09:01:59 +02:00
|
|
|
notify_permissions: Dict[str, str] = {"swCheckForUpdate": "superadmin"}
|
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:
|
2019-05-03 17:39:48 +02:00
|
|
|
# Check if the user is allowed to send this notify message
|
|
|
|
perm = self.notify_permissions.get(content["name"])
|
|
|
|
if perm is not None and not await async_has_perm(
|
|
|
|
consumer.scope["user"]["id"], perm
|
|
|
|
):
|
2019-05-14 15:46:48 +02:00
|
|
|
await consumer.send_error(
|
|
|
|
code=WEBSOCKET_NOT_AUTHORIZED,
|
|
|
|
message=f"You need '{perm}' to send this message.",
|
2019-05-03 17:39:48 +02:00
|
|
|
in_response=id,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
# Forward to all other active site consumers to handle the notify message.
|
|
|
|
await consumer.channel_layer.group_send(
|
|
|
|
"site",
|
|
|
|
{
|
|
|
|
"type": "send_notify",
|
|
|
|
"incomming": content,
|
|
|
|
"senderChannelName": consumer.channel_name,
|
|
|
|
"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-05-14 15:46:48 +02:00
|
|
|
await consumer.send_error(
|
|
|
|
code=WEBSOCKET_CHANGE_ID_TOO_HIGH, message=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
|
|
|
|
)
|
2018-12-23 11:05:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ListenToProjectors(BaseWebsocketClientMessage):
|
|
|
|
"""
|
|
|
|
The client tells, to which projector it listens.
|
|
|
|
|
|
|
|
Therefor it sends a list of projector ids. If it sends an empty list, it does
|
|
|
|
not want to get any projector information.
|
|
|
|
"""
|
|
|
|
|
|
|
|
identifier = "listenToProjectors"
|
|
|
|
schema = {
|
|
|
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
|
|
"titel": "Listen to projectors",
|
|
|
|
"description": "Listen to zero, one or more projectors..",
|
|
|
|
"type": "object",
|
|
|
|
"properties": {
|
|
|
|
"projector_ids": {
|
|
|
|
"type": "array",
|
|
|
|
"items": {"type": "integer"},
|
|
|
|
"uniqueItems": True,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"required": ["projector_ids"],
|
|
|
|
}
|
|
|
|
|
|
|
|
async def receive_content(
|
|
|
|
self, consumer: "ProtocollAsyncJsonWebsocketConsumer", content: Any, id: str
|
|
|
|
) -> None:
|
|
|
|
consumer.listen_projector_ids = content["projector_ids"]
|
|
|
|
if consumer.listen_projector_ids:
|
|
|
|
# listen to projector group
|
|
|
|
await consumer.channel_layer.group_add("projector", consumer.channel_name)
|
|
|
|
else:
|
|
|
|
# do not listen to projector group
|
|
|
|
await consumer.channel_layer.group_discard(
|
|
|
|
"projector", consumer.channel_name
|
|
|
|
)
|
|
|
|
|
|
|
|
# Send projector data
|
|
|
|
if consumer.listen_projector_ids:
|
2019-01-10 15:06:10 +01:00
|
|
|
projector_data = await get_projector_data(consumer.listen_projector_ids)
|
2018-12-23 11:05:38 +01:00
|
|
|
for projector_id, data in projector_data.items():
|
|
|
|
consumer.projector_hash[projector_id] = hash(str(data))
|
|
|
|
|
|
|
|
await consumer.send_json(
|
|
|
|
type="projector", content=projector_data, in_response=id
|
|
|
|
)
|
2019-04-15 14:19:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PingPong(BaseWebsocketClientMessage):
|
|
|
|
"""
|
|
|
|
Responds to pings from the client.
|
|
|
|
"""
|
|
|
|
|
|
|
|
identifier = "ping"
|
|
|
|
schema = {
|
|
|
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
|
|
"titel": "PingPong",
|
|
|
|
"description": "Does a ping pong handshake",
|
|
|
|
"anyOf": [{"type": "number"}, {"type": "null"}],
|
|
|
|
}
|
|
|
|
|
|
|
|
async def receive_content(
|
2019-05-06 13:00:40 +02:00
|
|
|
self,
|
|
|
|
consumer: "ProtocollAsyncJsonWebsocketConsumer",
|
|
|
|
latency: Optional[int],
|
|
|
|
id: str,
|
2019-04-15 14:19:40 +02:00
|
|
|
) -> None:
|
2019-05-06 13:00:40 +02:00
|
|
|
await consumer.send_json(type="pong", content=latency, in_response=id)
|
|
|
|
if latency is not None:
|
|
|
|
await WebsocketLatencyLogger.add_latency(latency)
|