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 | ||
c2b3e8a9 AS |
22 | def __len__(self): |
23 | return len(self._notifications) | |
24 | ||
d95ff94a C |
25 | def __iter__(self): |
26 | return iter(self._notifications) | |
27 | ||
28 | def __getitem__(self, n): | |
29 | return self._notifications[n] | |
30 | ||
31 | def _finalise(self, notifications): | |
32 | self._data['unread_count'] = notifications['unread_count'] | |
33 | self._data['unread_count_by_type'] = notifications['unread_count_by_type'] | |
34 | return [Notification(self._connection, n) for n in notifications.get('notification_list', [])] | |
35 | ||
36 | def last(self): | |
37 | """Returns list of most recent notifications. | |
38 | """ | |
39 | params = {'per_page': 5, '_': int(round(time.time(), 3)*1000)} | |
40 | headers = {'x-csrf-token': repr(self._connection)} | |
41 | ||
42 | request = self._connection.get('notifications.json', headers=headers, params=params) | |
43 | ||
44 | if request.status_code != 200: | |
c2b3e8a9 | 45 | raise Exception('status code: {0}: cannot retrieve notifications'.format(request.status_code)) |
d95ff94a C |
46 | return self._finalise(request.json()) |
47 | ||
48 | def _expand(self, new_notifications): | |
49 | ids = [notification.id for notification in self._notifications] | |
50 | notifications = self._notifications | |
51 | data = self._data | |
52 | for n in new_notifications: | |
53 | if n.id not in ids: | |
18891464 C |
54 | if n.unread: |
55 | data['unread_count'] +=1 | |
56 | data['unread_count_by_type'][n.type] +=1 | |
d95ff94a C |
57 | notifications.append(n) |
58 | ids.append(n.id) | |
59 | self._notifications = notifications | |
18891464 | 60 | self._data = data |
d95ff94a C |
61 | |
62 | def _update(self, new_notifications): | |
63 | ids = [notification.id for notification in self._notifications] | |
64 | notifications = self._notifications | |
65 | data = self._data | |
66 | ||
67 | update = False | |
68 | if new_notifications[len(new_notifications)-1].id not in ids: | |
69 | update = True | |
70 | ||
71 | for i in range(len(new_notifications)): | |
72 | if new_notifications[-i].id not in ids: | |
18891464 C |
73 | if new_notifications[-i].unread: |
74 | data['unread_count'] +=1 | |
75 | data['unread_count_by_type'][new_notifications[-i].type] +=1 | |
d95ff94a C |
76 | notifications = [new_notifications[-i]] + notifications |
77 | ids.append(new_notifications[-i].id) | |
78 | self._notifications = notifications | |
79 | self._data = data | |
80 | if update: self.update() # if there is a gap | |
81 | ||
82 | def update(self, per_page=5, page=1): | |
83 | result = self.get(per_page=per_page, page=page) | |
84 | if result: self._update( result ) | |
85 | ||
86 | def more(self, per_page=5, page=0): | |
87 | if not page: page = self.page + 1 | |
88 | self.page = page | |
89 | result = self.get(per_page=per_page, page=page) | |
18891464 C |
90 | if result: |
91 | self._expand( result ) | |
d95ff94a C |
92 | |
93 | def get(self, per_page=5, page=1): | |
94 | """Returns list of notifications. | |
95 | """ | |
96 | params = {'per_page': per_page, 'page': page} | |
97 | headers = {'x-csrf-token': repr(self._connection)} | |
98 | ||
99 | request = self._connection.get('notifications.json', headers=headers, params=params) | |
100 | ||
101 | if request.status_code != 200: | |
102 | raise Exception('status code: {0}: cannot retreive notifications'.format(request.status_code)) | |
103 | return self._finalise(request.json()) |