+ """This class represents notifications of a user.
+ """
+ def __init__(self, connection):
+ self._connection = connection
+ self._data = {}
+ self._notifications = self.get()
+ self.page = 1
+
+ def __len__(self):
+ return len(self._notifications)
+
+ def __iter__(self):
+ return iter(self._notifications)
+
+ def __getitem__(self, n):
+ return self._notifications[n]
+
+ def _finalise(self, notifications):
+ self._data['unread_count'] = notifications['unread_count']
+ self._data['unread_count_by_type'] = notifications['unread_count_by_type']
+ return [Notification(self._connection, n) for n in notifications.get('notification_list', [])]
+
+ def last(self):
+ """Returns list of most recent notifications.
+ """
+ params = {'per_page': 5, '_': int(round(time.time(), 3)*1000)}
+ headers = {'x-csrf-token': repr(self._connection)}
+
+ request = self._connection.get('notifications.json', headers=headers, params=params)
+
+ if request.status_code != 200:
+ raise Exception('status code: {0}: cannot retrieve 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['unread_count'] +=1
+ data['unread_count_by_type'][n.type] +=1
+ notifications.append(n)
+ ids.append(n.id)
+ self._notifications = notifications
+ self._data = data