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

fix Contact/Room filter func

上级 922b8717
...@@ -125,7 +125,6 @@ type WatchdogFood = { ...@@ -125,7 +125,6 @@ type WatchdogFood = {
, timeout?: number // millisecond , timeout?: number // millisecond
} }
// module.exports = Config.default = Config.Config = Config
export default Config export default Config
export { export {
Config Config
......
...@@ -135,7 +135,12 @@ class Contact { ...@@ -135,7 +135,12 @@ class Contact {
throw new Error('name not found') throw new Error('name not found')
} }
let filterFunction /**
* must be string because we need inject variable value
* into code as variable name
*/
let filterFunction: string
if (name instanceof RegExp) { if (name instanceof RegExp) {
filterFunction = `c => ${name.toString()}.test(c)` filterFunction = `c => ${name.toString()}.test(c)`
} else if (typeof name === 'string') { } else if (typeof name === 'string') {
......
...@@ -134,8 +134,9 @@ class IoClient { ...@@ -134,8 +134,9 @@ class IoClient {
this.log.verbose('IoClient', 'initIo() with token %s', this.token) this.log.verbose('IoClient', 'initIo() with token %s', this.token)
if (this.targetState() !== 'connected') { if (this.targetState() !== 'connected') {
this.log.warn('IoClient', 'initIo() targetState is not `connected`, skipped') const errMsg = 'initIo() targetState is not `connected`, skipped'
return Promise.resolve() this.log.warn('IoClient', errMsg)
return Promise.reject(errMsg)
} }
if (!this.wechaty) { if (!this.wechaty) {
......
...@@ -150,23 +150,23 @@ class Bridge { ...@@ -150,23 +150,23 @@ class Bridge {
}) })
} }
public contactFind(filterFunction): Promise<string[]> { public contactFind(filterFunc: string): Promise<string[]> {
return this.proxyWechaty('contactFindAsync', filterFunction) return this.proxyWechaty('contactFindAsync', filterFunc)
.catch(e => { .catch(e => {
log.error('PuppetWebBridge', 'contactFindAsync() exception: %s', e.message) log.error('PuppetWebBridge', 'contactFindAsync() exception: %s', e.message)
throw e throw e
}) })
} }
public roomFind(filterFunction): Promise<string[]> { public roomFind(filterFunc: string): Promise<string[]> {
return this.proxyWechaty('roomFind', filterFunction) return this.proxyWechaty('roomFind', filterFunc)
.catch(e => { .catch(e => {
log.error('PuppetWebBridge', 'roomFind() exception: %s', e.message) log.error('PuppetWebBridge', 'roomFind() exception: %s', e.message)
throw e throw e
}) })
} }
public roomDelMember(roomId, contactId): Promise<void> { public roomDelMember(roomId, contactId): Promise<number> {
if (!roomId || !contactId) { if (!roomId || !contactId) {
throw new Error('no roomId or contactId') throw new Error('no roomId or contactId')
} }
...@@ -178,7 +178,7 @@ class Bridge { ...@@ -178,7 +178,7 @@ class Bridge {
}) })
} }
public roomAddMember(roomId, contactId): Promise<void> { public roomAddMember(roomId, contactId): Promise<number> {
log.verbose('PuppetWebBridge', 'roomAddMember(%s, %s)', roomId, contactId) log.verbose('PuppetWebBridge', 'roomAddMember(%s, %s)', roomId, contactId)
if (!roomId || !contactId) { if (!roomId || !contactId) {
......
...@@ -114,11 +114,13 @@ class Browser extends EventEmitter { ...@@ -114,11 +114,13 @@ class Browser extends EventEmitter {
} }
} }
public open(url: string = 'https://wx.qq.com'): Promise<void> { public open(url: string = 'https://wx.qq.com'): Promise<any> {
log.verbose('PuppetWebBrowser', `open(${url})`) log.verbose('PuppetWebBrowser', `open(${url})`)
// TODO: set a timer to guard driver.get timeout, then retry 3 times 201607 // TODO: set a timer to guard driver.get timeout, then retry 3 times 201607
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// wrap another Promise is because selenium use another Promise library,
// which is incompatible with node typescript types
this.driver.get(url) this.driver.get(url)
.then(_ => resolve()) .then(_ => resolve())
.catch(e => { .catch(e => {
......
...@@ -368,26 +368,26 @@ class PuppetWeb extends Puppet { ...@@ -368,26 +368,26 @@ class PuppetWeb extends Puppet {
}) })
} }
public contactFind(filterFunction: Function): Promise<Contact[]> { public contactFind(filterFunc: string): Promise<Contact[]> {
if (!this.bridge) { if (!this.bridge) {
return Promise.reject(new Error('contactFind fail: no bridge(yet)!')) return Promise.reject(new Error('contactFind fail: no bridge(yet)!'))
} }
return this.bridge.contactFind(filterFunction) return this.bridge.contactFind(filterFunc)
.then(idList => idList.map(id => Contact.load(id))) .then(idList => idList.map(id => Contact.load(id)))
.catch(e => { .catch(e => {
log.warn('PuppetWeb', 'contactFind(%s) rejected: %s', filterFunction, e.message) log.warn('PuppetWeb', 'contactFind(%s) rejected: %s', filterFunc, e.message)
throw e throw e
}) })
} }
public roomFind(filterFunction: Function): Promise<Room[]> { public roomFind(filterFunc: string): Promise<Room[]> {
if (!this.bridge) { if (!this.bridge) {
return Promise.reject(new Error('findRoom fail: no bridge(yet)!')) return Promise.reject(new Error('findRoom fail: no bridge(yet)!'))
} }
return this.bridge.roomFind(filterFunction) return this.bridge.roomFind(filterFunc)
.then(idList => idList.map(id => Room.load(id))) .then(idList => idList.map(id => Room.load(id)))
.catch(e => { .catch(e => {
log.warn('PuppetWeb', 'roomFind(%s) rejected: %s', filterFunction, e.message) log.warn('PuppetWeb', 'roomFind(%s) rejected: %s', filterFunc, e.message)
throw e throw e
}) })
} }
......
...@@ -86,9 +86,9 @@ abstract class Puppet extends EventEmitter { ...@@ -86,9 +86,9 @@ abstract class Puppet extends EventEmitter {
public abstract roomDel(room: Room, contact: Contact): Promise<number> public abstract roomDel(room: Room, contact: Contact): Promise<number>
public abstract roomTopic(room: Room, topic: string): Promise<string> public abstract roomTopic(room: Room, topic: string): Promise<string>
public abstract roomCreate(contactList: Contact[], topic?: string): Promise<Room> public abstract roomCreate(contactList: Contact[], topic?: string): Promise<Room>
public abstract roomFind(filter: Function): Promise<Room[]> public abstract roomFind(filterFunc: string): Promise<Room[]>
public abstract contactFind(filter: Function): Promise<Contact[]> public abstract contactFind(filterFunc: string): Promise<Contact[]>
} }
export default Puppet export default Puppet
......
...@@ -341,7 +341,9 @@ class Room extends EventEmitter { ...@@ -341,7 +341,9 @@ class Room extends EventEmitter {
throw new Error('topic not found') throw new Error('topic not found')
} }
let filterFunction // see also Contact.findAll
let filterFunction: string
if (topic instanceof RegExp) { if (topic instanceof RegExp) {
filterFunction = `c => ${topic.toString()}.test(c)` filterFunction = `c => ${topic.toString()}.test(c)`
} else if (typeof topic === 'string') { } else if (typeof topic === 'string') {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册