def set_notifications_for_feed(request): user = get_user(request) feed_id = request.POST['feed_id'] notification_types = request.POST.getlist('notification_types') notification_filter = request.POST.get('notification_filter') try: notification = MUserFeedNotification.objects.get(user_id=user.pk, feed_id=feed_id) except MUserFeedNotification.DoesNotExist: params = { "user_id": user.pk, "feed_id": feed_id, } notification = MUserFeedNotification.objects.create(**params) web_was_off = not notification.is_web notification.is_focus = bool(notification_filter == "focus") notification.is_email = bool('email' in notification_types) notification.is_ios = bool('ios' in notification_types) notification.is_android = bool('android' in notification_types) notification.is_web = bool('web' in notification_types) notification.save() if (not notification.is_email and not notification.is_ios and not notification.is_android and not notification.is_web): notification.delete() r = redis.Redis(connection_pool=settings.REDIS_PUBSUB_POOL) if web_was_off and notification.is_web: r.publish(user.username, 'notification:setup:%s' % feed_id) notifications_by_feed = MUserFeedNotification.feeds_for_user(user.pk) return {"notifications_by_feed": notifications_by_feed}
def force_push(request): user = get_user(request) feed_id = request.REQUEST['feed_id'] count = int(request.REQUEST.get('count', 1)) logging.user(user, "~BM~FWForce pushing %s stories: ~SB%s" % (count, Feed.get_by_id(feed_id))) sent_count, user_count = MUserFeedNotification.push_feed_notifications(feed_id, new_stories=count, force=True) return {"message": "Pushed %s notifications to %s users" % (sent_count, user_count)}
def _publish_to_subscribers(self, feed, story_hash): try: r = redis.Redis(connection_pool=settings.REDIS_PUBSUB_POOL) listeners_count = r.publish("%s:story" % feed.pk, 'story:new:%s' % story_hash) if listeners_count: logging.debug(" ---> [%-30s] ~FMPublished to %s subscribers" % (feed.log_title[:30], listeners_count)) except redis.ConnectionError: logging.debug(" ***> [%-30s] ~BMRedis is unavailable for real-time." % (feed.log_title[:30],)) if MUserFeedNotification.feed_has_users(feed.pk) > 0: QueueNotifications.delay(feed.pk, 1)
def force_push(request): """ Intended to force a push notification for a feed for testing. Handier than the console. """ user = get_user(request) feed_id = request.GET['feed_id'] count = int(request.GET.get('count', 1)) logging.user( user, "~BM~FWForce pushing %s stories: ~SB%s" % (count, Feed.get_by_id(feed_id))) sent_count, user_count = MUserFeedNotification.push_feed_notifications( feed_id, new_stories=count, force=True) return { "message": "Pushed %s notifications to %s users" % (sent_count, user_count) }
def notifications_by_feed(request): user = get_user(request) notifications_by_feed = MUserFeedNotification.feeds_for_user(user.pk) return notifications_by_feed
def run(self, feed_id, new_stories): MUserFeedNotification.push_feed_notifications(feed_id, new_stories)
def QueueNotifications(feed_id, new_stories): MUserFeedNotification.push_feed_notifications(feed_id, new_stories)