From fe47a4d633417f98885afa617203c6e8517676b8 Mon Sep 17 00:00:00 2001 From: Xamora Date: Fri, 19 Jan 2024 16:48:20 +0100 Subject: [PATCH 01/77] Block fixed, looking for invite in game --- chat/consumersNotice.py | 189 +++++++++++++------ frontend/static/css/search.css | 16 +- frontend/static/js/api/chat/notice.js | 116 ++++++++---- frontend/static/js/api/client.js | 2 +- frontend/static/js/api/profile.js | 3 +- frontend/static/js/api/profiles.js | 4 +- frontend/static/js/views/PageNotFoundView.js | 1 + frontend/static/js/views/Search.js | 82 ++++++-- profiles/views.py | 8 +- 9 files changed, 306 insertions(+), 115 deletions(-) diff --git a/chat/consumersNotice.py b/chat/consumersNotice.py index 37267a2..66dea1f 100644 --- a/chat/consumersNotice.py +++ b/chat/consumersNotice.py @@ -1,6 +1,8 @@ from channels.generic.websocket import WebsocketConsumer from asgiref.sync import async_to_sync +from games.models import GameModel + import time import json @@ -31,19 +33,7 @@ class ChatNoticeConsumer(WebsocketConsumer): self.accept() - 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, - 'targets': targets, - 'time':message_time, - 'status': 200, - }) + self.sync() def disconnect(self, code): @@ -51,22 +41,15 @@ class ChatNoticeConsumer(WebsocketConsumer): if (user.is_anonymous or not user.is_authenticated): return - self.channel_layer.users_channels.pop(user.pk) + del self.channel_layer.users_channels[user.pk] + del self.channel_layer.invite[user.pk] - message_time: int = int(time.time() * 1000) + for inviter_id, inviteds_id in self.channel_layer.invite.items(): + if (user.pk in inviteds_id): + self.channel_layer.invite[inviter_id].remove(user.pk) + + self.sync() - 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, - 'targets': targets, - 'time':message_time, - 'status': 200, - }) def receive(self, text_data=None, bytes_data=None): @@ -89,50 +72,115 @@ class ChatNoticeConsumer(WebsocketConsumer): if (self.channel_layer == None): return - message_time: int = int(time.time() * 1000) - status = 200; + message_time: int = int(text_data_json.get('time')) + + print(message_time) + print(time.time()) + print(time.time() * 1000 - message_time) + + if (message_time == None): + message_time: int = int(time.time() * 1000) #print("receive" + str(user.pk)) + result = None try: - status = getattr(self, "pre_" + type_notice)(user, targets) + status, result = getattr(self, "pre_" + type_notice)(user, targets) except AttributeError: - print(f"La fonction pre_{type_notice} n'existe pas.") + status = 200 + #print(f"La fonction pre_{type_notice} n'existe pas.") - if targets == "all": - targets = list(self.channel_layer.users_channels.keys()) + if (status < 300): + if targets == "all": + 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): + if (channel == None): + status = 404 + continue + async_to_sync(self.channel_layer.send)(channel, { + 'type':type_notice, + 'author_id':user.pk, + 'content':content, + 'result':result, + 'targets': targets, + 'time':message_time, + 'status': 200, + }) - for target in targets: - 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)(channel, { - 'type':type_notice, - 'author_id':user.pk, - 'content':content, - 'targets': targets, - 'time':message_time, - 'status': 200, - }) 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", + 'result':result, 'targets': targets, 'time':message_time, 'status':status, }) + def sync(self, user = None, level = None): + + sendToUser = True + if (user == None): + user = self.scope["user"] + sendToUser = False + + + if (level == None): + level = 0 + + message_time: int = int(time.time() * 1000) + if (sendToUser): + targets = [user.pk] + else: + targets = list(self.channel_layer.users_channels.keys()) + + for target in targets: + channel = self.channel_layer.users_channels.get(target) + if (channel == None or (not sendToUser and target == user.pk)): + continue + async_to_sync(self.channel_layer.send)(channel, { + 'type':"online_users", + 'author_id':user.pk, + 'targets': targets, + 'time':message_time, + 'status': 200, + }) + if (level >= 1): + async_to_sync(self.channel_layer.send)(channel, { + 'type':"invite", + 'author_id':user.pk, + 'targets': targets, + 'time':message_time, + 'status': 200, + }) + def pre_invite(self, user, targets): - status = 200 + status = 200 for target in targets: if (target in self.channel_layer.invite[user.pk]): status = 409 - return status + continue + + channel = self.channel_layer.users_channels.get(target) + if (channel == None): + status = 404 + continue + + # Add the invited in "self.channel_layer.invite" + if (user.pk != target): + self.channel_layer.invite[user.pk].append(target) + + return status, None + + def get_invites(self, user): + invites = [] + for inviter_id, inviteds_id in self.channel_layer.invite.items(): + if (user.pk in inviteds_id and user.pk != inviter_id): + invites.append(inviter_id) def invite(self, event): @@ -140,23 +188,48 @@ class ChatNoticeConsumer(WebsocketConsumer): if (user.is_anonymous or not user.is_authenticated): return - if (user.pk in self.channel_layer.invite[event["author_id"]]): - return - - if (user.pk != event["author_id"]): - self.channel_layer.invite[event["author_id"]].append(user.pk) + invites = self.get_invites(user) self.send(text_data=json.dumps({ 'type':event['type'], 'author_id':event['author_id'], - 'content':event['content'], + 'invites': invites, 'targets': event['targets'], 'time': event['time'], 'status':event['status'], })) - def pre_online_users(self, user, targets): - pass + def pre_accept_invite(self, user, targets): + + if (user.pk not in self.channel_layer.invite[targets[0]]): + return 400, None + + self.channel_layer.invite[targets[0]].remove(user.pk) + + if (targets[0] in self.channel_layer.invite[user.pk]): + self.channel_layer.invite[user.pk].remove(targets[0]) + + id_game = GameModel().create([user.pk, targets[0]]); + + return 200, id_game + + def accept_invite(self, event): + + user = self.scope["user"] + if (user.is_anonymous or not user.is_authenticated): + return + + invites = self.get_invites(user) + + self.send(text_data=json.dumps({ + 'type':event['type'], + 'author_id':event['author_id'], + 'result': event['result'], + 'invites': invites, + 'time': event['time'], + 'status':event['status'], + })) + def online_users(self, event): diff --git a/frontend/static/css/search.css b/frontend/static/css/search.css index 1b70a0a..6aa1061 100644 --- a/frontend/static/css/search.css +++ b/frontend/static/css/search.css @@ -78,7 +78,6 @@ border: none; outline: none; border-bottom: 0.15em solid green; - caret-color: green; color: green; font-size: 0.8em; } @@ -106,9 +105,8 @@ word-wrap: break-word; } -#app #invite { +#app #invite, #app #yes, #app #no { position: relative; - background-color: green; border: none; color: white; text-align: center; @@ -118,3 +116,15 @@ width: 4em; cursor: pointer; } + +#app #yes, #app #no { + position: relative; + border: none; + color: white; + text-align: center; + text-decoration: none; + font-size: 0.8em; + height: 2em; + width: 2em; + cursor: pointer; +} diff --git a/frontend/static/js/api/chat/notice.js b/frontend/static/js/api/chat/notice.js index 1ae4d23..039ad52 100644 --- a/frontend/static/js/api/chat/notice.js +++ b/frontend/static/js/api/chat/notice.js @@ -1,3 +1,4 @@ +import { navigateTo } from "../../index.js"; import {create_popup} from "../../utils/noticeUtils.js"; class Notice { @@ -7,9 +8,8 @@ class Notice { // users online, invited by, asked friend by, let data_variable = ["online", "invited", "asked"]; - for (let i in data_variable) { + for (let i in data_variable) this.data[data_variable[i]] = []; - } this.connect(); @@ -20,12 +20,14 @@ class Notice { this.chatSocket = new WebSocket(url); this.chatSocket.onmessage = (event) =>{ - let data = JSON.parse(event.data); - //console.log("notice: ", data); - if (data.type == "invite") - this.receiveInvite(data); - else if (data.type == "online_users" || data.type == "disconnect") - this.receiveOnlineUser(data); + let send = JSON.parse(event.data); + console.log("notice: ", send); + if (send.type == "invite") + this.receiveInvite(send); + else if (send.type == "online_users" || send.type == "disconnect") + this.receiveOnlineUser(send); + else if (send.type == "accept_invite") + this.receiveAcceptInvite(send); } this.chatSocket.onopen = (event) => { this.getOnlineUser(); @@ -40,61 +42,96 @@ class Notice { } - async sendInvite(id_inviter, id_inviteds) { - - if (this.chatSocket == undefined) - return; + async acceptInvite(invitedBy) { - this.chatSocket.send(JSON.stringify({ - type: "invite", - targets: id_inviteds, - })); + this.sendRequest({ + type: "accept_invite", + targets: [invitedBy], + }); } - async receiveInvite(data) { + async receiveAcceptInvite(send) { + + let id_game = send["result"]; + navigateTo("/game/" + id_game); + + } + + async refuseInvite(invitedBy) { + + this.sendRequest({ + type: "refuse_invite", + targets: [invitedBy], + }); + + + } + + async sendInvite(id_inviteds) { + + this.sendRequest({ + type: "invite", + targets: id_inviteds, + time: new Date().getTime(), + }); + + } + + async receiveInvite(send) { - if (data.content === "notice return") { - if (data.status == 200) { - for (let target in data.targets) - this.data["invited"].push(target); + if (this.client.me == undefined) + return ; + + let content = send.content; + + if (send.author_id == this.client.me.id) { + if (send.status == 200) { + for (let target in send.targets) return create_popup("Invitation send"); } - else if (data.status == 404) + else if (send.status == 404) return create_popup("User not connected"); - else if (data.status == 409) + else if (send.status == 409) return create_popup("Already invited"); } else { - let sender = await this.client.profiles.getProfile(data.author_id); - this.inviter.push(data.author_id); + if (!content.includes(send.author_id) || + this.data["invited"].includes(send.author_id)) + return; + + this.data["invited"] = content; + let sender = await this.client.profiles.getProfile(send.author_id); + create_popup("Invitation received by " + sender.username); + if (this.rewrite_invite !== undefined) + this.rewrite_invite(); + // Géré la reception de l'invitation } } async getOnlineUser() { - if (this.chatSocket == undefined) - return; - this.online_users = {}; - this.chatSocket.send(JSON.stringify({ + this.sendRequest({ type: "online_users", - targets: "all", - })); + targets: [], + time: new Date().getTime(), + }); } - async receiveOnlineUser(data) { - if (data.content !== undefined) { + async receiveOnlineUser(send) { + let content = send.content; + if (content !== undefined) { - if (this.online_users.length > 0) { + if (this.data["online"].length > 0) { // get all disconnect user - let disconnects = this.online_users.filter(id => !Object.keys(data.content).includes(id)); + let disconnects = this.data["online"].filter(id => !Object.keys(content).includes(id)); // delete invite this.data["invited"] = this.data["invited"].filter(id => !disconnects.includes(id)); @@ -102,12 +139,19 @@ class Notice { //console.log(this.data["invited"]); } - this.data["online"] = Object.keys(data.content); + this.data["online"] = Object.keys(content); if (this.rewrite_usernames !== undefined) this.rewrite_usernames(); } } + + async sendRequest(content) { + if (this.chatSocket == undefined) + return; + + this.chatSocket.send(JSON.stringify(content)); + } } export {Notice} diff --git a/frontend/static/js/api/client.js b/frontend/static/js/api/client.js index bc3559c..2b42a69 100644 --- a/frontend/static/js/api/client.js +++ b/frontend/static/js/api/client.js @@ -30,7 +30,7 @@ class Client this.channels = new Channels(this); this.channel = undefined; - + this.notice = new Notice(this); } diff --git a/frontend/static/js/api/profile.js b/frontend/static/js/api/profile.js index b3f1b15..4ee1e92 100644 --- a/frontend/static/js/api/profile.js +++ b/frontend/static/js/api/profile.js @@ -34,6 +34,7 @@ class Profile let block_response = await this.client._get("/api/profiles/block"); + if (block_response.status != 200) return @@ -42,7 +43,7 @@ class Profile block_list.forEach(block => { let blocker = block.fields.blocker; let blocked = block.fields.blocked; - if (blocker == this.client.me.user_id && blocked == user_id) + if (blocker == this.client.me.id && blocked == this.id) return this.isBlocked = true; }); } diff --git a/frontend/static/js/api/profiles.js b/frontend/static/js/api/profiles.js index 177f5a0..54c8129 100644 --- a/frontend/static/js/api/profiles.js +++ b/frontend/static/js/api/profiles.js @@ -37,7 +37,7 @@ class Profiles // blocker & blocked let response = await this.client._post("/api/profiles/block", { - users_id:[this.client.me.user_id, user_id], + users_id:[this.client.me.id, user_id], }); let data = await response.json(); @@ -49,7 +49,7 @@ class Profiles // blocker & blocked let response = await this.client._delete("/api/profiles/block", { - users_id:[this.client.me.user_id, user_id], + users_id:[this.client.me.id, user_id], }); let data = await response.json(); diff --git a/frontend/static/js/views/PageNotFoundView.js b/frontend/static/js/views/PageNotFoundView.js index 5d875c3..334c1a2 100644 --- a/frontend/static/js/views/PageNotFoundView.js +++ b/frontend/static/js/views/PageNotFoundView.js @@ -8,6 +8,7 @@ export default class extends AbstractView { async getHtml() { return `

