From 3b15847ce6a629b635f7032f0f3a6f9461b0e833 Mon Sep 17 00:00:00 2001 From: Holger Trampe Date: Mon, 6 Jul 2020 19:44:52 +0200 Subject: [PATCH] Benachrichtigungen Chat PUSH fertig --- chat/models.py | 7 +-- chat/templates/chat/chat_content.html | 8 ++-- .../templates/chat/chat_content_basechat.html | 3 +- users/signals.py | 35 +++++++++++++- users/templates/users/base.html | 46 ++++++++++++++++--- 5 files changed, 79 insertions(+), 20 deletions(-) diff --git a/chat/models.py b/chat/models.py index 45b6f8a..3323845 100644 --- a/chat/models.py +++ b/chat/models.py @@ -6,22 +6,17 @@ from django.utils import timezone from django_cryptography.fields import encrypt ''' - MODEL ChatMessage - - ''' class ChatMessage(models.Model): author = models.ForeignKey(User, on_delete=models.CASCADE) content = encrypt(models.CharField(max_length=5000, blank=False, default="")) sendtime = models.DateTimeField(default=timezone.now, blank=True) room = models.ForeignKey("ChatRoom", on_delete=models.CASCADE) -''' +''' Model ChatRoom - ''' - class ChatRoom(models.Model): creator = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True) diff --git a/chat/templates/chat/chat_content.html b/chat/templates/chat/chat_content.html index a5457ea..16946fd 100644 --- a/chat/templates/chat/chat_content.html +++ b/chat/templates/chat/chat_content.html @@ -101,6 +101,7 @@ $(document).ready(function(){ chatwebsocket.onmessage = function(e) { + datainfo = e["data"].split("__"); @@ -114,7 +115,7 @@ $(document).ready(function(){ } } - if(datainfo[0] == "stoptyping") + if(datainfo[0] == "stoptyping") { $("#is_typing_name").html(" "); $("#typingspinner").hide(); @@ -133,11 +134,10 @@ $(document).ready(function(){ success: function( data ) { $("#chatmessages").append(data); - $('#chatcontentcomplete').scrollTop( $('#chatcontentcomplete').height()*200 ); + $('#chatcontentcomplete').scrollTop( $('#chatcontentcomplete').height()*200 ); } }); - } - console.log(datainfo) + } if(datainfo[0] == "delete_message"){ $("#usermessage_" + datainfo[1]).remove(); } diff --git a/chat/templates/chat/chat_content_basechat.html b/chat/templates/chat/chat_content_basechat.html index e62ecf7..ec89415 100644 --- a/chat/templates/chat/chat_content_basechat.html +++ b/chat/templates/chat/chat_content_basechat.html @@ -145,7 +145,6 @@ $(document).ready(function(){ chatwebsocket.onmessage = function(e) { - datainfo = e["data"].split("__"); if(datainfo[0] == "starttyping") { @@ -253,4 +252,4 @@ $(document).on('keypress',function(e) { } }); - \ No newline at end of file + \ No newline at end of file diff --git a/users/signals.py b/users/signals.py index 24c77b9..81d8312 100644 --- a/users/signals.py +++ b/users/signals.py @@ -31,6 +31,7 @@ from channels_presence.models import Room from channels_presence.models import Presence from channels_presence.signals import presence_changed from organizer.models import * +from chat.models import ChatMessage def loadingFreeDays(plz, year): # Getting land @@ -674,7 +675,39 @@ def save_newabsence(sender, instance, **kwargs): print("Absence-Object is no holiday...") post_save.connect(save_newabsence, sender=sender) - + +# Neue Chatnachricht wurde verschickt - Hier Reaktion NUR auf PUSH! +@receiver(signal=post_save, sender=ChatMessage) +def new_chat_message(sender, instance, **kwargs): + # GRUPPENCHAT + if instance.room.chatroomtype == 1: + + sended_users = [] + + for u in instance.room.chatmembers.all(): + if u != instance.author: + if u.usernotifications.chat_received_push and u not in sended_users: + channel_layer = channels.layers.get_channel_layer() + async_to_sync(channel_layer.group_send)("user_" + str(u.pk), {'type' : 'pushhandler', 'pushtext' : "pushnotification__Chat | Neue Nachricht im Gruppenchat " + instance.room.roomname + " von " + instance.author.first_name + " " + instance.author.last_name + ": " + instance.content}) + sended_users.append(u) + + for u in instance.room.chatmembers_admin.all(): + if u != instance.author: + if u.usernotifications.chat_received_push and u not in sended_users: + channel_layer = channels.layers.get_channel_layer() + async_to_sync(channel_layer.group_send)("user_" + str(u.pk), {'type' : 'pushhandler', 'pushtext' : "pushnotification__Chat | Neue Nachricht im Gruppenchat " + instance.room.roomname + " von " + instance.author.first_name + " " + instance.author.last_name + ": " + instance.content}) + sended_users.append(u) + + + elif instance.room.chatroomtype == 0: + + u = instance.room.chatmember_single + + if u.usernotifications.chat_received_push and u != instance.author: + channel_layer = channels.layers.get_channel_layer() + async_to_sync(channel_layer.group_send)("user_" + str(u.pk), {'type' : 'pushhandler', 'pushtext' : "pushnotification__Chat | Neue Nachricht von " + instance.author.first_name + " " + instance.author.last_name + ": " + instance.content}) + + @receiver(signal=pre_delete, sender=Absence) def delete_absence(sender, instance, **kwargs): diff --git a/users/templates/users/base.html b/users/templates/users/base.html index cbff605..0448ffb 100644 --- a/users/templates/users/base.html +++ b/users/templates/users/base.html @@ -615,9 +615,6 @@ $( window ).resize(function() { */ newunknownotificationscounter = 0; - - - function loadUnsendNotifications(){ $.ajax( { @@ -718,11 +715,46 @@ $(document).ready(function(){ if(e["data"] != "presence_update") { //HANDLER FOR ALL PUSHNOTIFICATIONS - if(e["data"].split("__")[0] == "pushnotification"){ + if(e["data"].split("__")[0] == "pushnotification"){ - var notify = new Notification('Digitale Agentur', { - body: e["data"].split("__")[1] - }); + /* + Check for Chat-Message in CHatview or invisible-Browser + */ + tempsplit = e["data"].split("__"); + finalsplit = tempsplit[1].split(" "); + + if(finalsplit[0] != "Chat"){ + var notify = new Notification('Digitale Agentur', { + body: e["data"].split("__")[1] + }); + } + else{ + {% if active_link == "chat" %} + chatopen= true; + {% else %} + chatopen= false; + {% endif %} + + windowvisible = false; + //Get Minimized-Window-Status to show up chat message, when window is in chat, but not focused by user + + if(!document.hasFocus()){ + var notify = new Notification('Digitale Agentur', { + body: e["data"].split("__")[1] + }); + } + else if(chatopen == false){ + if($("#dynamicchatwindow").is(":visible")){ + console.log("user in chat...") + } + else{ + var notify = new Notification('Digitale Agentur', { + body: e["data"].split("__")[1] + }); + } + } + } + } loadUnsendNotifications(); loadUnviewnNotifications();