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: | |
52 | data[n.type].unread_count +=1 | |
53 | data[n.type].unread_count_by_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)].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[new_notifications[-i].type].unread_count +=1 | |
72 | data[new_notifications[-i].type].unread_count_by_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: | |
78 | self.update() # if there is a gap | |
79 | ||
80 | def update(self, per_page=5, page=1): | |
81 | result = self.get(per_page=per_page, page=page) | |
82 | if result: | |
83 | self._expand( result ) | |
84 | ||
85 | def more(self, per_page=5, page=0): | |
86 | if not page: page = self.page + 1 | |
87 | self.page = page | |
88 | result = self.get(per_page=per_page, page=page) | |
89 | if result: | |
90 | self._expand( result ) | |
91 | ||
178faa46 MM |
92 | def get(self, per_page=5, page=1): |
93 | """Returns list of notifications. | |
94 | """ | |
95 | params = {'per_page': per_page, 'page': page} | |
bc49cc41 | 96 | headers = {'x-csrf-token': repr(self._connection)} |
178faa46 MM |
97 | |
98 | request = self._connection.get('notifications.json', headers=headers, params=params) | |
2ec93347 | 99 | |
178faa46 MM |
100 | if request.status_code != 200: |
101 | raise Exception('status code: {0}: cannot retreive notifications'.format(request.status_code)) | |
5b295bf4 | 102 | return self._finalise(request.json()) |