diff --git a/package.json b/package.json index 26f6825ade476a49f1a5eb1793261836727581bd..d197bb804ec0e22d76cd53adb12e965e3b4faff1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wechaty", - "version": "0.15.170", + "version": "0.15.172", "description": "Wechat for Bot(Personal Account)", "main": "dist/src/index.js", "typings": "dist/src/index.d.ts", diff --git a/src/contact.ts b/src/contact.ts index 0746a56e6fd1b81f3d4754f4d51b00ca22db9eda..14c2db45ab821a46abda1ef94f805bf56fa8b8ce 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 8aeea1266cd3d8e3e69271d1360dddab11211962..91d000905443a5e318fd136dfad1f565ab11fa81 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 0bba76337c4f9b64bca02c847b54228079673b16..9aededafed4429438e7a8dcdf3a7ce38b6a24137 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 } @@ -748,7 +766,17 @@ export class PadchatManager extends PadchatRpc { if (!memberListPayload || !('user_name' in memberListPayload)) { // check user_name too becasue the server might return {} // console.log('memberListPayload', memberListPayload) - throw new Error('no memberListPayload') + // throw new Error('no memberListPayload') + + /** + * Room Id not exist + * See: https://github.com/lijiarui/wechaty-puppet-padchat/issues/64#issuecomment-397319016 + */ + this.roomMemberRawPayloadDirty(roomId) + this.roomRawPayloadDirty(roomId) + + return {} + } log.silly('PuppetPadchatManager', 'syncRoomMember(%s) total %d members', diff --git a/src/puppet-padchat/padchat-rpc.ts b/src/puppet-padchat/padchat-rpc.ts index d13a39cedcbcac8123136d302c3b8903de45c8e8..3aaf518abd5dd90d99b9dc50a7f41cc18d7f012f 100644 --- a/src/puppet-padchat/padchat-rpc.ts +++ b/src/puppet-padchat/padchat-rpc.ts @@ -709,12 +709,18 @@ export class PadchatRpc extends EventEmitter { * Get all member of a room by room id * @param {any} roomId chatroom_id */ - public async WXGetChatRoomMember(roomId: string): Promise { + public async WXGetChatRoomMember(roomId: string): Promise { const result = await this.rpcCall('WXGetChatRoomMember', roomId) if (!result) { throw Error('PadchatRpc, WXGetChatRoomMember, cannot get result from websocket server!') } + // roomId not exist. (or no permision?) + // See: https://github.com/lijiarui/wechaty-puppet-padchat/issues/64#issuecomment-397319016 + if (result.status === -19) { + return null + } + log.silly('PadchatRpc', 'WXGetChatRoomMember() result: %s', JSON.stringify(result).substr(0, 500)) // 00:40:44 SILL PadchatRpc WXGetChatRoomMember() result: {"chatroom_id":0,"count":0,"member":"null\n","message":"","status":0,"user_name":""} diff --git a/src/puppet-padchat/puppet-padchat.ts b/src/puppet-padchat/puppet-padchat.ts index f9c65c1d8fba914e83e6d2d2b0bbe1b29ba798eb..551bec590e3da78545d7724e45a2c999fe37a6ca 100644 --- a/src/puppet-padchat/puppet-padchat.ts +++ b/src/puppet-padchat/puppet-padchat.ts @@ -18,8 +18,6 @@ */ import path from 'path' -// import fs from 'fs' -// import cuid from 'cuid' import LRU from 'lru-cache' import flatten from 'array-flatten' @@ -262,7 +260,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 +358,7 @@ export class PuppetPadchat extends Puppet { /** * Dirty Cache */ - this.roomMemberPayloadDirty(roomId) + await this.roomMemberPayloadDirty(roomId) return retry(new Error('roomMemberSearch() not found')) @@ -419,7 +417,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 +438,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 +455,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 +1174,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 df6b7bc60159b505163a289472312050d49774c8..e1ce608c3a9fe77c027aa71aeb31f6fb4dd63f00 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 b62c4fe84840323d0ea7b6802037639b027b18c4..8a800c65c95fc744445a571c8200f77f40c4f515 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)