404 Bozo

+

Git gud

`; } diff --git a/frontend/static/js/views/Search.js b/frontend/static/js/views/Search.js index 5f84845..13aae62 100644 --- a/frontend/static/js/views/Search.js +++ b/frontend/static/js/views/Search.js @@ -31,9 +31,11 @@ export default class extends AbstractView { //await client.notice.getOnlineUser(); await this.wait_get_online_users(); client.notice.rewrite_usernames = this.rewrite_usernames; + client.notice.rewrite_invite = this.display_invite; let search = document.getElementById("input_user"); - search.oninput = () => this.display_users(logged, profiles); + if (search != undefined) + search.oninput = () => this.display_users(logged, profiles); let chat_input = document.getElementById("input_chat"); //chat_input.addEventListener("keydown", this.display_chat_manager) @@ -175,13 +177,15 @@ export default class extends AbstractView { chat_input.maxLength=255; chat.appendChild(chat_input); + let members_id = client.channels.channel.members_id; + chat_input.onkeydown = async () => { if (event.keyCode == 13 && client.channels.channel != undefined) { //let chat_input = document.getElementById("input_chat"); let chat_text = chat_input.value; let receivers_id = []; - client.channels.channel.members_id.forEach((member_id) => { + members_id.forEach((member_id) => { if (member_id != client.me.id) receivers_id.push(profiles.filter(user => user.id == member_id)[0].id); }); @@ -196,7 +200,7 @@ export default class extends AbstractView { // Scroll to the bottom of messages messages.scrollTop = messages.scrollHeight; - this.display_invite(chat); + this.display_invite(); } @@ -242,10 +246,13 @@ export default class extends AbstractView { } async display_members(chat, profiles) { + + let members_id = client.channels.channel.members_id; + let members = document.createElement("h2"); members.id = "members"; let usernames = ""; - client.channels.channel.members_id.forEach((member_id) => { + members_id.forEach((member_id) => { if (member_id != client.me.id) { if (usernames.length > 0) usernames += ", "; @@ -258,17 +265,66 @@ export default class extends AbstractView { return members } - async display_invite(chat, profiles) { + async display_invite() { + + let chat = document.getElementById("chat"); + + if (chat == undefined) + return ; + + let members_id = client.channels.channel.members_id; + let others = members_id.filter(id => id !== client.me.id); - // Button to send invite to play let invite = document.getElementById("invite") || document.createElement("button"); - 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); + let yes = document.getElementById("yes") || document.createElement("button"); + let no = document.getElementById("no") || document.createElement("button"); + + let invitedBy = undefined; + for (let x in others) { + if (client.notice.data["invited"].includes(others[x])) { + invitedBy = others[x]; + } + } + + if (invitedBy == undefined) { + + if (yes && no) { + yes.remove(); + no.remove(); + } + + // Button to send invite to play + invite.id = "invite"; + invite.style.background = "orange"; + invite.innerText = "invite"; + invite.title = "Invite to play a game" + invite.onclick = async () => { + await client.notice.sendInvite(others); + }; + chat.appendChild(invite); + } + else { + + if (invite) + invite.remove() + + yes.id = "yes"; + yes.style.background = "green"; + yes.title = "Accept to play a game" + yes.onclick = async () => { + await client.notice.acceptInvite(invitedBy); + }; + + no.id = "no"; + no.style.background = "red"; + no.title = "Refuse to play a game" + no.onclick = async () => { + await client.notice.refuseInvite(invitedBy); + }; + + chat.appendChild(yes); + chat.appendChild(no); + } } diff --git a/profiles/views.py b/profiles/views.py index dbcbcab..7b556d3 100644 --- a/profiles/views.py +++ b/profiles/views.py @@ -33,7 +33,10 @@ class BlocksView(APIView): users_id = request.data.get("users_id", None) if (users_id == None): - return Response({"Error"}, status=status.HTTP_400_BAD_REQUEST) + return Response({"Error send None"}, status=status.HTTP_400_BAD_REQUEST) + + if (users_id[0] == None or users_id[1] == None): + return Response({"Error send blocker/ed None"}, status=status.HTTP_400_BAD_REQUEST) if (BlockModel.objects.filter(blocker=users_id[0], blocked=users_id[1])): return Response({"Already Exist"}, status=status.HTTP_409_CONFLICT) @@ -52,6 +55,9 @@ class BlocksView(APIView): if (users_id == None): return Response({"Error"}, status=status.HTTP_400_BAD_REQUEST) + if (users_id[0] == None or users_id[1] == None): + return Response({"Error send blocker/ed None"}, status=status.HTTP_400_BAD_REQUEST) + block = BlockModel.objects.filter(blocker=users_id[0], blocked=users_id[1]) print(list(block)) From 0f7953b2f3c9333a203daf63af19e035c54423ba Mon Sep 17 00:00:00 2001 From: Xamora Date: Wed, 24 Jan 2024 16:03:50 +0100 Subject: [PATCH 02/77] Chat finish; add invitation; friend; see online users if he is your friend --- chat/consumersNotice.py | 161 +++++++++++++++-- frontend/static/css/profile.css | 12 +- frontend/static/js/api/chat/notice.js | 188 +++++++++++++++++--- frontend/static/js/api/client.js | 2 +- frontend/static/js/api/profile.js | 36 +++- frontend/static/js/index.js | 1 + frontend/static/js/views/ProfilePageView.js | 93 ++++++++-- frontend/static/js/views/Search.js | 36 ++-- profiles/admin.py | 4 +- profiles/models.py | 64 +++++++ profiles/urls.py | 1 + profiles/views.py | 13 +- 12 files changed, 532 insertions(+), 79 deletions(-) diff --git a/chat/consumersNotice.py b/chat/consumersNotice.py index 66dea1f..c0f2723 100644 --- a/chat/consumersNotice.py +++ b/chat/consumersNotice.py @@ -2,6 +2,7 @@ from channels.generic.websocket import WebsocketConsumer from asgiref.sync import async_to_sync from games.models import GameModel +from profiles.models import FriendModel, AskFriendModel import time import json @@ -72,23 +73,17 @@ class ChatNoticeConsumer(WebsocketConsumer): if (self.channel_layer == None): return - message_time: int = int(text_data_json.get('time')) - - print(message_time) - print(time.time()) - print(time.time() * 1000 - message_time) + message_time: int = text_data_json.get('time') if (message_time == None): message_time: int = int(time.time() * 1000) - #print("receive" + str(user.pk)) - result = None try: status, result = getattr(self, "pre_" + type_notice)(user, targets) - except AttributeError: + except AttributeError as error: + print(error) status = 200 - #print(f"La fonction pre_{type_notice} n'existe pas.") if (status < 300): if targets == "all": @@ -98,7 +93,7 @@ class ChatNoticeConsumer(WebsocketConsumer): channel = self.channel_layer.users_channels.get(target) if (channel == None or target == user.pk): if (channel == None): - status = 404 + status = 444 # target not connected continue async_to_sync(self.channel_layer.send)(channel, { 'type':type_notice, @@ -159,6 +154,9 @@ class ChatNoticeConsumer(WebsocketConsumer): def pre_invite(self, user, targets): + if (user.is_anonymous or not user.is_authenticated): + return 400, None + status = 200 for target in targets: if (target in self.channel_layer.invite[user.pk]): @@ -181,6 +179,7 @@ class ChatNoticeConsumer(WebsocketConsumer): for inviter_id, inviteds_id in self.channel_layer.invite.items(): if (user.pk in inviteds_id and user.pk != inviter_id): invites.append(inviter_id) + return invites; def invite(self, event): @@ -201,6 +200,9 @@ class ChatNoticeConsumer(WebsocketConsumer): def pre_accept_invite(self, user, targets): + if (user.is_anonymous or not user.is_authenticated): + return 400, None + if (user.pk not in self.channel_layer.invite[targets[0]]): return 400, None @@ -224,27 +226,148 @@ class ChatNoticeConsumer(WebsocketConsumer): self.send(text_data=json.dumps({ 'type':event['type'], 'author_id':event['author_id'], - 'result': event['result'], + 'id_game': event['result'], 'invites': invites, 'time': event['time'], 'status':event['status'], })) - - def online_users(self, event): + def pre_refuse_invite(self, user, targets): + + if (user.is_anonymous or not user.is_authenticated): + return 400, None + + if (user.pk not in self.channel_layer.invite[targets[0]]): + return 400, None + + self.channel_layer.invite[targets[0]].remove(user.pk) + + if (targets[0] in self.channel_layer.invite[user.pk]): + self.channel_layer.invite[user.pk].remove(targets[0]) + + return 200, None + + def refuse_invite(self, event): user = self.scope["user"] - #if (user.is_anonymous or not user.is_authenticated): - #return + if (user.is_anonymous or not user.is_authenticated): + return - #print("online_users" + str(user.pk)) - - event['content'] = self.channel_layer.users_channels + invites = self.get_invites(user) self.send(text_data=json.dumps({ 'type':event['type'], 'author_id':event['author_id'], - 'content':event['content'], + 'invites': invites, + 'time': event['time'], + 'status':event['status'], + })) + + def pre_ask_friend(self, user, targets): + + if (user.is_anonymous or not user.is_authenticated): + return 400, None + + if (AskFriendModel.objects.filter(asker=user.pk, asked=targets[0])): + return 409, None + + if (FriendModel().isFriend(user.pk, targets[0])): + return 409, None + + if (targets[0] != None): + AskFriendModel(asker=user.pk, asked=targets[0]).save() + + return 200, None + + def send_ask_friend(self, event): + + user = self.scope["user"] + if (user.is_anonymous or not user.is_authenticated): + return + + asked = AskFriendModel().getAsked(user.pk) + asker = AskFriendModel().getAsker(user.pk) + + self.send(text_data=json.dumps({ + 'type':event['type'], + 'author_id':event['author_id'], + 'targets':event['targets'], + 'asker': asker, + 'asked': asked, + 'time': event['time'], + 'status':event['status'], + })) + + def ask_friend(self, event): + self.send_ask_friend(event) + + def delete_ask(self, asker, user_asked): + if (user_asked.is_anonymous or not user_asked.is_authenticated): + return 400, None + + asked = user_asked.pk + + if (not AskFriendModel.objects.filter(asker=asker, asked=asked)): + return 404, None + + if (FriendModel().isFriend(asker, asked)): + return 409, None + + if (not AskFriendModel().deleteAsk(asker, asked)): + return 400, None + + return 200, None + + def pre_accept_friend(self, user, targets): + status, result = self.delete_ask(targets[0], user) + + if (status == 200): + FriendModel(user_id1=user.pk, user_id2=targets[0]).save() + return status, result + + def accept_friend(self, event): + self.send_ask_friend(event) + + def pre_refuse_friend(self, user, targets): + return self.delete_ask(targets[0], user) + + def refuse_friend(self, event): + self.send_ask_friend(event) + + def pre_remove_friend(self, user, targets): + + if (user.is_anonymous or not user.is_authenticated): + return 400, None + + if (not FriendModel().isFriend(user.pk, targets[0])): + return 409, None + + if (not FriendModel().deleteFriend(user.pk, targets[0])): + return 400, None + + return 200, None + + def remove_friend(self, event): + self.send_ask_friend(event) + + + def online_users(self, event): + + user = self.scope["user"] + if (user.is_anonymous or not user.is_authenticated): + return + + online_friends = {} + for friend in FriendModel().getFriends(user.pk): + if (friend in self.channel_layer.users_channels): + online_friends[friend] = "green" + else: + online_friends[friend] = "red" + + self.send(text_data=json.dumps({ + 'type':event['type'], + 'author_id':event['author_id'], + 'content':online_friends, 'time': event['time'], 'status':event['status'], })) diff --git a/frontend/static/css/profile.css b/frontend/static/css/profile.css index b86bfb0..21fb0dc 100644 --- a/frontend/static/css/profile.css +++ b/frontend/static/css/profile.css @@ -3,12 +3,20 @@ font-size: 0.8em; } -#app #block { +#app #block, #app #friend { cursor: pointer; font-size: 0.7em; text-decoration: underline; } #app { - margin-top: 20px; + margin-top: 1em; + background-color: red; +} + +#app #yes, #app #no { + display:inline; + cursor: pointer; + font-size: 0.7em; + text-decoration: underline; } diff --git a/frontend/static/js/api/chat/notice.js b/frontend/static/js/api/chat/notice.js index 039ad52..4616179 100644 --- a/frontend/static/js/api/chat/notice.js +++ b/frontend/static/js/api/chat/notice.js @@ -6,8 +6,8 @@ class Notice { this.client = client; this.data = {}; - // users online, invited by, asked friend by, - let data_variable = ["online", "invited", "asked"]; + // users online, invited by ..., asked by ..., asker to ... + let data_variable = ["online", "invited", "asked", "asker"]; for (let i in data_variable) this.data[data_variable[i]] = []; @@ -21,16 +21,19 @@ class Notice { this.chatSocket = new WebSocket(url); this.chatSocket.onmessage = (event) =>{ let send = JSON.parse(event.data); - console.log("notice: ", send); - if (send.type == "invite") - this.receiveInvite(send); - else if (send.type == "online_users" || send.type == "disconnect") - this.receiveOnlineUser(send); - else if (send.type == "accept_invite") - this.receiveAcceptInvite(send); + //console.log("notice: ", send); + + try { + this["receive_" + send.type](send); + } + catch (error) { + console.log("receive_" + send.type + ": Function not found"); + } + } this.chatSocket.onopen = (event) => { this.getOnlineUser(); + this.ask_friend(); } } @@ -42,7 +45,7 @@ class Notice { } - async acceptInvite(invitedBy) { + async accept_invite(invitedBy) { this.sendRequest({ type: "accept_invite", @@ -51,14 +54,15 @@ class Notice { } - async receiveAcceptInvite(send) { + async receive_accept_invite(send) { - let id_game = send["result"]; + this.data["invited"] = send.invites; + let id_game = send["id_game"]; navigateTo("/game/" + id_game); } - async refuseInvite(invitedBy) { + async refuse_invite(invitedBy) { this.sendRequest({ type: "refuse_invite", @@ -67,8 +71,23 @@ class Notice { } + async receive_refuse_invite(send) { - async sendInvite(id_inviteds) { + this.data["invited"] = send.invites; + + if (send.author_id == this.client.me.id) { + if (this.rewrite_invite !== undefined) + this.rewrite_invite(); + } + else { + let sender = await this.client.profiles.getProfile(send.author_id); + create_popup(sender.username + " refuse your invitation"); + } + + } + + + async send_invite(id_inviteds) { this.sendRequest({ type: "invite", @@ -78,25 +97,27 @@ class Notice { } - async receiveInvite(send) { + async receive_invite(send) { if (this.client.me == undefined) return ; - let content = send.content; + let content = send.invites; if (send.author_id == this.client.me.id) { if (send.status == 200) { for (let target in send.targets) return create_popup("Invitation send"); } - else if (send.status == 404) + else if (send.status == 444) return create_popup("User not connected"); else if (send.status == 409) return create_popup("Already invited"); } else { + // Regarder qu'il est bien invité par l'auteur + // Et qu'il n'est pas déjà invité if (!content.includes(send.author_id) || this.data["invited"].includes(send.author_id)) return; @@ -108,8 +129,6 @@ class Notice { if (this.rewrite_invite !== undefined) this.rewrite_invite(); - - // Géré la reception de l'invitation } } @@ -125,13 +144,20 @@ class Notice { } - async receiveOnlineUser(send) { + async receive_online_users(send) { let content = send.content; if (content !== undefined) { if (this.data["online"].length > 0) { // get all disconnect user - let disconnects = this.data["online"].filter(id => !Object.keys(content).includes(id)); + //let disconnects = this.data["online"].filter(id => !Object.keys(content).includes(id)); + + let disconnects = []; + + for (const [key, value] of Object.entries(this.data["online"])) { + if (content[key] == "red" && value == "green") + disconnects.push(key); + } // delete invite this.data["invited"] = this.data["invited"].filter(id => !disconnects.includes(id)); @@ -139,13 +165,131 @@ class Notice { //console.log(this.data["invited"]); } - this.data["online"] = Object.keys(content); + this.data["online"] = content; if (this.rewrite_usernames !== undefined) this.rewrite_usernames(); } } + async ask_friend(user_id=undefined) { + this.sendRequest({ + type: "ask_friend", + targets: [user_id], + time: new Date().getTime(), + }); + } + + async receive_ask_friend(send) { + + let my_id = (this.client.me && this.client.me.id) || send.author_id; + if (send.author_id == my_id) { + if (send.status == 400) + create_popup("Friend ask error"); + else if (send.status == 409) + create_popup("Already asked friend or already friend"); + } + + //if (!send.asked.includes(send.author_id) || + //this.data["asked"].includes(send.author_id)) + //return; + + //if (!send.asker.includes(send.author_id) || + //this.data["asker"].includes(send.author_id)) + //return; + + this.data["asked"] = send.asked; + this.data["asker"] = send.asker; + + if (send.author_id != my_id) { + let sender = await this.client.profiles.getProfile(send.author_id); + if (this.data["asker"].includes(send.author_id)) + create_popup(sender.username + " ask you as friend"); + if (this.rewrite_profile !== undefined) + await this.rewrite_profile(); + } + + } + + async remove_friend(user_id) { + this.sendRequest({ + type: "remove_friend", + targets: [user_id], + time: new Date().getTime(), + }); + } + + async receive_remove_friend(send) { + this.data["asked"] = send.asked; + this.data["asker"] = send.asker; + + if (send.author_id == this.client.me.id) { + if (send.status == 400) + create_popup("Error remove Friend"); + else if (send.status == 409) + create_popup("Not friend, wtf"); + + } + + if (this.rewrite_profile !== undefined) + await this.rewrite_profile(); + } + + async accept_friend(user_id) { + this.sendRequest({ + type: "accept_friend", + targets: [user_id], + time: new Date().getTime(), + }); + } + + async receive_accept_friend(send) { + this.data["asked"] = send.asked; + this.data["asker"] = send.asker; + let sender = await this.client.profiles.getProfile(send.author_id); + + if (send.author_id == this.client.me.id) { + if (send.status == 400) + create_popup("Error accept Friend"); + else if (send.status == 404) + create_popup("Not found request Friend"); + else if (send.status == 409) + create_popup("Already Friend, wtf"); + } + else { + create_popup(sender.username + " accept your friend request"); + } + + if (this.rewrite_profile !== undefined) + await this.rewrite_profile(); + } + + async refuse_friend(user_id) { + this.sendRequest({ + type: "refuse_friend", + targets: [user_id], + time: new Date().getTime(), + }); + } + + async receive_refuse_friend(send) { + this.data["asked"] = send.asked; + this.data["asker"] = send.asker; + let sender = await this.client.profiles.getProfile(send.author_id); + + if (send.author_id == this.client.me.id) { + if (send.status == 400) + create_popup("Error refuse Friend"); + else if (send.status == 404) + create_popup("Not found request Friend"); + else if (send.status == 409) + create_popup("Already Friend, WTF"); + + } + if (this.rewrite_profile !== undefined) + await this.rewrite_profile(); + } + async sendRequest(content) { if (this.chatSocket == undefined) return; diff --git a/frontend/static/js/api/client.js b/frontend/static/js/api/client.js index 2b42a69..50c71b1 100644 --- a/frontend/static/js/api/client.js +++ b/frontend/static/js/api/client.js @@ -79,7 +79,7 @@ class Client async _patch_json(uri, data) { let response = await fetch(this._url + uri, { - method: "PATCH", + ethod: "PATCH", headers: { "X-CSRFToken": getCookie("csrftoken"), "Content-Type": "application/json", diff --git a/frontend/static/js/api/profile.js b/frontend/static/js/api/profile.js index 4ee1e92..91b5858 100644 --- a/frontend/static/js/api/profile.js +++ b/frontend/static/js/api/profile.js @@ -15,6 +15,7 @@ class Profile this.username = username; this.avatar_url = avatar_url; this.isBlocked = false; + this.isFriend = false; } async init() @@ -29,24 +30,47 @@ class Profile this.username = response_data.username; this.avatar_url = response_data.avatar_url; - if (this.client.me == undefined) - return; + await this.getBlock(); + await this.getFriend(); + } + + async getBlock() { let block_response = await this.client._get("/api/profiles/block"); - - + if (block_response.status != 200) return let block_data = await block_response.json(); - let block_list = JSON.parse(block_data); + let block_list = JSON.parse(block_data["blockeds"]); + let client_id = block_data["user_id"]; block_list.forEach(block => { let blocker = block.fields.blocker; let blocked = block.fields.blocked; - if (blocker == this.client.me.id && blocked == this.id) + if (blocker == client_id && blocked == this.id) return this.isBlocked = true; }); } + + async getFriend() { + let friend_response = await this.client._get("/api/profiles/friend"); + + this.isFriend = false; + if (friend_response.status != 200) + return this.isFriend; + + let friend_data = await friend_response.json(); + let friends_list = friend_data["friends"]; + let client_id = friend_data["user_id"]; + friends_list.forEach(friend => { + if (friend == this.id) { + this.isFriend = true; + return this.isFriend; + } + }); + return this.isFriend; + } + } export {Profile} diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index 385b838..9818b9c 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -58,6 +58,7 @@ async function renderView(view) let error_code = await view.postInit(); + if (error_code === 404) renderView(new PageNotFoundView()); else if (error_code === 403) diff --git a/frontend/static/js/views/ProfilePageView.js b/frontend/static/js/views/ProfilePageView.js index bb4a6fc..b26a339 100644 --- a/frontend/static/js/views/ProfilePageView.js +++ b/frontend/static/js/views/ProfilePageView.js @@ -4,17 +4,16 @@ import { client } from "../index.js" export default class extends AbstractView { constructor(params) { super(params, "Profile "); - this.user_id = params.id; + this.id = Number(params.id); } async postInit() { - let profile = await client.profiles.getProfile(this.user_id); + this.profile = await client.profiles.getProfile(this.id); - if (profile === null) + if (this.profile === null) return 404; - this.profile = await client.profiles.getProfile(this.user_id); this.info = document.getElementById("info"); // Username @@ -32,6 +31,14 @@ export default class extends AbstractView { this.info.appendChild(avatar); await this.blockButton(); + + await this.friendButton(); + + client.notice.rewrite_profile = async () => { + let result = await this.profile.getFriend(); + await this.profile.getBlock() + await this.friendButton(); + } } async blockButton() { @@ -39,23 +46,81 @@ export default class extends AbstractView { if (await client.isAuthentificate() === false) return; - if (client.me.id != this.user_id) { - let block = document.getElementById("block") || document.createElement("a"); + if (client.me.id != this.id) { + let block = document.getElementById("block"); + if (block == undefined) { + block = document.createElement("p"); + this.info.appendChild(block); + } + block.id = "block"; - block.innerText = ""; block.onclick = async () => { - if (!this.profile.isBlocked) - await client.profiles.block(this.user_id); + if (this.profile.isBlocked) + await client.profiles.deblock(this.id); else - await client.profiles.deblock(this.user_id); - this.profile = await client.profiles.getProfile(this.user_id); + await client.profiles.block(this.id); + this.profile = await client.profiles.getProfile(this.id); this.blockButton(); }; if (this.profile.isBlocked) - block.appendChild(document.createTextNode("Deblock")); + block.textContent = "Deblock"; else - block.appendChild(document.createTextNode("Block")); - this.info.appendChild(block); + block.textContent = "Block"; + } + } + + async friendButton() { + if (await client.isAuthentificate() === false) + return; + + if (client.me.id != this.id) { + let yes = document.getElementById("yes") || document.createElement("p"); + let no = document.getElementById("no") || document.createElement("p"); + let friend = document.getElementById("friend") || document.createElement("p"); + + if (client.notice.data["asker"].includes(this.id)) { + + if (friend) + friend.remove(); + + yes.id = "yes"; + yes.textContent = "Accept Friend"; + yes.onclick = async () => { + client.notice.accept_friend(this.id); + } + + no.id = "no"; + no.textContent = "Refuse Friend"; + no.onclick = async () => { + client.notice.refuse_friend(this.id); + } + + this.info.appendChild(yes); + this.info.appendChild(document.createTextNode(" ")); + this.info.appendChild(no); + + } + else { + + if (yes && no) + yes.remove(); no.remove(); + + friend.id = "friend" + friend.onclick = async () => { + if (this.profile.isFriend) + await client.notice.remove_friend(this.id); + else + await client.notice.ask_friend(this.id); + this.profile = await client.profiles.getProfile(this.id); + this.friendButton(); + }; + if (this.profile.isFriend) + friend.textContent = "Remove Friend"; + else { + friend.textContent = "Ask Friend"; + } + this.info.appendChild(friend); + } } } diff --git a/frontend/static/js/views/Search.js b/frontend/static/js/views/Search.js index 13aae62..812b5c4 100644 --- a/frontend/static/js/views/Search.js +++ b/frontend/static/js/views/Search.js @@ -10,12 +10,12 @@ export default class extends AbstractView { async wait_get_online_users() { return new Promise((resolve) => { const checkInterval = setInterval(() => { - //console.log(client.notice.data["online"].length); - if (client.notice.data["online"].length > 0) { + console.log(client.notice.data["online"]); + if (Object.keys(client.notice.data["online"]).length > 0) { clearInterval(checkInterval); resolve(); } - }, 100); + }, 1); }); } @@ -29,7 +29,7 @@ export default class extends AbstractView { return console.log("Error"); //await client.notice.getOnlineUser(); - await this.wait_get_online_users(); + //await this.wait_get_online_users(); client.notice.rewrite_usernames = this.rewrite_usernames; client.notice.rewrite_invite = this.display_invite; @@ -68,7 +68,14 @@ export default class extends AbstractView { username.setAttribute('data-link', ''); username.id = `username${user.id}` username.href = `/profiles/${user.id}`; - username.style.color = client.notice.data["online"].includes(user.id.toString()) ? "green" : "red"; + + if (user.id == client.me.id) + username.style.color = "green"; + else { + let online = client.notice.data["online"][user.id]; + username.style.color = online !== undefined ? online : "gray"; + } + username.appendChild(document.createTextNode(user.username)); new_user.appendChild(username); @@ -137,8 +144,14 @@ export default class extends AbstractView { profiles.filter(user => user.username.toLowerCase().startsWith(search) == true).forEach((user) => { let username = document.getElementById(`username${user.id}`); - if (username !== null) - username.style.color = client.notice.data["online"].includes(user.id.toString()) ? "green" : "red"; + if (username !== null) { + if (user.id == client.me.id) + username.style.color = "green"; + else { + let online = client.notice.data["online"][user.id]; + username.style.color = online !== undefined ? online : "gray"; + } + } }); } @@ -259,9 +272,10 @@ export default class extends AbstractView { usernames += (profiles.filter(user => user.id == member_id)[0].username); } }); - members.appendChild(document.createTextNode(usernames)); + members.textContent = usernames; chat.appendChild(members); + return members } @@ -299,7 +313,7 @@ export default class extends AbstractView { invite.innerText = "invite"; invite.title = "Invite to play a game" invite.onclick = async () => { - await client.notice.sendInvite(others); + await client.notice.send_invite(others); }; chat.appendChild(invite); } @@ -312,14 +326,14 @@ export default class extends AbstractView { yes.style.background = "green"; yes.title = "Accept to play a game" yes.onclick = async () => { - await client.notice.acceptInvite(invitedBy); + await client.notice.accept_invite(invitedBy); }; no.id = "no"; no.style.background = "red"; no.title = "Refuse to play a game" no.onclick = async () => { - await client.notice.refuseInvite(invitedBy); + await client.notice.refuse_invite(invitedBy); }; chat.appendChild(yes); diff --git a/profiles/admin.py b/profiles/admin.py index d79bf0b..45df28e 100644 --- a/profiles/admin.py +++ b/profiles/admin.py @@ -1,7 +1,9 @@ from django.contrib import admin -from .models import ProfileModel, BlockModel +from .models import ProfileModel, BlockModel, FriendModel, AskFriendModel # Register your models here. admin.site.register(ProfileModel) admin.site.register(BlockModel) +admin.site.register(FriendModel) +admin.site.register(AskFriendModel) diff --git a/profiles/models.py b/profiles/models.py index 1362463..10e197d 100644 --- a/profiles/models.py +++ b/profiles/models.py @@ -26,3 +26,67 @@ class BlockModel(models.Model): def __str__(self): return "blocker_id: " + str(self.blocker) + ", blocked_id: " + str(self.blocked) + +class AskFriendModel(models.Model): + asker = IntegerField(primary_key=False) + asked = IntegerField(primary_key=False) + + def getAsked(self, asker): + askeds = [] + + for ask in AskFriendModel.objects.filter(asker=asker): + askeds.append(ask.asked) + + return askeds + + def getAsker(self, asked): + askers = [] + + for ask in AskFriendModel.objects.filter(asked=asked): + askers.append(ask.asker) + + return askers + + def deleteAsk(self, asker, asked): + + deleted = AskFriendModel.objects.filter(asker=asker, asked=asked) + + if (deleted.count() == 0 or not deleted): + return False + + deleted.delete() + return True + +class FriendModel(models.Model): + user_id1 = IntegerField(primary_key=False) + user_id2 = IntegerField(primary_key=False) + + def getFriends(self, user_id): + friends = [] + + for friend in FriendModel.objects.filter(user_id1=user_id): + friends.append(friend.user_id2) + + for friend in FriendModel.objects.filter(user_id2=user_id): + friends.append(friend.user_id1) + + return friends + + def isFriend(self, user_id1, user_id2): + + return user_id2 in self.getFriends(user_id1) + + def deleteFriend(self, user_id1, user_id2): + + first = FriendModel.objects.filter(user_id1=user_id1, user_id2=user_id2) + if (first.count() == 1): + first.delete() + return True + + second = FriendModel.objects.filter(user_id1=user_id2, user_id2=user_id1) + if (second.count() == 1): + second.delete() + return True + + return False + diff --git a/profiles/urls.py b/profiles/urls.py index 8fe7c4f..ebc43c2 100644 --- a/profiles/urls.py +++ b/profiles/urls.py @@ -11,5 +11,6 @@ urlpatterns = [ path("", viewsets.ProfileViewSet.as_view({'get': 'list'}), name="profiles_list"), path("block", views.BlocksView.as_view(), name="block_page"), path("block/", views.BlockView.as_view(), name="block_page"), + path("friend", views.FriendsView.as_view(), name="friend_page"), ] + static("/static/avatars/", document_root="./avatars") diff --git a/profiles/views.py b/profiles/views.py index 7b556d3..19f6635 100644 --- a/profiles/views.py +++ b/profiles/views.py @@ -4,7 +4,7 @@ from rest_framework import authentication, permissions, status from rest_framework.authentication import SessionAuthentication from rest_framework.renderers import JSONRenderer from django.core import serializers -from .models import BlockModel +from .models import BlockModel, FriendModel class BlockView(APIView): permission_classes = (permissions.IsAuthenticated,) @@ -25,7 +25,7 @@ class BlocksView(APIView): def get(self, request): blocks = BlockModel.objects.filter(blocker=request.user.pk) if (blocks): - return Response(serializers.serialize("json", BlockModel.objects.filter(blocker=request.user.pk)), status=status.HTTP_200_OK) + return Response({"blockeds": serializers.serialize("json", BlockModel.objects.filter(blocker=request.user.pk)), "user_id": request.user.pk}, status=status.HTTP_200_OK) return Response({}, status=status.HTTP_204_NO_CONTENT) def post(self, request): @@ -60,7 +60,6 @@ class BlocksView(APIView): block = BlockModel.objects.filter(blocker=users_id[0], blocked=users_id[1]) - print(list(block)) if (block.count() > 1): return Response("Not normal >:[", status=status.HTTP_500_INTERNAL_SERVER_ERROR) @@ -69,3 +68,11 @@ class BlocksView(APIView): block.delete() return Response("Deleted", status=status.HTTP_200_OK) + +class FriendsView(APIView): + + def get(self, request): + friends = FriendModel().getFriends(request.user.pk) + if (friends): + return Response({"friends": friends, "user_id": request.user.pk}, status=status.HTTP_200_OK) + return Response({}, status=status.HTTP_204_NO_CONTENT) From feaaaec128c01b9578d4813ffec0418fc9a128b0 Mon Sep 17 00:00:00 2001 From: AdrienLSH Date: Mon, 29 Jan 2024 10:16:06 +0100 Subject: [PATCH 03/77] improved login redirections --- frontend/static/js/index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index 2d2af71..4005051 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -107,14 +107,16 @@ const router = async(uri) => { if (lastView !== undefined) await lastView.leavePage(); + if (uri !== '/login' && uri !== '/register' && uri !== '/logout') + lastPageUrlBeforeLogin = uri; + else + lastPageUrlBeforeLogin = undefined; const view = new match.route.view(getParams(match), lastPageUrlBeforeLogin); if (view instanceof AbstractRedirectView && await view.redirect()) return 1; lastView = view; - if (uri !== '/login' && uri !== '/register' && uri !== '/logout') - lastPageUrlBeforeLogin = uri; if (await renderView(view)) return 1; From f32caad343e6d974a6bf2d4a5b452e71081b67d7 Mon Sep 17 00:00:00 2001 From: AdrienLSH Date: Mon, 29 Jan 2024 10:19:11 +0100 Subject: [PATCH 04/77] bozo move --- frontend/static/js/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index 4005051..e2a7cfe 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -19,7 +19,7 @@ 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) +let client = new Client(location.origin) let lastView = undefined let lastPageUrlBeforeLogin = undefined From b0ba327f7f7283ce74279b3fc0ebb7992d958dad Mon Sep 17 00:00:00 2001 From: AdrienLSH Date: Mon, 29 Jan 2024 10:19:55 +0100 Subject: [PATCH 05/77] ; --- frontend/static/js/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index e2a7cfe..d94db7d 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -19,10 +19,10 @@ 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.origin) +let client = new Client(location.origin); -let lastView = undefined -let lastPageUrlBeforeLogin = undefined +let lastView = undefined; +let lastPageUrlBeforeLogin = undefined; const pathToRegex = path => new RegExp("^" + path.replace(/\//g, "\\/").replace(/:\w+/g, "(.+)") + "$"); From 8ee2beb432c61a09391a8e0893e7fe6e22321bfa Mon Sep 17 00:00:00 2001 From: AdrienLSH Date: Mon, 29 Jan 2024 10:50:18 +0100 Subject: [PATCH 06/77] lang: improved, Home view translation --- frontend/static/js/api/LanguageManager.js | 47 ++++++++++++++++------- frontend/static/js/index.js | 15 ++++++-- frontend/static/js/lang/en.json | 7 +++- frontend/static/js/lang/fr.json | 7 +++- frontend/static/js/views/HomeView.js | 11 +++--- 5 files changed, 64 insertions(+), 23 deletions(-) diff --git a/frontend/static/js/api/LanguageManager.js b/frontend/static/js/api/LanguageManager.js index 3e1c9b3..2c9ebb5 100644 --- a/frontend/static/js/api/LanguageManager.js +++ b/frontend/static/js/api/LanguageManager.js @@ -1,11 +1,17 @@ +import { reloadView } from '../index.js' + export default class LanguageManager { constructor() { this.availableLanguages = ['en', 'fr']; + this.dict = null; this.currentLang = 'en' this.chosenLang = localStorage.getItem('preferedLanguage') || this.currentLang; if (this.chosenLang !== this.currentLang && this.availableLanguages.includes(this.chosenLang)) { this.translatePage(); + this.currentLang = this.chosenLang; + } else { + this.loadDict(this.chosenLang); } } @@ -13,33 +19,48 @@ export default class LanguageManager { if (this.currentLang === this.chosenLang) return; - let dictUrl = `${location.origin}/static/js/lang/${this.chosenLang}.json`; - let translation = await fetch(dictUrl).then(response => { - if (response.status !== 200) - return null; - return response.json(); - }); - if (!translation) { - console.log(`No translation found for language ${this.chosenLang}`); + await this.loadDict(this.chosenLang); + if (!this.dict) return 1; - } + document.querySelectorAll('[data-i18n]').forEach(el => { let key = el.getAttribute('data-i18n'); - el.innerHTML = translation[key]; + el.innerHTML = this.dict[key]; }) + await reloadView(); - this.currentLang = this.chosenLang; return 0; } async changeLanguage(lang) { if (lang === this.currentLang || !this.availableLanguages.includes(lang)) - return; + return 1; this.chosenLang = lang; if (await this.translatePage() !== 0) - return; + return 1; + this.currentLang = this.chosenLang; localStorage.setItem('preferedLanguage', lang); + return 0; + } + + async loadDict(lang) { + let dictUrl = `${location.origin}/static/js/lang/${lang}.json`; + let response = await fetch(dictUrl); + + if (response.status !== 200) { + console.log(`No translation found for language ${lang}`); + return; + } + + this.dict = await response.json(); + } + + get(key, defaultTxt) { + if (!this.dict) + return defaultTxt; + + return this.dict[key] || defaultTxt; } } diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index d94db7d..02b1591 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -48,9 +48,11 @@ const navigateTo = async (uri) => { } }; +const reloadView = async _ => renderView(lastView); + async function renderView(view) { - let content = await view.getHtml(); + let content = await view?.getHtml(); if (content == null) return 1; @@ -140,12 +142,19 @@ document.addEventListener("DOMContentLoaded", async () => { //Languages Array.from(document.getElementById('languageSelector').children).forEach(el => { - el.onclick = _ => client.lang.changeLanguage(el.value); + el.onclick = async _ => { + if (await client.lang.changeLanguage(el.value)) + return; + document.querySelector('#languageSelector > .active')?.classList.remove('active'); + el.classList.add('active'); + }; }); + document.querySelector(`#languageSelector > [value=${client.lang.chosenLang}]`) + ?.classList.add('active'); await client.isAuthentificate(); router(location.pathname); document.querySelector('a[href=\'' + location.pathname + '\']')?.classList.add('active'); }); -export { client, navigateTo } +export { client, navigateTo, reloadView } diff --git a/frontend/static/js/lang/en.json b/frontend/static/js/lang/en.json index 3d9b14e..c558d5d 100644 --- a/frontend/static/js/lang/en.json +++ b/frontend/static/js/lang/en.json @@ -5,5 +5,10 @@ "navbarRegister": "Register", "navbarProfile": "My Profile", "navbarSettings": "Settings", - "navbarLogout": "Logout" + "navbarLogout": "Logout", + "homeTitle": "Home", + "homeOnline": "Play online", + "homeOffline": "Play offline", + "homeMe": "Me", + "homeLogout": "Logout" } diff --git a/frontend/static/js/lang/fr.json b/frontend/static/js/lang/fr.json index 3307204..76228c5 100644 --- a/frontend/static/js/lang/fr.json +++ b/frontend/static/js/lang/fr.json @@ -5,5 +5,10 @@ "navbarRegister": "S'inscrire", "navbarProfile": "Mon Profil", "navbarSettings": "Paramètres", - "navbarLogout": "Se déconnecter" + "navbarLogout": "Se déconnecter", + "homeTitle": "Maison", + "homeOnline": "Jouer en ligne", + "homeOffline": "Jouer hors ligne", + "homeMe": "Moi", + "homeLogout": "Se déconnecter" } diff --git a/frontend/static/js/views/HomeView.js b/frontend/static/js/views/HomeView.js index fb13511..1266d36 100644 --- a/frontend/static/js/views/HomeView.js +++ b/frontend/static/js/views/HomeView.js @@ -1,3 +1,4 @@ +import { client } from "../index.js"; import AbstractAuthentificateView from "./abstracts/AbstractAuthentifiedView.js"; export default class extends AbstractAuthentificateView { @@ -8,11 +9,11 @@ export default class extends AbstractAuthentificateView { async getHtml() { return /* HTML */ ` -

