From f9cd354a2c2ba91604bf487f79fac6417884e251 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 3 Mar 2014 15:46:38 -0500 Subject: [PATCH] FEATURE: Button to reset new --- .../discovery_topics_controller.js | 13 ++++++++++++ .../javascripts/discourse/models/topic.js | 5 +++++ .../discourse/models/topic_tracking_state.js | 9 ++++++++ .../templates/discovery/topics.js.handlebars | 3 +++ app/controllers/topics_controller.rb | 8 ++++++- config/locales/client.en.yml | 1 + config/routes.rb | 1 + spec/controllers/topics_controller_spec.rb | 21 +++++++++++++++++++ 8 files changed, 60 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/controllers/discovery_topics_controller.js b/app/assets/javascripts/discourse/controllers/discovery_topics_controller.js index 7cd2577ca3d..3f9e8b5001f 100644 --- a/app/assets/javascripts/discourse/controllers/discovery_topics_controller.js +++ b/app/assets/javascripts/discourse/controllers/discovery_topics_controller.js @@ -43,6 +43,15 @@ Discourse.DiscoveryTopicsController = Discourse.DiscoveryController.extend({ this.get('selected').clear(); }, + resetNew: function() { + var self = this; + + Discourse.TopicTrackingState.current().resetNew(); + Discourse.Topic.resetNew().then(function() { + self.send('refresh'); + }); + }, + dismissRead: function() { var self = this, selected = this.get('selected'), @@ -68,6 +77,10 @@ Discourse.DiscoveryTopicsController = Discourse.DiscoveryController.extend({ return this.get('filter') === 'unread' && this.get('topics.length') > 0; }.property('filter', 'topics.length'), + showResetNew: function() { + return this.get('filter') === 'new' && this.get('topics.length') > 0; + }.property('filter', 'topics.length'), + canBulkSelect: Em.computed.alias('currentUser.staff'), hasTopics: Em.computed.gt('topics.length', 0), showTable: Em.computed.or('hasTopics', 'topicTrackingState.hasIncoming'), diff --git a/app/assets/javascripts/discourse/models/topic.js b/app/assets/javascripts/discourse/models/topic.js index badaca103c6..b55c6e3d97e 100644 --- a/app/assets/javascripts/discourse/models/topic.js +++ b/app/assets/javascripts/discourse/models/topic.js @@ -428,8 +428,13 @@ Discourse.Topic.reopenClass({ type: 'PUT', data: { filter: filter, operation: operation } }); + }, + + resetNew: function() { + return Discourse.ajax("/topics/reset-new", {type: 'PUT'}); } + }); diff --git a/app/assets/javascripts/discourse/models/topic_tracking_state.js b/app/assets/javascripts/discourse/models/topic_tracking_state.js index 5e01c92a80a..f1283bba9fb 100644 --- a/app/assets/javascripts/discourse/models/topic_tracking_state.js +++ b/app/assets/javascripts/discourse/models/topic_tracking_state.js @@ -145,6 +145,15 @@ Discourse.TopicTrackingState = Discourse.Model.extend({ .length; }, + resetNew: function() { + var self = this; + Object.keys(this.states).forEach(function (id) { + if (self.states[id].last_read_post_number === null) { + delete self.states[id]; + } + }); + }, + countUnread: function(category_name){ return _.chain(this.states) .where(function(topic){ diff --git a/app/assets/javascripts/discourse/templates/discovery/topics.js.handlebars b/app/assets/javascripts/discourse/templates/discovery/topics.js.handlebars index 03f59009a79..6ad8a8a1603 100644 --- a/app/assets/javascripts/discourse/templates/discovery/topics.js.handlebars +++ b/app/assets/javascripts/discourse/templates/discovery/topics.js.handlebars @@ -68,6 +68,9 @@ {{#if showDismissRead}} {{/if}} + {{#if showResetNew}} + + {{/if}}

{{#if latest}} diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index cbb9011cbed..2a76e8e6b53 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -21,7 +21,8 @@ class TopicsController < ApplicationController :merge_topic, :clear_pin, :autoclose, - :bulk] + :bulk, + :reset_new] before_filter :consider_user_for_promotion, only: :show @@ -281,6 +282,11 @@ class TopicsController < ApplicationController render_json_dump topic_ids: changed_topic_ids end + def reset_new + current_user.user_stat.update_column(:new_since, Time.now) + render nothing: true + end + private def toggle_mute diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index e4ce9652e46..ab9ac2d92a8 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -630,6 +630,7 @@ en: bulk: reset_read: "Reset Read" dismiss_read: "Dismiss Read" + mark_seen: "Mark Seen" toggle: "toggle bulk selection of topics" actions: "Bulk Actions" change_category: "Change Category" diff --git a/config/routes.rb b/config/routes.rb index d42072a6e37..abae9ba6750 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -281,6 +281,7 @@ Discourse::Application.routes.draw do put "t/:id" => "topics#update" delete "t/:id" => "topics#destroy" put "topics/bulk" + put "topics/reset-new" => 'topics#reset_new' post "topics/timings" get "topics/similar_to" get "topics/created-by/:username" => "list#topics_by", as: "topics_by", constraints: {username: USERNAME_ROUTE_FORMAT} diff --git a/spec/controllers/topics_controller_spec.rb b/spec/controllers/topics_controller_spec.rb index 6de216c2c22..a4a59bb93e3 100644 --- a/spec/controllers/topics_controller_spec.rb +++ b/spec/controllers/topics_controller_spec.rb @@ -811,4 +811,25 @@ describe TopicsController do end end end + + + describe 'reset_new' do + it 'needs you to be logged in' do + lambda { xhr :put, :reset_new }.should raise_error(Discourse::NotLoggedIn) + end + + let(:user) { log_in(:user) } + + it "updates the `new_since` date" do + old_date = 2.years.ago + + user.user_stat.update_column(:new_since, old_date) + + xhr :put, :reset_new + user.reload + user.user_stat.new_since.to_date.should_not == old_date.to_date + + end + + end end