Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix contact update #42

Merged
merged 4 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Learn more about the Puppet at [Wechaty wiki: Puppet](https://github.com/Chatie/

## HISTORY

### v1.13.8 (Nov 22, 2022)

1.Fix Contact update

### v1.13.1 (Nov 18, 2022)

1.Support uos login
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechaty-puppet-wechat4u",
"version": "1.13.7",
"version": "1.13.11",
"description": "Wechat4u Puppet for Wechaty",
"type": "module",
"exports": {
Expand Down
64 changes: 48 additions & 16 deletions src/puppet-wechat4u.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { parseEmotionMessagePayload } from './wechat4u/messages/message-emotion.

import { wechat4uContactToWechaty } from './wechat4u/schema-mapper/contact.js'
import { wechat4uRoomMemberToWechaty, wechat4uRoomToWechaty } from './wechat4u/schema-mapper/room.js'
import { isRoomId } from './wechat4u/utils/is-type.js'

const MEMORY_SLOT_NAME = 'PUPPET-WECHAT4U'

Expand All @@ -69,7 +70,7 @@ export class PuppetWechat4u extends PUPPET.Puppet {

private scanQrCode?: string

private unknownContactId: string[] = []
private unknownContactId: string[][] = []
private getContactInterval: undefined | NodeJS.Timeout

private readonly cacheMessageRawPayload: QuickLru<string, WebMessageRawPayload>
Expand Down Expand Up @@ -147,19 +148,30 @@ export class PuppetWechat4u extends PUPPET.Puppet {
*/

private getContactsInfo () {
const tempArray: string[] = this.unknownContactId.splice(0, 50)
const tempArray: string[][] = this.unknownContactId.splice(0, 50)
if (tempArray.length === 0 && this.getContactInterval) {
clearInterval(this.getContactInterval)
this.getContactInterval = undefined
}
if (tempArray.length) {
const userDataList = tempArray.map(contactId => {
const userDataList = tempArray.map(contact => {
return {
EncryChatRoomId : '',
UserName : contactId,
EncryChatRoomId : contact[1],
UserName : contact[0],
}
})
this.wechat4u.batchGetContact(userDataList).then((result: any) => {
this.wechat4u.batchGetContact(userDataList).then((result: any[]) => {
result.forEach((item) => {
if (isRoomId(item.UserName)) {
const membersList = item.MemberList.map((mItem: any) => {
return {
...mItem,
EncryChatRoomId: item.UserName,
}
})
this.wechat4u.updateContacts(membersList)
}
})
this.wechat4u.updateContacts(result)
return null
}).catch((e: any) => {
Expand Down Expand Up @@ -360,6 +372,24 @@ export class PuppetWechat4u extends PUPPET.Puppet {
contacts.length,
Object.keys(wechat4u.contacts).length,
)
contacts.forEach((item: any) => {
if (isRoomId(item.UserName)) {
const membersList = item.MemberList.map((mItem: any) => {
this.unknownContactId.push([mItem.UserName, item.UserName])
return {
...mItem,
EncryChatRoomId: item.UserName,
}
})
this.wechat4u.updateContacts(membersList)
}
})
if (!this.getContactInterval) {
this.getContactsInfo()
this.getContactInterval = setInterval(() => {
this.getContactsInfo()
}, 800)
}
})
/**
* 错误事件,参数一般为Error对象
Expand All @@ -379,7 +409,6 @@ export class PuppetWechat4u extends PUPPET.Puppet {
log.warn('PuppetWechat4u', 'initHookEvents() wechat4u.on(message) no message id: %s', JSON.stringify(msg))
throw new Error('no id')
}

this.cacheMessageRawPayload.set(msg.MsgId, msg)
const event = await parseEvent(this, msg)
switch (event.type) {
Expand Down Expand Up @@ -497,14 +526,17 @@ export class PuppetWechat4u extends PUPPET.Puppet {

const name = payload.name
// add '&type=big' to get big image
const res = await this.wechat4u.getHeadImg(rawPayload.HeadImgUrl + '&type=big')
/**
* 如何获取联系人头像
*/
return FileBox.fromBuffer(
res.data,
`wechaty-contact-avatar-${name}.jpg`, // FIXME
)
if (rawPayload.HeadImgUrl) {
const res = await this.wechat4u.getHeadImg(rawPayload.HeadImgUrl + '&type=big')
/**
* 如何获取联系人头像
*/
return FileBox.fromBuffer(
res.data,
`wechaty-contact-avatar-${name}.jpg`, // FIXME
)
}

}

override async contactRawPayload (contactId: string): Promise<WebContactRawPayload> {
Expand All @@ -514,7 +546,7 @@ export class PuppetWechat4u extends PUPPET.Puppet {
)

if (!(contactId in this.wechat4u.contacts)) {
this.unknownContactId.push(contactId)
this.unknownContactId.push([contactId, ''])
if (!this.getContactInterval) {
this.getContactsInfo()
this.getContactInterval = setInterval(() => {
Expand Down
1 change: 0 additions & 1 deletion src/wechat4u/messages/message-sysmsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export async function parseSysmsgMessagePayload (message: WebMessageRawPayload):
}

const content = message.Content.trim()

const sysmsgIndex = content.indexOf('<sysmsg')
if (sysmsgIndex === -1) {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const sysmsgParser: MessageParser = async (webMessageRawPayload: WebMessa
if (!sysmsgPayload) {
return ret
}

switch (sysmsgPayload.type) {
case 'pat': {
const patMessagePayload: PatMessagePayload = sysmsgPayload.payload as PatMessagePayload
Expand Down