diff --git a/app/assets/javascripts/admin/controllers/admin-group.js.es6 b/app/assets/javascripts/admin/controllers/admin-group.js.es6 index 6c3da11d7f7..a0f4961b62f 100644 --- a/app/assets/javascripts/admin/controllers/admin-group.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-group.js.es6 @@ -22,9 +22,9 @@ export default Ember.Controller.extend({ ]; }.property(), - @computed('model.visible', 'model.public', 'model.alias_level') + @computed('model.visible', 'model.public') disableMembershipRequestSetting(visible, publicGroup) { - return !visible || publicGroup || !this.get('model.canEveryoneMention'); + return !visible || publicGroup; }, @computed('model.visible', 'model.allow_membership_requests') diff --git a/app/assets/javascripts/admin/templates/group.hbs b/app/assets/javascripts/admin/templates/group.hbs index 095b3f5f4b8..79dbcfc3b64 100644 --- a/app/assets/javascripts/admin/templates/group.hbs +++ b/app/assets/javascripts/admin/templates/group.hbs @@ -83,6 +83,12 @@ {{combo-box name="alias" valueAttribute="value" value=model.alias_level content=aliasLevelOptions}} +
+ + {{notifications-button i18nPrefix='groups.notifications' notificationLevel=model.default_notification_level}} +
+
+ {{#unless model.automatic}}
diff --git a/app/assets/javascripts/discourse/components/group-notifications-button.js.es6 b/app/assets/javascripts/discourse/components/group-notifications-button.js.es6 index 8f70ca102a3..ef2386086f6 100644 --- a/app/assets/javascripts/discourse/components/group-notifications-button.js.es6 +++ b/app/assets/javascripts/discourse/components/group-notifications-button.js.es6 @@ -6,6 +6,6 @@ export default NotificationsButton.extend({ i18nPrefix: 'groups.notifications', clicked(id) { - this.get('group').setNotification(id); + this.get('group').setNotification(id, this.get('user.id')); } }); diff --git a/app/assets/javascripts/discourse/components/notifications-button.js.es6 b/app/assets/javascripts/discourse/components/notifications-button.js.es6 index 10f5d54f26e..d01f17e43d5 100644 --- a/app/assets/javascripts/discourse/components/notifications-button.js.es6 +++ b/app/assets/javascripts/discourse/components/notifications-button.js.es6 @@ -43,7 +43,7 @@ export default DropdownButton.extend({ } }, - clicked(/* id */) { - // sub-class needs to implement this + clicked(id) { + this.set("notificationLevel", id); } }); diff --git a/app/assets/javascripts/discourse/models/group.js.es6 b/app/assets/javascripts/discourse/models/group.js.es6 index 7d1ab55e706..2af528f8187 100644 --- a/app/assets/javascripts/discourse/models/group.js.es6 +++ b/app/assets/javascripts/discourse/models/group.js.es6 @@ -139,7 +139,8 @@ const Group = RestModel.extend({ bio_raw: this.get('bio_raw'), public: this.get('public'), allow_membership_requests: this.get('allow_membership_requests'), - full_name: this.get('full_name') + full_name: this.get('full_name'), + default_notification_level: this.get('default_notification_level') }; }, @@ -191,10 +192,10 @@ const Group = RestModel.extend({ }); }, - setNotification(notification_level) { + setNotification(notification_level, userId) { this.set("group_user.notification_level", notification_level); return ajax(`/groups/${this.get("name")}/notifications`, { - data: { notification_level }, + data: { notification_level, user_id: userId }, type: "POST" }); } diff --git a/app/assets/javascripts/discourse/templates/user/messages.hbs b/app/assets/javascripts/discourse/templates/user/messages.hbs index d9be88452e1..ece0c407b7f 100644 --- a/app/assets/javascripts/discourse/templates/user/messages.hbs +++ b/app/assets/javascripts/discourse/templates/user/messages.hbs @@ -73,7 +73,7 @@ {{/if}} {{#if isGroup}} - {{group-notifications-button group=group}} + {{group-notifications-button group=group user=model}} {{/if}}
diff --git a/app/controllers/admin/groups_controller.rb b/app/controllers/admin/groups_controller.rb index d2a93629fc6..e963e506ed5 100644 --- a/app/controllers/admin/groups_controller.rb +++ b/app/controllers/admin/groups_controller.rb @@ -71,6 +71,10 @@ class Admin::GroupsController < Admin::AdminController group.bio_raw = group_params[:bio_raw] if group_params[:bio_raw] group.full_name = group_params[:full_name] if group_params[:full_name] + if group_params.key?(:default_notification_level) + group.default_notification_level = group_params[:default_notification_level] + end + if group_params[:allow_membership_requests] group.allow_membership_requests = group_params[:allow_membership_requests] end @@ -150,7 +154,8 @@ class Admin::GroupsController < Admin::AdminController :name, :alias_level, :visible, :automatic_membership_email_domains, :automatic_membership_retroactive, :title, :primary_group, :grant_trust_level, :incoming_email, :flair_url, :flair_bg_color, - :flair_color, :bio_raw, :public, :allow_membership_requests, :full_name + :flair_color, :bio_raw, :public, :allow_membership_requests, :full_name, + :default_notification_level ) end end diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 1314a763e2e..1a027838d77 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -245,8 +245,13 @@ class GroupsController < ApplicationController group = find_group(:id) notification_level = params.require(:notification_level) + user_id = current_user.id + if guardian.is_staff? + user_id = params[:user_id] || user_id + end + GroupUser.where(group_id: group.id) - .where(user_id: current_user.id) + .where(user_id: user_id) .update_all(notification_level: notification_level) render json: success_json diff --git a/app/models/color_scheme.rb b/app/models/color_scheme.rb index 4c4690e1620..6701f4cdf65 100644 --- a/app/models/color_scheme.rb +++ b/app/models/color_scheme.rb @@ -194,4 +194,5 @@ end # updated_at :datetime not null # via_wizard :boolean default(FALSE), not null # base_scheme_id :string +# theme_id :integer # diff --git a/app/models/group.rb b/app/models/group.rb index 74a4126b213..6d6a17d6342 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -552,6 +552,7 @@ end # public :boolean default(FALSE), not null # allow_membership_requests :boolean default(FALSE), not null # full_name :string +# default_notification_level :integer default(3), not null # # Indexes # diff --git a/app/models/group_user.rb b/app/models/group_user.rb index 8d1e3beba59..b5595803901 100644 --- a/app/models/group_user.rb +++ b/app/models/group_user.rb @@ -10,6 +10,7 @@ class GroupUser < ActiveRecord::Base after_save :set_primary_group after_destroy :remove_primary_group + before_create :set_notification_level after_save :grant_trust_level def self.notification_levels @@ -18,6 +19,10 @@ class GroupUser < ActiveRecord::Base protected + def set_notification_level + self.notification_level = group&.default_notification_level || 3 + end + def set_primary_group if group.primary_group self.class.exec_sql("UPDATE users diff --git a/app/models/theme_field.rb b/app/models/theme_field.rb index 3c67c589458..9f80dd1c567 100644 --- a/app/models/theme_field.rb +++ b/app/models/theme_field.rb @@ -137,6 +137,7 @@ end # created_at :datetime # updated_at :datetime # compiler_version :integer default(0), not null +# error :string # # Indexes # diff --git a/app/models/user_warning.rb b/app/models/user_warning.rb index 6bc201a7bc8..dd89c7f9957 100644 --- a/app/models/user_warning.rb +++ b/app/models/user_warning.rb @@ -6,7 +6,7 @@ end # == Schema Information # -# Table name: warnings +# Table name: user_warnings # # id :integer not null, primary key # topic_id :integer not null @@ -17,6 +17,6 @@ end # # Indexes # -# index_warnings_on_topic_id (topic_id) UNIQUE -# index_warnings_on_user_id (user_id) +# index_user_warnings_on_topic_id (topic_id) UNIQUE +# index_user_warnings_on_user_id (user_id) # diff --git a/app/serializers/basic_group_serializer.rb b/app/serializers/basic_group_serializer.rb index b37184e7232..ea3df8e08ae 100644 --- a/app/serializers/basic_group_serializer.rb +++ b/app/serializers/basic_group_serializer.rb @@ -19,7 +19,8 @@ class BasicGroupSerializer < ApplicationSerializer :bio_cooked, :public, :allow_membership_requests, - :full_name + :full_name, + :default_notification_level def include_incoming_email? staff? diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 8c9540977dd..98265b0275a 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -446,6 +446,7 @@ en: posts: "Posts" mentions: "Mentions" messages: "Messages" + notification_level: "Default notification level for group messages" alias_levels: title: "Who can message and @mention this group?" nobody: "Nobody" diff --git a/db/migrate/20170420163628_add_default_notification_level_to_group.rb b/db/migrate/20170420163628_add_default_notification_level_to_group.rb new file mode 100644 index 00000000000..dee6ae771e8 --- /dev/null +++ b/db/migrate/20170420163628_add_default_notification_level_to_group.rb @@ -0,0 +1,11 @@ +class AddDefaultNotificationLevelToGroup < ActiveRecord::Migration + def up + add_column :groups, :default_notification_level, :integer, default: 3, null: false + # don't auto watch 'moderators' it is just way too loud + execute 'UPDATE groups SET default_notification_level = 2 WHERE id = 2' + end + + def down + remove_column :groups, :default_notification_level + end +end diff --git a/spec/models/group_user_spec.rb b/spec/models/group_user_spec.rb new file mode 100644 index 00000000000..c7675984cf1 --- /dev/null +++ b/spec/models/group_user_spec.rb @@ -0,0 +1,33 @@ +require 'rails_helper' + +describe GroupUser do + + it 'correctly sets notification level' do + moderator = Fabricate(:moderator) + + Group.refresh_automatic_groups!(:moderators) + gu = GroupUser.find_by(user_id: moderator.id, group_id: Group::AUTO_GROUPS[:moderators]) + + expect(gu.notification_level).to eq(NotificationLevels.all[:tracking]) + + group = Group.create!(name: 'bob') + group.add(moderator) + group.save + + gu = GroupUser.find_by(user_id: moderator.id, group_id: group.id) + expect(gu.notification_level).to eq(NotificationLevels.all[:watching]) + + group.remove(moderator) + group.save + + group.default_notification_level = 1 + group.save + + group.add(moderator) + group.save + + gu = GroupUser.find_by(user_id: moderator.id, group_id: group.id) + expect(gu.notification_level).to eq(NotificationLevels.all[:regular]) + end + +end