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
Jarek Radosz 19214aff18
DEV: Clean up all message bus subscriptions (#19268)
1. "What Goes Up Must Come Down" – if you subscribe to message bus, make sure you also unsubscribe
2. When you unsubscribe - remove only your subscription, not **all** subscriptions on given channel

Attempt #2. The first attempt tried to extend a core `@bound` method in new-user-narrative plugin which did not work. I reworked that plugin in the meantime. This new PR also cleans up message bus subscriptions in now core-merged chat plugin.
2022-12-12 16:32:25 +01:00

42 lines
1015 B
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.onMessage);
},
teardown() {
this.messageBus?.unsubscribe("/user-tips", 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
)
);
});
},
};