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

check more and hold token check before instanciating

上级 33b940cc
...@@ -100,7 +100,7 @@ ...@@ -100,7 +100,7 @@
"cuid": "^2.1.1", "cuid": "^2.1.1",
"express": "^4.16.3", "express": "^4.16.3",
"file-box": "^0.8.19", "file-box": "^0.8.19",
"flash-store": "^0.4.0", "flash-store": "^0.4.2",
"fs-extra": "^6.0.1", "fs-extra": "^6.0.1",
"hot-import": "^0.2.1", "hot-import": "^0.2.1",
"jimp": "^0.2.28", "jimp": "^0.2.28",
......
...@@ -64,7 +64,7 @@ export class Bridge extends PadchatRpc { ...@@ -64,7 +64,7 @@ export class Bridge extends PadchatRpc {
private loginTimer?: NodeJS.Timer private loginTimer?: NodeJS.Timer
private selfId? : string private selfId? : string
private selfName? : string // private selfName? : string
// private password? : string // private password? : string
// private nickname? : string // private nickname? : string
...@@ -139,7 +139,16 @@ export class Bridge extends PadchatRpc { ...@@ -139,7 +139,16 @@ export class Bridge extends PadchatRpc {
this.cacheRoomRawPayload = new FlashStoreSync(path.join(baseDir, 'room-raw-payload')) this.cacheRoomRawPayload = new FlashStoreSync(path.join(baseDir, 'room-raw-payload'))
this.cacheRoomMemberRawPayload = new FlashStoreSync(path.join(baseDir, 'room-member-raw-payload')) this.cacheRoomMemberRawPayload = new FlashStoreSync(path.join(baseDir, 'room-member-raw-payload'))
log.silly('PuppetPadchatBridge', 'initCache() workdir="%s"', baseDir) await this.cacheContactRawPayload.ready()
await this.cacheRoomRawPayload.ready()
await this.cacheRoomMemberRawPayload.ready()
log.verbose('PuppetPadchatBridge', 'initCache() inited %d Contacts, %d Rooms, %d RoomMembers, cachedir="%s"',
this.cacheContactRawPayload.size,
this.cacheRoomRawPayload.size,
this.cacheRoomMemberRawPayload.size,
baseDir,
)
} }
private releaseCache(): void { private releaseCache(): void {
...@@ -174,6 +183,17 @@ export class Bridge extends PadchatRpc { ...@@ -174,6 +183,17 @@ export class Bridge extends PadchatRpc {
// await this.padchatRpc.start() // await this.padchatRpc.start()
await super.start() await super.start()
this.on('padchat-logout', () => {
if (this.selfId) {
this.emit('logout')
} else {
/**
* PadchatRpc will receive 'logout' message multiple times when the server is logouted.
*/
log.warn('PuppetPadchatBridge', 'start() on(padchat-logout) received but selfId is undefined')
}
})
// this.padchatRpc.on('message', messageRawPayload => { // this.padchatRpc.on('message', messageRawPayload => {
// log.silly('PuppetPadchatBridge', 'start() padchatRpc.on(message)') // log.silly('PuppetPadchatBridge', 'start() padchatRpc.on(message)')
// this.emit('message', messageRawPayload) // this.emit('message', messageRawPayload)
...@@ -218,9 +238,9 @@ export class Bridge extends PadchatRpc { ...@@ -218,9 +238,9 @@ export class Bridge extends PadchatRpc {
throw new Error('userId exist') throw new Error('userId exist')
} }
this.selfId = userId this.selfId = userId
if (userName) { // if (userName) {
this.selfName = userName // this.selfName = userName
} // }
await this.stopCheckScan() await this.stopCheckScan()
...@@ -492,7 +512,7 @@ export class Bridge extends PadchatRpc { ...@@ -492,7 +512,7 @@ export class Bridge extends PadchatRpc {
// const result = await this.padchatRpc.WXGetQRCode() // const result = await this.padchatRpc.WXGetQRCode()
const result = await this.WXGetQRCode() const result = await this.WXGetQRCode()
if (!result) { if (!result || !result.qr_code) {
log.verbose('PuppetPadchatBridge', `emitLoginQrCode() result not found. Call WXInitialize() and try again ...`) log.verbose('PuppetPadchatBridge', `emitLoginQrCode() result not found. Call WXInitialize() and try again ...`)
// await this.padchatRpc.WXInitialize() // await this.padchatRpc.WXInitialize()
await this.WXInitialize() await this.WXInitialize()
......
...@@ -4,8 +4,9 @@ import { ...@@ -4,8 +4,9 @@ import {
export const WECHATY_PUPPET_PADCHAT_ENDPOINT = process.env['WECHATY_PUPPET_PADCHAT_ENDPOINT'] || 'ws://54.223.36.77:9091/wx' export const WECHATY_PUPPET_PADCHAT_ENDPOINT = process.env['WECHATY_PUPPET_PADCHAT_ENDPOINT'] || 'ws://54.223.36.77:9091/wx'
const WECHATY_PUPPET_PADCHAT_TOKEN = process.env['WECHATY_PUPPET_PADCHAT_TOKEN'] as string function padchatToken() {
if (!WECHATY_PUPPET_PADCHAT_TOKEN) { const token = process.env['WECHATY_PUPPET_PADCHAT_TOKEN'] as string
if (!token) {
log.error('PuppetPadchatConfig', ` log.error('PuppetPadchatConfig', `
WECHATY_PUPPET_PADCHAT_TOKEN environment variable not found. WECHATY_PUPPET_PADCHAT_TOKEN environment variable not found.
...@@ -17,9 +18,11 @@ if (!WECHATY_PUPPET_PADCHAT_TOKEN) { ...@@ -17,9 +18,11 @@ if (!WECHATY_PUPPET_PADCHAT_TOKEN) {
Learn more about it at: https://github.com/Chatie/wechaty/issues/1296 Learn more about it at: https://github.com/Chatie/wechaty/issues/1296
`) `)
process.exit(1) throw new Error('set WECHATY_PUPPET_PADCHAT_TOKEN and try again')
}
return token
} }
export { export {
WECHATY_PUPPET_PADCHAT_TOKEN, padchatToken,
} }
...@@ -205,7 +205,7 @@ export class PadchatRpc extends EventEmitter { ...@@ -205,7 +205,7 @@ export class PadchatRpc extends EventEmitter {
payload.type, payload.type,
JSON.stringify(payload), JSON.stringify(payload),
) )
this.emit('logout', payload.msg) this.emit('padchat-logout', payload.msg)
return return
} }
...@@ -259,11 +259,11 @@ export class PadchatRpc extends EventEmitter { ...@@ -259,11 +259,11 @@ export class PadchatRpc extends EventEmitter {
// ) // )
// log.silly('PadchatRpc', 'onSocketPadchat(%s)', JSON.stringify(padchatPayload).substr(0, 500)) // log.silly('PadchatRpc', 'onSocketPadchat(%s)', JSON.stringify(padchatPayload).substr(0, 500))
if (padchatPayload.type === PadchatPayloadType.Logout) { // if (padchatPayload.type === PadchatPayloadType.Logout) {
// this.emit('logout', this.selfId()) // // this.emit('logout', this.selfId())
console.log('onSocketPadchat: ', JSON.stringify(padchatPayload)) // console.log('onSocketPadchat: ', JSON.stringify(padchatPayload))
this.emit('logout') // this.emit('logout')
} // }
let result: any let result: any
......
...@@ -53,7 +53,7 @@ import { ...@@ -53,7 +53,7 @@ import {
} from '../config' } from '../config'
import { import {
WECHATY_PUPPET_PADCHAT_TOKEN, padchatToken,
WECHATY_PUPPET_PADCHAT_ENDPOINT, WECHATY_PUPPET_PADCHAT_ENDPOINT,
} from './config' } from './config'
...@@ -113,7 +113,7 @@ export class PuppetPadchat extends Puppet { ...@@ -113,7 +113,7 @@ export class PuppetPadchat extends Puppet {
this.bridge = new Bridge({ this.bridge = new Bridge({
memory : this.options.memory, memory : this.options.memory,
token : WECHATY_PUPPET_PADCHAT_TOKEN, token : padchatToken(),
endpoint : WECHATY_PUPPET_PADCHAT_ENDPOINT, endpoint : WECHATY_PUPPET_PADCHAT_ENDPOINT,
}) })
} }
......
...@@ -327,6 +327,8 @@ export class PadchatPureFunctionHelper { ...@@ -327,6 +327,8 @@ export class PadchatPureFunctionHelper {
} }
public static async imageBase64ToQrcode(base64: string): Promise<string> { public static async imageBase64ToQrcode(base64: string): Promise<string> {
// console.log('base64: ', typeof base64, String(base64).substr(0, 500))
const imageBuffer = Buffer.from(base64, 'base64') const imageBuffer = Buffer.from(base64, 'base64')
const future = new Promise<string>((resolve, reject) => { const future = new Promise<string>((resolve, reject) => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册