discourse/plugins/discourse-narrative-bot/assets/javascripts/initializers/new-user-narrative.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

64 lines
1.5 KiB
JavaScript

import { withPluginApi } from "discourse/lib/plugin-api";
import { bind } from "discourse-common/utils/decorators";
export default {
name: "new-user-narrative",
initialize(container) {
const siteSettings = container.lookup("service:site-settings");
if (!siteSettings.discourse_narrative_bot_enabled) {
return;
}
this.messageBus = container.lookup("service:message-bus");
this.appEvents = container.lookup("service:app-events");
withPluginApi("0.8.7", (api) => {
this.currentUser = api.getCurrentUser();
if (!this.currentUser) {
return;
}
api.dispatchWidgetAppEvent(
"site-header",
"header",
"header:search-context-trigger"
);
api.attachWidgetAction(
"header",
"headerSearchContextTrigger",
function () {
if (this.site.mobileView) {
this.state.skipSearchContext = false;
} else {
this.state.contextEnabled = true;
this.state.searchContextType = "topic";
}
}
);
this.messageBus.subscribe(
`/new_user_narrative/tutorial_search/${this.currentUser.id}`,
this.onMessage
);
});
},
teardown() {
if (this.currentUser) {
this.messageBus?.unsubscribe(
`/new_user_narrative/tutorial_search/${this.currentUser.id}`,
this.onMessage
);
}
},
@bind
onMessage() {
this.appEvents.trigger("header:search-context-trigger");
},
};