FEATURE: propagate user status via message bus (#16944)

This commit is contained in:
Andrei Prigorshnev 2022-05-30 13:41:53 +04:00 committed by GitHub
parent 46302f0d40
commit 943cae82da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 4 deletions

View File

@ -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");

View File

@ -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 &&

View File

@ -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);
}
}

View File

@ -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

View File

@ -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