mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 17:02:45 +08:00
Merge pull request #2874 from cpradio/clear-notifications
FEATURE: Mark All as Read button for Notifications page
This commit is contained in:
commit
f3a67a48a3
|
@ -1,9 +1,20 @@
|
|||
|
||||
export default Ember.ArrayController.extend({
|
||||
needs: ['user-notifications'],
|
||||
canLoadMore: true,
|
||||
loading: false,
|
||||
showDismissButton: function() {
|
||||
return this.get('user').total_unread_notifications > 0;
|
||||
}.property('user'),
|
||||
|
||||
actions: {
|
||||
resetNew: function() {
|
||||
var self = this;
|
||||
Discourse.NotificationContainer.resetNew().then(function() {
|
||||
self.get('controllers.user-notifications').setEach('read', true);
|
||||
});
|
||||
},
|
||||
|
||||
loadMore: function() {
|
||||
if (this.get('canLoadMore') && !this.get('loading')) {
|
||||
this.set('loading', true);
|
||||
|
|
|
@ -46,5 +46,9 @@ Discourse.NotificationContainer.reopenClass({
|
|||
}).catch(function(error) {
|
||||
return Discourse.NotificationContainer.createFromError(error);
|
||||
});
|
||||
},
|
||||
|
||||
resetNew: function() {
|
||||
return Discourse.ajax("/notifications/reset-new", {type: 'PUT'});
|
||||
}
|
||||
});
|
||||
|
|
|
@ -7,6 +7,11 @@
|
|||
{{/if}}
|
||||
</div>
|
||||
{{/if}}
|
||||
{{#if showDismissButton}}
|
||||
<div class='notification-buttons'>
|
||||
<button title="{{i18n user.dismiss_notifications_tooltip}}" id='dismiss-notifications-top' class='btn notifications-read' {{action "resetNew"}}>{{i18n user.dismiss_notifications}}</button>
|
||||
</div>
|
||||
{{/if}}
|
||||
|
||||
{{#each itemController="notification"}}
|
||||
<div {{bind-attr class=":item :notification read::unread"}}>
|
||||
|
@ -16,9 +21,15 @@
|
|||
</span>
|
||||
</div>
|
||||
{{/each}}
|
||||
|
||||
{{#if loading}}
|
||||
<div class='spinner'></div>
|
||||
{{/if}}
|
||||
{{#unless canLoadMore}}
|
||||
<div class='end-of-stream'></div>
|
||||
{{#if showDismissButton}}
|
||||
<div class='notification-buttons'>
|
||||
<button title="{{i18n user.dismiss_notifications_tooltip}}" id='dismiss-notifications' class='btn notifications-read' {{action "resetNew"}}>{{i18n user.dismiss_notifications}}</button>
|
||||
</div>
|
||||
{{/if}}
|
||||
{{/unless}}
|
||||
|
|
|
@ -29,6 +29,11 @@
|
|||
width: 100%;
|
||||
}
|
||||
|
||||
.notification-buttons {
|
||||
margin: 10px 0;
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.user-navigation {
|
||||
|
||||
.map {
|
||||
|
|
|
@ -37,4 +37,21 @@ class NotificationsController < ApplicationController
|
|||
|
||||
render_serialized(notifications, NotificationSerializer)
|
||||
end
|
||||
|
||||
def reset_new
|
||||
params.permit(:user)
|
||||
|
||||
user = current_user
|
||||
if params[:user]
|
||||
user = User.find_by_username(params[:user].to_s)
|
||||
end
|
||||
|
||||
Notification.where(user_id: user.id).includes(:topic).where(read: false).update_all(read: true)
|
||||
|
||||
current_user.saw_notification_id(Notification.recent_report(current_user, 1).max)
|
||||
current_user.reload
|
||||
current_user.publish_notifications_state
|
||||
|
||||
render nothing: true
|
||||
end
|
||||
end
|
||||
|
|
|
@ -242,6 +242,7 @@ class User < ActiveRecord::Base
|
|||
|
||||
def reload
|
||||
@unread_notifications_by_type = nil
|
||||
@unread_total_notifications = nil
|
||||
@unread_pms = nil
|
||||
super
|
||||
end
|
||||
|
@ -254,6 +255,10 @@ class User < ActiveRecord::Base
|
|||
unread_notifications_by_type.except(Notification.types[:private_message]).values.sum
|
||||
end
|
||||
|
||||
def total_unread_notifications
|
||||
@unread_total_notifications ||= notifications.where("read = false").count
|
||||
end
|
||||
|
||||
def saw_notification_id(notification_id)
|
||||
User.where("id = ? and seen_notification_id < ?", id, notification_id)
|
||||
.update_all ["seen_notification_id = ?", notification_id]
|
||||
|
@ -266,7 +271,8 @@ class User < ActiveRecord::Base
|
|||
def publish_notifications_state
|
||||
MessageBus.publish("/notification/#{id}",
|
||||
{unread_notifications: unread_notifications,
|
||||
unread_private_messages: unread_private_messages},
|
||||
unread_private_messages: unread_private_messages,
|
||||
total_unread_notifications: total_unread_notifications},
|
||||
user_ids: [id] # only publish the notification to this user
|
||||
)
|
||||
end
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
class CurrentUserSerializer < BasicUserSerializer
|
||||
|
||||
attributes :name,
|
||||
:total_unread_notifications,
|
||||
:unread_notifications,
|
||||
:unread_private_messages,
|
||||
:admin?,
|
||||
|
|
|
@ -295,6 +295,8 @@ en:
|
|||
invited_by: "Invited By"
|
||||
trust_level: "Trust Level"
|
||||
notifications: "Notifications"
|
||||
dismiss_notifications: "Mark all as Read"
|
||||
dismiss_notifications_tooltip: "Mark all unread notifications as read"
|
||||
disable_jump_reply: "Don't jump to your new post after replying"
|
||||
dynamic_favicon: "Show incoming message notifications on favicon (experimental)"
|
||||
edit_history_public: "Let other users view my post revisions"
|
||||
|
|
|
@ -291,6 +291,7 @@ Discourse::Application.routes.draw do
|
|||
|
||||
get "notifications" => "notifications#recent"
|
||||
get "notifications/history" => "notifications#history"
|
||||
put "notifications/reset-new" => 'notifications#reset_new'
|
||||
|
||||
match "/auth/:provider/callback", to: "users/omniauth_callbacks#complete", via: [:get, :post]
|
||||
match "/auth/failure", to: "users/omniauth_callbacks#failure", via: [:get, :post]
|
||||
|
|
|
@ -15,18 +15,37 @@ describe NotificationsController do
|
|||
response.should be_success
|
||||
end
|
||||
|
||||
it 'should succeed for history' do
|
||||
xhr :get, :reset_new
|
||||
response.should be_success
|
||||
end
|
||||
|
||||
it 'should mark notifications as viewed' do
|
||||
notification = Fabricate(:notification, user: user)
|
||||
user.reload.unread_notifications.should == 1
|
||||
user.reload.total_unread_notifications.should == 1
|
||||
xhr :get, :recent
|
||||
user.reload.unread_notifications.should == 0
|
||||
user.reload.total_unread_notifications.should == 1
|
||||
end
|
||||
|
||||
it 'should not mark notifications as viewed if silent param is present' do
|
||||
notification = Fabricate(:notification, user: user)
|
||||
user.reload.unread_notifications.should == 1
|
||||
user.reload.total_unread_notifications.should == 1
|
||||
xhr :get, :recent, silent: true
|
||||
user.reload.unread_notifications.should == 1
|
||||
user.reload.total_unread_notifications.should == 1
|
||||
end
|
||||
|
||||
it "updates the `read` status" do
|
||||
notification = Fabricate(:notification, user: user)
|
||||
user.reload.unread_notifications.should == 1
|
||||
user.reload.total_unread_notifications.should == 1
|
||||
xhr :put, :reset_new
|
||||
user.reload
|
||||
user.reload.unread_notifications.should == 0
|
||||
user.reload.total_unread_notifications.should == 0
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -78,6 +78,10 @@ describe Notification do
|
|||
lambda { Fabricate(:notification, user: user); user.reload }.should change(user, :unread_notifications)
|
||||
end
|
||||
|
||||
it 'increases total_unread_notifications' do
|
||||
lambda { Fabricate(:notification, user: user); user.reload }.should change(user, :total_unread_notifications)
|
||||
end
|
||||
|
||||
it "doesn't increase unread_private_messages" do
|
||||
lambda { Fabricate(:notification, user: user); user.reload }.should_not change(user, :unread_private_messages)
|
||||
end
|
||||
|
@ -88,6 +92,10 @@ describe Notification do
|
|||
lambda { Fabricate(:private_message_notification, user: user); user.reload }.should_not change(user, :unread_notifications)
|
||||
end
|
||||
|
||||
it 'increases total_unread_notifications' do
|
||||
lambda { Fabricate(:notification, user: user); user.reload }.should change(user, :total_unread_notifications)
|
||||
end
|
||||
|
||||
it "increases unread_private_messages" do
|
||||
lambda { Fabricate(:private_message_notification, user: user); user.reload }.should change(user, :unread_private_messages)
|
||||
end
|
||||
|
@ -141,6 +149,7 @@ describe Notification do
|
|||
it 'should create and rollup private message notifications' do
|
||||
@target.notifications.first.notification_type.should == Notification.types[:private_message]
|
||||
@post.user.unread_notifications.should == 0
|
||||
@post.user.total_unread_notifications.should == 0
|
||||
@target.unread_private_messages.should == 1
|
||||
|
||||
Fabricate(:post, topic: @topic, user: @topic.user)
|
||||
|
@ -197,6 +206,7 @@ describe Notification do
|
|||
user.reload
|
||||
|
||||
user.unread_notifications.should == 0
|
||||
user.total_unread_notifications.should == 2
|
||||
user.unread_private_messages.should == 1
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user