Add filters to notifications controller

This commit is contained in:
Steffen van Bergerem 2014-03-13 21:26:05 +01:00
parent 835608f06f
commit f217a5bc11
14 changed files with 103 additions and 24 deletions

View file

@ -32,6 +32,7 @@
* Add permalinks for comments [#4577](https://github.com/diaspora/diaspora/pull/4577) * Add permalinks for comments [#4577](https://github.com/diaspora/diaspora/pull/4577)
* New menu for the mobile version [#4673](https://github.com/diaspora/diaspora/pull/4673) * New menu for the mobile version [#4673](https://github.com/diaspora/diaspora/pull/4673)
* Added comment count to statistic to enable calculations of posts/comments ratios [#4799](https://github.com/diaspora/diaspora/pull/4799) * Added comment count to statistic to enable calculations of posts/comments ratios [#4799](https://github.com/diaspora/diaspora/pull/4799)
* Add filters to notifications controller [#4814](https://github.com/diaspora/diaspora/pull/4814)
# 0.3.0.3 # 0.3.0.3

View file

@ -151,4 +151,10 @@ class ApplicationController < ActionController::Base
gon.preloads = {} gon.preloads = {}
end end
def self.use_bootstrap_for *routes
before_filter -> {
@css_framework = :bootstrap
gon.bootstrap = true
}, only: routes.flatten
end
end end

View file

@ -2,7 +2,7 @@ class ConversationsController < ApplicationController
before_filter :authenticate_user! before_filter :authenticate_user!
layout ->(c) { request.format == :mobile ? "application" : "with_header" } layout ->(c) { request.format == :mobile ? "application" : "with_header" }
before_filter -> { @css_framework = :bootstrap } use_bootstrap_for :index, :show, :new
respond_to :html, :mobile, :json, :js respond_to :html, :mobile, :json, :js

View file

@ -5,6 +5,9 @@
class NotificationsController < ApplicationController class NotificationsController < ApplicationController
before_filter :authenticate_user! before_filter :authenticate_user!
layout ->(c) { request.format == :mobile ? "application" : "with_header_with_footer" }
use_bootstrap_for :index
def update def update
note = Notification.where(:recipient_id => current_user.id, :id => params[:id]).first note = Notification.where(:recipient_id => current_user.id, :id => params[:id]).first
if note if note
@ -23,6 +26,10 @@ class NotificationsController < ApplicationController
def index def index
conditions = {:recipient_id => current_user.id} conditions = {:recipient_id => current_user.id}
if params[:type] && Notification.types.has_key?(params[:type])
conditions[:type] = Notification.types[params[:type]]
end
if params[:show] == "unread" then conditions[:unread] = true end
page = params[:page] || 1 page = params[:page] || 1
per_page = params[:per_page] || 25 per_page = params[:per_page] || 25
@notifications = WillPaginate::Collection.create(page, per_page, Notification.where(conditions).count ) do |pager| @notifications = WillPaginate::Collection.create(page, per_page, Notification.where(conditions).count ) do |pager|
@ -43,6 +50,12 @@ class NotificationsController < ApplicationController
@unread_notification_count = current_user.unread_notifications.count @unread_notification_count = current_user.unread_notifications.count
@grouped_unread_notification_counts = {}
Notification.types.each_with_object(current_user.unread_notifications.group_by(&:type)) {|(name, type), notifications|
@grouped_unread_notification_counts[name] = notifications.has_key?(type) ? notifications[type].count : 0
}
respond_to do |format| respond_to do |format|
format.html format.html
format.xml { render :xml => @notifications.to_xml } format.xml { render :xml => @notifications.to_xml }

View file

@ -9,7 +9,7 @@ class PostsController < ApplicationController
before_filter :set_format_if_malformed_from_status_net, :only => :show before_filter :set_format_if_malformed_from_status_net, :only => :show
before_filter :find_post, :only => [:show, :interactions] before_filter :find_post, :only => [:show, :interactions]
before_filter -> { @css_framework = :bootstrap } use_bootstrap_for :show
respond_to :html, respond_to :html,
:mobile, :mobile,

View file

@ -7,7 +7,7 @@ class StatusMessagesController < ApplicationController
before_filter :remove_getting_started, :only => [:create] before_filter :remove_getting_started, :only => [:create]
before_filter -> { @css_framework = :bootstrap }, :only => [:bookmarklet] use_bootstrap_for :bookmarklet
respond_to :html, respond_to :html,
:mobile, :mobile,

View file

@ -1,5 +1,6 @@
module NotificationsHelper module NotificationsHelper
include PeopleHelper include PeopleHelper
include PostsHelper
def object_link(note, actors) def object_link(note, actors)
target_type = note.popup_translation_key target_type = note.popup_translation_key
@ -7,13 +8,13 @@ module NotificationsHelper
if note.instance_of?(Notifications::Mentioned) if note.instance_of?(Notifications::Mentioned)
if post = note.linked_object if post = note.linked_object
translation(target_type, :actors => actors, :count => actors_count, :post_link => link_to(t('notifications.post'), post_path(post)).html_safe) translation(target_type, :actors => actors, :count => actors_count, :post_link => link_to(post_page_title(post), post_path(post)).html_safe)
else else
t(note.deleted_translation_key, :actors => actors, :count => actors_count).html_safe t(note.deleted_translation_key, :actors => actors, :count => actors_count).html_safe
end end
elsif note.instance_of?(Notifications::CommentOnPost) || note.instance_of?(Notifications::AlsoCommented) || note.instance_of?(Notifications::Reshared) || note.instance_of?(Notifications::Liked) elsif note.instance_of?(Notifications::CommentOnPost) || note.instance_of?(Notifications::AlsoCommented) || note.instance_of?(Notifications::Reshared) || note.instance_of?(Notifications::Liked)
if post = note.linked_object if post = note.linked_object
translation(target_type, :actors => actors, :count => actors_count, :post_author => h(post.author_name), :post_link => link_to(t('notifications.post'), post_path(post), 'data-ref' => post.id, :class => 'hard_object_link').html_safe) translation(target_type, :actors => actors, :count => actors_count, :post_author => h(post.author_name), :post_link => link_to(post_page_title(post), post_path(post), 'data-ref' => post.id, :class => 'hard_object_link').html_safe)
else else
t(note.deleted_translation_key, :actors => actors, :count => actors_count).html_safe t(note.deleted_translation_key, :actors => actors, :count => actors_count).html_safe
end end

View file

@ -91,4 +91,15 @@ private
def self.suppress_notification?(recipient, post) def self.suppress_notification?(recipient, post)
post.is_a?(Post) && recipient.is_shareable_hidden?(post) post.is_a?(Post) && recipient.is_shareable_hidden?(post)
end end
def self.types
{
"also_commented" => "Notifications::AlsoCommented",
"comment_on_post" => "Notifications::CommentOnPost",
"liked" => "Notifications::Liked",
"mentioned" => "Notifications::Mentioned",
"reshared" => "Notifications::Reshared",
"started_sharing" => "Notifications::StartedSharing"
}
end
end end

View file

@ -1,4 +1,4 @@
.notification_element{:data=>{:guid => n.id}, :class => (n.unread ? "unread" : "read")} .notification_element{:data=>{:guid => n.id, :type => (Notification.types.key(n.type) || '')}, :class => (n.unread ? "unread" : "read")}
= person_image_tag n.actors.first, :thumb_small = person_image_tag n.actors.first, :thumb_small
= notification_message_for(n) = notification_message_for(n)
%div %div

View file

@ -637,25 +637,25 @@ en:
one: "%{actors} sent you a message." one: "%{actors} sent you a message."
other: "%{actors} sent you a message." other: "%{actors} sent you a message."
comment_on_post: comment_on_post:
zero: "%{actors} commented on your %{post_link}." zero: "%{actors} commented on your post »%{post_link}«."
one: "%{actors} commented on your %{post_link}." one: "%{actors} commented on your post »%{post_link}«."
other: "%{actors} commented on your %{post_link}." other: "%{actors} commented on your post »%{post_link}«."
also_commented: also_commented:
zero: "%{actors} also commented on %{post_author}'s %{post_link}." zero: "%{actors} also commented on %{post_author}'s post »%{post_link}«."
one: "%{actors} also commented on %{post_author}'s %{post_link}." one: "%{actors} also commented on %{post_author}'s post »%{post_link}«."
other: "%{actors} also commented on %{post_author}'s %{post_link}." other: "%{actors} also commented on %{post_author}'s post »%{post_link}«."
mentioned: mentioned:
zero: "%{actors} have mentioned you in a %{post_link}." zero: "%{actors} have mentioned you in the post »%{post_link}«."
one: "%{actors} has mentioned you in a %{post_link}." one: "%{actors} has mentioned you in the post »%{post_link}«."
other: "%{actors} have mentioned you in a %{post_link}." other: "%{actors} have mentioned you in the »%{post_link}«."
liked: liked:
zero: "%{actors} have liked your %{post_link}." zero: "%{actors} have liked your post »%{post_link}«."
one: "%{actors} has liked your %{post_link}." one: "%{actors} has liked your post »%{post_link}«."
other: "%{actors} have liked your %{post_link}." other: "%{actors} have liked your post »%{post_link}«."
reshared: reshared:
zero: "%{actors} have reshared your %{post_link}." zero: "%{actors} have reshared your post »%{post_link}«."
one: "%{actors} has reshared your %{post_link}." one: "%{actors} has reshared your post »%{post_link}«."
other: "%{actors} have reshared your %{post_link}." other: "%{actors} have reshared your post »%{post_link}«."
post: "post" post: "post"
also_commented_deleted: also_commented_deleted:
zero: "%{actors} commented on a deleted post." zero: "%{actors} commented on a deleted post."
@ -675,8 +675,18 @@ en:
other: "%{actors} mentioned you in a deleted post." other: "%{actors} mentioned you in a deleted post."
index: index:
notifications: "Notifications" notifications: "Notifications"
mark_all_as_read: "Mark All as Read" mark_all_as_read: "Mark all as read"
mark_read: "Mark read"
mark_unread: "Mark unread" mark_unread: "Mark unread"
show_all: "show all"
show_unread: "show unread"
all_notifications: "All Notifications"
also_commented: "Also commented"
comment_on_post: "Comment on post"
liked: "Liked"
mentioned: "Mentioned"
reshared: "Reshared"
started_sharing: "Started sharing"
and_others: and_others:
zero: "and nobody else" zero: "and nobody else"
one: "and one more" one: "and one more"

View file

@ -98,6 +98,10 @@ en:
conversation: conversation:
participants: "Participants" participants: "Participants"
notifications:
mark_read: "Mark read"
mark_unread: "Mark unread"
stream: stream:
hide: "Hide" hide: "Hide"
public: "Public" public: "Public"

View file

@ -0,0 +1,19 @@
require 'spec_helper'
describe NotificationsController do
describe '#index' do
before do
sign_in :user, alice
@post = FactoryGirl.create(:status_message)
FactoryGirl.create(:notification, :recipient => alice, :target => @post)
get :read_all
FactoryGirl.create(:notification, :recipient => alice, :target => @post)
eve.share_with(alice.person, eve.aspects.first)
end
it "generates a jasmine fixture", :fixture => true do
get :index
save_fixture(html_for("body"), "notifications")
end
end
end

View file

@ -120,7 +120,21 @@ describe NotificationsController do
Nokogiri(response.body).css('.aspect_membership').should_not be_empty Nokogiri(response.body).css('.aspect_membership').should_not be_empty
end end
end
describe "filter notifications" do
it "supports filtering by notification type" do
eve.share_with(alice.person, eve.aspects.first)
get :index, "type" => "started_sharing"
assigns[:notifications].count.should == 1
end
it "supports filtering by read/unread" do
get :read_all
2.times { FactoryGirl.create(:notification, :recipient => alice, :target => @post) }
get :index, "show" => "unread"
assigns[:notifications].count.should == 2
end
end end
end end
end end

View file

@ -77,7 +77,7 @@ describe NotificationsHelper do
output.should include I18n.t("#{@notification.popup_translation_key}", output.should include I18n.t("#{@notification.popup_translation_key}",
:actors => notification_people_link(@notification), :actors => notification_people_link(@notification),
:count => @notification.actors.count, :count => @notification.actors.count,
:post_link => "<a href=\"#{post_path(@post)}\" class=\"hard_object_link\" data-ref=\"#{@post.id}\">#{t('notifications.post')}</a>") :post_link => link_to(post_page_title(@post), post_path(@post), 'data-ref' => @post.id, :class => 'hard_object_link').html_safe)
end end
context 'when post is deleted' do context 'when post is deleted' do