From 8863a8e51b5ac43f89dd825e3e131d905768b2c4 Mon Sep 17 00:00:00 2001 From: "Zhuohuan LI (CARPE DIEM)" Date: Thu, 6 Oct 2016 04:50:32 +0800 Subject: [PATCH] #34 Contact.find && findAll --- src/contact.js | 83 ++++++++++++++++++++++++++++++----- src/puppet-web/bridge.js | 8 ++++ src/puppet-web/puppet-web.js | 19 ++++++-- src/puppet-web/wechaty-bro.js | 27 +++++++++++- 4 files changed, 119 insertions(+), 18 deletions(-) diff --git a/src/contact.js b/src/contact.js index fe55c681..271a040f 100644 --- a/src/contact.js +++ b/src/contact.js @@ -88,16 +88,80 @@ class Contact { Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj[k]}`)) } - static find() { - return new Contact('-1') + // private + static _find({ + name + }) { + log.silly('Cotnact', '_find(%s)', name) + + if (!name) { + throw new Error('name not found') + } + + let filterFunction + if (name instanceof RegExp) { + filterFunction = `c => ${name.toString()}.test(c)` + } else if (typeof name === 'string') { + filterFunction = `c => c === '${name}'` + } else { + throw new Error('unsupport name type') + } + + return Config.puppetInstance() + .contactFind(filterFunction) + .then(idList => { + return idList + }) + .catch(e => { + log.error('Contact', '_find() rejected: %s', e.message) + throw e + }) } - static findAll() { - return [ - new Contact ('-2') - , new Contact ('-3') - ] + static find({ + name + }) { + log.verbose('Contact', 'find(%s)', name) + + return Contact._find({name}) + .then(idList => { + if (!idList || !Array.isArray(idList)){ + throw new Error('_find return error') + } + if (idList.length < 1) { + return null + } + const id = idList[0] + return Contact.load(id) + }) + .catch(e => { + log.error('Contact', 'find() rejected: %s', e.message) + return null // fail safe + }) } + + static findAll({ + name + }) { + log.verbose('Contact', 'findAll(%s)', name) + + return Contact._find({name}) + .then(idList => { + // console.log(idList) + if (!idList || !Array.isArray(idList)){ + throw new Error('_find return error') + } + if (idList.length < 1) { + return [] + } + return idList.map(i => Contact.load(i)) + }) + .catch(e => { + log.error('Contact', 'findAll() rejected: %s', e.message) + return [] // fail safe + }) + } + } Contact.init = function() { Contact.pool = {} } @@ -124,9 +188,4 @@ Contact.load = function(id) { // return [] // } -// Contact.attach = function(puppet) { -// log.warn('Contact', '@deprecated attach() to %s', puppet && puppet.constructor.name) -// // Config.puppetInstance(puppet) -// } - module.exports = Contact.default = Contact.Contact = Contact diff --git a/src/puppet-web/bridge.js b/src/puppet-web/bridge.js index b40450ae..3024ad09 100644 --- a/src/puppet-web/bridge.js +++ b/src/puppet-web/bridge.js @@ -143,6 +143,14 @@ class Bridge { }) } + contactFind(filterFunction) { + return this.proxyWechaty('contactFind', filterFunction) + .catch(e => { + log.error('PuppetWebBridge', 'contactFind() exception: %s', e.message) + throw e + }) + } + roomFind(filterFunction) { return this.proxyWechaty('roomFind', filterFunction) .catch(e => { diff --git a/src/puppet-web/puppet-web.js b/src/puppet-web/puppet-web.js index 9dd3c716..237e5f48 100644 --- a/src/puppet-web/puppet-web.js +++ b/src/puppet-web/puppet-web.js @@ -348,6 +348,17 @@ class PuppetWeb extends Puppet { }) } + contactFind(filterFunction) { + if (!this.bridge) { + return Promise.reject(new Error('contactFind fail: no bridge(yet)!')) + } + return this.bridge.contactFind(filterFunction) + .catch(e => { + log.warn('PuppetWeb', 'contactFind(%s) rejected: %s', filterFunction, e.message) + throw e + }) + } + roomFind(filterFunction) { if (!this.bridge) { return Promise.reject(new Error('findRoom fail: no bridge(yet)!')) @@ -400,7 +411,7 @@ class PuppetWeb extends Puppet { }) } - roomCreate(contactList) { + roomCreate(contactList, topic) { if (!this.bridge) { return Promise.reject(new Error('fail: no bridge(yet)!')) } @@ -409,11 +420,11 @@ class PuppetWeb extends Puppet { throw new Error('contactList not found') } - const contactIdList = contactList.map(c => c.get('id')) + const contactIdList = contactList.map(c => c.id) - return this.bridge.roomCreate(contactIdList) + return this.bridge.roomCreate(contactIdList, topic) .catch(e => { - log.warn('PuppetWeb', 'roomCreate(%s) rejected: %s', contact, e.message) + log.warn('PuppetWeb', 'roomCreate(%s, %s) rejected: %s', contactIdList.join(,), topic, e.message) throw e }) } diff --git a/src/puppet-web/wechaty-bro.js b/src/puppet-web/wechaty-bro.js index f8286fe8..b26a726a 100644 --- a/src/puppet-web/wechaty-bro.js +++ b/src/puppet-web/wechaty-bro.js @@ -451,6 +451,21 @@ : null } + function contactFind(filterFunction) { + var contactFactory = WechatyBro.glue.contactFactory + + var match + if (!filterFunction) { + match = function() { return true } + } else { + match = eval(filterFunction) + } + // log(match.toString()) + return contactFactory.getAllFriendContact() + .filter(r => match(r.NickName)) + .map(r => r.UserName) + } + function roomFind(filterFunction) { var contactFactory = WechatyBro.glue.contactFactory @@ -484,20 +499,25 @@ return chatroomFactory.modTopic(ChatRoomName, topic) } - function roomCreate(UserNameList) { + function roomCreate(UserNameList, topic) { const UserNameListArg = UserNameList.map(n => { return { UserName: n } }) const chatroomFactory = WechatyBro.glue.chatroomFactory const state = WechatyBro.glue.state + chatroomFactory.create(UserNameListArg) .then(r => { if (r.BaseResponse && 0 == r.BaseResponse.Ret || -2013 == e.BaseResponse.Ret) { state.go('chat', { userName: r.ChatRoomName }) // BE CAREFUL: key name is userName, not UserName! 20161001 + if (topic) { + roomModTopic(r.ChatRoomName, topic) + } } + return r.ChatRoomName }) .catch(e => { // TBD - console.log(e) + log(e) }) return 'no callback (yet)' } @@ -607,6 +627,9 @@ , getUserName: getUserName , getMsgImg: getMsgImg + // for Wechaty Contact Class + , contactFind + // for Wechaty Room Class , roomFind , roomCreate -- GitLab