|
| 1 | +from stream_framework.activity import NotificationActivity |
| 2 | +from stream_framework.aggregators.base import NotificationAggregator |
| 3 | +from stream_framework.feeds.aggregated_feed.base import AggregatedFeed |
| 4 | +from stream_framework.serializers.aggregated_activity_serializer import NotificationSerializer |
| 5 | +from stream_framework.storage.base_lists_storage import BaseListsStorage |
| 6 | + |
| 7 | +import logging |
| 8 | +logger = logging.getLogger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +class BaseNotificationFeed(AggregatedFeed): |
| 12 | + ''' |
| 13 | + Similar to an aggregated feed, but: |
| 14 | + - does not use the activity storage (serializes everything into the timeline storage) |
| 15 | + - tracks unseen/unread aggregated activities |
| 16 | + - enables counting of unseen/unread aggregated activities |
| 17 | + - enables marking of unseen/unread aggregated activities as seen/read |
| 18 | + ''' |
| 19 | + |
| 20 | + key_format = 'notification_feed:%(user_id)s' |
| 21 | + |
| 22 | + timeline_serializer = NotificationSerializer |
| 23 | + aggregator_class = NotificationAggregator |
| 24 | + aggregated_activity_class = NotificationActivity |
| 25 | + activity_storage_class = None |
| 26 | + activity_serializer = None |
| 27 | + |
| 28 | + # : the storage class responsible to keep track of unseen/unread activity ids |
| 29 | + markers_storage_class = BaseListsStorage |
| 30 | + |
| 31 | + # : define whether or not to keep track of unseen activity ids |
| 32 | + track_unseen = True |
| 33 | + # : define whether or not to keep track of unread activity ids |
| 34 | + track_unread = True |
| 35 | + |
| 36 | + # : the max number of tracked unseen/unread activity ids |
| 37 | + markers_max_length = 100 |
| 38 | + |
| 39 | + # : provides a part of the key used by markers_storage_class |
| 40 | + markers_key_format = 'notification_feed:%(user_id)s' |
| 41 | + |
| 42 | + #: the key used for distributed locking |
| 43 | + lock_format = 'notification_feed:%(user_id)s:lock' |
| 44 | + |
| 45 | + def __init__(self, user_id, **kwargs): |
| 46 | + super(BaseNotificationFeed, self).__init__(user_id, **kwargs) |
| 47 | + |
| 48 | + if self.markers_storage_class is None: |
| 49 | + if self.track_unread or self.track_unseen: |
| 50 | + raise ValueError('markers_storage_class must be set in case the unseen/unread activities are tracked') |
| 51 | + else: |
| 52 | + if not issubclass(self.markers_storage_class, BaseListsStorage): |
| 53 | + error_format = 'markers_storage_class attribute must be subclass of %s, encountered class %s' |
| 54 | + message = error_format % (BaseListsStorage, self.markers_storage_class) |
| 55 | + raise ValueError(message) |
| 56 | + |
| 57 | + markers_key = self.markers_key_format % {'user_id': user_id} |
| 58 | + self.feed_markers = self.markers_storage_class(key=markers_key, |
| 59 | + max_length=self.markers_max_length) |
| 60 | + |
| 61 | + def count_unseen(self): |
| 62 | + ''' |
| 63 | + Counts the number of aggregated activities which are unseen. |
| 64 | + ''' |
| 65 | + if self.track_unseen: |
| 66 | + return self.feed_markers.count('unseen') |
| 67 | + |
| 68 | + def count_unread(self): |
| 69 | + ''' |
| 70 | + Counts the number of aggregated activities which are unread. |
| 71 | + ''' |
| 72 | + if self.track_unread: |
| 73 | + return self.feed_markers.count('unread') |
| 74 | + |
| 75 | + def get_notification_data(self): |
| 76 | + ''' |
| 77 | + Provides custom notification data that is used by the transport layer |
| 78 | + when the feed is updated. |
| 79 | + ''' |
| 80 | + notification_data = dict() |
| 81 | + |
| 82 | + if self.track_unseen and self.track_unread: |
| 83 | + unseen_count, unread_count = self.feed_markers.count('unseen', 'unread') |
| 84 | + notification_data['unseen_count'] = unseen_count |
| 85 | + notification_data['unread_count'] = unread_count |
| 86 | + elif self.track_unseen: |
| 87 | + unseen_count = self.feed_markers.count('unseen') |
| 88 | + notification_data['unseen_count'] = unseen_count |
| 89 | + elif self.track_unread: |
| 90 | + unread_count = self.feed_markers.count('unread') |
| 91 | + notification_data['unread_count'] = unread_count |
| 92 | + |
| 93 | + return notification_data |
| 94 | + |
| 95 | + def update_markers(self, unseen_ids=None, unread_ids=None, operation='add'): |
| 96 | + ''' |
| 97 | + Starts or stops tracking aggregated activities as unseen and/or unread. |
| 98 | + ''' |
| 99 | + if self.markers_storage_class is not None: |
| 100 | + if operation not in ('add', 'remove'): |
| 101 | + raise TypeError('%s is not supported' % operation) |
| 102 | + |
| 103 | + kwargs = dict() |
| 104 | + if unseen_ids is not None and self.track_unseen: |
| 105 | + kwargs['unseen'] = unseen_ids |
| 106 | + if unread_ids is not None and self.track_unread: |
| 107 | + kwargs['unread'] = unread_ids |
| 108 | + |
| 109 | + func = getattr(self.feed_markers, operation) |
| 110 | + func(**kwargs) |
| 111 | + |
| 112 | + # TODO use a real-time transport layer to notify for these updates |
| 113 | + |
| 114 | + def get_activity_slice(self, start=None, stop=None, rehydrate=True): |
| 115 | + ''' |
| 116 | + Retrieves a slice of aggregated activities and annotates them as read and/or seen. |
| 117 | + ''' |
| 118 | + activities = super(BaseNotificationFeed, self).get_activity_slice(start, stop, rehydrate) |
| 119 | + if activities and self.markers_storage_class is not None: |
| 120 | + |
| 121 | + if self.track_unseen and self.track_unread: |
| 122 | + unseen_ids, unread_ids = self.feed_markers.get('unseen', 'unread') |
| 123 | + elif self.track_unseen: |
| 124 | + unseen_ids = self.feed_markers.get('unseen') |
| 125 | + elif self.track_unread: |
| 126 | + unread_ids = self.feed_markers.get('unread') |
| 127 | + |
| 128 | + for activity in activities: |
| 129 | + if self.track_unseen: |
| 130 | + activity.is_seen = activity.serialization_id not in unseen_ids |
| 131 | + if self.track_unread: |
| 132 | + activity.is_read = activity.serialization_id not in unread_ids |
| 133 | + |
| 134 | + return activities |
| 135 | + |
| 136 | + def add_many_aggregated(self, aggregated, *args, **kwargs): |
| 137 | + ''' |
| 138 | + Adds the activities to the notification feed and marks them as unread/unseen. |
| 139 | + ''' |
| 140 | + super(BaseNotificationFeed, self).add_many_aggregated(aggregated, *args, **kwargs) |
| 141 | + ids = [a.serialization_id for a in aggregated] |
| 142 | + self.update_markers(ids, ids, operation='add') |
| 143 | + |
| 144 | + def remove_many_aggregated(self, aggregated, *args, **kwargs): |
| 145 | + ''' |
| 146 | + Removes the activities from the notification feed and marks them as read/seen. |
| 147 | + ''' |
| 148 | + super(BaseNotificationFeed, self).remove_many_aggregated(aggregated, *args, **kwargs) |
| 149 | + ids = [a.serialization_id for a in aggregated] |
| 150 | + self.update_markers(ids, ids, operation='remove') |
| 151 | + |
| 152 | + def mark_activity(self, activity_id, seen=True, read=False): |
| 153 | + ''' |
| 154 | + Marks the given aggregated activity as seen or read or both. |
| 155 | + ''' |
| 156 | + self.mark_activities([activity_id], seen, read) |
| 157 | + |
| 158 | + def mark_activities(self, activity_ids, seen=True, read=False): |
| 159 | + ''' |
| 160 | + Marks all of the given aggregated activities as seen or read or both. |
| 161 | + ''' |
| 162 | + unseen_ids = activity_ids if seen else [] |
| 163 | + unread_ids = activity_ids if read else [] |
| 164 | + self.update_markers(unseen_ids=unseen_ids, |
| 165 | + unread_ids=unread_ids, |
| 166 | + operation='remove') |
| 167 | + |
| 168 | + def mark_all(self, seen=True, read=False): |
| 169 | + ''' |
| 170 | + Marks all of the feed's aggregated activities as seen or read or both. |
| 171 | + ''' |
| 172 | + args = [] |
| 173 | + if seen and self.track_unseen: |
| 174 | + args.append('unseen') |
| 175 | + if read and self.track_unread: |
| 176 | + args.append('unread') |
| 177 | + self.feed_markers.flush(*args) |
| 178 | + |
| 179 | + def delete(self): |
| 180 | + ''' |
| 181 | + Deletes the feed and its markers. |
| 182 | + ''' |
| 183 | + super(BaseNotificationFeed, self).delete() |
| 184 | + |
| 185 | + args = [] |
| 186 | + if self.track_unseen: |
| 187 | + args.append('unseen') |
| 188 | + if self.track_unread: |
| 189 | + args.append('unread') |
| 190 | + self.feed_markers.flush(*args) |
0 commit comments