2022-10-20 19:28:09 +08:00
|
|
|
import { debounce } from "discourse-common/utils/decorators";
|
2020-04-22 11:34:58 +08:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2022-01-08 05:02:03 +08:00
|
|
|
import { headerOffset } from "discourse/lib/offset-calculator";
|
2021-12-17 00:00:09 +08:00
|
|
|
import isElementInViewport from "discourse/lib/is-element-in-viewport";
|
2018-06-16 00:42:20 +08:00
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
2017-05-24 13:50:20 +08:00
|
|
|
|
2021-09-02 02:29:07 +08:00
|
|
|
const PLUGIN_ID = "new-user-narrative";
|
|
|
|
|
2017-05-24 13:50:20 +08:00
|
|
|
function initialize(api) {
|
2022-08-02 13:25:51 +08:00
|
|
|
const messageBus = api.container.lookup("service:message-bus");
|
2017-05-24 13:50:20 +08:00
|
|
|
const currentUser = api.getCurrentUser();
|
2019-10-04 22:06:08 +08:00
|
|
|
const appEvents = api.container.lookup("service:app-events");
|
2017-05-24 13:50:20 +08:00
|
|
|
|
2018-06-16 00:42:20 +08:00
|
|
|
api.modifyClass("component:site-header", {
|
2021-09-02 02:29:07 +08:00
|
|
|
pluginId: PLUGIN_ID,
|
2017-05-24 13:50:20 +08:00
|
|
|
didInsertElement() {
|
2019-01-19 17:05:51 +08:00
|
|
|
this._super(...arguments);
|
2018-06-16 00:42:20 +08:00
|
|
|
this.dispatch("header:search-context-trigger", "header");
|
2017-05-24 13:50:20 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-01-25 06:35:13 +08:00
|
|
|
api.modifyClass("controller:topic", {
|
2021-09-02 02:29:07 +08:00
|
|
|
pluginId: PLUGIN_ID,
|
|
|
|
|
2021-09-21 06:45:47 +08:00
|
|
|
_modifyBookmark(bookmark, post) {
|
2020-04-22 11:34:58 +08: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-21 06:45:47 +08:00
|
|
|
if (post && post.user_id === discobotUserId && !post.bookmarked) {
|
2020-04-22 11:34:58 +08:00
|
|
|
return ajax("/bookmarks", {
|
|
|
|
type: "POST",
|
2021-01-25 06:35:13 +08:00
|
|
|
data: { post_id: post.id },
|
2020-04-22 11:34:58 +08:00
|
|
|
}).then((response) => {
|
2021-01-25 06:35:13 +08:00
|
|
|
post.setProperties({
|
2020-04-22 11:34:58 +08:00
|
|
|
"topic.bookmarked": true,
|
2020-04-30 08:09:22 +08:00
|
|
|
bookmarked: true,
|
2020-04-22 11:34:58 +08:00
|
|
|
bookmark_id: response.id,
|
|
|
|
});
|
2021-01-25 06:35:13 +08:00
|
|
|
post.appEvents.trigger("post-stream:refresh", { id: this.id });
|
2020-04-22 11:34:58 +08:00
|
|
|
});
|
|
|
|
}
|
2021-09-21 06:45:47 +08:00
|
|
|
return this._super(bookmark, post);
|
2020-04-22 11:34:58 +08:00
|
|
|
},
|
2021-12-17 00:00:09 +08:00
|
|
|
|
|
|
|
subscribe() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
2022-09-12 20:05:21 +08:00
|
|
|
this.messageBus.subscribe(`/topic/${this.model.id}`, (data) => {
|
2021-12-17 00:00:09 +08:00
|
|
|
const topic = this.model;
|
|
|
|
|
|
|
|
// scroll only for discobot (-2 is discobot id)
|
|
|
|
if (
|
2022-09-12 20:05:21 +08:00
|
|
|
topic.isPrivateMessage &&
|
2021-12-17 00:00:09 +08:00
|
|
|
this.currentUser &&
|
2022-09-12 20:05:21 +08:00
|
|
|
this.currentUser.id !== data.user_id &&
|
2021-12-17 00:00:09 +08:00
|
|
|
data.user_id === -2 &&
|
|
|
|
data.type === "created"
|
|
|
|
) {
|
|
|
|
const postNumber = data.post_number;
|
|
|
|
const notInPostStream =
|
|
|
|
topic.get("highest_post_number") <= postNumber;
|
2022-09-12 20:05:21 +08:00
|
|
|
const postNumberDifference = postNumber - topic.currentPost;
|
2021-12-17 00:00:09 +08:00
|
|
|
|
|
|
|
if (
|
|
|
|
notInPostStream &&
|
|
|
|
postNumberDifference > 0 &&
|
|
|
|
postNumberDifference < 7
|
|
|
|
) {
|
|
|
|
this._scrollToDiscobotPost(data.post_number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// No need to unsubscribe, core unsubscribes /topic/* routes
|
|
|
|
},
|
|
|
|
|
2022-10-20 19:28:09 +08:00
|
|
|
@debounce(500)
|
2021-12-17 00:00:09 +08:00
|
|
|
_scrollToDiscobotPost(postNumber) {
|
2022-10-20 19:28:09 +08:00
|
|
|
const post = document.querySelector(
|
|
|
|
`.topic-post article#post_${postNumber}`
|
|
|
|
);
|
2021-12-17 00:00:09 +08:00
|
|
|
|
2022-10-20 19:28:09 +08:00
|
|
|
if (!post || isElementInViewport(post)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-17 00:00:09 +08:00
|
|
|
|
2022-10-20 19:28:09 +08:00
|
|
|
const viewportOffset = post.getBoundingClientRect();
|
|
|
|
|
|
|
|
window.scrollTo({
|
|
|
|
top: window.scrollY + viewportOffset.top - headerOffset(),
|
|
|
|
behavior: "smooth",
|
|
|
|
});
|
2021-12-17 00:00:09 +08:00
|
|
|
},
|
2020-04-22 11:34:58 +08:00
|
|
|
});
|
|
|
|
|
2018-06-16 00:42:20 +08:00
|
|
|
api.attachWidgetAction("header", "headerSearchContextTrigger", function () {
|
2017-05-24 13:50:20 +08:00
|
|
|
if (this.site.mobileView) {
|
|
|
|
this.state.skipSearchContext = false;
|
|
|
|
} else {
|
|
|
|
this.state.contextEnabled = true;
|
2018-06-16 00:42:20 +08:00
|
|
|
this.state.searchContextType = "topic";
|
2017-05-24 13:50:20 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (messageBus && currentUser) {
|
|
|
|
messageBus.subscribe(`/new_user_narrative/tutorial_search`, () => {
|
2018-06-16 00:42:20 +08:00
|
|
|
appEvents.trigger("header:search-context-trigger");
|
2017-05-24 13:50:20 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
2022-09-12 20:05:21 +08:00
|
|
|
name: "new-user-narrative",
|
2017-05-24 13:50:20 +08:00
|
|
|
|
|
|
|
initialize(container) {
|
2022-08-01 16:43:33 +08:00
|
|
|
const siteSettings = container.lookup("service:site-settings");
|
2020-09-22 22:28:28 +08:00
|
|
|
if (siteSettings.discourse_narrative_bot_enabled) {
|
2018-06-16 00:42:20 +08:00
|
|
|
withPluginApi("0.8.7", initialize);
|
2020-09-22 22:28:28 +08:00
|
|
|
}
|
2017-05-24 13:50:20 +08:00
|
|
|
},
|
|
|
|
};
|