FEATURE: the notice asking admins to get discussion started will update with live counts of topics and posts

This commit is contained in:
Neil Lalonde 2015-08-27 17:28:33 -04:00
parent 42e510753d
commit cc2dc4d550
8 changed files with 112 additions and 16 deletions

View File

@ -0,0 +1,78 @@
import computed from 'ember-addons/ember-computed-decorators';
import { observes } from 'ember-addons/ember-computed-decorators';
import LivePostCounts from 'discourse/models/live-post-counts';
export default Ember.Component.extend({
classNameBindings: ['hidden:hidden',':create-topics-notice'],
enabled: false,
publicTopicCount: null,
publicPostCount: null,
requiredTopics: 5,
requiredPosts: Ember.computed.alias('siteSettings.tl1_requires_read_posts'),
init() {
this._super();
if (this.get('shouldSee')) {
let topicCount = 0,
postCount = 0;
// Use data we already have before fetching live stats
_.each(this.site.get('categories'), function(c) {
if (!c.get('read_restricted')) {
topicCount += c.get('topic_count');
postCount += c.get('post_count');
}
});
if (topicCount < this.get('requiredTopics') || postCount < this.get('requiredPosts')) {
this.set('enabled', true);
this.fetchLiveStats();
}
}
},
@computed()
shouldSee() {
return Discourse.User.currentProp('admin') && this.siteSettings.show_create_topics_notice;
},
@computed('enabled', 'shouldSee', 'publicTopicCount', 'publicPostCount')
hidden() {
return !this.get('enabled') || !this.get('shouldSee') || this.get('publicTopicCount') == null || this.get('publicPostCount') == null;
},
@computed('publicTopicCount', 'publicPostCount', 'topicTrackingState.incomingCount')
message() {
return new Handlebars.SafeString(I18n.t('too_few_topics_notice', {
requiredTopics: this.get('requiredTopics'),
requiredPosts: this.get('requiredPosts'),
currentTopics: this.get('publicTopicCount'),
currentPosts: this.get('publicPostCount')
}));
},
@computed()
topicTrackingState() {
return Discourse.TopicTrackingState.current();
},
@observes('topicTrackingState.incomingCount')
fetchLiveStats() {
if (!this.get('enabled')) { return; }
var self = this;
LivePostCounts.find().then(function(stats) {
if(stats) {
self.set('publicTopicCount', stats.get('public_topic_count'));
self.set('publicPostCount', stats.get('public_post_count'));
if (self.get('publicTopicCount') >= self.get('requiredTopics')
&& self.get('publicPostCount') >= self.get('requiredPosts')) {
self.set('enabled', false); // No more checks
}
}
});
}
});

View File

@ -14,20 +14,6 @@ export default Ember.Component.extend(StringBuffer, {
notices.push([I18n.t("emails_are_disabled"), 'alert-emails-disabled']);
}
if (Discourse.User.currentProp('admin') && this.siteSettings.show_create_topics_notice) {
let topic_count = 0,
post_count = 0;
_.each(this.site.get('categories'), function(c) {
if (!c.get('read_restricted')) {
topic_count += c.get('topic_count');
post_count += c.get('post_count');
}
});
if (topic_count < 5 || post_count < this.siteSettings.tl1_requires_read_posts) {
notices.push([I18n.t("too_few_topics_notice", { posts: this.siteSettings.tl1_requires_read_posts }), 'alert-too-few-topics']);
}
}
if (!_.isEmpty(this.siteSettings.global_notice)) {
notices.push([this.siteSettings.global_notice, 'alert-global-notice']);
}

View File

@ -0,0 +1,9 @@
const LivePostCounts = Discourse.Model.extend({});
LivePostCounts.reopenClass({
find() {
return Discourse.ajax("/about/live_post_counts.json").then(result => LivePostCounts.create(result));
}
});
export default LivePostCounts;

View File

@ -6,6 +6,7 @@
{{custom-html "top"}}
{{/if}}
{{global-notice}}
{{create-topics-notice}}
</div>
{{outlet}}
{{render "user-card"}}

View File

@ -0,0 +1,7 @@
{{#unless hidden}}
<div class='row'>
<div class='alert alert-info alert-too-few-topics'>
{{message}}
</div>
</div>
{{/unless}}

View File

@ -1,3 +1,5 @@
require_dependency 'rate_limiter'
class AboutController < ApplicationController
skip_before_filter :check_xhr, only: [:show]
@ -5,4 +7,13 @@ class AboutController < ApplicationController
@about = About.new
render_serialized(@about, AboutSerializer)
end
def live_post_counts
RateLimiter.new(current_user, "live_post_counts", 1, 10.minutes).performed! unless current_user.staff?
category_topic_ids = Category.pluck(:topic_id).compact!
public_topics = Topic.listable_topics.visible.secured(Guardian.new(nil)).where.not(id: category_topic_ids)
stats = { public_topic_count: public_topics.count }
stats[:public_post_count] = public_topics.sum(:posts_count) - stats[:public_topic_count]
render json: stats
end
end

View File

@ -686,7 +686,7 @@ en:
read_only_mode:
enabled: "Read-only mode is enabled. You can continue to browse the site but interactions may not work."
login_disabled: "Login is disabled while the site is in read only mode."
too_few_topics_notice: "Create at least 5 public topics and %{posts} public replies to get discussion started. New users cannot earn trust levels unless there's content for them to read. This message appears only to staff."
too_few_topics_notice: "Get discussion started by creating %{requiredTopics} public topics and %{requiredPosts} posts. There are currently <strong>%{currentTopics} / %{requiredTopics}</strong> topics and <strong>%{currentPosts} / %{requiredPosts}</strong> posts. New users cannot earn trust levels unless there's content for them to read. This message appears only to staff."
learn_more: "learn more..."

View File

@ -23,7 +23,11 @@ Discourse::Application.routes.draw do
mount Logster::Web => "/logs", constraints: AdminConstraint.new
end
resources :about
resources :about do
collection do
get "live_post_counts"
end
end
resources :directory_items