2018-10-14 08:26:51 +02:00
|
|
|
from collections import defaultdict
|
2018-10-26 15:37:29 +02:00
|
|
|
from typing import Any, Dict, List
|
2018-10-19 16:32:48 +02:00
|
|
|
from urllib.parse import parse_qs
|
2018-07-09 23:22:26 +02:00
|
|
|
|
2018-11-03 23:40:20 +01:00
|
|
|
from .auth import async_anonymous_is_enabled
|
|
|
|
from .autoupdate import AutoupdateFormat
|
2018-07-09 23:22:26 +02:00
|
|
|
from .cache import element_cache, split_element_id
|
2018-10-26 15:37:29 +02:00
|
|
|
from .websocket import ProtocollAsyncJsonWebsocketConsumer, get_element_data
|
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
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
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
|
2019-01-06 16:22:33 +01:00
|
|
|
if not await async_anonymous_is_enabled() and not self.scope["user"]["id"]:
|
2018-07-09 23:22:26 +02:00
|
|
|
await self.close()
|
2018-10-19 16:32:48 +02:00
|
|
|
return
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
query_string = parse_qs(self.scope["query_string"])
|
|
|
|
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:
|
|
|
|
await self.close() # TODO: Find a way to send an error code
|
|
|
|
return
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
if b"autoupdate" in query_string and query_string[b"autoupdate"][
|
|
|
|
0
|
|
|
|
].lower() not in [b"0", b"off", b"false"]:
|
2018-10-19 16:32:48 +02:00
|
|
|
# a positive value in autoupdate. Start autoupdate
|
2019-01-06 16:22:33 +01:00
|
|
|
await self.channel_layer.group_add("autoupdate", self.channel_name)
|
2018-10-19 16:32:48 +02:00
|
|
|
|
|
|
|
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:
|
2018-11-02 05:10:49 +01:00
|
|
|
try:
|
2019-01-06 16:22:33 +01:00
|
|
|
data = await get_element_data(self.scope["user"]["id"], change_id)
|
2018-11-02 05:10:49 +01:00
|
|
|
except ValueError:
|
|
|
|
# When the change_id is to big, do nothing
|
|
|
|
pass
|
|
|
|
else:
|
2019-01-06 16:22:33 +01:00
|
|
|
await self.send_json(type="autoupdate", content=data)
|
2018-07-09 23:22:26 +02:00
|
|
|
|
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)
|
2018-10-19 16:32:48 +02:00
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
async def send_notify(self, event: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
Send a notify message to the user.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
user_id = self.scope["user"]["id"]
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
out = []
|
2019-01-06 16:22:33 +01:00
|
|
|
for item in event["incomming"]:
|
|
|
|
users = item.get("users")
|
|
|
|
reply_channels = item.get("replyChannels")
|
|
|
|
if (
|
|
|
|
(isinstance(users, list) and 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["senderReplyChannelName"] = event.get("senderReplyChannelName")
|
|
|
|
item["senderUserId"] = event.get("senderUserId")
|
2018-07-09 23:22:26 +02:00
|
|
|
out.append(item)
|
|
|
|
|
|
|
|
if out:
|
2019-01-06 16:22:33 +01:00
|
|
|
await self.send_json(type="notify", content=out)
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
async def send_data(self, event: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
Send changed or deleted elements to the user.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
change_id = event["change_id"]
|
2018-11-03 14:35:18 +01:00
|
|
|
changed_elements, deleted_elements_ids = await element_cache.get_restricted_data(
|
2019-01-06 16:22:33 +01:00
|
|
|
self.scope["user"]["id"], change_id, max_change_id=change_id
|
|
|
|
)
|
2018-10-14 08:26:51 +02:00
|
|
|
|
|
|
|
deleted_elements: Dict[str, List[int]] = defaultdict(list)
|
|
|
|
for element_id in deleted_elements_ids:
|
2018-07-09 23:22:26 +02:00
|
|
|
collection_string, id = split_element_id(element_id)
|
2018-10-14 08:26:51 +02:00
|
|
|
deleted_elements[collection_string].append(id)
|
2019-01-06 16:22:33 +01:00
|
|
|
await self.send_json(
|
|
|
|
type="autoupdate",
|
|
|
|
content=AutoupdateFormat(
|
|
|
|
changed=changed_elements,
|
|
|
|
deleted=deleted_elements,
|
|
|
|
from_change_id=change_id,
|
|
|
|
to_change_id=change_id,
|
|
|
|
all_data=False,
|
|
|
|
),
|
|
|
|
)
|