From a254e5a0c20a310662c4e864b58efa381691a426 Mon Sep 17 00:00:00 2001 From: Xamora Date: Sat, 13 Jan 2024 11:18:10 +0100 Subject: [PATCH] Move file tournament; chat can see who is online --- chat/consumers.py | 24 ++++++++++--- frontend/static/js/api/chat/notice.js | 35 +++++++++---------- frontend/static/js/index.js | 6 ++-- frontend/static/js/views/Search.js | 14 ++++---- .../{ => tournament}/TournamentCreateView.js | 6 ++-- .../{ => tournament}/TournamentPageView.js | 4 +-- .../{ => tournament}/TournamentsListView.js | 4 +-- 7 files changed, 54 insertions(+), 39 deletions(-) rename frontend/static/js/views/{ => tournament}/TournamentCreateView.js (87%) rename frontend/static/js/views/{ => tournament}/TournamentPageView.js (95%) rename frontend/static/js/views/{ => tournament}/TournamentsListView.js (96%) diff --git a/chat/consumers.py b/chat/consumers.py index 82b1a9a..133d50b 100644 --- a/chat/consumers.py +++ b/chat/consumers.py @@ -133,6 +133,19 @@ class ChatNoticeConsumer(WebsocketConsumer): self.channel_layer.users_channels.pop(user.pk) + message_time: int = int(time.time() * 1000) + + targets = list(self.channel_layer.users_channels.keys()) + for target in targets: + channel = self.channel_layer.users_channels.get(target) + if (channel == None or target == user.pk): + continue + async_to_sync(self.channel_layer.send)(channel, { + 'type':"online_users", + 'author_id':user.pk, + 'time':message_time, + }) + def receive(self, text_data=None, bytes_data=None): if text_data == None: @@ -156,14 +169,17 @@ class ChatNoticeConsumer(WebsocketConsumer): 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 + channel = self.channel_layer.users_channels.get(target) + if (channel == None or target == user.pk): + if (channel == None): + status = 404 continue - async_to_sync(self.channel_layer.send)(self.channel_layer.users_channels.get(target), { + async_to_sync(self.channel_layer.send)(channel, { 'type':type_notice, 'author_id':user.pk, 'content':content, @@ -192,7 +208,7 @@ class ChatNoticeConsumer(WebsocketConsumer): 'time': event['time'], })) - def online_user(self, event): + def online_users(self, event): user = self.scope["user"] if (user.is_anonymous or not user.is_authenticated): diff --git a/frontend/static/js/api/chat/notice.js b/frontend/static/js/api/chat/notice.js index 15f8383..87db6e0 100644 --- a/frontend/static/js/api/chat/notice.js +++ b/frontend/static/js/api/chat/notice.js @@ -3,14 +3,6 @@ class Notice { 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() { @@ -19,18 +11,26 @@ class 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") + else if (data.type == "online_users" || data.type == "disconnect") this.receiveOnlineUser(data); } + this.chatSocket.onopen = (event) => { + this.online_users = {}; + this.getOnlineUser(); + } } async disconnect() { if (this.chatSocket == undefined) this.chatSocket.close(); + + this.chatSocket.send(JSON.stringify({ + type: "online_users", + targets: "all", + })); } async sendInvite(id_inviter, id_inviteds) { @@ -60,26 +60,23 @@ class Notice { } } - async getOnlineUser(online_users_setter) { + async getOnlineUser() { if (this.chatSocket == undefined) return; - this.online_users_setter = online_users_setter; - this.chatSocket.send(JSON.stringify({ - type: "online_user", + type: "online_users", targets: "all", })); } async receiveOnlineUser(data) { - console.log("receiveOnlineUser"); - if (data.content !== undefined && - this.online_users_setter !== undefined) { - - this.online_users_setter(data.content); + if (data.content !== undefined) { + this.online_users = data.content; + if (this.rewrite_usernames !== undefined) + this.rewrite_usernames(); } } } diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index cb4bc67..2caae56 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -15,9 +15,9 @@ import AbstractRedirectView from "./views/abstracts/AbstractRedirectView.js"; import MeView from "./views/MeView.js"; import ProfilePageView from "./views/ProfilePageView.js"; import MatchMakingView from "./views/MatchMakingView.js"; -import TournamentPageView from "./views/TournamentPageView.js"; -import TournamentsView from "./views/TournamentsListView.js"; -import TournamentCreateView from "./views/TournamentCreateView.js"; +import TournamentPageView from "./views/tournament/TournamentPageView.js"; +import TournamentsView from "./views/tournament/TournamentsListView.js"; +import TournamentCreateView from "./views/tournament/TournamentCreateView.js"; let client = new Client(location.protocol + "//" + location.host) diff --git a/frontend/static/js/views/Search.js b/frontend/static/js/views/Search.js index d1ff630..0bcb36e 100644 --- a/frontend/static/js/views/Search.js +++ b/frontend/static/js/views/Search.js @@ -10,11 +10,11 @@ export default class extends AbstractView { async wait_get_online_users() { return new Promise((resolve) => { const checkInterval = setInterval(() => { - if (Object.keys(this.online_users).length > 0) { + if (Object.keys(client.notice.online_users).length > 0) { clearInterval(checkInterval); resolve(); } - }, 1); + }, 100); }); } @@ -24,6 +24,7 @@ export default class extends AbstractView { let profiles = await client.profiles.all(); await this.wait_get_online_users(); + client.notice.rewrite_usernames = this.rewrite_usernames; let search = document.getElementById("input_user"); search.oninput = () => this.display_users(logged, profiles); @@ -58,7 +59,7 @@ export default class extends AbstractView { 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.style.color = client.notice.online_users[user.id] !== undefined ? "green" : "red"; username.appendChild(document.createTextNode(user.username)); new_user.appendChild(username); @@ -115,14 +116,15 @@ export default class extends AbstractView { } - async rewrite_usernames(profiles) { - console.log("rewrite"); + async rewrite_usernames() { let search = document.getElementById("input_user").value.toLowerCase(); + let profiles = await client.profiles.all(); + 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"; + username.style.color = client.notice.online_users[user.id] !== undefined ? "green" : "red"; }); } diff --git a/frontend/static/js/views/TournamentCreateView.js b/frontend/static/js/views/tournament/TournamentCreateView.js similarity index 87% rename from frontend/static/js/views/TournamentCreateView.js rename to frontend/static/js/views/tournament/TournamentCreateView.js index ef93428..9cfc1a4 100644 --- a/frontend/static/js/views/TournamentCreateView.js +++ b/frontend/static/js/views/tournament/TournamentCreateView.js @@ -1,6 +1,6 @@ -import {client, navigateTo} from "../index.js"; -import { clear, fill_errors } from "../utils/formUtils.js"; -import AbstractAuthentifiedView from "./abstracts/AbstractAuthentifiedView.js"; +import {client, navigateTo} from "../../index.js"; +import { clear, fill_errors } from "../../utils/formUtils.js"; +import AbstractAuthentifiedView from "../abstracts/AbstractAuthentifiedView.js"; export default class extends AbstractAuthentifiedView { diff --git a/frontend/static/js/views/TournamentPageView.js b/frontend/static/js/views/tournament/TournamentPageView.js similarity index 95% rename from frontend/static/js/views/TournamentPageView.js rename to frontend/static/js/views/tournament/TournamentPageView.js index 7555037..034d27b 100644 --- a/frontend/static/js/views/TournamentPageView.js +++ b/frontend/static/js/views/tournament/TournamentPageView.js @@ -1,5 +1,5 @@ -import {client, navigateTo} from "../index.js"; -import AbstractAuthentifiedView from "./abstracts/AbstractAuthentifiedView.js"; +import {client, navigateTo} from "../../index.js"; +import AbstractAuthentifiedView from "../abstracts/AbstractAuthentifiedView.js"; export default class extends AbstractAuthentifiedView { diff --git a/frontend/static/js/views/TournamentsListView.js b/frontend/static/js/views/tournament/TournamentsListView.js similarity index 96% rename from frontend/static/js/views/TournamentsListView.js rename to frontend/static/js/views/tournament/TournamentsListView.js index 12794ca..d232698 100644 --- a/frontend/static/js/views/TournamentsListView.js +++ b/frontend/static/js/views/tournament/TournamentsListView.js @@ -1,5 +1,5 @@ -import {client} from "../index.js"; -import AbstractAuthentifiedView from "./abstracts/AbstractAuthentifiedView.js"; +import {client} from "../../index.js"; +import AbstractAuthentifiedView from "../abstracts/AbstractAuthentifiedView.js"; export default class extends AbstractAuthentifiedView {