From cf4f4896d41399c5478a5e0f7b63369d16cb0e0d Mon Sep 17 00:00:00 2001 From: Huan LI Date: Thu, 14 Jun 2018 21:25:32 +0800 Subject: [PATCH] code clean --- src/contact.ts | 9 ++---- src/friendship.ts | 2 +- src/puppet-padchat/padchat-manager.ts | 28 ++++++++++++++---- src/puppet-padchat/puppet-padchat.ts | 22 +++++++------- src/puppet/puppet.ts | 42 +++++++++++---------------- src/room.ts | 6 ++-- 6 files changed, 57 insertions(+), 52 deletions(-) diff --git a/src/contact.ts b/src/contact.ts index 0746a56e..14c2db45 100644 --- a/src/contact.ts +++ b/src/contact.ts @@ -530,18 +530,13 @@ export class Contact extends Accessory implements Sayable { * await contact.sync() */ public async sync(): Promise { - // TODO: make sure the contact.* works when we are refreshing the data - // if (this.isReady()) { - // this.dirtyObj = this.obj - // } - // this.payload = undefined await this.ready(true) } /** * @private */ - public async ready(noCache = false): Promise { + public async ready(dirty = false): Promise { log.silly('Contact', 'ready() @ %s', this.puppet) if (this.isReady()) { // already ready @@ -550,7 +545,7 @@ export class Contact extends Accessory implements Sayable { } try { - await this.puppet.contactPayload(this.id, noCache) + await this.puppet.contactPayload(this.id, dirty) // log.silly('Contact', `ready() this.puppet.contactPayload(%s) resolved`, this) } catch (e) { diff --git a/src/friendship.ts b/src/friendship.ts index 8aeea126..91d00090 100644 --- a/src/friendship.ts +++ b/src/friendship.ts @@ -167,7 +167,7 @@ export class Friendship extends Accessory { } /** - * no `noCache` support because Friendship has no rawPayload(yet) + * no `dirty` support because Friendship has no rawPayload(yet) */ public async ready(): Promise { if (this.payload) { diff --git a/src/puppet-padchat/padchat-manager.ts b/src/puppet-padchat/padchat-manager.ts index 0bba7633..db50fba2 100644 --- a/src/puppet-padchat/padchat-manager.ts +++ b/src/puppet-padchat/padchat-manager.ts @@ -164,11 +164,9 @@ export class PadchatManager extends PadchatRpc { && this.cacheRoomMemberRawPayload && this.cacheRoomRawPayload ) { - await Promise.all([ - this.cacheContactRawPayload.close(), - this.cacheRoomMemberRawPayload.close(), - this.cacheRoomRawPayload.close(), - ]) + await this.cacheContactRawPayload.close(), + await this.cacheRoomMemberRawPayload.close(), + await this.cacheRoomRawPayload.close(), this.cacheContactRawPayload = undefined this.cacheRoomMemberRawPayload = undefined @@ -485,6 +483,14 @@ export class PadchatManager extends PadchatRpc { const autoLoginResult = await this.WXAutoLogin(token) // const autoLoginResult = await this.padchatRpc.WXAutoLogin(this.autoData.token) if (!autoLoginResult) { + + /** + * 1.1 Delete token for prevent future useless auto login retry + */ + delete deviceInfo.token + this.options.memory.set(MEMORY_SLOT_NAME, memorySlot) + await this.options.memory.save() + await this.emitLoginQrcode() return false } @@ -513,6 +519,14 @@ export class PadchatManager extends PadchatRpc { * 4 Send Login Request to user fail, emit QrCode for scan. */ await this.emitLoginQrcode() + + /** + * 5 Delete token for prevent future useless auto login retry + */ + delete deviceInfo.token + this.options.memory.set(MEMORY_SLOT_NAME, memorySlot) + await this.options.memory.save() + return false } @@ -584,6 +598,10 @@ export class PadchatManager extends PadchatRpc { */ if (memorySlot.currentUserId === userId) { log.silly('PuppetPadchatManager', 'refresh62Data() userId did not change since last login, keep the data as the same') + + // Update Token + memorySlot.device[userId]!.token = await this.WXGetLoginToken() + return memorySlot } diff --git a/src/puppet-padchat/puppet-padchat.ts b/src/puppet-padchat/puppet-padchat.ts index f9c65c1d..0949cd73 100644 --- a/src/puppet-padchat/puppet-padchat.ts +++ b/src/puppet-padchat/puppet-padchat.ts @@ -262,7 +262,7 @@ export class PuppetPadchat extends Puppet { * 1. Sometimes will get duplicated same messages from rpc, drop the same message from here. */ if (this.cachePadchatMessagePayload.has(rawPayload.msg_id)) { - log.warn('PuppetPadchat', 'onPadchatMessage(id=%s) duplicate message: %s', + log.silly('PuppetPadchat', 'onPadchatMessage(id=%s) duplicate message: %s', rawPayload.msg_id, JSON.stringify(rawPayload).substr(0, 500), ) @@ -360,7 +360,7 @@ export class PuppetPadchat extends Puppet { /** * Dirty Cache */ - this.roomMemberPayloadDirty(roomId) + await this.roomMemberPayloadDirty(roomId) return retry(new Error('roomMemberSearch() not found')) @@ -419,7 +419,8 @@ export class PuppetPadchat extends Puppet { /** * Dirty Cache */ - this.roomMemberPayloadDirty(roomId) + await this.roomMemberPayloadDirty(roomId) + await this.roomPayloadDirty(roomId) this.emit('room-leave', roomId, leaverIdList, removerId) } @@ -439,8 +440,8 @@ export class PuppetPadchat extends Puppet { const roomId = roomTopicEvent.roomId log.silly('PuppetPadchat', 'onPadchatMessageRoomEventTopic() roomTopicEvent="%s"', JSON.stringify(roomTopicEvent)) - const roomPayload = await this.roomPayload(roomId) - const oldTopic = roomPayload.topic + const roomOldPayload = await this.roomPayload(roomId) + const oldTopic = roomOldPayload.topic const changerIdList = await this.roomMemberSearch(roomId, changerName) if (changerIdList.length < 1) { @@ -456,11 +457,10 @@ export class PuppetPadchat extends Puppet { /** * Update Room Payload to new Topic */ - const updateRoomPayload = await this.roomPayload(roomId) - updateRoomPayload.topic = newTopic - this.cacheRoomPayload.set(roomId, updateRoomPayload) - - this.roomPayloadDirty(roomId) + // const updateRoomPayload = await this.roomPayload(roomId) + // updateRoomPayload.topic = newTopic + // this.cacheRoomPayload.set(roomId, updateRoomPayload) + await this.roomPayloadDirty(roomId) this.emit('room-topic', roomId, newTopic, oldTopic, changerId) } @@ -1176,7 +1176,7 @@ export class PuppetPadchat extends Puppet { // this.padchatManager.friendshipRawPayloadDirty(friendshipId) } - this.friendshipPayloadDirty(friendshipId) + await super.friendshipPayloadDirty(friendshipId) } public async friendshipRawPayload(friendshipId: string): Promise { diff --git a/src/puppet/puppet.ts b/src/puppet/puppet.ts index df6b7bc6..e1ce608c 100644 --- a/src/puppet/puppet.ts +++ b/src/puppet/puppet.ts @@ -471,18 +471,16 @@ export abstract class Puppet extends EventEmitter implements Sayable { public async contactPayload( contactId: string, - noCache = false, + dirty = false, ): Promise { - // log.silly('Puppet', 'contactPayload(id=%s, noCache=%s) @ %s', contactId, noCache, this) + // log.silly('Puppet', 'contactPayload(id=%s, dirty=%s) @ %s', contactId, dirty, this) if (!contactId) { throw new Error('no id') } - if (noCache) { - log.silly('Puppet', 'contactPayload(%s) cache PURGE', contactId) - - this.cacheContactPayload.del(contactId) + if (dirty) { + this.contactPayloadDirty(contactId) } else { const cachedPayload = this.contactPayloadCache(contactId) @@ -539,18 +537,16 @@ export abstract class Puppet extends EventEmitter implements Sayable { public async friendshipPayload( friendshipId: string, - noCache = false, + dirty = false, ): Promise { - log.verbose('Puppet', 'friendshipPayload(id=%s, noCache=%s)', friendshipId, noCache) + log.verbose('Puppet', 'friendshipPayload(id=%s, dirty=%s)', friendshipId, dirty) if (!friendshipId) { throw new Error('no id') } - if (noCache) { - log.silly('Puppet', 'friendshipPayload(%s) cache PURGE', friendshipId) - - this.cacheFriendshipPayload.del(friendshipId) + if (dirty) { + this.friendshipPayloadDirty(friendshipId) } else { const cachedPayload = this.friendshipPayloadCache(friendshipId) @@ -608,18 +604,16 @@ export abstract class Puppet extends EventEmitter implements Sayable { public async messagePayload( messageId: string, - noCache = false, + dirty = false, ): Promise { - log.verbose('Puppet', 'messagePayload(id=%s, noCache=%s)', messageId, noCache) + log.verbose('Puppet', 'messagePayload(id=%s, dirty=%s)', messageId, dirty) if (!messageId) { throw new Error('no id') } - if (noCache) { - log.silly('Puppet', 'messagePayload(%s) cache PURGE', messageId) - - this.cacheMessagePayload.del(messageId) + if (dirty) { + this.messagePayloadDirty(messageId) } else { const cachedPayload = this.messagePayloadCache(messageId) @@ -828,18 +822,16 @@ export abstract class Puppet extends EventEmitter implements Sayable { public async roomPayload( roomId: string, - noCache = false, + dirty = false, ): Promise { - log.verbose('Puppet', 'roomPayload(id=%s, noCache=%s)', roomId, noCache) + log.verbose('Puppet', 'roomPayload(id=%s, dirty=%s)', roomId, dirty) if (!roomId) { throw new Error('no id') } - if (noCache) { - log.silly('Puppet', 'roomPayload(%s) cache PURGE', roomId) - - this.cacheRoomPayload.del(roomId) + if (dirty) { + this.roomPayloadDirty(roomId) } else { const cachedPayload = this.roomPayloadCache(roomId) @@ -905,7 +897,7 @@ export abstract class Puppet extends EventEmitter implements Sayable { contactId : string, dirty = false, ): Promise { - log.verbose('Puppet', 'roomMemberPayload(roomId=%s, contactId=%s noCache=%s)', roomId, contactId, dirty) + log.verbose('Puppet', 'roomMemberPayload(roomId=%s, contactId=%s dirty=%s)', roomId, contactId, dirty) if (!roomId || !contactId) { throw new Error('no id') diff --git a/src/room.ts b/src/room.ts index b62c4fe8..8a800c65 100644 --- a/src/room.ts +++ b/src/room.ts @@ -240,15 +240,15 @@ export class Room extends Accessory implements Sayable { * @private */ public async ready( - noCache = false, + dirty = false, ): Promise { log.verbose('Room', 'ready()') - if (!noCache && this.isReady()) { + if (!dirty && this.isReady()) { return } - await this.puppet.roomPayload(this.id, noCache) + await this.puppet.roomPayload(this.id, dirty) const memberIdList = await this.puppet.roomMemberList(this.id) -- GitLab