Skip to content

Commit

Permalink
fix(shell): Allow shell adapter to set user ID
Browse files Browse the repository at this point in the history
  • Loading branch information
timkinnane committed Aug 12, 2018
1 parent bce1506 commit cf1ae84
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
3 changes: 1 addition & 2 deletions .nycrc
Expand Up @@ -14,6 +14,5 @@
"reporter": [
"lcovonly",
"text"
],
"cache": false
]
}
17 changes: 14 additions & 3 deletions src/adapters/shell.ts
Expand Up @@ -13,7 +13,9 @@ export class Shell extends bBot.MessageAdapter {
settings = {
chatSize: 5
}
userName = process.env.BOT_SHELL_USER
// @todo extend bot settings instead...
userName = process.env.BOT_SHELL_USER_NAME
userId = process.env.BOT_SHELL_USER_ID
roomName = process.env.BOT_SHELL_ROOM
transport?: Transport
user?: bBot.User
Expand Down Expand Up @@ -61,21 +63,30 @@ export class Shell extends bBot.MessageAdapter {
/** Write prompt to collect room and user name, or take from env settings */
async roomSetup () {
if (this.userName && this.roomName) {
this.user = new this.bot.User({ name: this.userName })
this.user = new this.bot.User({ name: this.userName, id: this.userId })
this.room = { name: this.roomName }
} else {
const registration: any = await inquirer.prompt([{
type: 'input',
name: 'username',
message: 'Welcome! What shall I call you?',
default: 'user'
},{
type: 'input',
name: 'userId',
message: 'Use ID for user, or generate random?',
default: 'random'
},{
type: 'input',
name: 'room',
message: 'And what about this "room"?',
default: 'shell'
}])
this.user = new this.bot.User({ name: registration.username })
if (registration.userId !== 'random') this.userId = registration.userId
this.user = new this.bot.User({
name: registration.username,
id: this.userId
})
this.room = { name: registration.room }
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/lib/user.spec.ts
Expand Up @@ -9,6 +9,10 @@ describe('[user]', () => {
const testUser = new user.User()
expect(testUser.id).to.have.lengthOf(32)
})
it('assigns ID if given undefined attribute', () => {
const testUser = new user.User({ id: undefined })
expect(testUser.id).to.have.lengthOf(32)
})
it('accepts ID if given', () => {
const testUser = new user.User({ id: 'TEST_ID' })
expect(testUser.id).to.equal('TEST_ID')
Expand Down
11 changes: 8 additions & 3 deletions src/lib/user.ts
Expand Up @@ -23,10 +23,15 @@ export class User implements IUser {
[key: string]: any

/** Create a User */
constructor (meta?: IUser) {
this.id = (meta && meta.id) ? meta.id : bot.random()
if (meta) Object.keys(meta).forEach((key: string) => this[key] = meta[key])
constructor (meta: IUser = {}) {
this.id = meta.id || bot.random()
Object.keys(meta).forEach((key: string) => {
if (typeof(meta[key] !== 'undefined') && meta[key] !== null) {
this[key] = meta[key]
}
})
this.room = (meta && meta.room) ? meta.room : {}
if (!this.id) this.id = bot.random()
if (!this.name) this.name = this.id
}
}

0 comments on commit cf1ae84

Please sign in to comment.