HOME

- Play a game - Play offline - Me - Logout +

${client.lang.get('homeTitle', 'Home')}

+ ${client.lang.get('homeOnline', 'Play online')} + ${client.lang.get('homeOffline', 'Play offline')} + ${client.lang.get('homeMe', 'Me')} + ${client.lang.get('homeLogout', 'Logout')} `; } } From ab5c3deec1e348431c3ba56c67d7e47dc58d038b Mon Sep 17 00:00:00 2001 From: AdrienLSH Date: Mon, 29 Jan 2024 12:59:49 +0100 Subject: [PATCH 07/77] lang: window title translation --- frontend/static/js/lang/en.json | 1 + frontend/static/js/lang/fr.json | 7 ++++--- frontend/static/js/views/HomeView.js | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/frontend/static/js/lang/en.json b/frontend/static/js/lang/en.json index c558d5d..8fc8946 100644 --- a/frontend/static/js/lang/en.json +++ b/frontend/static/js/lang/en.json @@ -6,6 +6,7 @@ "navbarProfile": "My Profile", "navbarSettings": "Settings", "navbarLogout": "Logout", + "homeWindowTitle": "Home", "homeTitle": "Home", "homeOnline": "Play online", "homeOffline": "Play offline", diff --git a/frontend/static/js/lang/fr.json b/frontend/static/js/lang/fr.json index 76228c5..a879558 100644 --- a/frontend/static/js/lang/fr.json +++ b/frontend/static/js/lang/fr.json @@ -1,14 +1,15 @@ { "navbarSearch": "Recherche", "navbarHome": "Maison", - "navbarLogin": "Se connecter", + "navbarLogin": "Connexion", "navbarRegister": "S'inscrire", "navbarProfile": "Mon Profil", "navbarSettings": "Paramètres", - "navbarLogout": "Se déconnecter", + "navbarLogout": "Déconnexion", + "homeWindowTitle": "Maison", "homeTitle": "Maison", "homeOnline": "Jouer en ligne", "homeOffline": "Jouer hors ligne", "homeMe": "Moi", - "homeLogout": "Se déconnecter" + "homeLogout": "Déconnexion" } diff --git a/frontend/static/js/views/HomeView.js b/frontend/static/js/views/HomeView.js index 1266d36..a98fd9f 100644 --- a/frontend/static/js/views/HomeView.js +++ b/frontend/static/js/views/HomeView.js @@ -3,7 +3,7 @@ import AbstractAuthentificateView from "./abstracts/AbstractAuthentifiedView.js" export default class extends AbstractAuthentificateView { constructor(params) { - super(params, "Home"); + super(params, client.lang.get('homeWindowTitle', 'Home')); this.redirect_url = "/login" } From 2f5cfc0e57015dd0dfe3bda50838adee424d1717 Mon Sep 17 00:00:00 2001 From: AdrienLSH Date: Wed, 31 Jan 2024 12:53:19 +0100 Subject: [PATCH 08/77] accounts: 401 Response in case of invalid login --- accounts/views/login.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/accounts/views/login.py b/accounts/views/login.py index fc02e4a..afcc03d 100644 --- a/accounts/views/login.py +++ b/accounts/views/login.py @@ -18,6 +18,6 @@ class LoginView(APIView): serializer.is_valid(raise_exception=True) user = serializer.get_user(data) if user is None: - return Response({'login': ['Invalid username or password.']}, status.HTTP_400_BAD_REQUEST) + return Response({'login': ['Invalid username or password.']}, status.HTTP_401_UNAUTHORIZED) login(request, user) return Response({'id': user.pk}, status=status.HTTP_200_OK) From d4575c92dbbae76dc8d26c2eaaa8438e4036d1f1 Mon Sep 17 00:00:00 2001 From: AdrienLSH Date: Wed, 31 Jan 2024 13:54:36 +0100 Subject: [PATCH 09/77] lang: translation of django's response --- frontend/static/js/api/client.js | 3 ++- transcendence/settings.py | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/frontend/static/js/api/client.js b/frontend/static/js/api/client.js index 3d0a900..1e89882 100644 --- a/frontend/static/js/api/client.js +++ b/frontend/static/js/api/client.js @@ -114,7 +114,8 @@ class Client headers: { "Content-Type": "application/json", "X-CSRFToken": getCookie("csrftoken"), - }, + 'Accept-Language': this.lang.currentLang, + }, body: JSON.stringify(data), }); return response; diff --git a/transcendence/settings.py b/transcendence/settings.py index b75cedb..f6193a8 100644 --- a/transcendence/settings.py +++ b/transcendence/settings.py @@ -13,6 +13,7 @@ https://docs.djangoproject.com/en/4.2/ref/settings/ import os from pathlib import Path +from django.utils.translation import gettext_lazy as _ # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -79,6 +80,7 @@ MIDDLEWARE = [ 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'django.middleware.locale.LocaleMiddleware', ] ROOT_URLCONF = 'transcendence.urls' @@ -137,6 +139,11 @@ AUTH_PASSWORD_VALIDATORS = [ LANGUAGE_CODE = 'en-us' +LANGUAGES = [ + ('en', _('English')), + ('fr', _('French')), +] + TIME_ZONE = 'UTC' USE_I18N = True From 8c7c5a363791007fabc3661b0c368f67e15e140a Mon Sep 17 00:00:00 2001 From: Xamora Date: Thu, 1 Feb 2024 12:57:32 +0100 Subject: [PATCH 10/77] =?UTF-8?q?Sous=20une=20nuit=20=C3=A9toil=C3=A9,=20d?= =?UTF-8?q?ans=20la=20capitale=20o=C3=B9=20les=20habitants=20font=20gr?= =?UTF-8?q?=C3=A8ves=20en=20boucle,=20un=20jeune=20homme=20du=20nom=20d'Ad?= =?UTF-8?q?rien,=20plein=20d'espoir=20esp=C3=A9rant=20une=20douce=20nuit?= =?UTF-8?q?=20aupr=C3=A8s=20de=20sa=20belle=20et=20tendre=20'Princesse'.?= =?UTF-8?q?=20Acheta=20des=20capotes=20premier=20prix=20dans=20la=20pharma?= =?UTF-8?q?cie=20non=20loin=20de=20la=20gare.=20La=20suite=20au=20prochain?= =?UTF-8?q?=20commit...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chat/consumersNotice.py | 21 ++++++++++++++------- frontend/static/css/index.css | 9 --------- frontend/static/css/me.css | 12 +++--------- frontend/static/css/profile.css | 5 ++++- frontend/static/css/search.css | 4 ++++ frontend/static/js/api/chat/notice.js | 20 +++++++++++--------- frontend/static/js/api/client.js | 1 + frontend/static/js/api/profile.js | 8 ++++++-- frontend/static/js/api/profiles.js | 8 ++++++++ frontend/static/js/views/Search.js | 2 +- frontend/templates/index.html | 1 + profiles/urls.py | 5 +++-- profiles/views.py | 2 ++ profiles/viewsets.py | 9 +++++++++ 14 files changed, 67 insertions(+), 40 deletions(-) diff --git a/chat/consumersNotice.py b/chat/consumersNotice.py index c0f2723..f387ff0 100644 --- a/chat/consumersNotice.py +++ b/chat/consumersNotice.py @@ -288,12 +288,15 @@ class ChatNoticeConsumer(WebsocketConsumer): asked = AskFriendModel().getAsked(user.pk) asker = AskFriendModel().getAsker(user.pk) + online_friends = self.get_online_friend(user) + self.send(text_data=json.dumps({ 'type':event['type'], 'author_id':event['author_id'], 'targets':event['targets'], 'asker': asker, 'asked': asked, + 'online': online_friends, 'time': event['time'], 'status':event['status'], })) @@ -351,23 +354,27 @@ class ChatNoticeConsumer(WebsocketConsumer): self.send_ask_friend(event) - def online_users(self, event): - - user = self.scope["user"] - if (user.is_anonymous or not user.is_authenticated): - return - + def get_online_friend(self, user): online_friends = {} for friend in FriendModel().getFriends(user.pk): if (friend in self.channel_layer.users_channels): online_friends[friend] = "green" else: online_friends[friend] = "red" + return online_friends + + def online_users(self, event): + + user = self.scope["user"] + if (user.is_anonymous or not user.is_authenticated): + return + + online_friends = self.get_online_friend(user) self.send(text_data=json.dumps({ 'type':event['type'], 'author_id':event['author_id'], - 'content':online_friends, + 'online':online_friends, 'time': event['time'], 'status':event['status'], })) diff --git a/frontend/static/css/index.css b/frontend/static/css/index.css index 7e35fb0..9b843e5 100644 --- a/frontend/static/css/index.css +++ b/frontend/static/css/index.css @@ -1,12 +1,3 @@ -*{ - color: #cccccc; - font-size: 35px; - background-color: #1a1a1a; -} - -body { -} - #app #avatar { max-height: 10em; max-width: 10em; diff --git a/frontend/static/css/me.css b/frontend/static/css/me.css index 1d731ee..6879d41 100644 --- a/frontend/static/css/me.css +++ b/frontend/static/css/me.css @@ -1,17 +1,11 @@ -#app #main .account -{ - color: #1a1a1a; +#app * { + font-size: 30px; } + #app #main { width: 60%; display: flex; flex-direction: column; - color: #1a1a1a; -} - -#app #main .profile -{ - color: #1a1a1a; } diff --git a/frontend/static/css/profile.css b/frontend/static/css/profile.css index 21fb0dc..a1ead7d 100644 --- a/frontend/static/css/profile.css +++ b/frontend/static/css/profile.css @@ -1,3 +1,7 @@ +#app * { + font-size: 30px; +} + #app #username { font-size: 0.8em; @@ -11,7 +15,6 @@ #app { margin-top: 1em; - background-color: red; } #app #yes, #app #no { diff --git a/frontend/static/css/search.css b/frontend/static/css/search.css index 6aa1061..27b9734 100644 --- a/frontend/static/css/search.css +++ b/frontend/static/css/search.css @@ -1,3 +1,7 @@ +#app * { + font-size: 40px; +} + #app img { diff --git a/frontend/static/js/api/chat/notice.js b/frontend/static/js/api/chat/notice.js index 4616179..f882835 100644 --- a/frontend/static/js/api/chat/notice.js +++ b/frontend/static/js/api/chat/notice.js @@ -80,7 +80,7 @@ class Notice { this.rewrite_invite(); } else { - let sender = await this.client.profiles.getProfile(send.author_id); + let sender = await this.client.profiles.getProfileId(send.author_id); create_popup(sender.username + " refuse your invitation"); } @@ -123,7 +123,7 @@ class Notice { return; this.data["invited"] = content; - let sender = await this.client.profiles.getProfile(send.author_id); + let sender = await this.client.profiles.getProfileId(send.author_id); create_popup("Invitation received by " + sender.username); @@ -145,7 +145,7 @@ class Notice { } async receive_online_users(send) { - let content = send.content; + let content = send.online; if (content !== undefined) { if (this.data["online"].length > 0) { @@ -187,7 +187,7 @@ class Notice { if (send.status == 400) create_popup("Friend ask error"); else if (send.status == 409) - create_popup("Already asked friend or already friend"); + create_popup("Already asked friend"); } //if (!send.asked.includes(send.author_id) || @@ -202,7 +202,7 @@ class Notice { this.data["asker"] = send.asker; if (send.author_id != my_id) { - let sender = await this.client.profiles.getProfile(send.author_id); + let sender = await this.client.profiles.getProfileId(send.author_id); if (this.data["asker"].includes(send.author_id)) create_popup(sender.username + " ask you as friend"); if (this.rewrite_profile !== undefined) @@ -220,8 +220,6 @@ class Notice { } async receive_remove_friend(send) { - this.data["asked"] = send.asked; - this.data["asker"] = send.asker; if (send.author_id == this.client.me.id) { if (send.status == 400) @@ -233,6 +231,8 @@ class Notice { if (this.rewrite_profile !== undefined) await this.rewrite_profile(); + + this.receive_online_users(send); } async accept_friend(user_id) { @@ -246,7 +246,7 @@ class Notice { async receive_accept_friend(send) { this.data["asked"] = send.asked; this.data["asker"] = send.asker; - let sender = await this.client.profiles.getProfile(send.author_id); + let sender = await this.client.profiles.getProfileId(send.author_id); if (send.author_id == this.client.me.id) { if (send.status == 400) @@ -262,6 +262,8 @@ class Notice { if (this.rewrite_profile !== undefined) await this.rewrite_profile(); + + this.receive_online_users(send); } async refuse_friend(user_id) { @@ -275,7 +277,7 @@ class Notice { async receive_refuse_friend(send) { this.data["asked"] = send.asked; this.data["asker"] = send.asker; - let sender = await this.client.profiles.getProfile(send.author_id); + let sender = await this.client.profiles.getProfileId(send.author_id); if (send.author_id == this.client.me.id) { if (send.status == 400) diff --git a/frontend/static/js/api/client.js b/frontend/static/js/api/client.js index 7978053..57d3ce4 100644 --- a/frontend/static/js/api/client.js +++ b/frontend/static/js/api/client.js @@ -73,6 +73,7 @@ class Client this.notice = new Notice(this); this.lang = new LanguageManager; + } /** diff --git a/frontend/static/js/api/profile.js b/frontend/static/js/api/profile.js index c2601c4..d15d0a4 100644 --- a/frontend/static/js/api/profile.js +++ b/frontend/static/js/api/profile.js @@ -5,7 +5,7 @@ class Profile /** * @param {Client} client */ - constructor (client, username, id = undefined, avatar_url = undefined) + constructor (client, username=undefined, id=undefined, avatar_url=undefined) { /** * @type {Client} client @@ -36,7 +36,11 @@ class Profile async init() { - let response = await this.client._get(`/api/profiles/${this.username}`); + let response; + if (this.username !== undefined) + response = await this.client._get(`/api/profiles/${this.username}`); + else + response = await this.client._get(`/api/profiles/id/${this.id}`); if (response.status !== 200) return response.status; diff --git a/frontend/static/js/api/profiles.js b/frontend/static/js/api/profiles.js index 4200ff5..070769c 100644 --- a/frontend/static/js/api/profiles.js +++ b/frontend/static/js/api/profiles.js @@ -42,6 +42,14 @@ class Profiles return profile; } + async getProfileId(id) + { + let profile = new Profile(this.client, undefined, id); + if (await profile.init()) + return null; + return profile; + } + /** * Block a user * @param {Number} user_id diff --git a/frontend/static/js/views/Search.js b/frontend/static/js/views/Search.js index f388707..76358f8 100644 --- a/frontend/static/js/views/Search.js +++ b/frontend/static/js/views/Search.js @@ -68,7 +68,7 @@ export default class extends AbstractView { username.setAttribute('data-link', ''); username.id = `username${user.id}` username.href = `/profiles/${user.username}`; - if (user.id == client.me.id) + if (logged && user.id == client.me.id) username.style.color = "green"; else { let online = client.notice.data["online"][user.id]; diff --git a/frontend/templates/index.html b/frontend/templates/index.html index 499c006..585f672 100644 --- a/frontend/templates/index.html +++ b/frontend/templates/index.html @@ -6,6 +6,7 @@ Bozo Pong +