Skip to content

Commit

Permalink
added unread_count API view
Browse files Browse the repository at this point in the history
fixed unread calculation
fixed BaseUser.send_notification

rref #5367
rref #5366

ref #66
ref #65
  • Loading branch information
evrenesat committed Jul 25, 2016
1 parent efb956d commit 084c836
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion zengine/messaging/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def send_notification(self, title, message, typ=1, url=None):
"""
self.channel_set.channel.__class__.add_message(
self.created_channels.channel.add_message(
channel_key=self.prv_exchange,
body=message,
title=title,
Expand Down
2 changes: 1 addition & 1 deletion zengine/messaging/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ 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__lt=self.last_seen_msg_time).count()
timestamp__gt=self.last_seen_msg_time).count()
else:
return self.channel.message_set.objects.filter().count()

Expand Down
44 changes: 41 additions & 3 deletions zengine/messaging/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,10 @@ def report_last_seen_message(current):
'code': 200,
}
"""
Subscriber(current).objects.filter(channel_id=current.input['channel_key'],
user_id=current.user_id
).update(last_seen_msg_time=current.input['timestamp'])
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']
sbs.save()
current.output = {
'status': 'OK',
'code': 200}
Expand Down Expand Up @@ -289,6 +290,43 @@ def list_channels(current):
except ObjectDoesNotExist:
sbs.delete()

def unread_count(current):
"""
Number of unread messages for current user
.. code-block:: python
# request:
{
'view':'_zops_unread_count',
}
# response:
{
'status': 'OK',
'code': 200,
'notifications': int,
'messages': int,
}
"""
unread_ntf = 0
unread_msg = 0
for sbs in current.user.subscriptions.objects.filter(is_visible=True):
try:
if sbs.channel.key == current.user.prv_exchange:
unread_ntf += sbs.unread_count()
else:
unread_msg += sbs.unread_count()
except ObjectDoesNotExist:
sbs.delete()
current.output = {
'status': 'OK',
'code': 200,
'notifications': unread_ntf,
'messages': unread_msg
}


def create_channel(current):
"""
Expand Down
1 change: 1 addition & 0 deletions zengine/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
'_zops_pin_channel': 'zengine.messaging.views.pin_channel',
'_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_': 'zengine.messaging.views.',
}

Expand Down

0 comments on commit 084c836

Please sign in to comment.