2019-05-06 13:00:40 +02:00
|
|
|
import time
|
2019-10-18 14:41:55 +02:00
|
|
|
from typing import Any, Dict, List, Optional, cast
|
2018-10-19 16:32:48 +02:00
|
|
|
from urllib.parse import parse_qs
|
2018-07-09 23:22:26 +02:00
|
|
|
|
2019-10-15 10:54:31 +02:00
|
|
|
from channels.generic.websocket import AsyncWebsocketConsumer
|
|
|
|
|
2019-08-29 14:25:02 +02:00
|
|
|
from . import logging
|
2019-11-29 12:59:34 +01:00
|
|
|
from .auth import UserDoesNotExist, async_anonymous_is_enabled
|
2020-05-15 18:24:21 +02:00
|
|
|
from .cache import element_cache
|
|
|
|
from .consumer_autoupdate_strategy import ConsumerAutoupdateStrategy
|
2019-05-06 13:00:40 +02:00
|
|
|
from .utils import get_worker_id
|
2020-05-15 18:24:21 +02:00
|
|
|
from .websocket import BaseWebsocketException, ProtocollAsyncJsonWebsocketConsumer
|
2018-08-22 16:50:23 +02:00
|
|
|
|
|
|
|
|
2019-05-06 13:00:40 +02:00
|
|
|
logger = logging.getLogger("openslides.websocket")
|
|
|
|
|
|
|
|
|
2018-08-22 16:50:23 +02:00
|
|
|
class SiteConsumer(ProtocollAsyncJsonWebsocketConsumer):
|
2018-07-09 23:22:26 +02:00
|
|
|
"""
|
|
|
|
Websocket Consumer for the site.
|
|
|
|
"""
|
2018-10-19 16:32:48 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
groups = ["site"]
|
2018-07-09 23:22:26 +02:00
|
|
|
|
2019-05-06 13:00:40 +02:00
|
|
|
ID_COUNTER = 0
|
|
|
|
"""
|
|
|
|
ID counter for assigning each instance of this class an unique id.
|
|
|
|
"""
|
|
|
|
|
2018-12-23 11:05:38 +01:00
|
|
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
|
|
self.projector_hash: Dict[int, int] = {}
|
2019-05-06 13:00:40 +02:00
|
|
|
SiteConsumer.ID_COUNTER += 1
|
|
|
|
self._id = get_worker_id() + "-" + str(SiteConsumer.ID_COUNTER)
|
2020-05-15 18:24:21 +02:00
|
|
|
self.autoupdate_strategy = ConsumerAutoupdateStrategy(self)
|
2018-12-23 11:05:38 +01:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
async def connect(self) -> None:
|
|
|
|
"""
|
|
|
|
A user connects to the site.
|
|
|
|
|
|
|
|
If it is an anonymous user and anonymous is disabled, the connection is closed.
|
|
|
|
|
|
|
|
Sends the startup data to the user.
|
|
|
|
"""
|
2020-05-15 18:24:21 +02:00
|
|
|
self.user_id = self.scope["user"]["id"]
|
|
|
|
|
2019-05-06 13:00:40 +02:00
|
|
|
self.connect_time = time.time()
|
2018-11-03 23:40:20 +01:00
|
|
|
# self.scope['user'] is the full_data dict of the user. For an
|
|
|
|
# anonymous user is it the dict {'id': 0}
|
2018-10-19 16:32:48 +02:00
|
|
|
change_id = None
|
2020-05-15 18:24:21 +02:00
|
|
|
if not await async_anonymous_is_enabled() and not self.user_id:
|
2019-01-24 16:47:53 +01:00
|
|
|
await self.accept() # workaround for #4009
|
2018-07-09 23:22:26 +02:00
|
|
|
await self.close()
|
2019-05-06 13:00:40 +02:00
|
|
|
logger.debug(f"connect: denied ({self._id})")
|
2018-10-19 16:32:48 +02:00
|
|
|
return
|
|
|
|
|
2019-10-18 14:41:55 +02:00
|
|
|
query_string = cast(
|
|
|
|
Dict[bytes, List[bytes]], parse_qs(self.scope["query_string"])
|
|
|
|
)
|
2019-01-06 16:22:33 +01:00
|
|
|
if b"change_id" in query_string:
|
2018-10-19 16:32:48 +02:00
|
|
|
try:
|
2019-01-06 16:22:33 +01:00
|
|
|
change_id = int(query_string[b"change_id"][0])
|
2018-10-19 16:32:48 +02:00
|
|
|
except ValueError:
|
2019-01-24 16:47:53 +01:00
|
|
|
await self.accept() # workaround for #4009
|
2020-05-15 18:24:21 +02:00
|
|
|
await self.close()
|
2019-05-06 13:00:40 +02:00
|
|
|
logger.debug(f"connect: wrong change id ({self._id})")
|
2018-10-19 16:32:48 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
await self.accept()
|
2018-11-02 05:10:49 +01:00
|
|
|
|
2018-10-19 16:32:48 +02:00
|
|
|
if change_id is not None:
|
2019-05-06 13:00:40 +02:00
|
|
|
logger.debug(f"connect: change id {change_id} ({self._id})")
|
2020-05-15 18:24:21 +02:00
|
|
|
try:
|
|
|
|
await self.request_autoupdate(change_id)
|
|
|
|
except BaseWebsocketException as e:
|
|
|
|
await self.send_exception(e)
|
2019-05-06 13:00:40 +02:00
|
|
|
else:
|
|
|
|
logger.debug(f"connect: no change id ({self._id})")
|
2018-07-09 23:22:26 +02:00
|
|
|
|
2020-05-15 18:24:21 +02:00
|
|
|
await self.channel_layer.group_add("autoupdate", self.channel_name)
|
|
|
|
|
2018-10-19 16:32:48 +02:00
|
|
|
async def disconnect(self, close_code: int) -> None:
|
|
|
|
"""
|
|
|
|
A user disconnects. Remove it from autoupdate.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
await self.channel_layer.group_discard("autoupdate", self.channel_name)
|
2019-05-06 13:00:40 +02:00
|
|
|
active_seconds = int(time.time() - self.connect_time)
|
|
|
|
logger.debug(
|
|
|
|
f"disconnect code={close_code} active_secs={active_seconds} ({self._id})"
|
|
|
|
)
|
2018-10-19 16:32:48 +02:00
|
|
|
|
2020-05-15 18:24:21 +02:00
|
|
|
async def msg_new_change_id(self, event: Dict[str, Any]) -> None:
|
2018-07-09 23:22:26 +02:00
|
|
|
"""
|
2020-05-15 18:24:21 +02:00
|
|
|
Send changed or deleted elements to the user.
|
2018-07-09 23:22:26 +02:00
|
|
|
"""
|
2020-05-15 18:24:21 +02:00
|
|
|
change_id = event["change_id"]
|
2019-07-29 15:19:59 +02:00
|
|
|
try:
|
2020-05-15 18:24:21 +02:00
|
|
|
await self.autoupdate_strategy.new_change_id(change_id)
|
2019-11-29 12:59:34 +01:00
|
|
|
except UserDoesNotExist:
|
|
|
|
# Maybe the user was deleted, but a websocket connection is still open to the user.
|
|
|
|
# So we can close this connection and return.
|
|
|
|
await self.close()
|
2018-12-23 11:05:38 +01:00
|
|
|
|
2020-05-15 18:24:21 +02:00
|
|
|
async def msg_projector_data(self, event: Dict[str, Any]) -> None:
|
2018-12-23 11:05:38 +01:00
|
|
|
"""
|
|
|
|
The projector has changed.
|
|
|
|
"""
|
|
|
|
all_projector_data = event["data"]
|
2019-10-01 15:36:59 +02:00
|
|
|
change_id = event["change_id"]
|
|
|
|
|
2018-12-23 11:05:38 +01:00
|
|
|
projector_data: Dict[int, Dict[str, Any]] = {}
|
|
|
|
for projector_id in self.listen_projector_ids:
|
2019-02-01 13:56:08 +01:00
|
|
|
data = all_projector_data.get(projector_id, [])
|
2018-12-23 11:05:38 +01:00
|
|
|
new_hash = hash(str(data))
|
2019-01-18 19:11:22 +01:00
|
|
|
if new_hash != self.projector_hash.get(projector_id):
|
2018-12-23 11:05:38 +01:00
|
|
|
projector_data[projector_id] = data
|
|
|
|
self.projector_hash[projector_id] = new_hash
|
|
|
|
|
|
|
|
if projector_data:
|
2019-10-01 15:36:59 +02:00
|
|
|
await self.send_projector_data(projector_data, change_id=change_id)
|
|
|
|
|
2020-05-15 18:24:21 +02:00
|
|
|
async def msg_notify(self, event: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
Send a notify message to the user.
|
|
|
|
"""
|
|
|
|
item = event["incomming"]
|
|
|
|
|
|
|
|
users = item.get("users")
|
|
|
|
reply_channels = item.get("replyChannels")
|
|
|
|
if (
|
|
|
|
(isinstance(users, bool) and users)
|
|
|
|
or (isinstance(users, list) and self.user_id in users)
|
|
|
|
or (
|
|
|
|
isinstance(reply_channels, list) and self.channel_name in reply_channels
|
|
|
|
)
|
|
|
|
or (users is None and reply_channels is None)
|
|
|
|
):
|
|
|
|
item["senderChannelName"] = event["senderChannelName"]
|
|
|
|
item["senderUserId"] = event["senderUserId"]
|
|
|
|
await self.send_json(type="notify", content=item)
|
|
|
|
|
|
|
|
async def request_autoupdate(
|
|
|
|
self, change_id: int, in_response: Optional[str] = None
|
|
|
|
) -> None:
|
|
|
|
await self.autoupdate_strategy.request_change_id(
|
|
|
|
change_id, in_response=in_response
|
|
|
|
)
|
|
|
|
|
2019-10-01 15:36:59 +02:00
|
|
|
async def send_projector_data(
|
|
|
|
self,
|
|
|
|
data: Dict[int, Dict[str, Any]],
|
|
|
|
change_id: Optional[int] = None,
|
|
|
|
in_response: Optional[str] = None,
|
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Sends projector data to the consumer.
|
|
|
|
"""
|
|
|
|
if change_id is None:
|
|
|
|
change_id = await element_cache.get_current_change_id()
|
|
|
|
|
|
|
|
content = {"change_id": change_id, "data": data}
|
|
|
|
await self.send_json(type="projector", content=content, in_response=in_response)
|
2019-10-15 10:54:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CloseConsumer(AsyncWebsocketConsumer):
|
|
|
|
""" Auto-closes the connection """
|
|
|
|
|
|
|
|
groups: List[str] = []
|
|
|
|
|
|
|
|
def __init__(self, args: Dict[str, Any], **kwargs: Any) -> None:
|
|
|
|
logger.info(f'Closing connection to unknown websocket url {args["path"]}')
|
|
|
|
|
|
|
|
async def connect(self) -> None:
|
|
|
|
await self.accept()
|
|
|
|
await self.close()
|