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.
50 lines
1.1 KiB
JavaScript
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
|
|
)
|
|
);
|
|
});
|
|
},
|
|
};
|