提交 8863a8e5 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

#34 Contact.find && findAll

上级 abf5b258
......@@ -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
......@@ -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 => {
......
......@@ -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
})
}
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册