From d6ba3c970c95385354d317b733a9a34d79bdf9b3 Mon Sep 17 00:00:00 2001 From: Michael Sofaer Date: Sun, 29 May 2011 13:16:03 -0700 Subject: [PATCH] proof of concept for notifications overlay --- app/controllers/notifications_controller.rb | 22 +++++++++------ app/helpers/notifications_helper.rb | 1 - app/views/layouts/_header.html.haml | 2 +- app/views/layouts/application.html.haml | 1 + app/views/notifications/_overlay.html.haml | 15 +++++++++++ app/views/notifications/index.html.haml | 1 - public/javascripts/diaspora.js | 27 ++++++++++++++++++- .../notifications_controller_spec.rb | 23 +++++++++------- 8 files changed, 70 insertions(+), 22 deletions(-) create mode 100644 app/views/notifications/_overlay.html.haml diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index c52bac4ff..1f323be2b 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -3,19 +3,20 @@ # the COPYRIGHT file. class NotificationsController < VannaController + include NotificationsHelper - def update - note = Notification.where(:recipient_id => current_user.id, :id => params[:id]).first + def update(opts=params) + note = Notification.where(:recipient_id => current_user.id, :id => opts[:id]).first if note note.update_attributes(:unread => false) - render :nothing => true + {} else - render :nothing => true, :status => 404 + Response.new :status => 404 end end - def index + def index(opts=params) @aspect = :notification conditions = {:recipient_id => current_user.id} page = params[:page] || 1 @@ -31,12 +32,17 @@ class NotificationsController < VannaController pager.replace(result) end + notifications.each{|n| n[:actors] = n.actors} group_days = notifications.group_by{|note| I18n.l(note.created_at, :format => I18n.t('date.formats.fullmonth_day')) } - {:group_days => group_days, :current_user => current_user, :notifications => notifications} + {:group_days => group_days, :notifications => notifications} end - def read_all + def read_all(opts=params) Notification.where(:recipient_id => current_user.id).update_all(:unread => false) - redirect_to aspects_path + end + post_process :html do + def post_read_all + Response.new(:status => 302, :location => aspects_path) + end end end diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index 7c3391d45..31ca1a3e9 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -53,7 +53,6 @@ module NotificationsHelper number_of_actors = actors.count sentence_translations = {:two_words_connector => " #{t('notifications.index.and')} ", :last_word_connector => ", #{t('notifications.index.and')} " } actor_links = actors.collect{ |person| link_to("#{h(person.name.titlecase.strip)}", person_path(person))} - if number_of_actors < 4 message = actor_links.to_sentence(sentence_translations) else diff --git a/app/views/layouts/_header.html.haml b/app/views/layouts/_header.html.haml index fe3720368..62d925924 100644 --- a/app/views/layouts/_header.html.haml +++ b/app/views/layouts/_header.html.haml @@ -24,7 +24,7 @@ - if @notification_count #notification_badge = link_to "", notifications_path, :title => new_notification_text(@notification_count) - = image_tag 'icons/monotone_flag.png', :height => 20, :width => 20 + = image_tag 'icons/monotone_flag.png', :height => 20, :width => 20, :id => "notification-flag" .badge_count{:class => ("hidden" if @notification_count == 0)} = @notification_count #message_inbox_badge diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index e0d72d4ad..0fe815845 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -95,6 +95,7 @@ = yield .clearfix + =render :partial => 'notifications/overlay' /=render :partial => 'layouts/debug.haml' %footer diff --git a/app/views/notifications/_overlay.html.haml b/app/views/notifications/_overlay.html.haml new file mode 100644 index 000000000..f9b4295d8 --- /dev/null +++ b/app/views/notifications/_overlay.html.haml @@ -0,0 +1,15 @@ +.span-24.last#notifications_overlay + .stream.notifications + .day_group.span-24.last + .span-3 + .date + .day + .month + .span-8.notifications_for_day + .stream_element{:data=>{:guid => nil}} + .right + %span.from + = link_to("", "#", :class => "actor") + = link_to("", "#", :class => "object") + + %br diff --git a/app/views/notifications/index.html.haml b/app/views/notifications/index.html.haml index bf1141c3c..161c4ba6e 100644 --- a/app/views/notifications/index.html.haml +++ b/app/views/notifications/index.html.haml @@ -27,7 +27,6 @@ %span.from = notification_message_for(note) - %br %time= timeago(note.created_at) .span-13.last diff --git a/public/javascripts/diaspora.js b/public/javascripts/diaspora.js index 8b2e8c032..ff414ab04 100644 --- a/public/javascripts/diaspora.js +++ b/public/javascripts/diaspora.js @@ -34,7 +34,32 @@ if(typeof this.collection[widgetId].start !== "undefined") { this.collection[widgetId].start(); } - } + }; + $("#notification_badge a").click(function(event){ + event.preventDefault(); + $.ajax({ + "url":"/notifications", + "success":function(data){ + $("#notifications_overlay").show(); + var hash = eval("(" + data + ")"); + $.each(hash["group_days"], function(day){ + $("#notifications_overlay .month").text(day.split(" ")[0]) + $("#notifications_overlay .day").text(day.split(" ")[1]) + var notifications_for_day = hash["group_days"][day] + console.log(notifications_for_day); + $.each(notifications_for_day, function(i, notification_hash){ + $.each(notification_hash, function(notification_type, notification){ + console.log(notification_type); + console.log(notification); + $("#notifications_overlay .actor"). + text(notification["actors"][0]["name"]). + attr("href", notification["actors"][0]["url"]); + }); + }); + }) + } + }); + }); }; Diaspora.WidgetCollection.prototype.subscribe = function(id, callback, context) { diff --git a/spec/controllers/notifications_controller_spec.rb b/spec/controllers/notifications_controller_spec.rb index 4dcb50fc8..03dccdd6b 100644 --- a/spec/controllers/notifications_controller_spec.rb +++ b/spec/controllers/notifications_controller_spec.rb @@ -5,16 +5,18 @@ require 'spec_helper' describe NotificationsController do + render_views(false) before do - @user = alice + @user = alice @aspect = @user.aspects.first - sign_in :user, @user + @controller = NotificationsController.new + @controller.stub!(:current_user).and_return(@user) end describe '#update' do it 'marks a notification as read' do note = Factory(:notification, :recipient => @user) - put :update, :id => note.id + @controller.update :id => note.id Notification.first.unread.should == false end @@ -24,7 +26,7 @@ describe NotificationsController do Factory(:notification, :recipient => @user) note = Factory(:notification, :recipient => user2) - put :update, :id => note.id + @controller.update :id => note.id Notification.find(note.id).unread.should == true end @@ -37,7 +39,7 @@ describe NotificationsController do Factory(:notification, :recipient => @user) Notification.where(:unread => true).count.should == 2 - get :read_all + @controller.read_all({}) Notification.where(:unread => true).count.should == 0 end end @@ -50,11 +52,12 @@ describe NotificationsController do end it 'paginates the notifications' do - get :index - assigns[:notifications].count.should == 25 - - get :index, :page => 2 - assigns[:notifications].count.should == 1 + @controller.index({})[:notifications].count.should == 25 + @controller.index(:page => 2)[:notifications].count.should == 1 + end + it "includes the actors" do + notification = Factory(:notification, :recipient => @user) + @controller.index({})[:notifications].first[:actors].should == notification.actors end it 'eager loads the target' do