Move file tournament; chat can see who is online
This commit is contained in:
@ -133,6 +133,19 @@ class ChatNoticeConsumer(WebsocketConsumer):
|
|||||||
|
|
||||||
self.channel_layer.users_channels.pop(user.pk)
|
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):
|
def receive(self, text_data=None, bytes_data=None):
|
||||||
|
|
||||||
if text_data == None:
|
if text_data == None:
|
||||||
@ -156,14 +169,17 @@ class ChatNoticeConsumer(WebsocketConsumer):
|
|||||||
|
|
||||||
message_time: int = int(time.time() * 1000)
|
message_time: int = int(time.time() * 1000)
|
||||||
status = 200;
|
status = 200;
|
||||||
|
|
||||||
|
|
||||||
if targets == "all":
|
if targets == "all":
|
||||||
targets = list(self.channel_layer.users_channels.keys())
|
targets = list(self.channel_layer.users_channels.keys())
|
||||||
for target in targets:
|
for target in targets:
|
||||||
if (self.channel_layer.users_channels.get(target) == None):
|
channel = self.channel_layer.users_channels.get(target)
|
||||||
status = 404
|
if (channel == None or target == user.pk):
|
||||||
|
if (channel == None):
|
||||||
|
status = 404
|
||||||
continue
|
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,
|
'type':type_notice,
|
||||||
'author_id':user.pk,
|
'author_id':user.pk,
|
||||||
'content':content,
|
'content':content,
|
||||||
@ -192,7 +208,7 @@ class ChatNoticeConsumer(WebsocketConsumer):
|
|||||||
'time': event['time'],
|
'time': event['time'],
|
||||||
}))
|
}))
|
||||||
|
|
||||||
def online_user(self, event):
|
def online_users(self, event):
|
||||||
|
|
||||||
user = self.scope["user"]
|
user = self.scope["user"]
|
||||||
if (user.is_anonymous or not user.is_authenticated):
|
if (user.is_anonymous or not user.is_authenticated):
|
||||||
|
@ -3,14 +3,6 @@ class Notice {
|
|||||||
this.client = client;
|
this.client = client;
|
||||||
this.connect();
|
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() {
|
async connect() {
|
||||||
@ -19,18 +11,26 @@ class Notice {
|
|||||||
this.chatSocket = new WebSocket(url);
|
this.chatSocket = new WebSocket(url);
|
||||||
this.chatSocket.onmessage = (event) =>{
|
this.chatSocket.onmessage = (event) =>{
|
||||||
let data = JSON.parse(event.data);
|
let data = JSON.parse(event.data);
|
||||||
console.log("invite");
|
|
||||||
//console.log("notice: ", data);
|
//console.log("notice: ", data);
|
||||||
if (data.type == "invite")
|
if (data.type == "invite")
|
||||||
this.receiveInvite(data);
|
this.receiveInvite(data);
|
||||||
else if (data.type == "online_user")
|
else if (data.type == "online_users" || data.type == "disconnect")
|
||||||
this.receiveOnlineUser(data);
|
this.receiveOnlineUser(data);
|
||||||
}
|
}
|
||||||
|
this.chatSocket.onopen = (event) => {
|
||||||
|
this.online_users = {};
|
||||||
|
this.getOnlineUser();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async disconnect() {
|
async disconnect() {
|
||||||
if (this.chatSocket == undefined)
|
if (this.chatSocket == undefined)
|
||||||
this.chatSocket.close();
|
this.chatSocket.close();
|
||||||
|
|
||||||
|
this.chatSocket.send(JSON.stringify({
|
||||||
|
type: "online_users",
|
||||||
|
targets: "all",
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendInvite(id_inviter, id_inviteds) {
|
async sendInvite(id_inviter, id_inviteds) {
|
||||||
@ -60,26 +60,23 @@ class Notice {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getOnlineUser(online_users_setter) {
|
async getOnlineUser() {
|
||||||
|
|
||||||
if (this.chatSocket == undefined)
|
if (this.chatSocket == undefined)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
this.online_users_setter = online_users_setter;
|
|
||||||
|
|
||||||
this.chatSocket.send(JSON.stringify({
|
this.chatSocket.send(JSON.stringify({
|
||||||
type: "online_user",
|
type: "online_users",
|
||||||
targets: "all",
|
targets: "all",
|
||||||
}));
|
}));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async receiveOnlineUser(data) {
|
async receiveOnlineUser(data) {
|
||||||
console.log("receiveOnlineUser");
|
if (data.content !== undefined) {
|
||||||
if (data.content !== undefined &&
|
this.online_users = data.content;
|
||||||
this.online_users_setter !== undefined) {
|
if (this.rewrite_usernames !== undefined)
|
||||||
|
this.rewrite_usernames();
|
||||||
this.online_users_setter(data.content);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,9 +15,9 @@ import AbstractRedirectView from "./views/abstracts/AbstractRedirectView.js";
|
|||||||
import MeView from "./views/MeView.js";
|
import MeView from "./views/MeView.js";
|
||||||
import ProfilePageView from "./views/ProfilePageView.js";
|
import ProfilePageView from "./views/ProfilePageView.js";
|
||||||
import MatchMakingView from "./views/MatchMakingView.js";
|
import MatchMakingView from "./views/MatchMakingView.js";
|
||||||
import TournamentPageView from "./views/TournamentPageView.js";
|
import TournamentPageView from "./views/tournament/TournamentPageView.js";
|
||||||
import TournamentsView from "./views/TournamentsListView.js";
|
import TournamentsView from "./views/tournament/TournamentsListView.js";
|
||||||
import TournamentCreateView from "./views/TournamentCreateView.js";
|
import TournamentCreateView from "./views/tournament/TournamentCreateView.js";
|
||||||
|
|
||||||
let client = new Client(location.protocol + "//" + location.host)
|
let client = new Client(location.protocol + "//" + location.host)
|
||||||
|
|
||||||
|
@ -10,11 +10,11 @@ export default class extends AbstractView {
|
|||||||
async wait_get_online_users() {
|
async wait_get_online_users() {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const checkInterval = setInterval(() => {
|
const checkInterval = setInterval(() => {
|
||||||
if (Object.keys(this.online_users).length > 0) {
|
if (Object.keys(client.notice.online_users).length > 0) {
|
||||||
clearInterval(checkInterval);
|
clearInterval(checkInterval);
|
||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
}, 1);
|
}, 100);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,6 +24,7 @@ export default class extends AbstractView {
|
|||||||
let profiles = await client.profiles.all();
|
let profiles = await client.profiles.all();
|
||||||
|
|
||||||
await this.wait_get_online_users();
|
await this.wait_get_online_users();
|
||||||
|
client.notice.rewrite_usernames = this.rewrite_usernames;
|
||||||
|
|
||||||
let search = document.getElementById("input_user");
|
let search = document.getElementById("input_user");
|
||||||
search.oninput = () => this.display_users(logged, profiles);
|
search.oninput = () => this.display_users(logged, profiles);
|
||||||
@ -58,7 +59,7 @@ export default class extends AbstractView {
|
|||||||
let username = document.createElement("a");
|
let username = document.createElement("a");
|
||||||
username.id = `username${user.id}`
|
username.id = `username${user.id}`
|
||||||
username.href = `/profiles/${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));
|
username.appendChild(document.createTextNode(user.username));
|
||||||
new_user.appendChild(username);
|
new_user.appendChild(username);
|
||||||
|
|
||||||
@ -115,14 +116,15 @@ export default class extends AbstractView {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async rewrite_usernames(profiles) {
|
async rewrite_usernames() {
|
||||||
console.log("rewrite");
|
|
||||||
let search = document.getElementById("input_user").value.toLowerCase();
|
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) => {
|
profiles.filter(user => user.username.toLowerCase().startsWith(search) == true).forEach((user) => {
|
||||||
let username = document.getElementById(`username${user.id}`);
|
let username = document.getElementById(`username${user.id}`);
|
||||||
if (username !== null)
|
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";
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import {client, navigateTo} from "../index.js";
|
import {client, navigateTo} from "../../index.js";
|
||||||
import { clear, fill_errors } from "../utils/formUtils.js";
|
import { clear, fill_errors } from "../../utils/formUtils.js";
|
||||||
import AbstractAuthentifiedView from "./abstracts/AbstractAuthentifiedView.js";
|
import AbstractAuthentifiedView from "../abstracts/AbstractAuthentifiedView.js";
|
||||||
|
|
||||||
export default class extends AbstractAuthentifiedView
|
export default class extends AbstractAuthentifiedView
|
||||||
{
|
{
|
@ -1,5 +1,5 @@
|
|||||||
import {client, navigateTo} from "../index.js";
|
import {client, navigateTo} from "../../index.js";
|
||||||
import AbstractAuthentifiedView from "./abstracts/AbstractAuthentifiedView.js";
|
import AbstractAuthentifiedView from "../abstracts/AbstractAuthentifiedView.js";
|
||||||
|
|
||||||
export default class extends AbstractAuthentifiedView
|
export default class extends AbstractAuthentifiedView
|
||||||
{
|
{
|
@ -1,5 +1,5 @@
|
|||||||
import {client} from "../index.js";
|
import {client} from "../../index.js";
|
||||||
import AbstractAuthentifiedView from "./abstracts/AbstractAuthentifiedView.js";
|
import AbstractAuthentifiedView from "../abstracts/AbstractAuthentifiedView.js";
|
||||||
|
|
||||||
export default class extends AbstractAuthentifiedView
|
export default class extends AbstractAuthentifiedView
|
||||||
{
|
{
|
Reference in New Issue
Block a user