Skip to content

Commit

Permalink
Merge pull request #1256 from lijiarui/new-puppet-padchat
Browse files Browse the repository at this point in the history
New puppet padchat
  • Loading branch information
huan committed May 31, 2018
2 parents 1c39167 + 8f76169 commit 73fa8af
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 105 deletions.
179 changes: 108 additions & 71 deletions src/puppet-padchat/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export interface WXInitializeType {
status : number,
}

export interface WXAddChatRoomMemberType {
message : string, // "\n\u0010Everything+is+OK" (succeed) || "\n\u0014MemberList+are+wrong" ('user has in the room')
status : number, // 0
}

export interface WXGetQRCodeType {
qr_code : string,
}
Expand Down Expand Up @@ -82,6 +87,11 @@ export interface WXLoadWxDatType {
message : string, // ok
}

export interface StandardType {
status : number, // 0
message : string, // ''
}

export interface WXGetLoginTokenType {
message : string,
status : number, // 0,
Expand Down Expand Up @@ -257,64 +267,64 @@ export class Bridge extends EventEmitter {
return result
}

/**
* Load all Contact and Room
* see issue https://github.com/lijiarui/test-ipad-puppet/issues/39
* @returns {Promise<(PadchatRoomRawPayload | PadchatContactRawPayload)[]>}
*/
private async WXSyncContact(): Promise<(PadchatRoomRawPayload | PadchatContactRawPayload)[]> {
const result = await this.sendToWebSocket('WXSyncContact', [])
if (!result) {
throw Error('WXSyncContact error! canot get result from websocket server')
}
return result
}

public async checkSyncContactOrRoom(): Promise<{
contactMap: Map<string, PadchatContactRawPayload>,
roomMap: Map<string, PadchatRoomRawPayload>,
}> {
log.silly('PuppetPadchat', `checkSyncContact`)

let cont = true
const syncContactMap = new Map<string, PadchatContactRawPayload>()
const syncRoomMap = new Map<string, PadchatRoomRawPayload>()

while (cont) {
const syncContactList = await this.WXSyncContact()

await new Promise(r => setTimeout(r, 3 * 1000))

if (!Array.isArray(syncContactList)) {
log.error('PuppetPadchat', 'checkSyncContact cannot get array result!')
continue
}

syncContactList.forEach(syncContact => {
if (syncContact.continue === 0) {
log.info('PuppetPadchat', 'checkSyncContact sync contact done!')
cont = false
return
}

if (syncContact.continue === 1 && syncContact.msg_type === 2) {
if (/@chatroom$/.test(syncContact.user_name)) {
syncRoomMap.set(syncContact.user_name, syncContact as PadchatRoomRawPayload)
} else {
syncContactMap.set(syncContact.user_name, syncContact as PadchatContactRawPayload)
}
}
return
})

log.info('PuppetPadchat', `checkSyncContact, not load yet, continue to WXSyncContact`)
}
// /**
// * Load all Contact and Room
// * see issue https://github.com/lijiarui/test-ipad-puppet/issues/39
// * @returns {Promise<(PadchatRoomRawPayload | PadchatContactRawPayload)[]>}
// */
// private async WXSyncContact(): Promise<(PadchatRoomRawPayload | PadchatContactRawPayload)[]> {
// const result = await this.sendToWebSocket('WXSyncContact', [])
// if (!result) {
// throw Error('WXSyncContact error! canot get result from websocket server')
// }
// return result
// }

return {
contactMap: syncContactMap,
roomMap: syncRoomMap,
}
}
// public async checkSyncContactOrRoom(): Promise<{
// contactMap: Map<string, PadchatContactRawPayload>,
// roomMap: Map<string, PadchatRoomRawPayload>,
// }> {
// log.silly('PuppetPadchat', `checkSyncContact`)

// let cont = true
// const syncContactMap = new Map<string, PadchatContactRawPayload>()
// const syncRoomMap = new Map<string, PadchatRoomRawPayload>()

// while (cont) {
// const syncContactList = await this.WXSyncContact()

// await new Promise(r => setTimeout(r, 3 * 1000))

// if (!Array.isArray(syncContactList)) {
// log.error('PuppetPadchat', 'checkSyncContact cannot get array result!')
// continue
// }

// syncContactList.forEach(syncContact => {
// if (syncContact.continue === 0) {
// log.info('PuppetPadchat', 'checkSyncContact sync contact done!')
// cont = false
// return
// }

// if (syncContact.continue === 1 && syncContact.msg_type === 2) {
// if (/@chatroom$/.test(syncContact.user_name)) {
// syncRoomMap.set(syncContact.user_name, syncContact as PadchatRoomRawPayload)
// } else {
// syncContactMap.set(syncContact.user_name, syncContact as PadchatContactRawPayload)
// }
// }
// return
// })

// log.info('PuppetPadchat', `checkSyncContact, not load yet, continue to WXSyncContact`)
// }

// return {
// contactMap: syncContactMap,
// roomMap: syncRoomMap,
// }
// }

/**
* Generate 62 data
Expand Down Expand Up @@ -578,27 +588,51 @@ export class Bridge extends EventEmitter {
return
}

public async WXSetUserRemark(id: string, remark: string): Promise<any> {
public async WXSetUserRemark(id: string, remark: string): Promise<StandardType> {
const result = await this.sendToWebSocket('WXSetUserRemark', [id, remark])
console.log(result)
log.silly('PuppetPadchatBridge', 'WXSetUserRemark result: %s', JSON.stringify(result))
if (!result || result.status !== 0) {
throw Error('WXSetUserRemark error! canot get result from websocket server')
}
return result
}

public async WXDeleteChatRoomMember(roomId: string, contactId: string): Promise<any> {
public async WXDeleteChatRoomMember(roomId: string, contactId: string): Promise<StandardType> {
const result = await this.sendToWebSocket('WXDeleteChatRoomMember', [roomId, contactId])
console.log(result)
log.silly('PuppetPadchatBridge', 'WXDeleteChatRoomMember result: %s', JSON.stringify(result))
if (!result || result.status !== 0) {
throw Error('WXDeleteChatRoomMember error! canot get result from websocket server')
}
return result
}

public async WXAddChatRoomMember(roomId: string, contactId: string): Promise<any> {
const result = await this.sendToWebSocket('WXAddChatRoomMember', [roomId, contactId])
console.log(result)
return result
public async WXAddChatRoomMember(roomId: string, contactId: string): Promise<boolean> {
const result = (await this.sendToWebSocket('WXAddChatRoomMember', [roomId, contactId])) as WXAddChatRoomMemberType
log.silly('PuppetPadchatBridge', 'WXAddChatRoomMember result: %s', JSON.stringify(result))
if (result && result.status === -2028) {
// result: {"message":"","status":-2028}
// May be the owner has see not allow other people to join in the room (群聊邀请确认)
log.warn('PuppetPadchatBridge', 'WXAddChatRoomMember failed! maybe owner open the should confirm first to invited others to join in the room.')
return false
}

if (!result || result.status !== 0) {
throw Error('WXAddChatRoomMember error! canot get result from websocket server')
}

// see more in WXAddChatRoomMemberType
if (/OK/i.test(result.message)) {
return true
}
return false
}

public async WXSetChatroomName(roomId: string, topic: string): Promise<any> {
public async WXSetChatroomName(roomId: string, topic: string): Promise<StandardType> {
const result = await this.sendToWebSocket('WXSetChatroomName', [roomId, topic])
console.log(result)
log.silly('PuppetPadchatBridge', 'WXSetChatroomName result: %s', JSON.stringify(result))
if (!result || result.status !== 0) {
throw Error('WXSetChatroomName error! canot get result from websocket server')
}
return result
}

Expand All @@ -609,9 +643,12 @@ export class Bridge extends EventEmitter {
// return result
// }

public async WXQuitChatRoom(roomId: string): Promise<any> {
public async WXQuitChatRoom(roomId: string): Promise<StandardType> {
const result = await this.sendToWebSocket('WXQuitChatRoom', [roomId])
console.log(result)
log.silly('PuppetPadchatBridge', 'WXQuitChatRoom result: %s', JSON.stringify(result))
if (!result || result.status !== 0) {
throw Error('WXQuitChatRoom error! canot get result from websocket server')
}
return result
}

Expand All @@ -634,13 +671,13 @@ export class Bridge extends EventEmitter {
type = '14'
verify = 'hello'
const result = await this.sendToWebSocket('WXAddUser', [strangerV1, strangerV2, type, verify])
console.log(result)
log.silly('PuppetPadchatBridge', 'WXAddUser result: %s', JSON.stringify(result))
return result
}

public async WXAcceptUser(stranger: string, ticket: string): Promise<any> {
const result = await this.sendToWebSocket('WXAcceptUser', [stranger, ticket])
console.log(result)
log.silly('PuppetPadchatBridge', 'WXAcceptUser result: %s', JSON.stringify(result))
return result
}

Expand Down
62 changes: 28 additions & 34 deletions src/puppet-padchat/puppet-padchat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ export class PuppetPadchat extends Puppet {
await this.bridge.WXSendMsg(this.bridge.autoData.user_name, 'Bot on line!')

this.state.on(true)
this.emit('start')
// this.emit('start')
this.initWatchdog()

return
Expand Down Expand Up @@ -426,18 +426,18 @@ export class PuppetPadchat extends Puppet {
// await some tasks...
this.state.off(true)

this.emit('stop')
// this.emit('stop')
}

public async logout(): Promise<void> {
log.verbose('PuppetPadchat', 'logout()')

if (!this.id) {
if (!this.userId) {
throw new Error('logout before login?')
}

this.emit('logout', this.id) // becore we will throw above by logonoff() when this.user===undefined
this.id = undefined
this.emit('logout', this.userId) // becore we will throw above by logonoff() when this.user===undefined
this.userId = undefined

// TODO: this.bridge.logout
}
Expand Down Expand Up @@ -658,7 +658,7 @@ export class PuppetPadchat extends Puppet {
type = MessageType.Video
break
default:
type = MessageType.Text
type = this.Message.Type.Unknown
}

const payload: MessagePayload = {
Expand Down Expand Up @@ -731,24 +731,18 @@ export class PuppetPadchat extends Puppet {
messageId,
)

// const msg = this.Message.create(messageId)
// await msg.ready()

const payload = await this.messagePayload(messageId)
const msg = this.Message.create(messageId)
await msg.ready()

if (payload.type === MessageType.Text) {
if (!payload.text) {
throw new Error('no text!')
}
if (msg.type() === this.Message.Type.Text) {
await this.messageSendText(
receiver,
payload.text,
msg.text(),
)
} else {
const file = await this.messageFile(messageId)
await this.messageSendFile(
receiver,
file,
await msg.file(),
)
}
}
Expand Down Expand Up @@ -832,16 +826,15 @@ export class PuppetPadchat extends Puppet {
log.verbose('PuppetPadchat', 'roomFindAll(%s)', query)

// TODO: query
const rooomMap = (await this.bridge.checkSyncContactOrRoom()).roomMap
const roomIdList: string[] = []
rooomMap.forEach(async (value , id) => {
roomIdList.push(id)
// this.Room.load(id, await this.roomRawPayloadParser(value))
})

// TODO implenmentation
// const rooomMap = (await this.bridge.checkSyncContactOrRoom()).roomMap
// const roomIdList: string[] = []
// rooomMap.forEach(async (value , id) => {
// roomIdList.push(id)
// this.Room.load(id, await this.roomRawPayloadParser(value))
// })

return roomIdList
// return roomIdList
return []
}

public async roomDel(
Expand All @@ -850,6 +843,7 @@ export class PuppetPadchat extends Puppet {
): Promise<void> {
log.verbose('PuppetPadchat', 'roomDel(%s, %s)', roomId, contactId)

// Should check whether user is in the room. WXDeleteChatRoomMember won't check if user in the room automatically
await this.bridge.WXDeleteChatRoomMember(roomId, contactId)
}

Expand Down Expand Up @@ -930,16 +924,16 @@ export class PuppetPadchat extends Puppet {
): Promise<void> {
log.verbose('PuppetPadchat', 'friendRequestAccept(%s, %s)', contactId, ticket)

const rawPayload = await this.contactRawPayload(contactId)
// const rawPayload = await this.contactRawPayload(contactId)

if (!rawPayload.ticket) {
throw new Error('no ticket')
}
// if (!rawPayload.ticket) {
// throw new Error('no ticket')
// }

await this.bridge.WXAcceptUser(
rawPayload.stranger,
rawPayload.ticket,
)
// await this.bridge.WXAcceptUser(
// rawPayload.stranger,
// rawPayload.ticket,
// )
}

}
Expand Down

0 comments on commit 73fa8af

Please sign in to comment.