Skip to content

Commit

Permalink
fix(channel-web): set last_heard_on for new convos
Browse files Browse the repository at this point in the history
  • Loading branch information
emirotin committed Jul 23, 2018
1 parent 160432e commit 6c05b92
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/channels/botpress-channel-web/package.json
Expand Up @@ -72,6 +72,7 @@
"lodash": "^4.17.4",
"mime": "^2.0.3",
"moment": "^2.18.1",
"ms": "^2.1.1",
"multer": "^1.3.0",
"multer-s3": "^2.7.0",
"query-string": "^5.0.1",
Expand Down
12 changes: 9 additions & 3 deletions packages/channels/botpress-channel-web/src/db.js
@@ -1,11 +1,15 @@
import moment from 'moment'
import _ from 'lodash'
import uuid from 'uuid'
import ms from 'ms'

import { DatabaseHelpers as helpers } from 'botpress'

import { sanitizeUserId } from './util'

// TODO make these vars configurable
const RECENT_CONVERSATION_LIFETIME = ms('6 hours')

module.exports = knex => {
async function getUserInfo(userId) {
const user = await knex('users')
Expand Down Expand Up @@ -132,6 +136,7 @@ module.exports = knex => {
.insert({
userId,
created_on: helpers(knex).date.now(),
last_heard_on: helpers(knex).date.now(),
title
})
.then()
Expand All @@ -141,7 +146,6 @@ module.exports = knex => {
.select('id')
.then()
.get(0)
.then()

return conversation && conversation.id
}
Expand All @@ -162,8 +166,10 @@ module.exports = knex => {
async function getOrCreateRecentConversation(userId) {
userId = sanitizeUserId(userId)

// TODO make the conversation lifetime configurable
const recentCondition = helpers(knex).date.isAfter('last_heard_on', moment().subtract(6, 'hours'))
const recentCondition = helpers(knex).date.isAfter(
'last_heard_on',
moment().subtract(RECENT_CONVERSATION_LIFETIME, 'ms')
)
const conversation = await knex('web_conversations')
.select('id')
.where({ userId })
Expand Down
4 changes: 4 additions & 0 deletions packages/channels/botpress-channel-web/yarn.lock
Expand Up @@ -3731,6 +3731,10 @@ ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

ms@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.1.tgz#30a5864eb3ebb0a66f2ebe6d727af06a09d86e0a"

multer-s3@^2.7.0:
version "2.7.0"
resolved "https://registry.yarnpkg.com/multer-s3/-/multer-s3-2.7.0.tgz#5e09ce855d77f91abd1ff57f4cf32118a3e16475"
Expand Down

0 comments on commit 6c05b92

Please sign in to comment.