Skip to content

Commit

Permalink
added _zops_get_notifications method
Browse files Browse the repository at this point in the history
rref #5367
rref #5366

ref #66
ref #65
  • Loading branch information
evrenesat committed Jul 27, 2016
1 parent d83de26 commit ec19cc5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
8 changes: 7 additions & 1 deletion zengine/messaging/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 59 additions & 3 deletions zengine/messaging/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions zengine/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
}

Expand Down

0 comments on commit ec19cc5

Please sign in to comment.