Add online/disconnect color on Search, not finish
This commit is contained in:
parent
56d352fead
commit
4c114a34df
@ -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'],
|
||||
}))
|
||||
|
@ -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()),
|
||||
]
|
||||
|
@ -0,0 +1,87 @@
|
||||
class Notice {
|
||||
constructor(client) {
|
||||
this.client = client;
|
||||
this.connect();
|
||||
|
||||
this.online_users_setter = undefined;
|
||||
this.online_users = {};
|
||||
await client.notice.getOnlineUser(
|
||||
async (content) => {
|
||||
this.online_users = content;
|
||||
this.rewrite_usernames(profiles);
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
async connect() {
|
||||
let url = `${window.location.protocol[4] === 's' ? 'wss' : 'ws'}://${window.location.host}/ws/chat/notice`;
|
||||
|
||||
this.chatSocket = new WebSocket(url);
|
||||
this.chatSocket.onmessage = (event) =>{
|
||||
let data = JSON.parse(event.data);
|
||||
console.log("invite");
|
||||
//console.log("notice: ", data);
|
||||
if (data.type == "invite")
|
||||
this.receiveInvite(data);
|
||||
else if (data.type == "online_user")
|
||||
this.receiveOnlineUser(data);
|
||||
}
|
||||
}
|
||||
|
||||
async disconnect() {
|
||||
if (this.chatSocket == undefined)
|
||||
this.chatSocket.close();
|
||||
}
|
||||
|
||||
async sendInvite(id_inviter, id_inviteds) {
|
||||
|
||||
if (this.chatSocket == undefined)
|
||||
return;
|
||||
|
||||
this.chatSocket.send(JSON.stringify({
|
||||
type: "invite",
|
||||
targets: id_inviteds,
|
||||
}));
|
||||
|
||||
}
|
||||
|
||||
async receiveInvite(data) {
|
||||
|
||||
if (data.content === "notice return") {
|
||||
if (data.status == 200)
|
||||
return
|
||||
// Notification pour dire que la notif a été bien envoyé
|
||||
else if (data.status == 404)
|
||||
return
|
||||
// Pas connecté
|
||||
}
|
||||
else {
|
||||
// Géré la reception de l'invitation
|
||||
}
|
||||
}
|
||||
|
||||
async getOnlineUser(online_users_setter) {
|
||||
|
||||
if (this.chatSocket == undefined)
|
||||
return;
|
||||
|
||||
this.online_users_setter = online_users_setter;
|
||||
|
||||
this.chatSocket.send(JSON.stringify({
|
||||
type: "online_user",
|
||||
targets: "all",
|
||||
}));
|
||||
|
||||
}
|
||||
|
||||
async receiveOnlineUser(data) {
|
||||
console.log("receiveOnlineUser");
|
||||
if (data.content !== undefined &&
|
||||
this.online_users_setter !== undefined) {
|
||||
|
||||
this.online_users_setter(data.content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export {Notice}
|
@ -5,6 +5,7 @@ import { Channels } from './chat/channels.js';
|
||||
import { MyProfile } from "./MyProfile.js";
|
||||
import { navigateTo } from "../index.js"
|
||||
import { Tourmanents } from "./tournament/tournaments.js";
|
||||
import {Notice} from "./chat/notice.js"
|
||||
|
||||
function getCookie(name)
|
||||
{
|
||||
@ -29,6 +30,8 @@ class Client
|
||||
|
||||
this.channels = new Channels(this);
|
||||
this.channel = undefined;
|
||||
|
||||
this.notice = new Notice(this);
|
||||
}
|
||||
|
||||
async isAuthentificate()
|
||||
|
@ -39,7 +39,7 @@ export default class extends AbstractView {
|
||||
if (await client.isAuthentificate() === false)
|
||||
return;
|
||||
|
||||
if (client.me.user_id != this.user_id) {
|
||||
if (client.me.id != this.user_id) {
|
||||
let block = document.getElementById("block") || document.createElement("a");
|
||||
block.id = "block";
|
||||
block.innerText = "";
|
||||
|
@ -7,11 +7,24 @@ export default class extends AbstractView {
|
||||
super(params, "Search");
|
||||
}
|
||||
|
||||
async wait_get_online_users() {
|
||||
return new Promise((resolve) => {
|
||||
const checkInterval = setInterval(() => {
|
||||
if (Object.keys(this.online_users).length > 0) {
|
||||
clearInterval(checkInterval);
|
||||
resolve();
|
||||
}
|
||||
}, 1);
|
||||
});
|
||||
}
|
||||
|
||||
async postInit() {
|
||||
|
||||
let logged = await client.isAuthentificate();
|
||||
let profiles = await client.profiles.all();
|
||||
|
||||
await this.wait_get_online_users();
|
||||
|
||||
let search = document.getElementById("input_user");
|
||||
search.oninput = () => this.display_users(logged, profiles);
|
||||
|
||||
@ -43,7 +56,9 @@ export default class extends AbstractView {
|
||||
|
||||
// username
|
||||
let username = document.createElement("a");
|
||||
username.id = `username${user.id}`
|
||||
username.href = `/profiles/${user.id}`;
|
||||
username.style.color = this.online_users[user.id] !== undefined ? "green" : "red";
|
||||
username.appendChild(document.createTextNode(user.username));
|
||||
new_user.appendChild(username);
|
||||
|
||||
@ -100,6 +115,18 @@ export default class extends AbstractView {
|
||||
|
||||
}
|
||||
|
||||
async rewrite_usernames(profiles) {
|
||||
console.log("rewrite");
|
||||
let search = document.getElementById("input_user").value.toLowerCase();
|
||||
|
||||
profiles.filter(user => user.username.toLowerCase().startsWith(search) == true).forEach((user) => {
|
||||
let username = document.getElementById(`username${user.id}`);
|
||||
if (username !== null)
|
||||
username.style.color = this.online_users[user.id] !== undefined ? "green" : "red";
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
async display_chat(logged, profiles)
|
||||
{
|
||||
let reloads = ["members", "messages"];
|
||||
@ -160,7 +187,8 @@ export default class extends AbstractView {
|
||||
invite.id = "invite";
|
||||
invite.innerText = "invite";
|
||||
invite.onclick = async () => {
|
||||
|
||||
await client.notice.sendInvite(client.me.id,
|
||||
client.channels.channel.members_id.filter(id => id !== client.me.id));
|
||||
};
|
||||
chat.appendChild(invite);
|
||||
|
||||
@ -238,6 +266,7 @@ export default class extends AbstractView {
|
||||
if (client.channels.channel != undefined)
|
||||
client.channels.channel.disconnect();
|
||||
client.channels.channel = undefined;
|
||||
|
||||
}
|
||||
|
||||
async getHtml() {
|
||||
|
Loading…
Reference in New Issue
Block a user