From ec19cc591491191cf0d227a7fec41144c40a3820 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evren=20Esat=20=C3=96zkan?= Date: Wed, 27 Jul 2016 16:09:47 +0300 Subject: [PATCH] added _zops_get_notifications method rref #5367 rref #5366 ref zetaops/zengine#66 ref zetaops/zengine#65 --- zengine/messaging/model.py | 8 ++++- zengine/messaging/views.py | 62 ++++++++++++++++++++++++++++++++++++-- zengine/settings.py | 1 + 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/zengine/messaging/model.py b/zengine/messaging/model.py index 3efef9ca..73108fe0 100644 --- a/zengine/messaging/model.py +++ b/zengine/messaging/model.py @@ -223,13 +223,19 @@ def is_online(self): user=self.user).get().user.is_online() def unread_count(self): - # FIXME: track and return actual unread message count if self.last_seen_msg_time: return self.channel.message_set.objects.filter( timestamp__gt=self.last_seen_msg_time).count() else: return self.channel.message_set.objects.filter().count() + def get_unread_messages(self, amount): + if self.last_seen_msg_time: + return self.channel.message_set.objects.filter( + timestamp__gt=self.last_seen_msg_time)[:amount] + else: + return self.channel.message_set.objects.filter()[:amount] + def create_exchange(self): """ Creates user's private exchange diff --git a/zengine/messaging/views.py b/zengine/messaging/views.py index 8c7e3407..b8ffc255 100644 --- a/zengine/messaging/views.py +++ b/zengine/messaging/views.py @@ -8,7 +8,7 @@ # (GPLv3). See LICENSE.txt for details. from pyoko.conf import settings from pyoko.db.adapter.db_riak import BlockSave -from pyoko.exceptions import ObjectDoesNotExist +from pyoko.exceptions import ObjectDoesNotExist, MultipleObjectsReturned from pyoko.lib.utils import get_object_from_path from zengine.log import log from zengine.lib.exceptions import HTTPError @@ -236,13 +236,14 @@ def report_last_seen_message(current): } """ sbs = Subscriber(current).objects.filter(channel_id=current.input['channel_key'], - user_id=current.user_id)[0] - sbs.last_seen_msg_time=current.input['timestamp'] + user_id=current.user_id)[0] + sbs.last_seen_msg_time = current.input['timestamp'] sbs.save() current.output = { 'status': 'OK', 'code': 200} + def list_channels(current): """ List channel memberships of current user @@ -288,8 +289,11 @@ def list_channels(current): 'actions': sbs.get_actions(), 'unread': sbs.unread_count()}) except ObjectDoesNotExist: + # FIXME: This should not happen, + log.exception("UNPAIRED DIRECT EXCHANGES!!!!") sbs.delete() + def unread_count(current): """ Number of unread messages for current user @@ -328,6 +332,58 @@ def unread_count(current): } +def get_notifications(current): + """ + Returns last N notifications for current user + + + .. code-block:: python + + # request: + { + 'view':'_zops_unread_messages', + 'amount': int, # Optional, defaults to 8 + } + + # response: + { + 'status': 'OK', + 'code': 200, + 'notifications': [{'title':string, + 'body': string, + 'channel_key': key, + 'type': int, + 'url': string, # could be a in app JS URL prefixed with "#" or + # full blown URL prefixed with "http" + 'message_key': key, + 'timestamp': datetime},], + } + """ + current.output = { + 'status': 'OK', + 'code': 200, + 'notifications': [], + } + amount = current.input.get('amount', 8) + try: + notif_sbs = current.user.subscriptions.objects.get(channel_id=current.user.prv_exchange) + except MultipleObjectsReturned: + # FIXME: This should not happen, + log.exception("MULTIPLE PRV EXCHANGES!!!!") + sbs = current.user.subscriptions.objects.filter(channel_id=current.user.prv_exchange) + sbs[0].delete() + notif_sbs = sbs[1] + for msg in notif_sbs.channel.message_set.objects.filter()[:amount]: + current.output['notifications'].insert(0, { + 'title': msg.msg_title, + 'body': msg.body, + 'type': msg.typ, + 'url': msg.url, + 'channel_key': msg.channel.key, + 'message_key': msg.key, + 'timestamp': msg.updated_at}) + + def create_channel(current): """ Create a public channel. Can be a broadcast channel or normal chat room. diff --git a/zengine/settings.py b/zengine/settings.py index 5796f08e..d2c5f4d6 100644 --- a/zengine/settings.py +++ b/zengine/settings.py @@ -144,6 +144,7 @@ '_zops_flag_message': 'zengine.messaging.views.flag_message', '_zops_unflag_message': 'zengine.messaging.views.unflag_message', '_zops_unread_count': 'zengine.messaging.views.unread_count', + '_zops_unread_notifications': 'zengine.messaging.views.unread_notifications', # '_zops_': 'zengine.messaging.views.', }