Move file tournament; chat can see who is online

This commit is contained in:
2024-01-13 11:18:10 +01:00
parent 09f3c92153
commit a254e5a0c2
7 changed files with 54 additions and 39 deletions

View File

@ -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:
@ -157,13 +170,16 @@ 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):

View File

@ -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();
}
}
}

View File

@ -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)

View File

@ -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";
});
}

View File

@ -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
{

View File

@ -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
{

View File

@ -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
{