diff --git a/chat/consumers.py b/chat/consumers.py index d1c33d3..9f07678 100644 --- a/chat/consumers.py +++ b/chat/consumers.py @@ -2,6 +2,7 @@ import json from channels.generic.websocket import WebsocketConsumer from asgiref.sync import async_to_sync from .models import MemberModel, MessageModel +from profiles.models import BlockModel import time class ChatConsumer(WebsocketConsumer): @@ -34,6 +35,9 @@ class ChatConsumer(WebsocketConsumer): text_data_json = json.loads(text_data) message = text_data_json['message'] + receivers_id = text_data_json['receivers_id'] + + print(text_data) channel_id : int = int(self.scope['path'].split('/')[3]) user = self.scope["user"] @@ -46,9 +50,14 @@ class ChatConsumer(WebsocketConsumer): if (self.channel_layer == None): return - print(message) - message_time : int = int(time.time() * 1000) + + if (len(receivers_id) == 1 and + BlockModel.objects.filter(blocker=user.pk, blocked=receivers_id[0]) or + BlockModel.objects.filter(blocker=receivers_id[0], blocked=user.pk) + ): + return + async_to_sync(self.channel_layer.group_send)( self.room_group_name, { diff --git a/frontend/static/js/api/chat/channel.js b/frontend/static/js/api/chat/channel.js index 13c3db6..e1b7870 100644 --- a/frontend/static/js/api/chat/channel.js +++ b/frontend/static/js/api/chat/channel.js @@ -54,13 +54,14 @@ class Channel { return new_messages; } - async sendMessageChannel(message) { + async sendMessageChannel(message, receivers_id) { if (this.chatSocket == undefined) return; this.chatSocket.send(JSON.stringify({ - 'message':message + 'message':message, + 'receivers_id':receivers_id, })); } diff --git a/frontend/static/js/api/client.js b/frontend/static/js/api/client.js index ec3d54b..630225e 100644 --- a/frontend/static/js/api/client.js +++ b/frontend/static/js/api/client.js @@ -35,10 +35,11 @@ class Client return this.logged; } - async _get(uri) + async _get(uri, data) { let response = await fetch(this._url + uri, { method: "GET", + body: JSON.stringify(data), }); return response; } diff --git a/frontend/static/js/api/profile.js b/frontend/static/js/api/profile.js index 1ec7987..1e94980 100644 --- a/frontend/static/js/api/profile.js +++ b/frontend/static/js/api/profile.js @@ -12,8 +12,9 @@ class Profile */ this.client = client; this.username = username; - this.avatar_url = avatar_url - this.user_id = user_id + this.avatar_url = avatar_url; + this.user_id = user_id; + this.isBlocked = false; } async init(user_id) @@ -24,7 +25,21 @@ class Profile this.user_id = response_data.user_id; this.username = response_data.username; this.avatar_url = response_data.avatar_url; + + let block_response = await this.client._get("/api/profiles/block"); + + if (block_response.status == 404) + return + + let block_data = await block_response.json(); + let block_list = JSON.parse(block_data); + block_list.forEach(block => { + let blocker = block.fields.blocker; + let blocked = block.fields.blocked; + if (blocker == this.client.me.user_id && blocked == user_id) + return this.isBlocked = true; + }); } } -export {Profile} \ No newline at end of file +export {Profile} diff --git a/frontend/static/js/api/profiles.js b/frontend/static/js/api/profiles.js index b12691f..19b7f3d 100644 --- a/frontend/static/js/api/profiles.js +++ b/frontend/static/js/api/profiles.js @@ -35,13 +35,30 @@ class Profiles async block(user_id) { // blocker & blocked - let response = await this.client._post("/api/block/", - [this.client.me.user_id, user_id], - ); + let response = await this.client._post("/api/profiles/block", { + users_id:[this.client.me.user_id, user_id], + }); let data = await response.json(); + console.log(response.status); + console.log(data); + return data; } + + async deblock(user_id) { + + // blocker & blocked + let response = await this.client._delete("/api/profiles/block", { + users_id:[this.client.me.user_id, user_id], + }); + + let data = await response.json(); + console.log(response.status); + console.log(data); + return data; + + } } export {Profiles} diff --git a/frontend/static/js/views/ProfilePageView.js b/frontend/static/js/views/ProfilePageView.js index 90c3d4e..82bd1f2 100644 --- a/frontend/static/js/views/ProfilePageView.js +++ b/frontend/static/js/views/ProfilePageView.js @@ -9,34 +9,48 @@ export default class extends AbstractView { async postInit() { - let profile = await client.profiles.getProfile(this.user_id); - let info = document.getElementById("info"); + this.profile = await client.profiles.getProfile(this.user_id); + this.info = document.getElementById("info"); // Username let username = document.createElement("a"); username.id = "username"; - username.appendChild(document.createTextNode(profile.username)); - info.appendChild(username); + username.appendChild(document.createTextNode(this.profile.username)); + this.info.appendChild(username); - info.appendChild(document.createElement("br")); + this.info.appendChild(document.createElement("br")); // Avatar let avatar = document.createElement("img"); avatar.id = "avatar"; - avatar.src = profile.avatar_url; - info.appendChild(avatar); - - // Block option - let block = document.createElement("a"); - block.id = "block"; - block.addEventListener("click", async () => { - if (client.me.user_id != user.user_id) { - } - }); - block.appendChild(document.createTextNode("Block")); - info.appendChild(block); + avatar.src = this.profile.avatar_url; + this.info.appendChild(avatar); + + await this.blockButton(); } + async blockButton() { + // Block option + if (client.me.user_id != this.user_id) { + let block = document.getElementById("block") || document.createElement("a"); + block.id = "block"; + block.innerText = ""; + block.onclick = async () => { + if (!this.profile.isBlocked) + await client.profiles.block(this.user_id); + else + await client.profiles.deblock(this.user_id); + this.profile = await client.profiles.getProfile(this.user_id); + this.blockButton(); + }; + if (this.profile.isBlocked) + block.appendChild(document.createTextNode("Deblock")); + else + block.appendChild(document.createTextNode("Block")); + this.info.appendChild(block); + } + } + async getHtml() { return ` diff --git a/frontend/static/js/views/Search.js b/frontend/static/js/views/Search.js index 924ad56..0cbd0ac 100644 --- a/frontend/static/js/views/Search.js +++ b/frontend/static/js/views/Search.js @@ -100,6 +100,7 @@ export default class extends AbstractView { async chat() { + let users = await client.profiles.all(); let logged = await client.isAuthentificate(); /*let reload = document.getElementById("messages"); if (reload != null) @@ -136,9 +137,6 @@ export default class extends AbstractView { let i = 0; client.channel.messages.forEach((message) => { if (messages.children[i] == null || message.content != messages.children[i].innerText) { - console.log(message.content); - if (messages.children[i] != null) - console.log(messages.children[i].innerText); let text = document.createElement("p"); text.appendChild(document.createTextNode(message.content)); if (message.author_id == client.me.user_id) @@ -161,21 +159,25 @@ export default class extends AbstractView { chat_input.maxLength=255; chat.appendChild(chat_input); - chat_input.addEventListener("keydown", async () => { + chat_input.onkeydown = async () => { if (event.keyCode == 13 && client.channel != undefined) { //let chat_input = document.getElementById("input_chat"); let chat_text = chat_input.value; - await client.channel.sendMessageChannel(chat_text) + let receivers_id = []; + client.channel.members_id.forEach((member_id) => { + if (member_id != client.me.user_id) + receivers_id.push(users.filter(user => user.user_id == member_id)[0].user_id); + }); + await client.channel.sendMessageChannel(chat_text, receivers_id) // Reset chat_input.value = ""; } - }); + }; } // nom des membres du chat - let users = await client.profiles.all(); let members = document.createElement("h2"); members.id = "members"; let usernames = ""; diff --git a/profiles/admin.py b/profiles/admin.py index 124b2a0..d79bf0b 100644 --- a/profiles/admin.py +++ b/profiles/admin.py @@ -1,6 +1,7 @@ from django.contrib import admin -from .models import ProfileModel +from .models import ProfileModel, BlockModel # Register your models here. -admin.site.register(ProfileModel) \ No newline at end of file +admin.site.register(ProfileModel) +admin.site.register(BlockModel) diff --git a/profiles/urls.py b/profiles/urls.py index 0218f09..8fe7c4f 100644 --- a/profiles/urls.py +++ b/profiles/urls.py @@ -9,5 +9,7 @@ urlpatterns = [ path("me", viewsets.MyProfileViewSet.as_view({'patch': 'partial_update', 'get': 'retrieve'}), name="my_profile_page"), path("", viewsets.ProfileViewSet.as_view({'get': 'retrieve'}), name="profile_page"), path("", viewsets.ProfileViewSet.as_view({'get': 'list'}), name="profiles_list"), - path("block", views.BlockView.as_view(), name="block_page"), + path("block", views.BlocksView.as_view(), name="block_page"), + path("block/", views.BlockView.as_view(), name="block_page"), + ] + static("/static/avatars/", document_root="./avatars") diff --git a/profiles/views.py b/profiles/views.py index aeb17f6..06b5f0c 100644 --- a/profiles/views.py +++ b/profiles/views.py @@ -2,17 +2,65 @@ from rest_framework.views import APIView from rest_framework.response import Response 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 class BlockView(APIView): permission_classes = (permissions.IsAuthenticated,) authentication_classes = (SessionAuthentication,) - - def post(self, request, pk): - pass def get(self, request, pk): - pass + block = BlockModel.objects.filter(pk=pk) + if (block): + return Response(serializers.serialize("json", block), status=status.HTTP_200_OK) + else: + return Response("Not Found", status=status.HTTP_404_NOT_FOUND) - def delete(self, request, pk): - pass + +class BlocksView(APIView): + permission_classes = (permissions.IsAuthenticated,) + authentication_classes = (SessionAuthentication,) + + 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) + else: + return Response({}, status=status.HTTP_404_NOT_FOUND) + + def post(self, request): + data: dict = request.data + users_id = request.data.get("users_id", None) + + if (users_id == None): + return Response({"Error"}, 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) + + new_block = BlockModel() + new_block.blocker = users_id[0] + new_block.blocked = users_id[1] + new_block.save() + + return Response({"block_id": new_block.pk}, status=status.HTTP_201_CREATED) + + def delete(self, request): + data: dict = request.data + users_id = request.data.get("users_id", None) + + if (users_id == None): + return Response({"Error"}, status=status.HTTP_400_BAD_REQUEST) + + 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) + + if (not block): + return Response("Don't exist", status=status.HTTP_404_NOT_FOUND) + + block.delete() + return Response("Deleted", status=status.HTTP_200_OK)