2022-10-20 07:28:09 -04:00
|
|
|
import { debounce } from "discourse-common/utils/decorators";
|
2020-04-21 23:34:58 -04:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
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) {
|
2022-08-02 01:25:51 -04:00
|
|
|
const messageBus = api.container.lookup("service:message-bus");
|
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);
|
|
|
|
|
2022-09-12 08:05:21 -04:00
|
|
|
this.messageBus.subscribe(`/topic/${this.model.id}`, (data) => {
|
2021-12-16 11:00:09 -05:00
|
|
|
const topic = this.model;
|
|
|
|
|
|
|
|
// scroll only for discobot (-2 is discobot id)
|
|
|
|
if (
|
2022-09-12 08:05:21 -04:00
|
|
|
topic.isPrivateMessage &&
|
2021-12-16 11:00:09 -05:00
|
|
|
this.currentUser &&
|
2022-09-12 08:05:21 -04:00
|
|
|
this.currentUser.id !== data.user_id &&
|
2021-12-16 11:00:09 -05:00
|
|
|
data.user_id === -2 &&
|
|
|
|
data.type === "created"
|
|
|
|
) {
|
|
|
|
const postNumber = data.post_number;
|
|
|
|
const notInPostStream =
|
|
|
|
topic.get("highest_post_number") <= postNumber;
|
2022-09-12 08:05:21 -04:00
|
|
|
const postNumberDifference = postNumber - topic.currentPost;
|
2021-12-16 11:00:09 -05:00
|
|
|
|
|
|
|
if (
|
|
|
|
notInPostStream &&
|
|
|
|
postNumberDifference > 0 &&
|
|
|
|
postNumberDifference < 7
|
|
|
|
) {
|
|
|
|
this._scrollToDiscobotPost(data.post_number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// No need to unsubscribe, core unsubscribes /topic/* routes
|
|
|
|
},
|
|
|
|
|
2022-10-20 07:28:09 -04:00
|
|
|
@debounce(500)
|
2021-12-16 11:00:09 -05:00
|
|
|
_scrollToDiscobotPost(postNumber) {
|
2022-10-20 07:28:09 -04:00
|
|
|
const post = document.querySelector(
|
|
|
|
`.topic-post article#post_${postNumber}`
|
|
|
|
);
|
2021-12-16 11:00:09 -05:00
|
|
|
|
2022-10-20 07:28:09 -04:00
|
|
|
if (!post || isElementInViewport(post)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-16 11:00:09 -05:00
|
|
|
|
2022-10-20 07:28:09 -04:00
|
|
|
const viewportOffset = post.getBoundingClientRect();
|
|
|
|
|
|
|
|
window.scrollTo({
|
|
|
|
top: window.scrollY + viewportOffset.top - headerOffset(),
|
|
|
|
behavior: "smooth",
|
|
|
|
});
|
2021-12-16 11:00:09 -05:00
|
|
|
},
|
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 {
|
2022-09-12 08:05:21 -04:00
|
|
|
name: "new-user-narrative",
|
2017-05-24 01:50:20 -04:00
|
|
|
|
|
|
|
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
|
|
|
},
|
|
|
|
};
|