From db16e04634465536c831c81d8a43c6359ed731a6 Mon Sep 17 00:00:00 2001 From: Ryan Erwin Date: Fri, 21 Sep 2018 14:36:41 +0700 Subject: [PATCH] Badge Based Access - Addressed comments from PR See: https://github.com/angusmcleod/discourse-quick-messages/issues/33 --- .../show-quick-messages.js.es6 | 7 +++++++ .../show-quick-messages.hbs | 16 ++++++-------- config/locales/server.en.yml | 1 + config/settings.yml | 4 ++++ lib/setting_quick_messages_badge.rb | 21 +++++++++++++++++++ plugin.rb | 18 +++++++++++++--- 6 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 assets/javascripts/discourse/connectors/user-preferences-interface/show-quick-messages.js.es6 create mode 100644 lib/setting_quick_messages_badge.rb diff --git a/assets/javascripts/discourse/connectors/user-preferences-interface/show-quick-messages.js.es6 b/assets/javascripts/discourse/connectors/user-preferences-interface/show-quick-messages.js.es6 new file mode 100644 index 0000000..a4925ab --- /dev/null +++ b/assets/javascripts/discourse/connectors/user-preferences-interface/show-quick-messages.js.es6 @@ -0,0 +1,7 @@ +export default { + shouldRender(args, component) { + return Discourse.SiteSettings.quick_messages_enabled && + Discourse.SiteSettings.quick_message_user_preference && + args.model.quick_messages_access + } +} \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/connectors/user-preferences-interface/show-quick-messages.hbs b/assets/javascripts/discourse/templates/connectors/user-preferences-interface/show-quick-messages.hbs index 7d03aaf..0004db8 100644 --- a/assets/javascripts/discourse/templates/connectors/user-preferences-interface/show-quick-messages.hbs +++ b/assets/javascripts/discourse/templates/connectors/user-preferences-interface/show-quick-messages.hbs @@ -1,10 +1,6 @@ -{{#if siteSettings.quick_message_enabled }} - {{#if siteSettings.quick_message_user_preference }} -
- -
- {{/if}} -{{/if}} \ No newline at end of file +
+ +
\ No newline at end of file diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index fbcbdc4..5fcc56d 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -2,6 +2,7 @@ en: site_settings: quick_message_enabled: "Enable Quick Messages." quick_message_user_preference: "Messages menu is enabled according to user interface setting 'Enable messages menu in header'." + quick_message_required_badge: "Require a badge to access Quick Messages, for example require 'Certified' to reward users who completed the new user tutorial" quick_message_rate_limit_create: "After posting, users must wait (n) seconds before creating another quick message." quick_message_min_post_length: "Minimum allowed quick message length in characters." quick_message_icon: "Font Awesome name of Quick Messages icon. Font Awesome Cheatsheet." diff --git a/config/settings.yml b/config/settings.yml index 0c444cf..89a32e8 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -14,3 +14,7 @@ plugins: quick_message_icon: default: 'envelope' client: true + quick_message_required_badge: + default: 0 + client: true + enum: SettingQuickMessagesBadge \ No newline at end of file diff --git a/lib/setting_quick_messages_badge.rb b/lib/setting_quick_messages_badge.rb new file mode 100644 index 0000000..de54533 --- /dev/null +++ b/lib/setting_quick_messages_badge.rb @@ -0,0 +1,21 @@ +require_dependency 'enum_site_setting' + +# Adds an Enum type used in the Admin to select a badge by name. +# Examples see: https://github.com/discourse/discourse/blob/master/config/site_settings.yml + +class SettingQuickMessagesBadge < EnumSiteSetting + def self.valid_value?(any) + true + end + + def self.values + @values ||= begin + @values = Badge.where(enabled: true).map{ |b| { name: b.name, value: b.id } } + @values.unshift(name: 'None', value: 0) + end + end + + def self.translate_names? + false + end +end \ No newline at end of file diff --git a/plugin.rb b/plugin.rb index d39d8e5..d0aa7ac 100644 --- a/plugin.rb +++ b/plugin.rb @@ -6,6 +6,7 @@ register_asset 'stylesheets/common/quick_menu.scss' register_asset 'stylesheets/common/quick_composer.scss' register_asset 'stylesheets/mobile/quick_mobile.scss', :mobile +require_relative 'lib/setting_quick_messages_badge' after_initialize do @@ -59,17 +60,28 @@ def limit_posts_per_day end end - User.class_eval do + class ::User def show_quick_messages if SiteSetting.quick_message_enabled if SiteSetting.quick_message_user_preference if ActiveModel::Type::Boolean.new.cast(custom_fields['show_quick_messages']) - return true + # return true - would have called self.quick_messages_access + if SiteSetting.quick_message_required_badge > 0 + return self.badge_ids.include? (SiteSetting.quick_message_required_badge) + else + return true + end else return false end else - return true + # return true - would have called self.quick_messages_access + # Badge Access is enabed, but the user preference is not + if SiteSetting.quick_message_required_badge > 0 + return self.badge_ids.include? (SiteSetting.quick_message_required_badge) + else + return true + end end else return false