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

minor fixes

上级 5e1d4c38
...@@ -90,7 +90,7 @@ bot ...@@ -90,7 +90,7 @@ bot
* 1. reply 'dong' * 1. reply 'dong'
*/ */
log.info('Bot', 'REPLY: dong') log.info('Bot', 'REPLY: dong')
msg.say('dong') await msg.say('dong')
const joinWechaty = `Join Wechaty Developers' Community\n\n` + const joinWechaty = `Join Wechaty Developers' Community\n\n` +
`Wechaty is used in many ChatBot projects by hundreds of developers.\n\n` + `Wechaty is used in many ChatBot projects by hundreds of developers.\n\n` +
...@@ -107,7 +107,7 @@ bot ...@@ -107,7 +107,7 @@ bot
BOT_QR_CODE_IMAGE_FILE, BOT_QR_CODE_IMAGE_FILE,
) )
log.info('Bot', 'REPLY: %s', fileBox) log.info('Bot', 'REPLY: %s', fileBox.toString())
await msg.say(fileBox) await msg.say(fileBox)
/** /**
......
...@@ -61,11 +61,7 @@ export class FriendRequest extends Accessory { ...@@ -61,11 +61,7 @@ export class FriendRequest extends Accessory {
id : string, id : string,
): T['prototype'] { ): T['prototype'] {
const newFriendRequest = new (this as any)(id) const newFriendRequest = new (this as any)(id)
newFriendRequest.payload = this.puppet.cacheFriendRequestPayload.get(id)
const hitPayload = this.puppet.cacheFriendRequestPayload.get(id)
if (hitPayload) {
newFriendRequest.payload = hitPayload
}
return newFriendRequest return newFriendRequest
} }
......
...@@ -152,15 +152,16 @@ export class Message extends Accessory implements Sayable { ...@@ -152,15 +152,16 @@ export class Message extends Accessory implements Sayable {
// `#${MessageDirection[this.direction]}`, // `#${MessageDirection[this.direction]}`,
`#${MessageType[this.type()]}`, `#${MessageType[this.type()]}`,
'(', '(',
this.room() ? (this.room() + '') : '',
this.from(), this.from(),
this.room() ? '@' + this.room() : '', '',
this.to(), this.to(),
')', ')',
] ]
if (this.type() === Message.Type.Text) { if (this.type() === Message.Type.Text) {
msgStrList.push(`<${this.text()}>`) msgStrList.push(`<${this.text()}>`)
} else { } else {
log.verbose('Message', 'toString() this.type()=%s', Message.Type[this.type()]) log.silly('Message', 'toString() for message type: ', Message.Type[this.type()])
if (!this.payload) { if (!this.payload) {
throw new Error('no payload') throw new Error('no payload')
......
...@@ -528,8 +528,29 @@ export class Bridge extends EventEmitter { ...@@ -528,8 +528,29 @@ export class Bridge extends EventEmitter {
} }
public async getMessage(id: string): Promise<WebMessageRawPayload> { public async getMessage(id: string): Promise<WebMessageRawPayload> {
const rawPayload = await this.proxyWechaty('getMessage', id) try {
return rawPayload return await Misc.retry(async (retry, attempt) => {
log.silly('PuppetPuppeteerBridge', 'getMessage(%s) retry attempt %d',
id,
attempt,
)
try {
const rawPayload = await this.proxyWechaty('getMessage', id)
if (rawPayload && Object.keys(rawPayload).length > 0) {
return rawPayload
}
throw new Error('got empty return value at attempt: ' + attempt)
} catch (e) {
log.verbose('PuppetPuppeteerBridge', 'getMessage() proxyWechaty(getMessage, %s) exception: %s', id, e.message)
retry(e)
}
})
} catch (e) {
log.error('PuppetPuppeteerBridge', 'promiseRetry() getContact() finally FAIL: %s', e.message)
throw e
}
} }
public async getContact(id: string): Promise<WebContactRawPayload | WebRoomRawPayload> { public async getContact(id: string): Promise<WebContactRawPayload | WebRoomRawPayload> {
...@@ -552,20 +573,6 @@ export class Bridge extends EventEmitter { ...@@ -552,20 +573,6 @@ export class Bridge extends EventEmitter {
} }
}) })
// return await retryPromise({ max: max, backoff: backoff }, async (attempt: number) => {
// log.silly('PuppetPuppeteerBridge', 'getContact() retryPromise: attampt %d/%d time for timeout %d',
// attempt, max, timeout)
// try {
// const r = await this.proxyWechaty('getContact', id)
// if (r) {
// return r
// }
// throw new Error('got empty return value at attempt: ' + attempt)
// } catch (e) {
// log.silly('PuppetPuppeteerBridge', 'proxyWechaty(getContact, %s) exception: %s', id, e.message)
// throw e
// }
// })
} catch (e) { } catch (e) {
log.error('PuppetPuppeteerBridge', 'promiseRetry() getContact() finally FAIL: %s', e.message) log.error('PuppetPuppeteerBridge', 'promiseRetry() getContact() finally FAIL: %s', e.message)
throw e throw e
......
...@@ -308,7 +308,7 @@ export class Firer { ...@@ -308,7 +308,7 @@ export class Firer {
try { try {
[leaverName, removerName] = this.parseRoomLeave(rawPayload.Content) [leaverName, removerName] = this.parseRoomLeave(rawPayload.Content)
} catch (e) { } catch (e) {
log.warn('PuppetPuppeteerFirer', 'fireRoomLeave() exception: %s', e.message) log.silly('PuppetPuppeteerFirer', 'fireRoomLeave() %s', e.message)
return false return false
} }
log.silly('PuppetPuppeteerFirer', 'fireRoomLeave() got leaverName: %s', leaverName) log.silly('PuppetPuppeteerFirer', 'fireRoomLeave() got leaverName: %s', leaverName)
......
...@@ -355,6 +355,10 @@ export class PuppetPuppeteer extends Puppet { ...@@ -355,6 +355,10 @@ export class PuppetPuppeteer extends Puppet {
} else { } else {
throw new Error('parse found a room message, but neither FromUserName nor ToUserName is a room(/^@@/)') throw new Error('parse found a room message, but neither FromUserName nor ToUserName is a room(/^@@/)')
} }
// console.log('rawPayload.FromUserName: ', rawPayload.FromUserName)
// console.log('rawPayload.ToUserName: ', rawPayload.ToUserName)
// console.log('rawPayload.MMPeerUserName: ', rawPayload.MMPeerUserName)
} }
if (rawPayload.ToUserName) { if (rawPayload.ToUserName) {
...@@ -576,7 +580,7 @@ export class PuppetPuppeteer extends Puppet { ...@@ -576,7 +580,7 @@ export class PuppetPuppeteer extends Puppet {
receiver : Receiver, receiver : Receiver,
text : string, text : string,
): Promise<void> { ): Promise<void> {
log.verbose('PuppetPuppeteer', 'messageSendText(receiver=%s, text=%s)', receiver, text) log.verbose('PuppetPuppeteer', 'messageSendText(receiver=%s, text=%s)', JSON.stringify(receiver), text)
let destinationId let destinationId
...@@ -860,7 +864,7 @@ export class PuppetPuppeteer extends Puppet { ...@@ -860,7 +864,7 @@ export class PuppetPuppeteer extends Puppet {
// let currNum = rawPayload.MemberList && rawPayload.MemberList.length || 0 // let currNum = rawPayload.MemberList && rawPayload.MemberList.length || 0
// let prevNum = room.memberList().length // rawPayload && rawPayload.MemberList && this.rawObj.MemberList.length || 0 // let prevNum = room.memberList().length // rawPayload && rawPayload.MemberList && this.rawObj.MemberList.length || 0
let prevLength = -1 let prevLength = 0
/** /**
* @todo use Misc.retry() to replace the following loop * @todo use Misc.retry() to replace the following loop
...@@ -870,19 +874,27 @@ export class PuppetPuppeteer extends Puppet { ...@@ -870,19 +874,27 @@ export class PuppetPuppeteer extends Puppet {
rawPayload = await this.bridge.getContact(id) as undefined | WebRoomRawPayload rawPayload = await this.bridge.getContact(id) as undefined | WebRoomRawPayload
if (rawPayload) { if (rawPayload) {
const currLength = rawPayload.MemberList && rawPayload.MemberList.length || 0 const currLength = rawPayload.MemberList && rawPayload.MemberList.length || -1
log.silly('PuppetPuppeteer', `roomPayload() this.bridge.getContact(%s) MemberList.length:%d at ttl:%d`, log.silly('PuppetPuppeteer', `roomPayload() this.bridge.getContact(%s) MemberList.length:(prev:%d, curr:%d) at ttl:%d`,
id, id,
prevLength,
currLength, currLength,
ttl, ttl,
) )
if (prevLength === currLength) { if (prevLength === currLength) {
log.silly('PuppetPuppeteer', `roomPayload() puppet.getContact(${id}) done at ttl:%d`, ttl) log.silly('PuppetPuppeteer', `roomPayload() puppet.getContact(%s) done at ttl:%d with length:%d`, this.id, ttl, currLength)
return rawPayload return rawPayload
} }
prevLength = currLength if (currLength >= prevLength) {
prevLength = currLength
} else {
log.warn('PuppetPuppeteer', 'roomRawPayload() currLength(%d) <= prevLength(%d) ???',
currLength,
prevLength,
)
}
} }
log.silly('PuppetPuppeteer', `roomPayload() puppet.getContact(${id}) retry at ttl:%d`, ttl) log.silly('PuppetPuppeteer', `roomPayload() puppet.getContact(${id}) retry at ttl:%d`, ttl)
...@@ -1589,7 +1601,7 @@ export class PuppetPuppeteer extends Puppet { ...@@ -1589,7 +1601,7 @@ export class PuppetPuppeteer extends Puppet {
file : FileBox, file : FileBox,
): Promise<void> { ): Promise<void> {
log.verbose('PuppetPuppeteer', 'messageSendFile(receiver=%s, file=%s)', log.verbose('PuppetPuppeteer', 'messageSendFile(receiver=%s, file=%s)',
receiver, JSON.stringify(receiver),
file.toString(), file.toString(),
) )
......
...@@ -457,6 +457,10 @@ export abstract class Puppet extends EventEmitter implements Sayable { ...@@ -457,6 +457,10 @@ export abstract class Puppet extends EventEmitter implements Sayable {
): Promise<ContactPayload> { ): Promise<ContactPayload> {
log.silly('Puppet', 'contactPayload(id=%s, noCache=%s) @ %s', id, noCache, this) log.silly('Puppet', 'contactPayload(id=%s, noCache=%s) @ %s', id, noCache, this)
if (!id) {
throw new Error('no id')
}
if (noCache) { if (noCache) {
log.silly('Puppet', 'contactPayload() cache PURGE') log.silly('Puppet', 'contactPayload() cache PURGE')
this.cacheContactPayload.del(id) this.cacheContactPayload.del(id)
...@@ -494,6 +498,10 @@ export abstract class Puppet extends EventEmitter implements Sayable { ...@@ -494,6 +498,10 @@ export abstract class Puppet extends EventEmitter implements Sayable {
): Promise<FriendRequestPayload> { ): Promise<FriendRequestPayload> {
log.verbose('Puppet', 'friendRequestPayload(id=%s, noCache=%s)', id, noCache) log.verbose('Puppet', 'friendRequestPayload(id=%s, noCache=%s)', id, noCache)
if (!id) {
throw new Error('no id')
}
if (noCache) { if (noCache) {
log.silly('Puppet', 'friendRequestPayload() cache PURGE') log.silly('Puppet', 'friendRequestPayload() cache PURGE')
this.cacheFriendRequestPayload.del(id) this.cacheFriendRequestPayload.del(id)
...@@ -530,6 +538,10 @@ export abstract class Puppet extends EventEmitter implements Sayable { ...@@ -530,6 +538,10 @@ export abstract class Puppet extends EventEmitter implements Sayable {
): Promise<MessagePayload> { ): Promise<MessagePayload> {
log.verbose('Puppet', 'messagePayload(id=%s, noCache=%s)', id, noCache) log.verbose('Puppet', 'messagePayload(id=%s, noCache=%s)', id, noCache)
if (!id) {
throw new Error('no id')
}
if (noCache) { if (noCache) {
log.silly('Puppet', 'messagePayload() cache PURGE') log.silly('Puppet', 'messagePayload() cache PURGE')
this.cacheMessagePayload.del(id) this.cacheMessagePayload.del(id)
...@@ -687,6 +699,10 @@ export abstract class Puppet extends EventEmitter implements Sayable { ...@@ -687,6 +699,10 @@ export abstract class Puppet extends EventEmitter implements Sayable {
): Promise<RoomPayload> { ): Promise<RoomPayload> {
log.verbose('Puppet', 'roomPayload(id=%s, noCache=%s)', id, noCache) log.verbose('Puppet', 'roomPayload(id=%s, noCache=%s)', id, noCache)
if (!id) {
throw new Error('no id')
}
if (noCache) { if (noCache) {
log.silly('Puppet', 'roomPayload() cache PURGE') log.silly('Puppet', 'roomPayload() cache PURGE')
this.cacheRoomPayload.del(id) this.cacheRoomPayload.del(id)
......
...@@ -219,7 +219,7 @@ export class Room extends Accessory implements Sayable { ...@@ -219,7 +219,7 @@ export class Room extends Accessory implements Sayable {
*/ */
public toString() { public toString() {
if (this.payload && this.payload.topic) { if (this.payload && this.payload.topic) {
return `Room<${this.topic()}>` return `Room<${this.payload.topic}>`
} }
return `Room<${this.id || ''}>` return `Room<${this.id || ''}>`
} }
...@@ -776,13 +776,7 @@ export class Room extends Accessory implements Sayable { ...@@ -776,13 +776,7 @@ export class Room extends Accessory implements Sayable {
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
public async sync(): Promise<void> { public async sync(): Promise<void> {
// TODO: make it work with the old dirty payload when in re-syncing... await this.ready(true)
// if (this.isReady()) {
// this.dirtyObj = this.payload
// }
this.payload = undefined
await this.ready()
} }
/** /**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册