Add online/disconnect color on Search, not finish

This commit is contained in:
2024-01-11 17:34:00 +01:00
parent 56d352fead
commit 4c114a34df
6 changed files with 230 additions and 7 deletions

View File

@ -16,16 +16,14 @@ class ChatConsumer(WebsocketConsumer):
channel_id : int = int(self.scope['url_route']['kwargs']['chat_id'])
self.room_group_name = f'chat{channel_id}'
if ChatMemberModel.objects.filter(member_id=user.pk, channel_id=int(channel_id)).count() != 1:
return
self.room_group_name = f'chat{channel_id}'
if (self.channel_layer == None):
return
self.room_group_name = f'chat{channel_id}'
async_to_sync(self.channel_layer.group_add)(
self.room_group_name,
self.channel_name
@ -103,3 +101,108 @@ class ChatConsumer(WebsocketConsumer):
'content':event['content'],
'time': event['time'],
}))
class ChatNoticeConsumer(WebsocketConsumer):
def connect(self):
user = self.scope["user"]
if (user.is_anonymous or not user.is_authenticated):
return
if (self.channel_layer == None):
return
self.room_group_name = f'chatNotice{user.pk}'
if (not hasattr(self.channel_layer, "users_channels")):
self.channel_layer.users_channels = {}
self.channel_layer.users_channels[user.pk] = self.channel_name
async_to_sync(self.channel_layer.group_add)(
self.room_group_name,
self.channel_name
)
self.accept()
def disconnect(self, code):
user = self.scope["user"]
if (user.is_anonymous or not user.is_authenticated):
return
self.channel_layer.users_channels.pop(user.pk)
def receive(self, text_data=None, bytes_data=None):
if text_data == None:
return
user = self.scope["user"]
if (user.is_anonymous or not user.is_authenticated):
return
text_data_json = json.loads(text_data)
type_notice = text_data_json.get('type')
targets : list = text_data_json.get('targets')
content : dict = text_data_json.get('content')
if (type_notice == None or targets == None):
return
if (self.channel_layer == None):
return
message_time: int = int(time.time() * 1000)
status = 200;
if targets == "all":
targets = list(self.channel_layer.users_channels.keys())
for target in targets:
if (self.channel_layer.users_channels.get(target) == None):
status = 404
continue
async_to_sync(self.channel_layer.send)(self.channel_layer.users_channels.get(target), {
'type':type_notice,
'author_id':user.pk,
'content':content,
'time':message_time,
})
async_to_sync(self.channel_layer.send)(self.channel_layer.users_channels.get(user.pk), {
'type':type_notice,
'author_id':user.pk,
'content':"notice return",
'time':message_time,
'status':status,
})
def invite(self, event):
user = self.scope["user"]
if (user.is_anonymous or not user.is_authenticated):
return
self.send(text_data=json.dumps({
'type':event['type'],
'author_id':event['author_id'],
'content':event['content'],
'time': event['time'],
}))
def online_user(self, event):
user = self.scope["user"]
if (user.is_anonymous or not user.is_authenticated):
return
event['content'] = self.channel_layer.users_channels
self.send(text_data=json.dumps({
'type':event['type'],
'author_id':event['author_id'],
'content':event['content'],
'time': event['time'],
}))

View File

@ -2,5 +2,6 @@ from django.urls import re_path
from . import consumers
websocket_urlpatterns = [
re_path(r'ws/chat/(?P<chat_id>\d+)$', consumers.ChatConsumer.as_asgi())
re_path(r'ws/chat/(?P<chat_id>\d+)$', consumers.ChatConsumer.as_asgi()),
re_path(r'ws/chat/notice$', consumers.ChatNoticeConsumer.as_asgi()),
]