2020-04-21 23:34:58 -04:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2018-06-15 12:42:20 -04:00
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
2017-05-24 01:50:20 -04:00
|
|
|
|
2021-09-01 14:29:07 -04:00
|
|
|
const PLUGIN_ID = "new-user-narrative";
|
|
|
|
|
2017-05-24 01:50:20 -04:00
|
|
|
function initialize(api) {
|
2018-06-15 12:42:20 -04:00
|
|
|
const messageBus = api.container.lookup("message-bus:main");
|
2017-05-24 01:50:20 -04:00
|
|
|
const currentUser = api.getCurrentUser();
|
2019-10-04 10:06:08 -04:00
|
|
|
const appEvents = api.container.lookup("service:app-events");
|
2017-05-24 01:50:20 -04:00
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
api.modifyClass("component:site-header", {
|
2021-09-01 14:29:07 -04:00
|
|
|
pluginId: PLUGIN_ID,
|
2017-05-24 01:50:20 -04:00
|
|
|
didInsertElement() {
|
2019-01-19 04:05:51 -05:00
|
|
|
this._super(...arguments);
|
2018-06-15 12:42:20 -04:00
|
|
|
this.dispatch("header:search-context-trigger", "header");
|
2017-05-24 01:50:20 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-01-24 17:35:13 -05:00
|
|
|
api.modifyClass("controller:topic", {
|
2021-09-01 14:29:07 -04:00
|
|
|
pluginId: PLUGIN_ID,
|
|
|
|
|
2021-09-20 18:45:47 -04:00
|
|
|
_modifyBookmark(bookmark, post) {
|
2020-04-21 23:34:58 -04:00
|
|
|
// if we are talking to discobot then any bookmarks should just
|
|
|
|
// be created without reminder options, to streamline the new user
|
|
|
|
// narrative.
|
|
|
|
const discobotUserId = -2;
|
2021-09-20 18:45:47 -04:00
|
|
|
if (post && post.user_id === discobotUserId && !post.bookmarked) {
|
2020-04-21 23:34:58 -04:00
|
|
|
return ajax("/bookmarks", {
|
|
|
|
type: "POST",
|
2021-01-24 17:35:13 -05:00
|
|
|
data: { post_id: post.id },
|
2020-04-21 23:34:58 -04:00
|
|
|
}).then((response) => {
|
2021-01-24 17:35:13 -05:00
|
|
|
post.setProperties({
|
2020-04-21 23:34:58 -04:00
|
|
|
"topic.bookmarked": true,
|
2020-04-29 20:09:22 -04:00
|
|
|
bookmarked: true,
|
2020-04-21 23:34:58 -04:00
|
|
|
bookmark_id: response.id,
|
|
|
|
});
|
2021-01-24 17:35:13 -05:00
|
|
|
post.appEvents.trigger("post-stream:refresh", { id: this.id });
|
2020-04-21 23:34:58 -04:00
|
|
|
});
|
|
|
|
}
|
2021-09-20 18:45:47 -04:00
|
|
|
return this._super(bookmark, post);
|
2020-04-21 23:34:58 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
api.attachWidgetAction("header", "headerSearchContextTrigger", function () {
|
2017-05-24 01:50:20 -04:00
|
|
|
if (this.site.mobileView) {
|
|
|
|
this.state.skipSearchContext = false;
|
|
|
|
} else {
|
|
|
|
this.state.contextEnabled = true;
|
2018-06-15 12:42:20 -04:00
|
|
|
this.state.searchContextType = "topic";
|
2017-05-24 01:50:20 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (messageBus && currentUser) {
|
|
|
|
messageBus.subscribe(`/new_user_narrative/tutorial_search`, () => {
|
2018-06-15 12:42:20 -04:00
|
|
|
appEvents.trigger("header:search-context-trigger");
|
2017-05-24 01:50:20 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: "new-user-narratve",
|
|
|
|
|
|
|
|
initialize(container) {
|
2018-06-15 12:42:20 -04:00
|
|
|
const siteSettings = container.lookup("site-settings:main");
|
2020-09-22 10:28:28 -04:00
|
|
|
if (siteSettings.discourse_narrative_bot_enabled) {
|
2018-06-15 12:42:20 -04:00
|
|
|
withPluginApi("0.8.7", initialize);
|
2020-09-22 10:28:28 -04:00
|
|
|
}
|
2017-05-24 01:50:20 -04:00
|
|
|
},
|
|
|
|
};
|