diff --git a/api/utils.py b/api/utils.py new file mode 100644 index 0000000..097424c --- /dev/null +++ b/api/utils.py @@ -0,0 +1,42 @@ +import json +import textwrap +from django.utils.html import strip_tags +from django.utils.translation import ugettext as _ + +from fcm_django.models import FCMDevice +from fcm_django.fcm import fcm_send_message + +from chat.serializers import ChatSerializer + +def sendChatPushNotification(user, message): + device = FCMDevice.objects.filter(user = user, active = True).first() + + if not device is None: + serializer = ChatSerializer(message) + + json_r = json.dumps(serializer.data) + json_r = json.loads(json_r) + + info = {} + + info["data"] = {} + info["data"]["messages"] = [] + info["data"]["message_sent"] = json_r + + info["message"] = "" + info["type"] = "" + info["title"] = "" + info["success"] = True + info["number"] = 1 + info['extra'] = 0 + + response = json.dumps(info) + + title = str(user).join(_(" sent a message")) + + simple_notify = textwrap.shorten(strip_tags(message.text), width = 30, placeholder = "...") + + if message.image: + simple_notify += " ".join(_("[Photo]")) + + device.send_message(title = title, body = simple_notify, data = {"response": response}) \ No newline at end of file diff --git a/api/views.py b/api/views.py index ecf65a5..2ee8c70 100644 --- a/api/views.py +++ b/api/views.py @@ -35,6 +35,8 @@ from django.http import HttpResponse from fcm_django.models import FCMDevice +from .utils import sendChatPushNotification + @csrf_exempt def getToken(request): oauth = Application.objects.filter(name = "amadeus-droid") @@ -349,6 +351,8 @@ class ChatViewset(viewsets.ModelViewSet): info["message"] = _("Message sent successfully!") info["success"] = True info["number"] = 1 + + sendChatPushNotification(user_to, message) else: info["message"] = _("Error while sending message!") info["success"] = False diff --git a/bulletin/utils.py b/bulletin/utils.py index 1ea0b5c..0477151 100644 --- a/bulletin/utils.py +++ b/bulletin/utils.py @@ -11,14 +11,11 @@ from django.utils.translation import ugettext_lazy as _ from channels import Group +from api.utils import sendChatPushNotification from chat.models import Conversation, TalkMessages, ChatVisualizations from users.models import User - - - - def brodcast_dificulties(request, message, subject): msg = TalkMessages() msg.text = message @@ -54,4 +51,6 @@ def brodcast_dificulties(request, message, subject): Group("user-%s" % p.id).send({'text': notification}) + sendChatPushNotification(p, msg) + ChatVisualizations.objects.create(viewed = False, message = msg, user = p) diff --git a/chat/views.py b/chat/views.py index 378a091..3fc39bb 100644 --- a/chat/views.py +++ b/chat/views.py @@ -24,15 +24,13 @@ from log.models import Log from log.mixins import LogMixin import time -from fcm_django.models import FCMDevice -from fcm_django.fcm import fcm_send_message - from categories.models import Category from subjects.models import Subject from users.models import User +from api.utils import sendChatPushNotification + from .models import Conversation, TalkMessages, ChatVisualizations, ChatFavorites -from .serializers import ChatSerializer from .forms import ChatMessageForm class GeneralIndex(LoginRequiredMixin, LogMixin, generic.ListView): @@ -373,44 +371,7 @@ class SendMessage(LoginRequiredMixin, LogMixin, generic.edit.CreateView): Group("user-%s" % user.id).send({'text': notification}) - device = FCMDevice.objects.filter(user = user, active = True).first() - - if not device is None: - data = { - "user_icon": self.object.user.image_url, - "notify_title": str(self.object.user), - "text_notify": simple_notify, - "image_url": "", - "message": strip_tags(self.object.text), - "datetime": str(self.object.create_date) - } - - if self.object.image: - data['image_url'] = self.object.image.url - - data = ChatSerializer(self.object) - - json_r = json.dumps(data.data) - json_r = json.loads(json_r) - - info = {} - - info["data"] = {} - info["data"]["messages"] = [] - info["data"]["message_sent"] = json_r - - info["message"] = "" - info["type"] = "" - info["title"] = "" - info["success"] = True - info["number"] = 1 - info['extra'] = 0 - - response = json.dumps(info) - - title = str(self.object.user) - - device.send_message(title = title, body = simple_notify, data = {"response": response}) + sendChatPushNotification(user, self.object) ChatVisualizations.objects.create(viewed = False, message = self.object, user = user) diff --git a/goals/utils.py b/goals/utils.py index 3f6344f..bc8e91a 100644 --- a/goals/utils.py +++ b/goals/utils.py @@ -10,6 +10,8 @@ from django.utils.translation import ugettext_lazy as _ from channels import Group +from api.utils import sendChatPushNotification + from chat.models import Conversation, TalkMessages, ChatVisualizations from users.models import User @@ -63,5 +65,7 @@ def brodcast_dificulties(request, message, subject): Group("user-%s" % p.id).send({'text': notification}) + sendChatPushNotification(p, msg) + ChatVisualizations.objects.create(viewed = False, message = msg, user = p) -- libgit2 0.21.2