From aee56601d358fe8ff03e2bda16ac4b64cfc3cab5 Mon Sep 17 00:00:00 2001 From: "Zhuohuan LI (CARPE DIEM)" Date: Wed, 5 Oct 2016 04:37:00 +0800 Subject: [PATCH] code clean --- example/api-ai-bot.js | 2 +- example/ding-dong-bot.js | 2 +- src/contact.js | 4 ++-- src/io-client.js | 14 +++++++------- src/message.js | 4 ++-- src/puppet-web/event.js | 5 ++--- 6 files changed, 15 insertions(+), 16 deletions(-) diff --git a/example/api-ai-bot.js b/example/api-ai-bot.js index 8b1c6408..9db08bef 100644 --- a/example/api-ai-bot.js +++ b/example/api-ai-bot.js @@ -52,7 +52,7 @@ bot const msg = yield m.ready() const room = Wechaty.Room.load(m.room()) - if (room && /Wechaty/i.test(room.name())) { + if (room && /Wechaty/i.test(room.topic())) { log.info('Bot', 'talk: %s' , msg) talk(m) } else { diff --git a/example/ding-dong-bot.js b/example/ding-dong-bot.js index 8b9f6614..f6c29b65 100644 --- a/example/ding-dong-bot.js +++ b/example/ding-dong-bot.js @@ -48,7 +48,7 @@ bot const room = m.room() const from = m.from() - console.log((room ? '['+room.name()+']' : '') + console.log((room ? '['+room.topic()+']' : '') + '<'+from.name()+'>' + ':' + m.toStringDigest() ) diff --git a/src/contact.js b/src/contact.js index 443b696e..fe55c681 100644 --- a/src/contact.js +++ b/src/contact.js @@ -26,7 +26,7 @@ class Contact { parse(rawObj) { return !rawObj ? {} : { id: rawObj.UserName - , uin: rawObj.Uin // stable id? 4763975 || getCookie("wxuin") + , uin: rawObj.Uin // stable id: 4763975 || getCookie("wxuin") , weixin: rawObj.Alias // Wechat ID , name: rawObj.NickName , remark: rawObj.RemarkName @@ -105,7 +105,7 @@ Contact.init() Contact.load = function(id) { if (!id || typeof id !== 'string') { - throw new Error('id must be string') + return null } if (!(id in Contact.pool)) { diff --git a/src/io-client.js b/src/io-client.js index 9161da35..90611771 100644 --- a/src/io-client.js +++ b/src/io-client.js @@ -135,7 +135,7 @@ class IoClient { } const wechaty = this.wechaty - + const io = new Io({ wechaty , token: this.token() @@ -174,7 +174,7 @@ class IoClient { const room = m.room() // this.log.info('Bot', '%s<%s>:%s' - // , (room ? '['+room.name()+']' : '') + // , (room ? '['+room.topic()+']' : '') // , from.name() // , m.toStringDigest() // ) @@ -196,10 +196,10 @@ class IoClient { this.log.warn('IoClient', 'start() with a pending state, not the time') return Promise.reject('pending') } - + this.targetState('connected') this.currentState('connecting') - + return this.initIo(this.wechaty) .then(io => { this.io = io @@ -252,7 +252,7 @@ class IoClient { quit() { this.log.verbose('IoClient', 'quit()') - + if (this.currentState() === 'disconnecting') { this.log.warn('IoClient', 'quit() with currentState() = `disconnecting`, skipped') return Promise.reject('quit() with currentState = `disconnecting`') @@ -270,13 +270,13 @@ class IoClient { if (this.io) { yield this.io.quit() - this.io = null + this.io = null } else { this.log.warn('IoClient', 'quit() no this.io') } this.currentState('disconnected') }).catch(e => { this.log.error('IoClient', 'exception: %s', e.message) - + // XXX fail safe? this.currentState('disconnected') diff --git a/src/message.js b/src/message.js index 29211b47..2855ea33 100644 --- a/src/message.js +++ b/src/message.js @@ -83,9 +83,9 @@ class Message { } from() { return Contact.load(this.obj.from) } - to() { return Contact.load(this.obj.to) } + to() { return this.obj.to ? Contact.load(this.obj.to) : null } content() { return this.obj.content } - room() { return Room.load(this.obj.room) } + room() { return this.obj.room ? Room.load(this.obj.room) : null } type() { return this.obj.type } typeEx() { return Message.Type[this.obj.type] } diff --git a/src/puppet-web/event.js b/src/puppet-web/event.js index 12830fec..16f45510 100644 --- a/src/puppet-web/event.js +++ b/src/puppet-web/event.js @@ -346,8 +346,7 @@ function onServerMessage(data) { const request = new FriendRequest() request.receive(data.RecommendInfo) - const contact = Contact.load(request.contactId) - this.emit('friend', contact, request) + this.emit('friend', request.contact, request) break case Message.Type.SYS: @@ -358,7 +357,7 @@ function onServerMessage(data) { /** * try to find FriendRequest Confirmation Message */ - if (/^You have added (.+) as your WeChat contact. Start chatting!$/.test(m.get('content'))) { + if (/^You have added(.+)as your WeChat contact. Start chatting!$/.test(m.get('content'))) { const request = new FriendRequest() const contact = Contact.load(m.get('from')) request.confirm(contact) -- GitLab