Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
Badge Based Access - Addressed comments from PR
Browse files Browse the repository at this point in the history
See: #33
  • Loading branch information
ryanerwin committed Sep 21, 2018
1 parent 945a5e6 commit db16e04
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{{#if siteSettings.quick_message_enabled }}
{{#if siteSettings.quick_message_user_preference }}
<div class="controls">
<label class="checkbox-label">
{{input type="checkbox" checked=model.custom_fields.show_quick_messages}}
{{i18n "user_preferences.quick_messages_preference"}}
</label>
</div>
{{/if}}
{{/if}}
<div class="controls">
<label class="checkbox-label">
{{input type="checkbox" checked=model.custom_fields.show_quick_messages}}
{{i18n "user_preferences.quick_messages_preference"}}
</label>
</div>
1 change: 1 addition & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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. <a href='http://fontawesome.io/cheatsheet/'>Font Awesome Cheatsheet</a>."
4 changes: 4 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ plugins:
quick_message_icon:
default: 'envelope'
client: true
quick_message_required_badge:
default: 0
client: true
enum: SettingQuickMessagesBadge
21 changes: 21 additions & 0 deletions lib/setting_quick_messages_badge.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 15 additions & 3 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit db16e04

Please sign in to comment.