Skip to content

Commit

Permalink
retry to search new members on room join event. (#1324)
Browse files Browse the repository at this point in the history
  • Loading branch information
huan committed Jun 13, 2018
1 parent c130f7f commit 72d3480
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
10 changes: 9 additions & 1 deletion src/puppet-padchat/padchat-manager.ts
Expand Up @@ -660,12 +660,20 @@ export class PadchatManager extends PadchatRpc {
return roomIdList
}

public async getRoomMemberIdList(roomId: string): Promise<string[]> {
public async getRoomMemberIdList(
roomId: string,
noCache = false,
): Promise<string[]> {
log.verbose('PuppetPadchatManager', 'getRoomMemberIdList(%d)', roomId)
if (!this.cacheRoomMemberRawPayload) {
throw new Error('cache not inited' )
}

if (noCache) {
log.verbose('PuppetPadchatManager', 'getRoomMemberIdList(%d) cache PURGE', roomId)
this.cacheRoomMemberRawPayload.delete(roomId)
}

const memberRawPayloadDict = this.cacheRoomMemberRawPayload.get(roomId)
|| await this.syncRoomMember(roomId)

Expand Down
42 changes: 35 additions & 7 deletions src/puppet-padchat/puppet-padchat.ts
Expand Up @@ -28,6 +28,8 @@ import {
FileBox,
} from 'file-box'

import Misc from '../misc'

import {
ContactPayload,

Expand Down Expand Up @@ -333,22 +335,48 @@ export class PuppetPadchat extends Puppet {
const inviterName = roomJoin.inviterName
const roomId = roomJoin.roomId

const inviteeIdList = flatten<string>(
await Promise.all(
inviteeNameList.map(
inviteeName => this.roomMemberSearch(roomId, inviteeName),
const inviteeIdList = await Misc.retry(async (retry, attempt) => {
log.verbose('PuppetPadchat', 'onPadchatMessageRoomEvent({id=%s}) roomJoin retry(attempt=%d)', attempt)

const tryIdList = flatten<string>(
await Promise.all(
inviteeNameList.map(
inviteeName => this.roomMemberSearch(roomId, inviteeName),
),
),
),
)
)

if (tryIdList.length) {
return tryIdList
}

if (!this.bridge) {
throw new Error('no manager')
}

/**
* PURGE Cache and Reload
*/
await this.bridge.getRoomMemberIdList(roomId, true)

return retry(new Error('roomMemberSearch() not found'))

}).catch(e => {
log.warn('PuppetPadchat', 'onPadchatMessageRoomEvent({id=%s}) roomJoin retry() fail: %s', e.message)
return [] as string[]
})

const inviterIdList = await this.roomMemberSearch(roomId, inviterName)

if (inviterIdList.length < 1) {
throw new Error('no inviterId found')
} else if (inviterIdList.length > 1) {
log.warn('PuppetPadchat', 'onPadchatMessageRoomEvent() case PadchatMesssageSys: inviterId found more than 1, use the first one.')
}

const inviterId = inviterIdList[0]

this.emit('room-join', roomId, inviteeIdList, inviterId)
this.emit('room-join', roomId, inviteeIdList, inviterId)
}
/**
* 2. Look for room leave event
Expand Down
1 change: 0 additions & 1 deletion src/puppet/puppet.ts
Expand Up @@ -695,7 +695,6 @@ export abstract class Puppet extends EventEmitter implements Sayable {
/**
* 2. for RoomMemberQueryFilter
*/
// const roomPayload = await this.roomPayload(roomId)
const memberIdList = await this.roomMemberList(roomId)

let idList: string[] = []
Expand Down

0 comments on commit 72d3480

Please sign in to comment.