digitaleagenturnc/users/mainwebsocket.py

104 lines
3.7 KiB
Python

import json
from channels.generic.websocket import WebsocketConsumer
from asgiref.sync import async_to_sync
from channels_presence.models import Room
from channels_presence.decorators import touch_presence, remove_presence
from channels_presence.models import Presence
import channels
from django.contrib.auth.models import User
class UsersConsumer(WebsocketConsumer):
'''
CONNECT A WEBSOCKET
Die Clients werden in Channel-Layer pro Agentur gepackt, damit gesendete Websocket-Nachrichten
auch nur Clients innerhalb der Agentur treffen!
'''
def connect(self):
super().connect()
loggeduser = self.scope["user"]
Presence.objects.touch(self.channel_name)
Room.objects.add("agency_" + str(loggeduser.profile.agency.pk), self.channel_name, self.scope["user"])
def disconnect(self, close_code):
loggeduser = self.scope["user"]
Room.objects.remove("agency_" + str(loggeduser.profile.agency.pk), self.channel_name)
Presence.objects.touch(self.channel_name)
'''
def receive(self, text_data):
loggeduser = self.scope["user"]
async_to_sync(self.channel_layer.group_send)(
'allusers',
{
'type': 'chat_message',
'message': 'von mainwebsocket.py'
}
)
'''
# WEBSOCKET-DATA-CONTENT
def receive(self, text_data=None, bytes_data=None):
if text_data == '"heartbeat"':
Presence.objects.touch(self.channel_name)
# UPDATET STANDARD
def update_standard(self, event):
self.send("standard_update")
# NEW AGENCY NEWS
def agency_newnews(self, event):
self.send("Neue Agenturnews!")
# SOMETHING IN PRESENCE CHANGED
def update_presence_live(self, event):
self.send("presence_update")
class UsersChat(UsersConsumer):
'''
CONNECT A WEBSOCKET
Die Clients werden in Channel-Layer pro Agentur gepackt, damit gesendete Websocket-Nachrichten
auch nur Clients innerhalb der Agentur treffen!
'''
def connect(self):
super().connect()
loggeduser = self.scope["user"]
roomname = "privatechat_" + str(self.scope["url_route"]["kwargs"]["creator"]) + "_" + str(self.scope["url_route"]["kwargs"]["single"])
Room.objects.add(roomname, self.channel_name, self.scope["user"])
def disconnect(self, close_code):
Room.objects.remove("", self.channel_name)
# WEBSOCKET-DATA-CONTENT
def receive(self, text_data=None, bytes_data=None):
datainfo = text_data.split("__")
typinguserid = datainfo[1]
if datainfo[0] == 'starttyping':
channel_layer = channels.layers.get_channel_layer()
async_to_sync(channel_layer.group_send)(datainfo[2], {'type' : 'start_typing', 'typingname' : typinguserid})
elif datainfo[0] == 'stoptyping':
channel_layer = channels.layers.get_channel_layer()
async_to_sync(channel_layer.group_send)(datainfo[2], {'type' : 'stop_typing'})
elif datainfo[0] == 'load':
channel_layer = channels.layers.get_channel_layer()
async_to_sync(channel_layer.group_send)(datainfo[2], {'type' : 'reloadmessages'})
def start_typing(self, event):
useristyping = User.objects.get(pk=event["typingname"])
self.send("starttyping__" + str(useristyping.pk) + "_" + useristyping.first_name + " " + useristyping.last_name + " tippt...")
def stop_typing(self, event):
self.send("stoptyping")
def reloadmessages(self, event):
self.send("reloadmessages")