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

work as expected

上级 4faf933f
......@@ -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.
*
......
......@@ -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.
*
......
......@@ -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
......@@ -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,
}
......
......@@ -90,6 +90,7 @@ export type PuppetName = 'android-pad'
| 'hostie'
| 'ios-app-phone'
| 'ios-app-pad'
| 'mock'
| 'web'
| 'win32'
......
......@@ -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) {
......
......@@ -329,18 +329,19 @@ export class PuppetWeb extends Puppet {
}
}
private async uploadMedia(mediaMessage: WebMessage, toUserName: string): Promise<MediaData> {
if (!mediaMessage)
throw new Error('require mediaMessage')
private async uploadMedia(message: WebMessage, toUserName: string): Promise<MediaData> {
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 = <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<void> {
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')
}
}
}
......
......@@ -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())
}
......
......@@ -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
}
......
......@@ -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)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册