diff --git a/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js b/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js index ba00693014a..b8482db4e96 100644 --- a/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js +++ b/app/assets/javascripts/discourse/app/initializers/subscribe-user-notifications.js @@ -112,6 +112,10 @@ export default { user.updateDoNotDisturbStatus(data.ends_at); }); + bus.subscribe(`/user-status/${user.id}`, (data) => { + user.updateStatus(data); + }); + const site = container.lookup("site:main"); const siteSettings = container.lookup("site-settings:main"); const router = container.lookup("router:main"); diff --git a/app/assets/javascripts/discourse/app/models/user.js b/app/assets/javascripts/discourse/app/models/user.js index 646ac080051..c84356eb23d 100644 --- a/app/assets/javascripts/discourse/app/models/user.js +++ b/app/assets/javascripts/discourse/app/models/user.js @@ -1002,6 +1002,11 @@ const User = RestModel.extend({ this.appEvents.trigger("do-not-disturb:changed", this.do_not_disturb_until); }, + updateStatus(status) { + this.set("status", status); + this.appEvents.trigger("user-status:changed"); + }, + isInDoNotDisturb() { return ( this.do_not_disturb_until && diff --git a/app/assets/javascripts/discourse/app/services/user-status.js b/app/assets/javascripts/discourse/app/services/user-status.js index 3df5b74d4cb..a306a5db33c 100644 --- a/app/assets/javascripts/discourse/app/services/user-status.js +++ b/app/assets/javascripts/discourse/app/services/user-status.js @@ -11,8 +11,7 @@ export default class UserStatusService extends Service { data: { description: status.description }, }); - this.currentUser.set("status", status); - this.appEvents.trigger("do-not-disturb:changed"); + this.currentUser.updateStatus(status); } async clear() { @@ -21,7 +20,6 @@ export default class UserStatusService extends Service { type: "DELETE", }); - this.currentUser.set("status", null); - this.appEvents.trigger("do-not-disturb:changed"); + this.currentUser.updateStatus(null); } } diff --git a/app/models/user.rb b/app/models/user.rb index a62902ac6ce..d2e3323f3fb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -653,6 +653,14 @@ class User < ActiveRecord::Base MessageBus.publish("/do-not-disturb/#{id}", { ends_at: ends_at&.httpdate }, user_ids: [id]) end + def publish_user_status(status) + payload = status ? + { description: status.description, emoji: status.emoji } : + nil + + MessageBus.publish("/user-status/#{id}", payload, user_ids: [id]) + end + def password=(password) # special case for passwordless accounts unless password.blank? @@ -1503,6 +1511,7 @@ class User < ActiveRecord::Base def clear_status! user_status.destroy! if user_status + publish_user_status(nil) end def set_status!(description) @@ -1516,6 +1525,8 @@ class User < ActiveRecord::Base set_at: now ) end + + publish_user_status(user_status) end protected diff --git a/spec/requests/user_status_controller_spec.rb b/spec/requests/user_status_controller_spec.rb index 86a05e02a60..ffa730746d2 100644 --- a/spec/requests/user_status_controller_spec.rb +++ b/spec/requests/user_status_controller_spec.rb @@ -47,6 +47,15 @@ describe UserStatusController do user.reload expect(user.user_status.description).to eq(new_status) end + + it "publishes to message bus" do + status = "off to dentist" + messages = MessageBus.track_publish { put "/user-status.json", params: { description: status } } + + expect(messages.size).to eq(1) + expect(messages[0].channel).to eq("/user-status/#{user.id}") + expect(messages[0].data[:description]).to eq(status) + end end end @@ -81,6 +90,14 @@ describe UserStatusController do user.reload expect(user.user_status).to be_nil end + + it "publishes to message bus" do + messages = MessageBus.track_publish { delete "/user-status.json" } + + expect(messages.size).to eq(1) + expect(messages[0].channel).to eq("/user-status/#{user.id}") + expect(messages[0].data).to eq(nil) + end end end end