2023-03-15 16:02:20 -04:00
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
|
|
|
import showModal from "discourse/lib/show-modal";
|
|
|
|
|
|
|
|
function initializeComposerAIHelper(api) {
|
|
|
|
api.modifyClass("component:composer-editor", {
|
2023-04-25 07:19:57 -04:00
|
|
|
pluginId: "discourse-ai",
|
|
|
|
|
2023-03-15 16:02:20 -04:00
|
|
|
actions: {
|
|
|
|
extraButtons(toolbar) {
|
|
|
|
this._super(toolbar);
|
|
|
|
|
|
|
|
const removeAiHelperFromPM =
|
|
|
|
this.composerModel.privateMessage &&
|
|
|
|
!this.siteSettings.ai_helper_allowed_in_pm;
|
|
|
|
|
|
|
|
if (removeAiHelperFromPM) {
|
|
|
|
const extrasGroup = toolbar.groups.find((g) => g.group === "extras");
|
|
|
|
const newButtons = extrasGroup.buttons.filter(
|
|
|
|
(b) => b.id !== "ai-helper"
|
|
|
|
);
|
|
|
|
|
|
|
|
extrasGroup.buttons = newButtons;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
api.modifyClass("component:d-editor", {
|
|
|
|
pluginId: "discourse-ai",
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
openAIHelper() {
|
|
|
|
if (this.value) {
|
|
|
|
showModal("composer-ai-helper").setProperties({ editor: this });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
api.onToolbarCreate((toolbar) => {
|
|
|
|
toolbar.addButton({
|
|
|
|
id: "ai-helper",
|
|
|
|
title: "discourse_ai.ai_helper.title",
|
|
|
|
group: "extras",
|
2023-04-20 11:11:24 -04:00
|
|
|
icon: "discourse-sparkles",
|
2023-03-15 16:02:20 -04:00
|
|
|
className: "composer-ai-helper",
|
|
|
|
sendAction: () => toolbar.context.send("openAIHelper"),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
2023-04-25 07:19:57 -04:00
|
|
|
name: "discourse-ai-composer-helper",
|
2023-03-15 16:02:20 -04:00
|
|
|
|
|
|
|
initialize(container) {
|
2023-04-25 07:19:57 -04:00
|
|
|
const settings = container.lookup("service:site-settings");
|
2023-03-15 16:02:20 -04:00
|
|
|
const user = container.lookup("service:current-user");
|
|
|
|
const helperEnabled =
|
|
|
|
settings.discourse_ai_enabled && settings.composer_ai_helper_enabled;
|
|
|
|
|
|
|
|
const allowedGroups = settings.ai_helper_allowed_groups
|
|
|
|
.split("|")
|
|
|
|
.map(parseInt);
|
2023-04-25 07:19:57 -04:00
|
|
|
const canUseAssistant = user?.groups.some((g) =>
|
|
|
|
allowedGroups.includes(g.id)
|
|
|
|
);
|
2023-03-15 16:02:20 -04:00
|
|
|
|
|
|
|
if (helperEnabled && canUseAssistant) {
|
|
|
|
withPluginApi("1.6.0", initializeComposerAIHelper);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|