<commit_before>import Bluebird from 'bluebird'
import * as sdk from 'botpress/sdk'
import _ from 'lodash'
import moment from 'moment'
import ms from 'ms'
import uuid from 'uuid'

import { Config } from '../config'

export default class WebchatDb {
  knex: any
  users: typeof sdk.users

  constructor(private bp: typeof sdk) {
    this.users = bp.users
    this.knex = bp['database'] // TODO Fixme
  }

  async getUserInfo(userId) {
    const { result: user } = await this.users.getOrCreateUser('web', userId)

    const fullName = `${user.attributes['first_name']} ${user.attributes['last_name']}`
    const avatar = (user && user.attributes['picture_url']) || undefined

    return {
      fullName,
      avatar_url: avatar
    }
  }

  async initialize() {
    return this.knex
      .createTableIfNotExists('web_conversations', function(table) {
        table.increments('id').primary()
        table.string('userId')
        table.string('botId')
        table.string('title')
        table.string('description')
        table.string('logo_url')
        table.timestamp('created_on')
        table.timestamp('last_heard_on') // The last time the user interacted with the bot. Used for "recent" conversation
        table.timestamp('user_last_seen_on')
        table.timestamp('bot_last_seen_on')
      })<commit_msg>fix(channel-web): prevent user name from being undefined<commit_after>import Bluebird from 'bluebird'
import * as sdk from 'botpress/sdk'
import _ from 'lodash'
import moment from 'moment'
import ms from 'ms'
import uuid from 'uuid'

import { Config } from '../config'

export default class WebchatDb {
  knex: any
  users: typeof sdk.users

  constructor(private bp: typeof sdk) {
    this.users = bp.users
    this.knex = bp['database'] // TODO Fixme
  }

  async getUserInfo(userId) {
    const { result: user } = await this.users.getOrCreateUser('web', userId)

    let fullName = 'User'

    if (user && user.attributes) {
      const { first_name, last_name } = user.attributes

      if (first_name || last_name) {
        fullName = `${first_name || ''} ${last_name || ''}`
      }
    }

    return { fullName, avatar_url: _.get(user, 'attributes.picture_url') }
  }

  async initialize() {
    return this.knex
      .createTableIfNotExists('web_conversations', function(table) {
        table.increments('id').primary()
        table.string('userId')
        table.string('botId')
        table.string('title')
        table.string('description')
        table.string('logo_url')
        table.timestamp('created_on')
        table.timestamp('last_heard_on') // The last time the user interacted with the bot. Used for "recent" conversation
        table.timestamp('user_last_seen_on')
        table.timestamp('bot_last_seen_on')
      })
{'fraction_non_alphanumeric': 0.12630819198845183, 'fraction_numerical': 0.0, 'mean_word_length': 3.187311178247734, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1994035', 'n_tokens_mistral': 853, 'n_tokens_neox': 835, 'n_words': 208}