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.', }