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

work as expected

上级 4faf933f
...@@ -44,6 +44,8 @@ export interface ContactQueryFilter { ...@@ -44,6 +44,8 @@ export interface ContactQueryFilter {
alias?: string | RegExp, alias?: string | RegExp,
} }
export type ContactQueryName = keyof ContactQueryFilter
/** /**
* All wechat contacts(friend) will be encapsulated as a Contact. * All wechat contacts(friend) will be encapsulated as a Contact.
* *
......
...@@ -34,11 +34,6 @@ import Contact from './contact' ...@@ -34,11 +34,6 @@ import Contact from './contact'
import Room from './room' import Room from './room'
import PuppetAccessory from './puppet-accessory' import PuppetAccessory from './puppet-accessory'
// export type TypeName = 'attachment'
// | 'audio'
// | 'image'
// | 'video'
/** /**
* All wechat messages will be encapsulated as a Message. * All wechat messages will be encapsulated as a Message.
* *
......
...@@ -12,14 +12,13 @@ export abstract class PuppetAccessory extends EventEmitter { ...@@ -12,14 +12,13 @@ export abstract class PuppetAccessory extends EventEmitter {
private static _puppet?: Puppet private static _puppet?: Puppet
public static set puppet(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 this._puppet = puppet
} }
public static get puppet(): Puppet { public static get puppet(): Puppet {
log.silly('PuppetAssessory', 'static get puppet()') log.silly('PuppetAssessory', 'static get puppet()')
if (this._puppet) { if (this._puppet) {
log.silly('PuppetAssessory', 'static get puppet() from instance properties')
return this._puppet return this._puppet
} }
...@@ -32,20 +31,19 @@ export abstract class PuppetAccessory extends EventEmitter { ...@@ -32,20 +31,19 @@ export abstract class PuppetAccessory extends EventEmitter {
private _puppet?: Puppet private _puppet?: Puppet
public set puppet(puppet: Puppet) { public set puppet(puppet: Puppet) {
log.silly('PuppetAssessory', 'set puppet()') log.silly('PuppetAssessory', 'set puppet(%s)', puppet.constructor.name)
this._puppet = puppet this._puppet = puppet
} }
public get puppet(): Puppet { public get puppet(): Puppet {
log.silly('PuppetAssessory', 'get puppet()') log.silly('PuppetAssessory', 'get puppet() from instance')
if (this._puppet) { if (this._puppet) {
log.silly('PuppetAssessory', 'get puppet() from instance properties')
return this._puppet 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 export default PuppetAccessory
...@@ -32,14 +32,14 @@ export type RoomEventName = 'join' ...@@ -32,14 +32,14 @@ export type RoomEventName = 'join'
| 'leave' | 'leave'
| 'topic' | 'topic'
export type RoomMemberQueryName = 'name' | 'roomAlias' | 'contactAlias'
export interface RoomMemberQueryFilter { export interface RoomMemberQueryFilter {
name?: string, name?: string,
roomAlias?: string, roomAlias?: string,
contactAlias?: string, contactAlias?: string,
} }
export type RoomMemberQueryName = keyof RoomMemberQueryFilter
export interface RoomQueryFilter { export interface RoomQueryFilter {
topic: string | RegExp, topic: string | RegExp,
} }
......
...@@ -90,6 +90,7 @@ export type PuppetName = 'android-pad' ...@@ -90,6 +90,7 @@ export type PuppetName = 'android-pad'
| 'hostie' | 'hostie'
| 'ios-app-phone' | 'ios-app-phone'
| 'ios-app-pad' | 'ios-app-pad'
| 'mock'
| 'web' | 'web'
| 'win32' | 'win32'
......
...@@ -432,7 +432,7 @@ export class Bridge extends EventEmitter { ...@@ -432,7 +432,7 @@ export class Bridge extends EventEmitter {
try { try {
const ret = await this.proxyWechaty('send', toUserName, content) const ret = await this.proxyWechaty('send', toUserName, content)
if (ret) { if (!ret) {
throw new Error('send fail') throw new Error('send fail')
} }
} catch (e) { } catch (e) {
......
...@@ -329,18 +329,19 @@ export class PuppetWeb extends Puppet { ...@@ -329,18 +329,19 @@ export class PuppetWeb extends Puppet {
} }
} }
private async uploadMedia(mediaMessage: WebMessage, toUserName: string): Promise<MediaData> { private async uploadMedia(message: WebMessage, toUserName: string): Promise<MediaData> {
if (!mediaMessage) if (message.type() === WebMessage.Type.TEXT) {
throw new Error('require mediaMessage') throw new Error('require a Media Message')
}
const filename = mediaMessage.filename() const filename = message.filename()
const ext = mediaMessage.ext() const ext = message.ext()
// const contentType = Misc.mime(ext) // const contentType = Misc.mime(ext)
// const contentType = mime.getType(ext) // const contentType = mime.getType(ext)
const contentType = mediaMessage.mimeType() const contentType = message.mimeType()
if (!contentType) { 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 let mediatype: MediaType
...@@ -359,7 +360,7 @@ export class PuppetWeb extends Puppet { ...@@ -359,7 +360,7 @@ export class PuppetWeb extends Puppet {
mediatype = MediaType.ATTACHMENT mediatype = MediaType.ATTACHMENT
} }
const readStream = await mediaMessage.readyStream() const readStream = await message.readyStream()
const buffer = <Buffer>await new Promise((resolve, reject) => { const buffer = <Buffer>await new Promise((resolve, reject) => {
readStream.pipe(bl((err, data) => { readStream.pipe(bl((err, data) => {
if (err) reject(err) if (err) reject(err)
...@@ -682,6 +683,8 @@ export class PuppetWeb extends Puppet { ...@@ -682,6 +683,8 @@ export class PuppetWeb extends Puppet {
} }
public async send(message: WebMessage): Promise<void> { public async send(message: WebMessage): Promise<void> {
log.verbose('PuppetWeb', 'send(%s)', message)
const to = message.to() const to = message.to()
const room = message.room() const room = message.room()
...@@ -689,33 +692,35 @@ export class PuppetWeb extends Puppet { ...@@ -689,33 +692,35 @@ export class PuppetWeb extends Puppet {
if (room) { if (room) {
destinationId = room.id destinationId = room.id
} else { } else if (to) {
if (!to) {
throw new Error('PuppetWeb.send(): message with neither room nor to?')
}
destinationId = to.id destinationId = to.id
} else {
throw new Error('PuppetWeb.send(): message with neither room nor to?')
} }
if (message.type() !== MsgType.TEXT) { if (message.type() === MsgType.TEXT) {
const ret = await this.sendMedia(message) log.silly('PuppetWeb', 'send() TEXT message.')
if (!ret) { const text = message.text()
throw new Error('sendMedia fail')
}
} else {
const content = message.text()
log.silly('PuppetWeb', 'send() destination: %s, content: %s)', log.silly('PuppetWeb', 'send() destination: %s, text: %s)',
destinationId, destinationId,
content, text,
) )
try { try {
await this.bridge.send(destinationId, content) await this.bridge.send(destinationId, text)
} catch (e) { } catch (e) {
log.error('PuppetWeb', 'send() exception: %s', e.message) log.error('PuppetWeb', 'send() exception: %s', e.message)
Raven.captureException(e) Raven.captureException(e)
throw e throw e
} }
} else {
log.silly('PuppetWeb', 'send() non-TEXT message.')
const ret = await this.sendMedia(message)
if (!ret) {
throw new Error('sendMedia fail')
}
} }
} }
......
...@@ -329,7 +329,8 @@ export class WebMessage extends Message { ...@@ -329,7 +329,8 @@ export class WebMessage extends Message {
* @returns {MsgType} * @returns {MsgType}
*/ */
public type(): 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 { ...@@ -817,7 +818,7 @@ export class WebMessage extends Message {
} }
break break
} }
log.error('WebMessage', `ext() got unknown type: ${this.type()}`) log.silly('WebMessage', `ext() got unknown type: ${this.type()}`)
return String('.' + this.type()) return String('.' + this.type())
} }
......
...@@ -125,7 +125,7 @@ export class WebRoom extends Room { ...@@ -125,7 +125,7 @@ export class WebRoom extends Room {
const currNum = roomRawObj.MemberList && roomRawObj.MemberList.length || 0 const currNum = roomRawObj.MemberList && roomRawObj.MemberList.length || 0
const prevNum = this.rawObj && this.rawObj.MemberList && this.rawObj.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, this.id,
currNum, currNum,
ttl, ttl,
...@@ -133,27 +133,27 @@ export class WebRoom extends Room { ...@@ -133,27 +133,27 @@ export class WebRoom extends Room {
if (currNum) { if (currNum) {
if (prevNum === 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 break
} }
this.rawObj = roomRawObj 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 new Promise(r => setTimeout(r, 1000)) // wait for 1 second
} }
await this.readyAllMembers(this.rawObj && this.rawObj.MemberList || []) await this.readyAllMembers(this.rawObj && this.rawObj.MemberList || [])
this.obj = this.parse(this.rawObj) this.obj = this.parse(this.rawObj)
if (!this.obj) { 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())) await Promise.all(this.obj.memberList.map(c => c.ready()))
return this return this
} catch (e) { } 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) Raven.captureException(e)
throw e throw e
} }
......
...@@ -38,10 +38,10 @@ import { ...@@ -38,10 +38,10 @@ import {
Sayable, Sayable,
VERSION, VERSION,
WechatyEvent, WechatyEvent,
} from './config' } from './config'
import { import {
} from './message' } from './message'
import Profile from './profile' import Profile from './profile'
import { import {
Contact, Contact,
...@@ -50,9 +50,10 @@ import { ...@@ -50,9 +50,10 @@ import {
Puppet, Puppet,
PuppetAccessory, PuppetAccessory,
Room, 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 { export interface WechatyOptions {
puppet?: PuppetName, puppet?: PuppetName,
...@@ -350,6 +351,13 @@ export class Wechaty extends PuppetAccessory implements Sayable { ...@@ -350,6 +351,13 @@ export class Wechaty extends PuppetAccessory implements Sayable {
}) })
break break
case 'mock':
puppet = new PuppetMock({
profile: this.profile,
wechaty: this,
})
break
default: default:
throw new Error('Puppet unsupport(yet?): ' + this.options.puppet) throw new Error('Puppet unsupport(yet?): ' + this.options.puppet)
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册