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(): | |
14 | """This class represents notifications of a user. | |
15 | """ | |
16 | def __init__(self, connection): | |
17 | self._connection = connection | |
8a18ad2c | 18 | self._data = {} |
178faa46 | 19 | self._notifications = self.get() |
b31d70ab | 20 | self.page = 1 |
178faa46 MM |
21 | |
22 | def __iter__(self): | |
23 | return iter(self._notifications) | |
24 | ||
2ec93347 MM |
25 | def __getitem__(self, n): |
26 | return self._notifications[n] | |
27 | ||
5b295bf4 MM |
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 | ||
178faa46 MM |
33 | def last(self): |
34 | """Returns list of most recent notifications. | |
35 | """ | |
36 | params = {'per_page': 5, '_': int(round(time.time(), 3)*1000)} | |
bc49cc41 | 37 | headers = {'x-csrf-token': repr(self._connection)} |
178faa46 MM |
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)) | |
5b295bf4 | 43 | return self._finalise(request.json()) |
178faa46 | 44 | |
b31d70ab C |
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: | |
00663355 | 52 | data['unread_count'] +=1 |
0c28bf0b | 53 | data['unread_count_by_type'][n.type] +=1 |
b31d70ab C |
54 | notifications.append(n) |
55 | ids.append(n.id) | |
56 | self._notifications = notifications | |
57 | self._data = data | |
0c28bf0b | 58 | |
b31d70ab C |
59 | def _update(self, new_notifications): |
60 | ids = [notification.id for notification in self._notifications] | |
61 | notifications = self._notifications | |
62 | data = self._data | |
0c28bf0b | 63 | |
b31d70ab | 64 | update = False |
0c28bf0b | 65 | if new_notifications[len(new_notifications)-1].id not in ids: |
b31d70ab | 66 | update = True |
0c28bf0b | 67 | |
b31d70ab C |
68 | for i in range(len(new_notifications)): |
69 | if new_notifications[-i].id not in ids: | |
70 | if new_notifications[-i].unread: | |
0c28bf0b C |
71 | data['unread_count'] +=1 |
72 | data['unread_count_by_type'][new_notifications[-i].type] +=1 | |
b31d70ab C |
73 | notifications = [new_notifications[-i]] + notifications |
74 | ids.append(new_notifications[-i].id) | |
75 | self._notifications = notifications | |
76 | self._data = data | |
0c28bf0b | 77 | if update: self.update() # if there is a gap |
b31d70ab C |
78 | |
79 | def update(self, per_page=5, page=1): | |
80 | result = self.get(per_page=per_page, page=page) | |
0c28bf0b C |
81 | if result: self._update( result ) |
82 | ||
b31d70ab C |
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 | ||
178faa46 MM |
90 | def get(self, per_page=5, page=1): |
91 | """Returns list of notifications. | |
92 | """ | |
93 | params = {'per_page': per_page, 'page': page} | |
bc49cc41 | 94 | headers = {'x-csrf-token': repr(self._connection)} |
178faa46 MM |
95 | |
96 | request = self._connection.get('notifications.json', headers=headers, params=params) | |
2ec93347 | 97 | |
178faa46 MM |
98 | if request.status_code != 200: |
99 | raise Exception('status code: {0}: cannot retreive notifications'.format(request.status_code)) | |
5b295bf4 | 100 | return self._finalise(request.json()) |