diff --git a/src/abstract-puppet/contact.ts b/src/abstract-puppet/contact.ts index 64200d54c025a327cdcb32cc1fc7af8ee9ce9b44..964d015847875c0b423fefa1effa042d013d3315 100644 --- a/src/abstract-puppet/contact.ts +++ b/src/abstract-puppet/contact.ts @@ -44,6 +44,8 @@ export interface ContactQueryFilter { alias?: string | RegExp, } +export type ContactQueryName = keyof ContactQueryFilter + /** * All wechat contacts(friend) will be encapsulated as a Contact. * diff --git a/src/abstract-puppet/message.ts b/src/abstract-puppet/message.ts index 51f09e4815392619304e3a1a25023d5554f79e5d..11a3f7f323971e48b2a4efca849b6f1faf2d28cc 100644 --- a/src/abstract-puppet/message.ts +++ b/src/abstract-puppet/message.ts @@ -34,11 +34,6 @@ import Contact from './contact' import Room from './room' import PuppetAccessory from './puppet-accessory' -// export type TypeName = 'attachment' -// | 'audio' -// | 'image' -// | 'video' - /** * All wechat messages will be encapsulated as a Message. * diff --git a/src/abstract-puppet/puppet-accessory.ts b/src/abstract-puppet/puppet-accessory.ts index a2c699aaf4c841e074ad21db7a100ab364e759ba..d6dee1aac2f48dfb0e79a2a0a0bf999ad11242a8 100644 --- a/src/abstract-puppet/puppet-accessory.ts +++ b/src/abstract-puppet/puppet-accessory.ts @@ -12,14 +12,13 @@ export abstract class PuppetAccessory extends EventEmitter { private static _puppet?: Puppet public static set puppet(puppet: Puppet) { - log.silly('PuppetAssessory', 'static set puppet()') + log.silly('PuppetAssessory', 'static set puppet(%s)', puppet.constructor.name) this._puppet = puppet } public static get puppet(): Puppet { log.silly('PuppetAssessory', 'static get puppet()') if (this._puppet) { - log.silly('PuppetAssessory', 'static get puppet() from instance properties') return this._puppet } @@ -32,20 +31,19 @@ export abstract class PuppetAccessory extends EventEmitter { private _puppet?: Puppet public set puppet(puppet: Puppet) { - log.silly('PuppetAssessory', 'set puppet()') + log.silly('PuppetAssessory', 'set puppet(%s)', puppet.constructor.name) this._puppet = puppet } public get puppet(): Puppet { - log.silly('PuppetAssessory', 'get puppet()') + log.silly('PuppetAssessory', 'get puppet() from instance') if (this._puppet) { - log.silly('PuppetAssessory', 'get puppet() from instance properties') return this._puppet } - return (this.constructor as any).puppet + // use the Class Static puppet + return (this.constructor as any as PuppetAccessory).puppet } - } export default PuppetAccessory diff --git a/src/abstract-puppet/room.ts b/src/abstract-puppet/room.ts index ad9021a1e2998e6216718e2f1c966d43d3831f2c..cedde0333b25c9e2e9e3681014653ac8f2a0ae22 100644 --- a/src/abstract-puppet/room.ts +++ b/src/abstract-puppet/room.ts @@ -32,14 +32,14 @@ export type RoomEventName = 'join' | 'leave' | 'topic' -export type RoomMemberQueryName = 'name' | 'roomAlias' | 'contactAlias' - export interface RoomMemberQueryFilter { name?: string, roomAlias?: string, contactAlias?: string, } +export type RoomMemberQueryName = keyof RoomMemberQueryFilter + export interface RoomQueryFilter { topic: string | RegExp, } diff --git a/src/config.ts b/src/config.ts index 7cb8e06bfb220b1b27dfb2a1f99cd765ee140480..11d1dbdcaced61e298a3f405b8fcad957dbbed1a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -90,6 +90,7 @@ export type PuppetName = 'android-pad' | 'hostie' | 'ios-app-phone' | 'ios-app-pad' + | 'mock' | 'web' | 'win32' diff --git a/src/puppet-web/bridge.ts b/src/puppet-web/bridge.ts index d20347aaf53b0bab36ac2b54b13f9b1dcf872047..3a7f64a44473f17b95c17e3d3a70a4ae0ee516b3 100644 --- a/src/puppet-web/bridge.ts +++ b/src/puppet-web/bridge.ts @@ -432,7 +432,7 @@ export class Bridge extends EventEmitter { try { const ret = await this.proxyWechaty('send', toUserName, content) - if (ret) { + if (!ret) { throw new Error('send fail') } } catch (e) { diff --git a/src/puppet-web/puppet-web.ts b/src/puppet-web/puppet-web.ts index d51c906c6d1ec4f40370363ae9c6cc1d65ea8797..adcd19b9410f951c5d5a2daf73930aa7f0880519 100644 --- a/src/puppet-web/puppet-web.ts +++ b/src/puppet-web/puppet-web.ts @@ -329,18 +329,19 @@ export class PuppetWeb extends Puppet { } } - private async uploadMedia(mediaMessage: WebMessage, toUserName: string): Promise { - if (!mediaMessage) - throw new Error('require mediaMessage') + private async uploadMedia(message: WebMessage, toUserName: string): Promise { + if (message.type() === WebMessage.Type.TEXT) { + throw new Error('require a Media Message') + } - const filename = mediaMessage.filename() - const ext = mediaMessage.ext() + const filename = message.filename() + const ext = message.ext() // const contentType = Misc.mime(ext) // const contentType = mime.getType(ext) - const contentType = mediaMessage.mimeType() + const contentType = message.mimeType() if (!contentType) { - throw new Error('no MIME Type found on mediaMessage: ' + mediaMessage.filename()) + throw new Error('no MIME Type found on mediaMessage: ' + message.filename()) } let mediatype: MediaType @@ -359,7 +360,7 @@ export class PuppetWeb extends Puppet { mediatype = MediaType.ATTACHMENT } - const readStream = await mediaMessage.readyStream() + const readStream = await message.readyStream() const buffer = await new Promise((resolve, reject) => { readStream.pipe(bl((err, data) => { if (err) reject(err) @@ -682,6 +683,8 @@ export class PuppetWeb extends Puppet { } public async send(message: WebMessage): Promise { + log.verbose('PuppetWeb', 'send(%s)', message) + const to = message.to() const room = message.room() @@ -689,33 +692,35 @@ export class PuppetWeb extends Puppet { if (room) { destinationId = room.id - } else { - if (!to) { - throw new Error('PuppetWeb.send(): message with neither room nor to?') - } + } else if (to) { destinationId = to.id + } else { + throw new Error('PuppetWeb.send(): message with neither room nor to?') } - if (message.type() !== MsgType.TEXT) { - const ret = await this.sendMedia(message) - if (!ret) { - throw new Error('sendMedia fail') - } - } else { - const content = message.text() + if (message.type() === MsgType.TEXT) { + log.silly('PuppetWeb', 'send() TEXT message.') + const text = message.text() - log.silly('PuppetWeb', 'send() destination: %s, content: %s)', - destinationId, - content, - ) + log.silly('PuppetWeb', 'send() destination: %s, text: %s)', + destinationId, + text, + ) try { - await this.bridge.send(destinationId, content) + await this.bridge.send(destinationId, text) } catch (e) { log.error('PuppetWeb', 'send() exception: %s', e.message) Raven.captureException(e) throw e } + } else { + log.silly('PuppetWeb', 'send() non-TEXT message.') + + const ret = await this.sendMedia(message) + if (!ret) { + throw new Error('sendMedia fail') + } } } diff --git a/src/puppet-web/web-message.ts b/src/puppet-web/web-message.ts index 673932af3fca4439341fe19cbefcd2fc9673472c..9e1be6b7ed65b483ffedf0876e9e1cfda1817ff7 100644 --- a/src/puppet-web/web-message.ts +++ b/src/puppet-web/web-message.ts @@ -329,7 +329,8 @@ export class WebMessage extends Message { * @returns {MsgType} */ public type(): MsgType { - return this.obj.type + log.silly('WebMessage', 'type() = %s', MsgType[this.obj.type]) + return this.obj.type || MsgType.TEXT } /** @@ -817,7 +818,7 @@ export class WebMessage extends Message { } break } - log.error('WebMessage', `ext() got unknown type: ${this.type()}`) + log.silly('WebMessage', `ext() got unknown type: ${this.type()}`) return String('.' + this.type()) } diff --git a/src/puppet-web/web-room.ts b/src/puppet-web/web-room.ts index c0b488b75646a8dd6439bf5d9243d36b602ad9d1..0d3b120ef2d2fd28de4b5a0ba963dc0646d402a8 100644 --- a/src/puppet-web/web-room.ts +++ b/src/puppet-web/web-room.ts @@ -125,7 +125,7 @@ export class WebRoom extends Room { const currNum = roomRawObj.MemberList && roomRawObj.MemberList.length || 0 const prevNum = this.rawObj && this.rawObj.MemberList && this.rawObj.MemberList.length || 0 - log.silly('WebRoom', `ready() contactGetter(%s) MemberList.length:%d at ttl:%d`, + log.silly('WebRoom', `ready() puppet.getContact(%s) MemberList.length:%d at ttl:%d`, this.id, currNum, ttl, @@ -133,27 +133,27 @@ export class WebRoom extends Room { if (currNum) { if (prevNum === currNum) { - log.verbose('WebRoom', `ready() contactGetter(${this.id}) done at ttl:%d`, ttl) + log.silly('WebRoom', `ready() puppet.getContact(${this.id}) done at ttl:%d`, ttl) break } this.rawObj = roomRawObj } - log.silly('WebRoom', `ready() contactGetter(${this.id}) retry at ttl:%d`, ttl) + log.silly('WebRoom', `ready() puppet.getContact(${this.id}) retry at ttl:%d`, ttl) await new Promise(r => setTimeout(r, 1000)) // wait for 1 second } await this.readyAllMembers(this.rawObj && this.rawObj.MemberList || []) this.obj = this.parse(this.rawObj) if (!this.obj) { - throw new Error('no this.obj set after contactGetter') + throw new Error('no this.obj set after puppet.getContact') } await Promise.all(this.obj.memberList.map(c => c.ready())) return this } catch (e) { - log.error('WebRoom', 'contactGetter(%s) exception: %s', this.id, e.message) + log.error('WebRoom', 'puppet.getContact(%s) exception: %s', this.id, e.message) Raven.captureException(e) throw e } diff --git a/src/wechaty.ts b/src/wechaty.ts index 95b58b1af85a90b94cd12b148f4c4df08edadedf..7f0047c683b3b7f02fbc09b62d97f7fd21f07c17 100644 --- a/src/wechaty.ts +++ b/src/wechaty.ts @@ -38,10 +38,10 @@ import { Sayable, VERSION, WechatyEvent, -} from './config' +} from './config' import { -} from './message' -import Profile from './profile' +} from './message' +import Profile from './profile' import { Contact, @@ -50,9 +50,10 @@ import { Puppet, PuppetAccessory, Room, -} from './abstract-puppet/' +} from './abstract-puppet/' -import PuppetWeb from './puppet-web/' +import PuppetWeb from './puppet-web/' +import PuppetMock from './puppet-mock/' export interface WechatyOptions { puppet?: PuppetName, @@ -350,6 +351,13 @@ export class Wechaty extends PuppetAccessory implements Sayable { }) break + case 'mock': + puppet = new PuppetMock({ + profile: this.profile, + wechaty: this, + }) + break + default: throw new Error('Puppet unsupport(yet?): ' + this.options.puppet) }