From b31d70ab0eb64eccc41fec5eadabdce56ad24819 Mon Sep 17 00:00:00 2001 From: CYBERDEViLNL Date: Tue, 27 Mar 2018 02:16:17 +0200 Subject: [PATCH] update() and more() functionality for notifications. --- diaspy/notifications.py | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/diaspy/notifications.py b/diaspy/notifications.py index 983ea2b..b644729 100644 --- a/diaspy/notifications.py +++ b/diaspy/notifications.py @@ -17,6 +17,7 @@ class Notifications(): self._connection = connection self._data = {} self._notifications = self.get() + self.page = 1 def __iter__(self): return iter(self._notifications) @@ -41,6 +42,53 @@ class Notifications(): raise Exception('status code: {0}: cannot retreive notifications'.format(request.status_code)) return self._finalise(request.json()) + def _expand(self, new_notifications): + ids = [notification.id for notification in self._notifications] + notifications = self._notifications + data = self._data + for n in new_notifications: + if n.id not in ids: + if n.unread: + data[n.type].unread_count +=1 + data[n.type].unread_count_by_type +=1 + notifications.append(n) + ids.append(n.id) + self._notifications = notifications + self._data = data + + def _update(self, new_notifications): + ids = [notification.id for notification in self._notifications] + notifications = self._notifications + data = self._data + + update = False + if new_notifications[len(new_notifications)].id not in ids: + update = True + + for i in range(len(new_notifications)): + if new_notifications[-i].id not in ids: + if new_notifications[-i].unread: + data[new_notifications[-i].type].unread_count +=1 + data[new_notifications[-i].type].unread_count_by_type +=1 + notifications = [new_notifications[-i]] + notifications + ids.append(new_notifications[-i].id) + self._notifications = notifications + self._data = data + if update: + self.update() # if there is a gap + + def update(self, per_page=5, page=1): + result = self.get(per_page=per_page, page=page) + if result: + self._expand( result ) + + def more(self, per_page=5, page=0): + if not page: page = self.page + 1 + self.page = page + result = self.get(per_page=per_page, page=page) + if result: + self._expand( result ) + def get(self, per_page=5, page=1): """Returns list of notifications. """ -- 2.25.1