diff --git a/src/components/ConversationSettings/BotsSettings.vue b/src/components/ConversationSettings/BotsSettings.vue new file mode 100644 index 00000000000..4902171d513 --- /dev/null +++ b/src/components/ConversationSettings/BotsSettings.vue @@ -0,0 +1,183 @@ + + + + + + + diff --git a/src/components/ConversationSettings/ConversationSettingsDialog.vue b/src/components/ConversationSettings/ConversationSettingsDialog.vue index 2e80672b838..4c60f7bd15b 100644 --- a/src/components/ConversationSettings/ConversationSettingsDialog.vue +++ b/src/components/ConversationSettings/ConversationSettingsDialog.vue @@ -3,7 +3,7 @@ - - @author Vincent Petry - - - @license GNU AGPL version 3 or any later version + - @license AGPL-3.0-or-later - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as @@ -84,6 +84,13 @@ + + + + + + * + * @author Maksim Sukharev + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +import axios from '@nextcloud/axios' +import { generateOcsUrl } from '@nextcloud/router' + +/** + * Get information about available bots for this conversation + * + * @param {string} token The conversation token + * @return {object} The axios response + */ +const getConversationBots = async function(token) { + return axios.get(generateOcsUrl('/apps/spreed/api/v1/bot/{token}', { token })) +} + +/** + * Enable bot for conversation + * + * @param {string} token The conversation token + * @param {number} id The bot id + * @return {object} The axios response + */ +const enableBotForConversation = async function(token, id) { + return axios.post(generateOcsUrl('/apps/spreed/api/v1/bot/{token}/{id}', { token, id })) +} + +/** + * Disable bot for conversation + * + * @param {string} token The conversation token + * @param {number} id The bot id + * @return {object} The axios response + */ +const disableBotForConversation = async function(token, id) { + return axios.delete(generateOcsUrl('/apps/spreed/api/v1/bot/{token}/{id}', { token, id })) +} + +/** + * Send a message to bot in conversation + * + * @param {string} token The conversation token + * @param {object} object Object with arguments + * @param {string} object.message The message to send + * @param {string} object.referenceId for the message to be able to later identify it again + * @param {number} object.replyTo Parent id which this message is a reply to + * @param {boolean} object.silent If sent silent the chat message will not create any notifications + * @return {object} The axios response + */ +const sendMessageToBot = async function(token, { message, referenceId, replyTo, silent }) { + return axios.post(generateOcsUrl('/apps/spreed/api/v1/bot/{token}/message', { token }), { + message, + referenceId, + replyTo, + silent, + }) +} + +export { + getConversationBots, + enableBotForConversation, + disableBotForConversation, + sendMessageToBot, +} diff --git a/src/stores/bots.js b/src/stores/bots.js new file mode 100644 index 00000000000..2d82a380c1a --- /dev/null +++ b/src/stores/bots.js @@ -0,0 +1,73 @@ +/** + * @copyright Copyright (c) 2023 Maksim Sukharev + * + * @author Maksim Sukharev + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +import { defineStore } from 'pinia' +import Vue from 'vue' + +import { BOT } from '../constants.js' +import { disableBotForConversation, enableBotForConversation, getConversationBots } from '../services/botsService.js' + +export const useBotsStore = defineStore('bots', { + state: () => ({ + bots: {}, + }), + + actions: { + getConversationBots(token) { + return this.bots[token] ? Object.values(this.bots[token]) : [] + }, + + /** + * Fetch a list of available bots for conversation and save them to store + * + * @param {string} token The conversation token + * @return {Array} An array of bots ids + */ + async loadConversationBots(token) { + if (!this.bots[token]) { + Vue.set(this.bots, token, {}) + } + + const response = await getConversationBots(token) + + return response.data.ocs.data.map((bot) => { + Vue.set(this.bots[token], bot.id, bot) + return bot.id + }) + }, + + /** + * Enable or disable a bot for conversation + * + * @param {string} token The conversation token + * @param {object} bot The bot to toggle state + */ + async toggleBotState(token, bot) { + const response = bot.state === BOT.STATE.ENABLED + ? await disableBotForConversation(token, bot.id) + : await enableBotForConversation(token, bot.id) + + Vue.set(this.bots[token], bot.id, response.data.ocs.data) + }, + + }, +})