Skip to content

Commit

Permalink
Merge pull request #2895 from botpress/ya-adj-markdown
Browse files Browse the repository at this point in the history
fix(cms): markdown property
  • Loading branch information
allardy committed Jan 28, 2020
2 parents 3d9c7b0 + 1d4d63b commit 1a52d8b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 12 deletions.
8 changes: 7 additions & 1 deletion modules/builtin/src/content-types/single_choice.js
Expand Up @@ -18,7 +18,8 @@ function render(data) {
title: c.title,
payload: c.value.toUpperCase()
})),
typing: data.typing
typing: data.typing,
markdown: data.markdown
}
]
}
Expand Down Expand Up @@ -123,6 +124,11 @@ module.exports = {
}
}
},
markdown: {
type: 'boolean',
title: 'Use markdown',
default: true
},
...base.typingIndicators
}
},
Expand Down
62 changes: 62 additions & 0 deletions modules/builtin/src/migrations/v12_5_0-1577776334-text-markdown.ts
@@ -0,0 +1,62 @@
import * as sdk from 'botpress/sdk'

const ELEMENTS_DIR = 'content-elements'

const migration: sdk.ModuleMigration = {
info: {
description: 'Enable markdown by default for all text messages',
target: 'bot',
type: 'content'
},
up: async ({ bp, metadata }: sdk.ModuleMigrationOpts): Promise<sdk.MigrationResult> => {
const checkFile = (fileContent: string, nbElements: number) => {
const parsed = JSON.parse(fileContent)
return parsed.length === nbElements
}

const updateFormData = ({ formData }: sdk.ContentElement) => {
for (const key of Object.keys(formData)) {
if (key.startsWith('text$')) {
const language = key.substr(key.indexOf('$') + 1, key.length)
const markdownKey = 'markdown$' + language

if (!(markdownKey in formData)) {
formData[markdownKey] = true
}
}
}
}

const migrateBotTextContent = async (botId: string) => {
const bpfs = bp.ghost.forBot(botId)
const entFiles = await bpfs.directoryListing(ELEMENTS_DIR, '*.json')

for (const fileName of entFiles) {
try {
const contentElements = await bpfs.readFileAsObject<sdk.ContentElement[]>(ELEMENTS_DIR, fileName)
contentElements.forEach(element => updateFormData(element))

const fileContent = JSON.stringify(contentElements, undefined, 2)

// Just double-checking before writing the content back
if (checkFile(fileContent, contentElements.length)) {
await bpfs.upsertFile(ELEMENTS_DIR, fileName, fileContent, { ignoreLock: true })
}
} catch (err) {
console.error(`Error processing file ${fileName} for bot ${botId}`)
}
}
}

if (metadata.botId) {
await migrateBotTextContent(metadata.botId)
} else {
const bots = await bp.bots.getAllBots()
await Promise.map(bots.keys(), botId => migrateBotTextContent(botId))
}

return { success: true, message: 'Text content type updated successfully' }
}
}

export default migration
27 changes: 16 additions & 11 deletions src/bp/core/services/cms.ts
Expand Up @@ -110,18 +110,23 @@ export class CMSService implements IDisposeOnExit {
async loadElementsForBot(botId: string): Promise<any[]> {
const contentElements = await this.getAllElements(botId)

const elements = await Promise.map(contentElements, element => {
return this.memDb(this.contentTable)
.insert(this.transformItemApiToDb(botId, element))
.catch(err => {
// ignore duplicate key errors
// TODO: Knex error handling
})
})
try {
const elements = await Promise.map(contentElements, element => {
return this.memDb(this.contentTable)
.insert(this.transformItemApiToDb(botId, element))
.catch(err => {
// ignore duplicate key errors
// TODO: Knex error handling
})
})

await this.recomputeElementsForBot(botId)
await this.recomputeElementsForBot(botId)

return elements
return elements
} catch (err) {
this.logger.error(`Error while processing content elements for bot ${botId}`)
throw err
}
}

async deleteAllElements(botId: string): Promise<void> {
Expand Down Expand Up @@ -479,7 +484,7 @@ export class CMSService implements IDisposeOnExit {

private async fillComputedProps(contentType: ContentType, formData: object, languages: string[], defaultLanguage) {
if (formData == undefined) {
throw new Error('"formData" must be a valid object')
throw new Error(`"formData" must be a valid object (content type: ${contentType.id})`)
}

const expandedFormData = await this.resolveRefs(formData)
Expand Down

0 comments on commit 1a52d8b

Please sign in to comment.