Commit | Line | Data |
---|---|---|
178faa46 MM |
1 | #!/usr/bin/env python3 |
2 | ||
3 | ||
4 | import time | |
2ec93347 | 5 | |
178faa46 MM |
6 | from diaspy.models import Notification |
7 | ||
8 | ||
9 | """This module abstracts notifications. | |
10 | """ | |
11 | ||
12 | ||
13 | class Notifications(): | |
d95ff94a C |
14 | """This class represents notifications of a user. |
15 | """ | |
16 | def __init__(self, connection): | |
17 | self._connection = connection | |
18 | self._data = {} | |
19 | self._notifications = self.get() | |
20 | self.page = 1 | |
21 | ||
22 | def __iter__(self): | |
23 | return iter(self._notifications) | |
24 | ||
25 | def __getitem__(self, n): | |
26 | return self._notifications[n] | |
27 | ||
28 | def _finalise(self, notifications): | |
29 | self._data['unread_count'] = notifications['unread_count'] | |
30 | self._data['unread_count_by_type'] = notifications['unread_count_by_type'] | |
31 | return [Notification(self._connection, n) for n in notifications.get('notification_list', [])] | |
32 | ||
33 | def last(self): | |
34 | """Returns list of most recent notifications. | |
35 | """ | |
36 | params = {'per_page': 5, '_': int(round(time.time(), 3)*1000)} | |
37 | headers = {'x-csrf-token': repr(self._connection)} | |
38 | ||
39 | request = self._connection.get('notifications.json', headers=headers, params=params) | |
40 | ||
41 | if request.status_code != 200: | |
42 | raise Exception('status code: {0}: cannot retreive notifications'.format(request.status_code)) | |
43 | return self._finalise(request.json()) | |
44 | ||
45 | def _expand(self, new_notifications): | |
46 | ids = [notification.id for notification in self._notifications] | |
47 | notifications = self._notifications | |
48 | data = self._data | |
49 | for n in new_notifications: | |
50 | if n.id not in ids: | |
51 | if n.unread: | |
52 | data['unread_count'] +=1 | |
53 | data['unread_count_by_type'][n.type] +=1 | |
54 | notifications.append(n) | |
55 | ids.append(n.id) | |
56 | self._notifications = notifications | |
57 | self._data = data | |
58 | ||
59 | def _update(self, new_notifications): | |
60 | ids = [notification.id for notification in self._notifications] | |
61 | notifications = self._notifications | |
62 | data = self._data | |
63 | ||
64 | update = False | |
65 | if new_notifications[len(new_notifications)-1].id not in ids: | |
66 | update = True | |
67 | ||
68 | for i in range(len(new_notifications)): | |
69 | if new_notifications[-i].id not in ids: | |
70 | if new_notifications[-i].unread: | |
71 | data['unread_count'] +=1 | |
72 | data['unread_count_by_type'][new_notifications[-i].type] +=1 | |
73 | notifications = [new_notifications[-i]] + notifications | |
74 | ids.append(new_notifications[-i].id) | |
75 | self._notifications = notifications | |
76 | self._data = data | |
77 | if update: self.update() # if there is a gap | |
78 | ||
79 | def update(self, per_page=5, page=1): | |
80 | result = self.get(per_page=per_page, page=page) | |
81 | if result: self._update( result ) | |
82 | ||
83 | def more(self, per_page=5, page=0): | |
84 | if not page: page = self.page + 1 | |
85 | self.page = page | |
86 | result = self.get(per_page=per_page, page=page) | |
87 | if result: | |
88 | self._expand( result ) | |
89 | ||
90 | def get(self, per_page=5, page=1): | |
91 | """Returns list of notifications. | |
92 | """ | |
93 | params = {'per_page': per_page, 'page': page} | |
94 | headers = {'x-csrf-token': repr(self._connection)} | |
95 | ||
96 | request = self._connection.get('notifications.json', headers=headers, params=params) | |
97 | ||
98 | if request.status_code != 200: | |
99 | raise Exception('status code: {0}: cannot retreive notifications'.format(request.status_code)) | |
100 | return self._finalise(request.json()) |