This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/app/initializers/user-tips.js
David Taylor 79bea9464c
PERF: Move user-tips and narrative to per-user messagebus channels (#19773)
Using a shared channel with per-message permissions means that every client is updated with the channel's 'last_id', even if there are no messages available to them. Per-user channel names avoid this problem - the last_id will only be incremented when there is a message for the given user.
2023-01-30 11:48:09 +00:00

50 lines
1.1 KiB
JavaScript

import { bind } from "discourse-common/utils/decorators";
export default {
name: "user-tips",
after: "message-bus",
initialize(container) {
this.currentUser = container.lookup("service:current-user");
if (!this.currentUser) {
return;
}
this.messageBus = container.lookup("service:message-bus");
this.site = container.lookup("service:site");
this.messageBus.subscribe(
`/user-tips/${this.currentUser.id}`,
this.onMessage
);
},
teardown() {
if (this.currentUser) {
this.messageBus?.unsubscribe(
`/user-tips/${this.currentUser.id}`,
this.onMessage
);
}
},
@bind
onMessage(seenUserTips) {
this.currentUser.set("seen_popups", seenUserTips);
if (!this.currentUser.user_option) {
this.currentUser.set("user_option", {});
}
this.currentUser.set("user_option.seen_popups", seenUserTips);
(seenUserTips || []).forEach((userTipId) => {
this.currentUser.hideUserTipForever(
Object.keys(this.site.user_tips).find(
(id) => this.site.user_tips[id] === userTipId
)
);
});
},
};