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

refactor contactValidate() & roomValidate()

上级 8d3ca9aa
......@@ -131,14 +131,40 @@ export class Contact extends Accessory implements Sayable {
log.verbose('Contact', 'find(%s)', JSON.stringify(query))
const contactList = await this.findAll(query)
if (!contactList || !contactList.length) {
if (!contactList) {
return null
}
if (contactList.length < 1) {
return null
}
if (contactList.length > 1) {
log.warn('Contact', 'function find(%s) get %d contacts, use the first one by default', JSON.stringify(query), contactList.length)
log.warn('Contact', 'find() got more than one(%d) result', contactList.length)
}
let n = 0
for (n = 0; n < contactList.length; n++) {
const contact = contactList[n]
// use puppet.contactValidate() to confirm double confirm that this contactId is valid.
// https://github.com/lijiarui/wechaty-puppet-padchat/issues/64
// https://github.com/Chatie/wechaty/issues/1345
const valid = await this.puppet.contactValidate(contact.id)
if (valid) {
log.verbose('Contact', 'find() confirm contact[#%d] with id=%d is vlaid result, return it.',
n,
contact.id,
)
return contact
} else {
log.verbose('Contact', 'find() confirm contact[#%d] with id=%d is INVALID result, try next',
n,
contact.id,
)
}
}
return contactList[0]
log.warn('Contact', 'find() got %d contacts but no one is valid.', contactList.length)
return null
}
/**
......
......@@ -145,11 +145,6 @@ export class PuppetMock extends Puppet {
return
}
public async contactValid(contactId: string): Promise<boolean> {
log.verbose('PuppetMock', 'contactValid(%s)', contactId)
return true
}
public async contactList(): Promise<string[]> {
log.verbose('PuppetMock', 'contactList()')
......@@ -303,11 +298,6 @@ export class PuppetMock extends Puppet {
return payload
}
public async roomValid(roomId: string): Promise<boolean> {
log.verbose('PuppetMock', 'roomValid(%s)', roomId)
return true
}
public async roomList(): Promise<string[]> {
log.verbose('PuppetMock', 'roomList()')
......
......@@ -558,7 +558,7 @@ export class PuppetPadchat extends Puppet {
return
}
public async contactValid(contactId: string): Promise<boolean> {
public async contactValidate(contactId: string): Promise<boolean> {
log.verbose('PuppetPadchat', 'contactValid(%s)', contactId)
return true
}
......@@ -944,7 +944,7 @@ export class PuppetPadchat extends Puppet {
return memberIdList
}
public async roomValid(roomId: string): Promise<boolean> {
public async roomValidate(roomId: string): Promise<boolean> {
log.verbose('PuppetPadchat', 'roomValid(%s)', roomId)
if (!this.padchatManager) {
throw new Error('no padchat manager')
......
......@@ -858,11 +858,6 @@ export class PuppetPuppeteer extends Puppet {
// return filterFunction
// }
public async contactValid(contactId: string): Promise<boolean> {
log.verbose('PuppetPuppeteer', 'contactValid(%s)', contactId)
return true
}
public async contactList(): Promise<string[]> {
const idList = await this.bridge.contactList()
return idList
......@@ -993,11 +988,6 @@ export class PuppetPuppeteer extends Puppet {
return roomPayload
}
public async roomValid(roomId: string): Promise<boolean> {
log.verbose('PuppetPuppeteer', 'roomValid(%s)', roomId)
return true
}
public async roomList(): Promise<string[]> {
log.verbose('PuppetPupppeteer', 'roomList()')
......
......@@ -305,11 +305,6 @@ export class PuppetWechat4u extends Puppet {
await this.wechat4u.updateRemarkName(contactId, alias)
}
public async contactValid(contactId: string): Promise<boolean> {
log.verbose('PuppetWechat4u', 'contactValid(%s)', contactId)
return true
}
public async contactList(): Promise<string[]> {
log.verbose('PuppetWechat4u', 'contactList()')
......@@ -756,11 +751,6 @@ export class PuppetWechat4u extends Puppet {
return roomPayload
}
public async roomValid(roomId: string): Promise<boolean> {
log.verbose('PuppetWechat4u', 'roomValid(%s)', roomId)
return true
}
public async roomList(): Promise<string[]> {
log.verbose('PuppetWechat4u', 'roomList()')
......
......@@ -57,7 +57,6 @@ class PuppetTest extends Puppet {
public async contactAvatar(contactId: string, file: FileBox) : Promise<void>
public async contactAvatar(contactId: string, file?: FileBox) : Promise<void | FileBox> { return {contactId, file} as any }
public async contactValid(contactId: string) : Promise<boolean> { return !!contactId }
public async contactList() : Promise<string[]> { return {} as any }
public async contactQrcode(contactId: string) : Promise<string> { return {contactId} as any }
......@@ -109,7 +108,6 @@ class PuppetTest extends Puppet {
public async roomTopic(roomId: string, topic: string) : Promise<void>
public async roomTopic(roomId: string, topic?: string) : Promise<string | void> { return {roomId, topic} as any }
public async roomValid(roomId: string) : Promise<boolean> { return !!roomId }
public async roomList() : Promise<string[]> { return {} as any }
public async roomMemberList(roomId: string) : Promise<string[]> { return {roomId} as any }
......
......@@ -365,7 +365,6 @@ export abstract class Puppet extends EventEmitter implements Sayable {
public abstract async contactAvatar(contactId: string) : Promise<FileBox>
public abstract async contactAvatar(contactId: string, file: FileBox) : Promise<void>
public abstract async contactValid(contactId: string) : Promise<boolean>
public abstract async contactList() : Promise<string[]>
public abstract async contactQrcode(contactId: string) : Promise<string>
......@@ -454,6 +453,15 @@ export abstract class Puppet extends EventEmitter implements Sayable {
return filterFunction
}
/**
* Check a Contact Id if it's still valid.
* For example: talk to the server, and see if it should be deleted in the local cache.
*/
public async contactValidate(contactId: string) : Promise<boolean> {
log.silly('Puppet', 'contactValidate(%s) base class just return `true`', contactId)
return true
}
public contactPayloadCache(contactId: string): undefined | ContactPayload {
// log.silly('Puppet', 'contactPayloadCache(id=%s) @ %s', contactId, this)
if (!contactId) {
......@@ -631,7 +639,6 @@ export abstract class Puppet extends EventEmitter implements Sayable {
public abstract async roomAvatar(roomId: string) : Promise<FileBox>
public abstract async roomCreate(contactIdList: string[], topic?: string) : Promise<string>
public abstract async roomDel(roomId: string, contactId: string) : Promise<void>
public abstract async roomValid(roomId: string) : Promise<boolean>
public abstract async roomQuit(roomId: string) : Promise<void>
public abstract async roomTopic(roomId: string) : Promise<string>
......@@ -787,6 +794,15 @@ export abstract class Puppet extends EventEmitter implements Sayable {
return filterFunction
}
/**
* Check a Room Id if it's still valid.
* For example: talk to the server, and see if it should be deleted in the local cache.
*/
public async roomValidate(roomId: string): Promise<boolean> {
log.silly('Puppet', 'roomValidate(%s) base class just return `true`', roomId)
return true
}
public roomPayloadCache(roomId: string): undefined | RoomPayload {
// log.silly('Puppet', 'roomPayloadCache(id=%s) @ %s', roomId, this)
if (!roomId) {
......
......@@ -163,10 +163,10 @@ export class Room extends Accessory implements Sayable {
let n = 0
for (n = 0; n < roomList.length; n++) {
const room = roomList[n]
// use puppet.roomValid() to confirm double confirm that this roomId is valid.
// use puppet.roomValidate() to confirm double confirm that this roomId is valid.
// https://github.com/lijiarui/wechaty-puppet-padchat/issues/64
// https://github.com/Chatie/wechaty/issues/1345
const valid = await this.puppet.roomValid(room.id)
const valid = await this.puppet.roomValidate(room.id)
if (valid) {
log.verbose('Room', 'find() confirm room[#%d] with id=%d is vlaid result, return it.',
n,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册