Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(web-channel): race condition from visit event #12435

Merged
merged 1 commit into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 0 additions & 16 deletions modules/channel-web/src/backend/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,28 +226,12 @@ export default async (bp: typeof sdk, db: Database) => {
asyncMiddleware(async (req: ChatRequest, res: Response) => {
const { botId, userId } = req

const user = await bp.users.getOrCreateUser('web', userId, botId)
const payload = req.body.payload || {}

if (!SUPPORTED_MESSAGES.includes(payload.type)) {
return res.status(400).send(ERR_MSG_TYPE)
}

if (payload.type === 'visit') {
const { timezone, language } = payload
const isValidTimezone = _.isNumber(timezone) && timezone >= -12 && timezone <= 14 && timezone % 0.5 === 0
const isValidLanguage = language.length < 4 && !_.get(user, 'result.attributes.language')

const newAttributes = {
...(isValidTimezone && { timezone }),
...(isValidLanguage && { language })
}

if (Object.getOwnPropertyNames(newAttributes).length) {
await bp.users.updateAttributes('web', userId, newAttributes)
}
}

if (!req.conversationId) {
req.conversationId = (await getRecent(req.messaging, userId)).id
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const _ = require('lodash')

async function visitEvent() {
if (event.type === 'visit') {
const user = await bp.users.getOrCreateUser('web', event.target, event.botId)

const { timezone, language } = event.payload
const isValidTimezone = _.isNumber(timezone) && timezone >= -12 && timezone <= 14 && timezone % 0.5 === 0
const isValidLanguage = language.length < 4 && !_.get(user, 'result.attributes.language')

const newAttributes = {
...(isValidTimezone && { timezone }),
...(isValidLanguage && { language })
}

if (Object.getOwnPropertyNames(newAttributes).length) {
event.state.user = { ...event.state.user, ...newAttributes }
}
}
}

return visitEvent()
2 changes: 1 addition & 1 deletion modules/channel-web/src/views/lite/core/constants.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
/** These types are sent using the /message/ endpoint */
MESSAGE_TYPES: ['text', 'quick_reply', 'form', 'login_prompt', 'visit', 'postback'],
MESSAGE_TYPES: ['text', 'quick_reply', 'form', 'login_prompt', 'postback'],
/** The duration of the hide / show chat */
ANIM_DURATION: 300,
MIN_TIME_BETWEEN_SOUNDS: 1000,
Expand Down
3 changes: 2 additions & 1 deletion packages/bp/src/core/dialog/state-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,13 @@ export class StateManager {
}

const batchCount = this.batch.length >= BATCH_SIZE ? BATCH_SIZE : this.batch.length
const elements = this.batch.splice(0, batchCount)
const elements = this.batch.slice(0, batchCount)

this.currentPromise = this.knex
.transaction(async trx => {
for (const { event, ignoreContext } of elements) {
await this._saveState(event, ignoreContext, trx)
_.remove(this.batch, { event })
}
})
.finally(() => {
Expand Down