Skip to content

Commit

Permalink
Unread notifications polling from the UI
Browse files Browse the repository at this point in the history
Add notifications count route
Add JS notifications unread polling

Refs: diaspora#6473
  • Loading branch information
jaywink committed Jan 7, 2016
1 parent 00a6cb4 commit c41a502
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
19 changes: 19 additions & 0 deletions app/assets/javascripts/app/views/notification_dropdown_view.js
Expand Up @@ -16,6 +16,8 @@ app.views.NotificationDropdown = app.views.Base.extend({
this.dropdownNotifications = this.dropdown.find(".notifications");
this.ajaxLoader = this.dropdown.find(".ajax-loader");
this.perfectScrollbarInitialized = false;
this.updateHeaderCounts();
setInterval(this.updateHeaderCounts, 30000);
},

toggleDropdown: function(evt){
Expand Down Expand Up @@ -105,6 +107,7 @@ app.views.NotificationDropdown = app.views.Base.extend({
}
});
});
this.updateHeaderCounts();

this.hideAjaxLoader();

Expand All @@ -131,6 +134,22 @@ app.views.NotificationDropdown = app.views.Base.extend({
this.dropdownNotifications.perfectScrollbar("destroy");
this.perfectScrollbarInitialized = false;
}
},

updateHeaderCounts: function() {
$.getJSON(Routes.notificationsCounts(), function(data) {
var headerBadge = $(".notifications-link .badge"),
markAllReadLink = $("a#mark_all_read_link");
if (data.notifications > 0) {
headerBadge.html(parseInt(data.notifications));
headerBadge.removeClass("hidden");
markAllReadLink.removeClass("enabled");
} else {
headerBadge.addClass("hidden");
headerBadge.html(0);
markAllReadLink.removeClass("disabled");
}
});
}
});
// @license-end
7 changes: 7 additions & 0 deletions app/controllers/notifications_controller.rb
Expand Up @@ -5,6 +5,8 @@
class NotificationsController < ApplicationController
before_action :authenticate_user!

respond_to :json, only: :counts

def update
note = Notification.where(:recipient_id => current_user.id, :id => params[:id]).first
if note
Expand Down Expand Up @@ -78,4 +80,9 @@ def read_all

end

def counts
render json: {
notifications: current_user.unread_notifications.count
}
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -88,6 +88,7 @@
end
end

get "notifications/counts" => "notifications#counts"

resources :tags, :only => [:index]

Expand Down
15 changes: 15 additions & 0 deletions spec/controllers/notifications_controller_spec.rb
Expand Up @@ -187,4 +187,19 @@
expect(response).not_to be_redirect
end
end

describe "counts" do
it "succeeds" do
get :counts
expect(response).to be_success
end

it "returns unread notifications count" do
post = FactoryGirl.create(:status_message)
FactoryGirl.create(:notification, recipient: alice, target: post)
FactoryGirl.create(:notification, recipient: alice, target: post, unread: false)
get :counts
expect(response.body).to eq('{"notifications":1}')
end
end
end

0 comments on commit c41a502

Please sign in to comment.