Skip to content

Commit

Permalink
refactor(bots): migrate bots to Typescript
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Feb 27, 2024
1 parent 7feb3ac commit 9c8539f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/components/AdminSettings/BotsSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadi
import NcPopover from '@nextcloud/vue/dist/Components/NcPopover.js'

import { BOT } from '../../constants.js'
import { getAllBots } from '../../services/botsService.js'
import { getAllBots } from '../../services/botsService.ts'

export default {
name: 'BotsSettings',
Expand Down
2 changes: 1 addition & 1 deletion src/components/ConversationSettings/BotsSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import Vue from 'vue'
import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'

import { BOT } from '../../constants.js'
import { useBotsStore } from '../../stores/bots.js'
import { useBotsStore } from '../../stores/bots.ts'

export default {
name: 'BotsSettings',
Expand Down
25 changes: 11 additions & 14 deletions src/services/botsService.js → src/services/botsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,44 +23,41 @@
import axios from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'

import type { Bot, getBotsResponse, getBotsAdminResponse, enableBotResponse, disableBotResponse } from '../types'

/**
* Get information about available bots for this instance
*
* @return {object} The axios response
*/
const getAllBots = async function() {
const getAllBots = async function(): getBotsAdminResponse {
return axios.get(generateOcsUrl('/apps/spreed/api/v1/bot/admin'))
}

/**
* Get information about available bots for this conversation
*
* @param {string} token The conversation token
* @return {object} The axios response
* @param token The conversation token
*/
const getConversationBots = async function(token) {
const getConversationBots = async function(token: string): getBotsResponse {
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
* @param token The conversation token
* @param id The bot id
*/
const enableBotForConversation = async function(token, id) {
const enableBotForConversation = async function(token: string, id: Bot['id']): enableBotResponse {
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
* @param token The conversation token
* @param id The bot id
*/
const disableBotForConversation = async function(token, id) {
const disableBotForConversation = async function(token: string, id: Bot['id']): disableBotResponse {
return axios.delete(generateOcsUrl('/apps/spreed/api/v1/bot/{token}/{id}', { token, id }))
}

Expand Down
6 changes: 3 additions & 3 deletions src/stores/__tests__/bots.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {
getConversationBots,
disableBotForConversation,
enableBotForConversation,
} from '../../services/botsService.js'
} from '../../services/botsService.ts'
import { generateOCSResponse } from '../../test-helpers.js'
import { useBotsStore } from '../bots.js'
import { useBotsStore } from '../bots.ts'

jest.mock('../../services/botsService', () => ({
jest.mock('../../services/botsService.ts', () => ({
getConversationBots: jest.fn(),
disableBotForConversation: jest.fn(),
enableBotForConversation: jest.fn(),
Expand Down
24 changes: 13 additions & 11 deletions src/stores/bots.js → src/stores/bots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,35 @@ import { defineStore } from 'pinia'
import Vue from 'vue'

import { BOT } from '../constants.js'
import { disableBotForConversation, enableBotForConversation, getConversationBots } from '../services/botsService.js'
import { disableBotForConversation, enableBotForConversation, getConversationBots } from '../services/botsService.ts'
import type { Bot } from '../types'

type State = {
bots: Record<string, Record<number, Bot>>
}
export const useBotsStore = defineStore('bots', {
state: () => ({
state: (): State => ({
bots: {},
}),

actions: {
getConversationBots(token) {
getConversationBots(token: string): Bot[] {
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
* @param token The conversation token
*/
async loadConversationBots(token) {
async loadConversationBots(token: string): Promise<Bot['id'][]> {
if (!this.bots[token]) {
Vue.set(this.bots, token, {})
}

const response = await getConversationBots(token)

return response.data.ocs.data.map((bot) => {
return response.data.ocs.data.map((bot: Bot) => {
Vue.set(this.bots[token], bot.id, bot)
return bot.id
})
Expand All @@ -58,16 +61,15 @@ export const useBotsStore = defineStore('bots', {
/**
* Enable or disable a bot for conversation
*
* @param {string} token The conversation token
* @param {object} bot The bot to toggle state
* @param token The conversation token
* @param bot The bot to toggle state
*/
async toggleBotState(token, bot) {
async toggleBotState(token: string, bot: Bot): Promise<void> {
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)
},

},
})
16 changes: 16 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { components, paths } from './openapi/openapi-full.ts'

// General
type ApiResponse<T> = Promise<{ data: T }>

// Conversations
export type Conversation = components['schemas']['Room']

// Bots
export type Bot = components['schemas']['Bot']
export type BotWithDetails = components['schemas']['BotWithDetails']

export type getBotsResponse = ApiResponse<paths['/ocs/v2.php/apps/spreed/api/{apiVersion}/bot/{token}']['get']['responses'][200]['content']['application/json']>
export type getBotsAdminResponse = ApiResponse<paths['/ocs/v2.php/apps/spreed/api/{apiVersion}/bot/admin']['get']['responses'][200]['content']['application/json']>
export type enableBotResponse = ApiResponse<paths['/ocs/v2.php/apps/spreed/api/{apiVersion}/bot/{token}/{botId}']['post']['responses'][201]['content']['application/json']>
export type disableBotResponse = ApiResponse<paths['/ocs/v2.php/apps/spreed/api/{apiVersion}/bot/{token}/{botId}']['delete']['responses'][200]['content']['application/json']>

0 comments on commit 9c8539f

Please sign in to comment.