2020-04-21 23:34:58 -04:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2021-12-16 11:00:09 -05:00
|
|
|
import discourseDebounce from "discourse-common/lib/debounce";
|
2022-01-07 16:02:03 -05:00
|
|
|
import { headerOffset } from "discourse/lib/offset-calculator";
|
2021-12-16 11:00:09 -05:00
|
|
|
import isElementInViewport from "discourse/lib/is-element-in-viewport";
|
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
|
|
|
},
|
2021-12-16 11:00:09 -05:00
|
|
|
|
|
|
|
subscribe() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
this.messageBus.subscribe(`/topic/${this.get("model.id")}`, (data) => {
|
|
|
|
const topic = this.model;
|
|
|
|
|
|
|
|
// scroll only for discobot (-2 is discobot id)
|
|
|
|
if (
|
|
|
|
topic.get("isPrivateMessage") &&
|
|
|
|
this.currentUser &&
|
|
|
|
this.currentUser.get("id") !== data.user_id &&
|
|
|
|
data.user_id === -2 &&
|
|
|
|
data.type === "created"
|
|
|
|
) {
|
|
|
|
const postNumber = data.post_number;
|
|
|
|
const notInPostStream =
|
|
|
|
topic.get("highest_post_number") <= postNumber;
|
|
|
|
const postNumberDifference = postNumber - topic.get("currentPost");
|
|
|
|
|
|
|
|
if (
|
|
|
|
notInPostStream &&
|
|
|
|
postNumberDifference > 0 &&
|
|
|
|
postNumberDifference < 7
|
|
|
|
) {
|
|
|
|
this._scrollToDiscobotPost(data.post_number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// No need to unsubscribe, core unsubscribes /topic/* routes
|
|
|
|
},
|
|
|
|
|
|
|
|
_scrollToDiscobotPost(postNumber) {
|
|
|
|
discourseDebounce(
|
|
|
|
this,
|
|
|
|
function () {
|
|
|
|
const post = document.querySelector(
|
|
|
|
`.topic-post article#post_${postNumber}`
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!post || isElementInViewport(post)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const viewportOffset = post.getBoundingClientRect();
|
|
|
|
|
|
|
|
window.scrollTo({
|
2021-12-29 11:03:21 -05:00
|
|
|
top: window.scrollY + viewportOffset.top - headerOffset(),
|
2021-12-16 11:00:09 -05:00
|
|
|
behavior: "smooth",
|
|
|
|
});
|
|
|
|
},
|
|
|
|
postNumber,
|
|
|
|
500
|
|
|
|
);
|
|
|
|
},
|
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) {
|
2022-08-01 04:43:33 -04:00
|
|
|
const siteSettings = container.lookup("service:site-settings");
|
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
|
|
|
},
|
|
|
|
};
|