From 96b931e181d4d84a18efaba34e181c0600a145e8 Mon Sep 17 00:00:00 2001 From: Huan LI Date: Thu, 24 May 2018 22:23:34 +0800 Subject: [PATCH] code clean --- examples/ding-dong-bot.ts | 18 ++++++---------- package.json | 2 +- src/puppet-mock/puppet-mock.ts | 27 ++++++++++++------------ src/puppet-puppeteer/puppet-puppeteer.ts | 10 +++------ src/puppet/puppet.ts | 9 ++++---- tests/fixtures/smoke-testing.ts | 2 +- 6 files changed, 31 insertions(+), 37 deletions(-) diff --git a/examples/ding-dong-bot.ts b/examples/ding-dong-bot.ts index f90a103d..e17f1a43 100644 --- a/examples/ding-dong-bot.ts +++ b/examples/ding-dong-bot.ts @@ -80,26 +80,22 @@ bot }) } }) -.on('message', async m => { +.on('message', async msg => { try { - const room = m.room() - console.log( - (room ? `${room}` : '') - + `${m.from()}:${m}`, - ) + console.log(msg.toString()) - if (/^(ding|ping|bing|code)$/i.test(m.text()) && !m.self()) { + if (/^(ding|ping|bing|code)$/i.test(msg.text()) && !msg.self()) { /** * 1. reply 'dong' */ log.info('Bot', 'REPLY: dong') - m.say('dong') + msg.say('dong') const joinWechaty = `Join Wechaty Developers' Community\n\n` + `Wechaty is used in many ChatBot projects by hundreds of developers.\n\n` + `If you want to talk with other developers, just scan the following QR Code in WeChat with secret code: wechaty,\n\n` + `you can join our Wechaty Developers' Home at once` - await m.say(joinWechaty) + await msg.say(joinWechaty) /** * 2. reply qrcode image @@ -107,12 +103,12 @@ bot const fileBox = FileBox.fromLocal(BOT_QR_CODE_IMAGE_FILE) log.info('Bot', 'REPLY: %s', fileBox) - await m.say(fileBox) + await msg.say(fileBox) /** * 3. reply 'scan now!' */ - await m.say('Scan now, because other Wechaty developers want to talk with you too!\n\n(secret code: wechaty)') + await msg.say('Scan now, because other Wechaty developers want to talk with you too!\n\n(secret code: wechaty)') } } catch (e) { diff --git a/package.json b/package.json index fc43c055..f34591fc 100644 --- a/package.json +++ b/package.json @@ -94,7 +94,7 @@ "dependencies": { "bl": "^1.2.0", "brolog": "^1.2.0", - "clone-class": "^0.6.10", + "clone-class": "^0.6.11", "cuid": "^2.1.1", "file-box": "^0.2.1", "hot-import": "^0.2.1", diff --git a/src/puppet-mock/puppet-mock.ts b/src/puppet-mock/puppet-mock.ts index f31baeb0..9f642748 100644 --- a/src/puppet-mock/puppet-mock.ts +++ b/src/puppet-mock/puppet-mock.ts @@ -77,10 +77,6 @@ export class PuppetMock extends Puppet { super(options) } - public toString() { - return `PuppetMock<${this.options.profile.name}>` - } - public ding(data?: any): Promise { return data } @@ -92,14 +88,15 @@ export class PuppetMock extends Puppet { // await some tasks... this.state.on(true) - const user = this.Contact.load('logined_user_id') - const msg = this.Message.createMT('mock_id') - - this.user = user + this.userId = 'logined_user_id' + const user = this.Contact.load(this.userId) this.emit('login', user) + const msg = this.Message.createMT('mock_id') + await msg.ready() + setInterval(() => { - log.verbose('PuppetMock', `start() setInterval() pretending received a new message: ${msg}`) + log.verbose('PuppetMock', `start() setInterval() pretending received a new message: ${msg + ''}`) this.emit('message', msg) }, 3000) @@ -126,8 +123,8 @@ export class PuppetMock extends Puppet { throw new Error('logout before login?') } - this.emit('logout', this.user!) // becore we will throw above by logonoff() when this.user===undefined - this.user = undefined + this.emit('logout', this.userId!) // becore we will throw above by logonoff() when this.user===undefined + this.userId = undefined // TODO: do the logout job } @@ -224,7 +221,9 @@ export class PuppetMock extends Puppet { * Room * */ - public async roomRawPayload(id: string): Promise { + public async roomRawPayload( + id: string, + ): Promise { log.verbose('PuppetMock', 'roomRawPayload(%s)', id) const rawPayload: MockRoomRawPayload = { @@ -235,7 +234,9 @@ export class PuppetMock extends Puppet { return rawPayload } - public async roomRawPayloadParser(rawPayload: MockRoomRawPayload): Promise { + public async roomRawPayloadParser( + rawPayload: MockRoomRawPayload, + ): Promise { log.verbose('PuppetMock', 'roomRawPayloadParser(%s)', rawPayload) const payload: RoomPayload = { diff --git a/src/puppet-puppeteer/puppet-puppeteer.ts b/src/puppet-puppeteer/puppet-puppeteer.ts index df259198..8e5acbca 100644 --- a/src/puppet-puppeteer/puppet-puppeteer.ts +++ b/src/puppet-puppeteer/puppet-puppeteer.ts @@ -445,7 +445,7 @@ export class PuppetPuppeteer extends Puppet { throw new Error('forward() Due to webWx restrictions, more than 25MB of files can not be downloaded and can not be forwarded.') } - newMsg.FromUserName = this.user && this.user.id || '' + newMsg.FromUserName = this.userId || '' newMsg.isTranspond = true newMsg.MsgIdBeforeTranspond = rawPayload.MsgIdBeforeTranspond || rawPayload.MsgId newMsg.MMSourceMsgId = rawPayload.MsgId @@ -525,14 +525,10 @@ export class PuppetPuppeteer extends Puppet { public async login(user: Contact): Promise { log.warn('PuppetPuppeteer', 'login(%s)', user) - this.user = user + this.userId = user.id this.emit('login', user) } - public logonoff(): boolean { - return !!(this.user) - } - /** * logout from browser, then server will emit `logout` event */ @@ -552,7 +548,7 @@ export class PuppetPuppeteer extends Puppet { Raven.captureException(e) throw e } finally { - this.user = undefined + this.userId = undefined this.emit('logout', user) } } diff --git a/src/puppet/puppet.ts b/src/puppet/puppet.ts index 9adc03ec..b6389080 100644 --- a/src/puppet/puppet.ts +++ b/src/puppet/puppet.ts @@ -96,7 +96,7 @@ export abstract class Puppet extends EventEmitter implements Sayable { protected readonly watchdog: Watchdog - protected user?: Contact + protected userId?: string /* tslint:disable:variable-name */ public readonly Contact : typeof Contact @@ -242,11 +242,12 @@ export abstract class Puppet extends EventEmitter implements Sayable { public userSelf(): Contact { log.verbose('Puppet', 'self()') - if (!this.user) { + if (!this.userId) { throw new Error('not logged in, no userSelf yet.') } - return this.user + const user = this.Contact.load(this.userId) + return user } public async say(textOrFile: string | FileBox) : Promise { @@ -277,7 +278,7 @@ export abstract class Puppet extends EventEmitter implements Sayable { * Login / Logout */ public logonoff(): boolean { - if (this.user) { + if (this.userId) { return true } else { return false diff --git a/tests/fixtures/smoke-testing.ts b/tests/fixtures/smoke-testing.ts index 5ad9880b..2516b92e 100644 --- a/tests/fixtures/smoke-testing.ts +++ b/tests/fixtures/smoke-testing.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env node +#!/usr/bin/env ts-node import { Wechaty } from 'wechaty' -- GitLab