Skip to content

Commit

Permalink
Adding in push notifications for web. Needs filtering.
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelclay committed Nov 16, 2016
1 parent fd4db68 commit 0284793
Show file tree
Hide file tree
Showing 9 changed files with 646 additions and 5 deletions.
37 changes: 36 additions & 1 deletion apps/notifications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def __unicode__(self):
self.last_notification_date,
)

@classmethod
def users_for_feed(cls, feed_id):
notifications = cls.objects.filter(feed_id=feed_id)

return notifications

@classmethod
def feeds_for_user(cls, user_id):
notifications = cls.objects.filter(user_id=user_id)
Expand All @@ -69,4 +75,33 @@ def feeds_for_user(cls, user_id):

return notifications_by_feed


@classmethod
def send_notifications(cls, story):
notifications = cls.objects.filter(feed_id=story.story_feed_id)
for notification in notifications:
if notification.is_focus and not notification.story_visible_in_focus(story):
continue
notification.send_web(story)
notification.send_ios(story)
notification.send_android(story)
notification.send_email(story)

def send_web(self, story):
if not self.is_web: return
user = User.objects.get(pk=self.user_id)
r = redis.Redis(connection_pool=settings.REDIS_PUBSUB_POOL)
r.publish(user.username, 'notification:%s,%s' % (story.story_hash, story.story_title))

def send_ios(self, story):
if not self.is_ios: return


def send_android(self, story):
if not self.is_android: return


def send_email(self, story):
if not self.is_email: return

def story_visible_in_focus(self, story):
pass
9 changes: 8 additions & 1 deletion apps/notifications/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import redis
from django.conf import settings
from django.contrib.auth.decorators import login_required
from utils import json_functions as json
from utils.user_functions import get_user, ajax_login_required
Expand Down Expand Up @@ -29,6 +31,7 @@ def set_notifications_for_feed(request):
}
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)
Expand All @@ -41,7 +44,11 @@ def set_notifications_for_feed(request):
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}
5 changes: 5 additions & 0 deletions apps/rss_feeds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from apps.rss_feeds.tasks import UpdateFeeds, PushFeeds, ScheduleCountTagsForUser
from apps.rss_feeds.text_importer import TextImporter
from apps.search.models import SearchStory, SearchFeed
from apps.notifications.models import MUserFeedNotification
from apps.statistics.rstats import RStats
from utils import json_functions as json
from utils import feedfinder2 as feedfinder, feedparser
Expand Down Expand Up @@ -1176,6 +1177,7 @@ def _1(story, story_content, existing_stories, new_story_hashes):
logging.info(' ---> [%-30s] ~SN~FRIntegrityError on new story: %s - %s' % (self.feed_title[:30], story.get('guid'), e))
if self.search_indexed:
s.index_story_for_search()
s.send_notifications()
elif existing_story and story_has_changed and not updates_off and ret_values['updated'] < 3:
# update story
original_content = None
Expand Down Expand Up @@ -2276,6 +2278,9 @@ def remove_from_search_index(self):
SearchStory.remove(self.story_hash)
except NotFoundException:
pass

def send_notifications(self):
MUserFeedNotification.send_notifications(self)

@classmethod
def trim_feed(cls, cutoff, feed_id=None, feed=None, verbose=True):
Expand Down
1 change: 1 addition & 0 deletions assets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ javascripts:
- media/js/vendor/tag-it.js
- media/js/vendor/chart.js
- media/js/vendor/audio.js
- media/js/vendor/push.js
- media/js/vendor/socket.io-client.*.js
- media/js/vendor/inflector.js
- media/js/vendor/underscore-*.js
Expand Down
46 changes: 46 additions & 0 deletions media/js/newsblur/reader/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4704,6 +4704,14 @@
}
} else if (message == "interaction:new") {
this.update_interactions_count();
} else if (_.string.startsWith(message, "notification:setup:")) {
message = message.replace('notification:setup:', '');
this.push_notification_setup(parseInt(message, 10));
} else if (_.string.startsWith(message, "notification:")) {
message = message.replace('notification:', '');
var story_hash = message.slice(0, message.indexOf(','));
var story_title = message.slice(message.indexOf(',')+1);
this.push_notification(story_hash, story_title);
} else if (_.string.startsWith(message, "search_index_complete:")) {
message = message.replace('search_index_complete:', '');
if (NEWSBLUR.app.active_search) {
Expand Down Expand Up @@ -4887,6 +4895,44 @@
}, $.noop);
},

push_notification_setup: function(feed_id) {
var feed = NEWSBLUR.assets.get_feed(feed_id);
if (!feed) {
console.log(['Notification setup failed, no feed!', feed_id]);
}
Push.create("NewsBlur", {
body: feed.get('feed_title') + " notifications are setup",
icon: $.favicon(feed),
timeout: 3000,
onClick: function () {
window.focus();
this.close();
}
});
},

push_notification: function(story_hash, story_title) {
var feed_id = story_hash.slice(0, story_hash.indexOf(':'));
var feed = NEWSBLUR.assets.get_feed(feed_id);
if (!feed) {
console.log(['Error: Couldn\'t find find for notification', feed_id, story_hash, story_title]);
return;
}

Push.create(feed.get('feed_title'), {
body: story_title,
icon: $.favicon(feed),
timeout: 4000,
onClick: function () {
window.focus();
this.close();
window.NEWSBLUR.reader.open_feed(feed_id, {
'story_hash': story_hash
});
}
});
},

// ===================
// = Mouse Indicator =
// ===================
Expand Down
2 changes: 1 addition & 1 deletion media/js/newsblur/reader/reader_notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ _.extend(NEWSBLUR.ReaderNotifications.prototype, {
$.make('h2', { className: 'NB-modal-title' }, [
$.make('div', { className: 'NB-modal-loading' }),
$.make('div', { className: 'NB-icon' }),
'Notificatons',
'Notifications',
$.make('div', { className: 'NB-icon-dropdown' })
]),
(this.feed && $.make('div', { className: 'NB-fieldset NB-modal-submit' }, [
Expand Down
1 change: 0 additions & 1 deletion media/js/newsblur/views/feed_notification_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ NEWSBLUR.Views.FeedNotificationView = Backbone.View.extend({
},

initialize: function(m) {
console.log(['feed notificaiton', this.model, m]);
},

render: function() {
Expand Down
Loading

0 comments on commit 0284793

Please sign in to comment.