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

code clean

上级 dc1f6792
......@@ -290,28 +290,29 @@ export class Contact extends Accessory implements Sayable {
* console.error(e)
* }
*/
public async say(file: FileBox): Promise<void>
public async say(file: FileBox) : Promise<void>
public async say(contact: Contact) : Promise<void>
public async say(textOrContactOrFile: string | Contact | FileBox): Promise<void> {
log.verbose('Contact', 'say(%s)', textOrContactOrFile)
if (typeof textOrContactOrFile === 'string') {
/**
* Text
* 1. Text
*/
await this.puppet.messageSendText({
contactId: this.id,
}, textOrContactOrFile)
} else if (textOrContactOrFile instanceof Contact) {
/**
* Contact
* 2. Contact
*/
await this.puppet.messageSendContact({
contactId: this.id,
}, textOrContactOrFile.id)
} else if (textOrContactOrFile instanceof FileBox) {
/**
* File
* 3. File
*/
await this.puppet.messageSendFile({
contactId: this.id,
......
......@@ -415,8 +415,8 @@ export class PuppetMock extends Puppet {
log.verbose('PuppetMock', 'friendshipAccept(%s)', friendshipId)
}
public ding(data?: any): Promise<string> {
return data
public async ding(): Promise<false | 'dong'> {
return 'dong'
}
}
......
......@@ -132,8 +132,8 @@ export class PuppetPadchat extends Puppet {
return `PuppetPadchat<${this.options.memory.name}>`
}
public ding(data?: any): Promise<string> {
return data
public async ding(): Promise<false | 'dong'> {
return 'dong'
}
public startWatchdog(): void {
......
......@@ -719,13 +719,14 @@ export class PuppetPuppeteer extends Puppet {
}
}
public async ding(data?: any): Promise<string> {
public async ding(data?: any): Promise<false | 'dong'> {
try {
return await this.bridge.ding(data)
await this.bridge.ding(data)
return 'dong'
} catch (e) {
log.warn('PuppetPuppeteer', 'ding(%s) rejected: %s', data, e.message)
Raven.captureException(e)
throw e
return false
}
}
......
......@@ -946,8 +946,8 @@ export class PuppetWechat4u extends Puppet {
}
}
public ding(data?: any): Promise<string> {
return data
public async ding(): Promise<false | 'dong'> {
return 'dong'
}
private filename(
......
......@@ -41,8 +41,8 @@ class PuppetTest extends Puppet {
public async start() : Promise<void> { return {} as any }
public async stop() : Promise<void> { return {} as any }
public async ding(data?: any) : Promise<string> { return {data} as any }
public async logout(): Promise<void> { return {} as any }
public async ding() : Promise<false | 'dong'> { return 'dong' }
public async logout() : Promise<void> { return {} as any }
/**
*
......
......@@ -36,11 +36,7 @@ import {
WatchdogFood,
} from 'watchdog'
// import {
// PUPPET_EVENT_DICT,
// } from './schemas/puppet'
import {
Sayable,
log,
} from '../config'
......@@ -81,7 +77,7 @@ let PUPPET_COUNTER = 0
* See: https://github.com/Chatie/wechaty/wiki/Puppet
*
*/
export abstract class Puppet extends EventEmitter implements Sayable {
export abstract class Puppet extends EventEmitter {
public readonly cacheContactPayload : LRU.Cache<string, ContactPayload>
public readonly cacheFriendshipPayload : LRU.Cache<string, FriendshipPayload>
......@@ -294,11 +290,18 @@ export abstract class Puppet extends EventEmitter implements Sayable {
}
/**
*
*
* Misc
*
*
*/
/**
* Check whether the puppet is work property.
* @returns `false` if something went wrong
* 'dong' if everything is OK
*/
public abstract async ding(data?: any) : Promise<string>
public abstract async ding() : Promise<false | 'dong'>
public version(): string {
if (this.childPkg) {
......@@ -335,23 +338,23 @@ export abstract class Puppet extends EventEmitter implements Sayable {
// return this.pkg.engines.wechaty
}
public async say(textOrFile: string | FileBox) : Promise<void> {
if (!this.logonoff()) {
throw new Error('can not say before login')
}
if (typeof textOrFile === 'string') {
await this.messageSendText({
contactId: this.selfId(),
}, textOrFile)
} else if (textOrFile instanceof FileBox) {
await this.messageSendFile({
contactId: this.selfId(),
}, textOrFile)
} else {
throw new Error('say() arg unknown')
}
}
// public async say(textOrFile: string | FileBox) : Promise<void> {
// if (!this.logonoff()) {
// throw new Error('can not say before login')
// }
// if (typeof textOrFile === 'string') {
// await this.messageSendText({
// contactId: this.selfId(),
// }, textOrFile)
// } else if (textOrFile instanceof FileBox) {
// await this.messageSendFile({
// contactId: this.selfId(),
// }, textOrFile)
// } else {
// throw new Error('say() arg unknown')
// }
// }
/**
*
......@@ -360,7 +363,6 @@ export abstract class Puppet extends EventEmitter implements Sayable {
*/
public abstract async contactAlias(contactId: string) : Promise<string>
public abstract async contactAlias(contactId: string, alias: string | null) : Promise<void>
// public abstract async contactAlias(contactId: string, alias?: string|null) : Promise<string | void>
public abstract async contactAvatar(contactId: string) : Promise<FileBox>
public abstract async contactAvatar(contactId: string, file: FileBox) : Promise<void>
......@@ -852,6 +854,9 @@ export abstract class Puppet extends EventEmitter implements Sayable {
return payload
}
/**
* Concat roomId & contactId to one string
*/
private cacheKeyRoomMember(
roomId : string,
contactId : string,
......@@ -887,8 +892,8 @@ export abstract class Puppet extends EventEmitter implements Sayable {
/**
* 1. Try to get from cache
*/
const cacheKey = this.cacheKeyRoomMember(roomId, contactId)
const cachedPayload = this.cacheRoomMemberPayload.get(cacheKey)
const CACHE_KEY = this.cacheKeyRoomMember(roomId, contactId)
const cachedPayload = this.cacheRoomMemberPayload.get(CACHE_KEY)
if (cachedPayload) {
return cachedPayload
......@@ -900,7 +905,7 @@ export abstract class Puppet extends EventEmitter implements Sayable {
const rawPayload = await this.roomMemberRawPayload(roomId, contactId)
const payload = await this.roomMemberRawPayloadParser(rawPayload)
this.cacheRoomMemberPayload.set(cacheKey, payload)
this.cacheRoomMemberPayload.set(CACHE_KEY, payload)
log.silly('Puppet', 'roomMemberPayload(%s) cache SET', roomId)
return payload
......
......@@ -585,10 +585,13 @@ export class Room extends Accessory implements Sayable {
}
}
// TODO
/**
* Room QR Code
*/
public async qrcode(): Promise<string> {
log.verbose('Room', 'qrcode()')
return 'qrcode url for entry room'
const qrcode = await this.puppet.roomQrcode(this.id)
return qrcode
}
/**
......
......@@ -26,6 +26,9 @@ import {
cloneClass,
// instanceToClass,
} from 'clone-class'
import {
FileBox,
} from 'file-box'
import {
callerResolve,
hotImport,
......@@ -826,14 +829,24 @@ export class Wechaty extends Accessory implements Sayable {
}
/**
* Send message to filehelper
* Send message to userSelf
*
* @param {string} text
* @param {string} textOrContactOrFile
* @returns {Promise<boolean>}
*/
public async say(text: string): Promise<void> {
log.verbose('Wechaty', 'say(%s)', text)
await this.puppet.say(text)
public async say(textOrContactOrFile: string | Contact | FileBox): Promise<void> {
log.verbose('Wechaty', 'say(%s)', textOrContactOrFile)
// Make Typescript Happy:
if (typeof textOrContactOrFile === 'string') {
await this.userSelf().say(textOrContactOrFile)
} else if (textOrContactOrFile instanceof Contact) {
await this.userSelf().say(textOrContactOrFile)
} else if (textOrContactOrFile instanceof FileBox) {
await this.userSelf().say(textOrContactOrFile)
} else {
throw new Error('unsupported')
}
}
/**
......@@ -848,13 +861,13 @@ export class Wechaty extends Accessory implements Sayable {
/**
* @private
*/
public async ding(): Promise<string> {
public async ding(): Promise<false | 'dong'> {
try {
return await this.puppet.ding() // should return 'dong'
} catch (e) {
log.error('Wechaty', 'ding() exception: %s', e.message)
Raven.captureException(e)
throw e
return false
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册