A platform for community discussion. Free, open, simple.
FEATURE: Introduce a staff only override key for discourse-presence.
Guo Xiang Tan623920f13d85
diff --git a/plugins/discourse-presence/assets/javascripts/discourse/components/composer-presence-display.js.es6 b/plugins/discourse-presence/assets/javascripts/discourse/components/composer-presence-display.js.es6index c896be345a6..a01c982471e 100644--- a/plugins/discourse-presence/assets/javascripts/discourse/components/composer-presence-display.js.es6+++ b/plugins/discourse-presence/assets/javascripts/discourse/components/composer-presence-display.js.es6@@ -17,36 +17,30 @@ import { REPLY, EDIT } from "discourse/models/composer";export default Component.extend({// Passed in variables- action: null,- post: null,- topic: null,- reply: null,- title: null,- isWhispering: null,presenceManager: service(),- @discourseComputed("topic.id")+ @discourseComputed("model.topic.id")users(topicId) {return this.presenceManager.users(topicId);},- @discourseComputed("topic.id")+ @discourseComputed("model.topic.id")editingUsers(topicId) {return this.presenceManager.editingUsers(topicId);},- isReply: equal("action", REPLY),+ isReply: equal("model.action", REPLY),@on("didInsertElement")subscribe() {- this.presenceManager.subscribe(this.get("topic.id"), COMPOSER_TYPE);+ this.presenceManager.subscribe(this.get("model.topic.id"), COMPOSER_TYPE);},@discourseComputed(- "post.id",+ "model.post.id","[email protected]_seen","[email protected]_seen",- "action"+ "model.action")presenceUsers(postId, editingUsers, users, action) {if (action === EDIT) {@@ -59,23 +53,24 @@ export default Component.extend({shouldDisplay: gt("presenceUsers.length", 0),- @observes("reply", "title")+ @observes("model.reply", "model.title")typing() {throttle(this, this._typing, KEEP_ALIVE_DURATION_SECONDS * 1000);},_typing() {- const action = this.action;+ const action = this.get("model.action");if (action !== REPLY && action !== EDIT) {return;}let data = {- topicId: this.get("topic.id"),+ topicId: this.get("model.topic.id"),state: action === EDIT ? EDITING : REPLYING,- whisper: this.whisper,- postId: this.get("post.id")+ whisper: this.get("model.whisper"),+ postId: this.get("model.post.id"),+ presenceStaffOnly: this.get("model._presenceStaffOnly")};this._prevPublishData = data;@@ -84,16 +79,17 @@ export default Component.extend({data.topicId,data.state,data.whisper,- data.postId+ data.postId,+ data.presenceStaffOnly);},- @observes("whisper")+ @observes("model.whisper")cancelThrottle() {this._cancelThrottle();},- @observes("action", "topic.id")+ @observes("model.action", "model.topic.id")composerState() {if (this._prevPublishData) {this.presenceManager.publish(diff --git a/plugins/discourse-presence/assets/javascripts/discourse/lib/presence.js.es6 b/plugins/discourse-presence/assets/javascripts/discourse/lib/presence.js.es6index 83424e8277a..1bc90d93ca4 100644--- a/plugins/discourse-presence/assets/javascripts/discourse/lib/presence.js.es6+++ b/plugins/discourse-presence/assets/javascripts/discourse/lib/presence.js.es6@@ -99,7 +99,7 @@ const Presence = EmberObject.extend({return `/presence/${topicId}`;},- publish(state, whisper, postId) {+ publish(state, whisper, postId, staffOnly) {if (this.get("currentUser.hide_profile_and_presence")) return;const data = {@@ -108,13 +108,17 @@ const Presence = EmberObject.extend({};if (whisper) {- data.is_whisper = 1;+ data.is_whisper = true;}if (postId && state === EDITING) {data.post_id = postId;}+ if (staffOnly) {+ data.staff_only = true;+ }+return ajax("/presence/publish", {type: "POST",datadiff --git a/plugins/discourse-presence/assets/javascripts/discourse/services/presence-manager.js.es6 b/plugins/discourse-presence/assets/javascripts/discourse/services/presence-manager.js.es6index 63af48d1948..3913009b30f 100644--- a/plugins/discourse-presence/assets/javascripts/discourse/services/presence-manager.js.es6+++ b/plugins/discourse-presence/assets/javascripts/discourse/services/presence-manager.js.es6@@ -36,9 +36,14 @@ const PresenceManager = Service.extend({return this._getPresence(topicId).editingUsers;},- publish(topicId, state, whisper, postId) {+ publish(topicId, state, whisper, postId, staffOnly) {if (!topicId) return;- return this._getPresence(topicId).publish(state, whisper, postId);+ return this._getPresence(topicId).publish(+ state,+ whisper,+ postId,+ staffOnly+ );},cleanUpPresence(type) {diff --git a/plugins/discourse-presence/assets/javascripts/discourse/templates/connectors/composer-fields/presence.hbs b/plugins/discourse-presence/assets/javascripts/discourse/templates/connectors/composer-fields/presence.hbsindex 8fa1fb3862c..daae93fc744 100644--- a/plugins/discourse-presence/assets/javascripts/discourse/templates/connectors/composer-fields/presence.hbs+++ b/plugins/discourse-presence/assets/javascripts/discourse/templates/connectors/composer-fields/presence.hbs@@ -1,8 +1 @@-{{composer-presence-display- action=model.action- post=model.post- topic=model.topic- reply=model.reply- title=model.title- whisper=model.whisper-}}+{{composer-presence-display model=model}}diff --git a/plugins/discourse-presence/plugin.rb b/plugins/discourse-presence/plugin.rbindex ce91b1ad2e5..de6a3e0fd9b 100644--- a/plugins/discourse-presence/plugin.rb+++ b/plugins/discourse-presence/plugin.rb@@ -73,61 +73,65 @@ after_initialize domax_backlog_age: Presence::MAX_BACKLOG_AGE_SECONDS}- case permitted_params[:state]- when EDITING_STATE+ if permitted_params[:staff_only]opts[:group_ids] = [Group::AUTO_GROUPS[:staff]]+ else+ case permitted_params[:state]+ when EDITING_STATE+ opts[:group_ids] = [Group::AUTO_GROUPS[:staff]]- if !post.locked? && !permitted_params[:is_whisper]- opts[:user_ids] = [post.user_id]-- if topic.private_message?- if post.wiki- opts[:user_ids] = opts[:user_ids].concat(- topic.allowed_users.where(- "trust_level >= ? AND NOT admin OR moderator",- SiteSetting.min_trust_to_edit_wiki_post- ).pluck(:id)- )-- opts[:user_ids].uniq!-- # Ignore trust level and just publish to all allowed groups since- # trying to figure out which users in the allowed groups have- # the necessary trust levels can lead to a large array of user ids- # if the groups are big.- opts[:group_ids] = opts[:group_ids].concat(- topic.allowed_groups.pluck(:id)- )+ if !post.locked? && !permitted_params[:is_whisper]+ opts[:user_ids] = [post.user_id]++ if topic.private_message?+ if post.wiki+ opts[:user_ids] = opts[:user_ids].concat(+ topic.allowed_users.where(+ "trust_level >= ? AND NOT admin OR moderator",+ SiteSetting.min_trust_to_edit_wiki_post+ ).pluck(:id)+ )++ opts[:user_ids].uniq!++ # Ignore trust level and just publish to all allowed groups since+ # trying to figure out which users in the allowed groups have+ # the necessary trust levels can lead to a large array of user ids+ # if the groups are big.+ opts[:group_ids] = opts[:group_ids].concat(+ topic.allowed_groups.pluck(:id)+ )+ end+ else+ if post.wiki+ opts[:group_ids] << Group::AUTO_GROUPS[:"trust_level_#{SiteSetting.min_trust_to_edit_wiki_post}"]+ elsif SiteSetting.trusted_users_can_edit_others?+ opts[:group_ids] << Group::AUTO_GROUPS[:trust_level_4]+ endend+ end+ when REPLYING_STATE+ if permitted_params[:is_whisper]+ opts[:group_ids] = [Group::AUTO_GROUPS[:staff]]+ elsif topic.private_message?+ opts[:user_ids] = topic.allowed_users.pluck(:id)++ opts[:group_ids] = [Group::AUTO_GROUPS[:staff]].concat(+ topic.allowed_groups.pluck(:id)+ )else- if post.wiki- opts[:group_ids] << Group::AUTO_GROUPS[:"trust_level_#{SiteSetting.min_trust_to_edit_wiki_post}"]- elsif SiteSetting.trusted_users_can_edit_others?- opts[:group_ids] << Group::AUTO_GROUPS[:trust_level_4]- end+ opts[:group_ids] = topic.secure_group_ids+ end+ when CLOSED_STATE+ if topic.private_message?+ opts[:user_ids] = topic.allowed_users.pluck(:id)++ opts[:group_ids] = [Group::AUTO_GROUPS[:staff]].concat(+ topic.allowed_groups.pluck(:id)+ )+ else+ opts[:group_ids] = topic.secure_group_idsend- end- when REPLYING_STATE- if permitted_params[:is_whisper]- opts[:group_ids] = [Group::AUTO_GROUPS[:staff]]- elsif topic.private_message?- opts[:user_ids] = topic.allowed_users.pluck(:id)-- opts[:group_ids] = [Group::AUTO_GROUPS[:staff]].concat(- topic.allowed_groups.pluck(:id)- )- else- opts[:group_ids] = topic.secure_group_ids- end- when CLOSED_STATE- if topic.private_message?- opts[:user_ids] = topic.allowed_users.pluck(:id)-- opts[:group_ids] = [Group::AUTO_GROUPS[:staff]].concat(- topic.allowed_groups.pluck(:id)- )- else- opts[:group_ids] = topic.secure_group_idsendend@@ -158,7 +162,7 @@ after_initialize doenddef permitted_params- params.permit(:state, :topic_id, :post_id, :is_whisper)+ params.permit(:state, :topic_id, :post_id, :is_whisper, :staff_only)endenddiff --git a/plugins/discourse-presence/spec/requests/presence_controller_spec.rb b/plugins/discourse-presence/spec/requests/presence_controller_spec.rbindex c13e04fa6c3..b0b3343097b 100644--- a/plugins/discourse-presence/spec/requests/presence_controller_spec.rb+++ b/plugins/discourse-presence/spec/requests/presence_controller_spec.rb@@ -193,6 +193,28 @@ describe ::Presence::PresencesController doexpect(message.user_ids).to eq(nil)end+ it 'publishes the message to staff group when staff_only param override is present' do+ messages = MessageBus.track_publish do+ post '/presence/publish.json', params: {+ topic_id: public_topic.id,+ state: 'replying',+ staff_only: true+ }++ expect(response.status).to eq(200)+ end++ expect(messages.length).to eq(1)++ message = messages.first++ expect(message.channel).to eq("/presence/#{public_topic.id}")+ expect(message.data.dig(:user, :id)).to eq(user.id)+ expect(message.data[:published_at]).to eq(Time.zone.now.to_i)+ expect(message.group_ids).to contain_exactly(Group::AUTO_GROUPS[:staff])+ expect(message.user_ids).to eq(nil)+ end+it 'publishes the message to staff group when a staff is editing a whisper' doSiteSetting.enable_whispers = truesign_in(admin)