wechaty-puppet-matrix 0.0.30 → 0.0.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/dist/cjs/src/matrix/messages/message-appmsg.d.ts +2 -0
  2. package/dist/cjs/src/matrix/messages/message-appmsg.d.ts.map +1 -1
  3. package/dist/cjs/src/matrix/messages/message-appmsg.js +3 -1
  4. package/dist/cjs/src/matrix/messages/message-emotion.d.ts +3 -0
  5. package/dist/cjs/src/matrix/messages/message-emotion.d.ts.map +1 -1
  6. package/dist/cjs/src/matrix/messages/message-emotion.js +4 -1
  7. package/dist/cjs/src/matrix/messages/message-image.d.ts +4 -0
  8. package/dist/cjs/src/matrix/messages/message-image.d.ts.map +1 -1
  9. package/dist/cjs/src/matrix/messages/message-image.js +3 -1
  10. package/dist/cjs/src/matrix/messages/message-video.d.ts +10 -0
  11. package/dist/cjs/src/matrix/messages/message-video.d.ts.map +1 -1
  12. package/dist/cjs/src/matrix/messages/message-video.js +8 -6
  13. package/dist/cjs/src/matrix/service/request.d.ts +4 -1
  14. package/dist/cjs/src/matrix/service/request.d.ts.map +1 -1
  15. package/dist/cjs/src/matrix/service/request.js +55 -0
  16. package/dist/cjs/src/matrix/utils/index.d.ts +1 -0
  17. package/dist/cjs/src/matrix/utils/index.d.ts.map +1 -1
  18. package/dist/cjs/src/matrix/utils/index.js +25 -0
  19. package/dist/cjs/src/puppet-matrix.d.ts.map +1 -1
  20. package/dist/cjs/src/puppet-matrix.js +19 -6
  21. package/dist/esm/src/matrix/messages/message-appmsg.d.ts +2 -0
  22. package/dist/esm/src/matrix/messages/message-appmsg.d.ts.map +1 -1
  23. package/dist/esm/src/matrix/messages/message-appmsg.js +3 -1
  24. package/dist/esm/src/matrix/messages/message-emotion.d.ts +3 -0
  25. package/dist/esm/src/matrix/messages/message-emotion.d.ts.map +1 -1
  26. package/dist/esm/src/matrix/messages/message-emotion.js +4 -1
  27. package/dist/esm/src/matrix/messages/message-image.d.ts +4 -0
  28. package/dist/esm/src/matrix/messages/message-image.d.ts.map +1 -1
  29. package/dist/esm/src/matrix/messages/message-image.js +3 -1
  30. package/dist/esm/src/matrix/messages/message-video.d.ts +10 -0
  31. package/dist/esm/src/matrix/messages/message-video.d.ts.map +1 -1
  32. package/dist/esm/src/matrix/messages/message-video.js +8 -6
  33. package/dist/esm/src/matrix/service/request.d.ts +4 -1
  34. package/dist/esm/src/matrix/service/request.d.ts.map +1 -1
  35. package/dist/esm/src/matrix/service/request.js +55 -0
  36. package/dist/esm/src/matrix/utils/index.d.ts +1 -0
  37. package/dist/esm/src/matrix/utils/index.d.ts.map +1 -1
  38. package/dist/esm/src/matrix/utils/index.js +24 -0
  39. package/dist/esm/src/puppet-matrix.d.ts.map +1 -1
  40. package/dist/esm/src/puppet-matrix.js +20 -7
  41. package/package.json +1 -1
  42. package/src/matrix/messages/message-appmsg.ts +7 -1
  43. package/src/matrix/messages/message-emotion.ts +10 -1
  44. package/src/matrix/messages/message-image.ts +7 -1
  45. package/src/matrix/messages/message-video.ts +18 -6
  46. package/src/matrix/service/request.ts +63 -1
  47. package/src/matrix/utils/index.ts +46 -0
  48. package/src/puppet-matrix.ts +20 -7
@@ -94,3 +94,49 @@ export function generateCustomRandomString (prefix: string, length: number) {
94
94
 
95
95
  return prefix + remainingPart
96
96
  }
97
+
98
+ function isPlainObject (value: unknown): value is Record<string, unknown> {
99
+ return Object.prototype.toString.call(value) === '[object Object]'
100
+ }
101
+
102
+ /**
103
+ * 合并两个对象:
104
+ * - 如果新对象的属性为 null、undefined、'',则用旧对象的值
105
+ * - 如果是嵌套对象,递归合并
106
+ * - 其他情况用新对象的值
107
+ */
108
+ export function mergeWithFallback<T extends Record<string, any>> (
109
+ oldObj: T,
110
+ newObj: Partial<T>,
111
+ ): T {
112
+ const result = Array.isArray(oldObj) ? ([ ...oldObj ] as any) : { ...oldObj }
113
+
114
+ for (const key in newObj) {
115
+ const newVal = newObj[key]
116
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
117
+ const oldVal = oldObj ? oldObj[key] : undefined
118
+
119
+ // 如果两个都是数组
120
+ if (Array.isArray(newVal) && Array.isArray(oldVal)) {
121
+ result[key] = newVal as any
122
+ continue
123
+ }
124
+
125
+ // 如果是普通对象,递归
126
+ if (isPlainObject(newVal) && isPlainObject(oldVal)) {
127
+ result[key] = mergeWithFallback(oldVal, newVal as any)
128
+ // eslint-disable-next-line brace-style
129
+ }
130
+ // 如果新值为空,则用旧值
131
+ else if (newVal === null || newVal === undefined || newVal === '' || newVal === 0) {
132
+ result[key] = oldVal
133
+ // eslint-disable-next-line brace-style
134
+ }
135
+ // 其他情况直接用新值
136
+ else {
137
+ result[key] = newVal as any
138
+ }
139
+ }
140
+
141
+ return result as T
142
+ }
@@ -10,7 +10,7 @@ import type { FileBoxMetadataMessage } from './matrix/types.js'
10
10
  import Client from './matrix/service/request.js'
11
11
  import { getUnixTime } from 'date-fns'
12
12
 
13
- import { delay } from './matrix/utils/index.js'
13
+ import { delay, mergeWithFallback } from './matrix/utils/index.js'
14
14
  import path from 'path'
15
15
  // 参考
16
16
  import { CacheManager, RoomMemberMap } from './matrix/cache-manager.js'
@@ -415,6 +415,7 @@ class PuppetMatrix extends PUPPET.Puppet {
415
415
  public async ready (): Promise<void> {
416
416
  try {
417
417
  await this._client?.getContactList(1, 100)
418
+ await this._client?.getOpenImContactList(1, 100)
418
419
  await delay(2000)
419
420
  await this._client?.getGroupList(1, 100)
420
421
  await delay(2000)
@@ -795,7 +796,12 @@ class PuppetMatrix extends PUPPET.Puppet {
795
796
  return this._getMessageFileFileBox(messageId, messagePayload)
796
797
  case PUPPET.types.Message.Emoticon: {
797
798
  const emotionPayload = await parseEmotionMessagePayload(messagePayload)
798
- const emoticonBox = FileBox.fromUrl(emotionPayload.cdnurl, { name: `message-${messageId}-emoticon.jpg` })
799
+ const emoticonBox = emotionPayload.is_ww ? await this._client?.downloadWWMedia({
800
+ aes_key: emotionPayload.aeskey || '',
801
+ auth_key: emotionPayload.authkey || '',
802
+ file_id: emotionPayload.cdnurl || '',
803
+ file_name: `message-${messageId}-emoticon.jpg`,
804
+ }, 3) : FileBox.fromUrl(emotionPayload.cdnurl, { name: `message-${messageId}-emoticon.jpg` })
799
805
 
800
806
  emoticonBox.metadata = {
801
807
  payload: emotionPayload,
@@ -1523,7 +1529,7 @@ class PuppetMatrix extends PUPPET.Puppet {
1523
1529
  const roomId = contact.wxid
1524
1530
  let finalPayload: ContactPayload = contact
1525
1531
  if (oldRoomPayload) {
1526
- finalPayload = { ...oldRoomPayload, ...contact }
1532
+ finalPayload = mergeWithFallback(oldRoomPayload, contact)
1527
1533
  }
1528
1534
  await this._cacheMgr!.setRoom(roomId, finalPayload)
1529
1535
 
@@ -1535,7 +1541,7 @@ class PuppetMatrix extends PUPPET.Puppet {
1535
1541
  } else {
1536
1542
  const oldContactPaylod = await this._cacheMgr!.getContact(contact.wxid)
1537
1543
  if (oldContactPaylod) {
1538
- await this._cacheMgr!.setContact(contact.wxid, { ...oldContactPaylod, ...contact })
1544
+ await this._cacheMgr!.setContact(contact.wxid, mergeWithFallback(oldContactPaylod, contact))
1539
1545
  } else {
1540
1546
  await this._cacheMgr!.setContact(contact.wxid, contact)
1541
1547
  }
@@ -1707,7 +1713,7 @@ class PuppetMatrix extends PUPPET.Puppet {
1707
1713
  if (!imageInfo.file_id) {
1708
1714
  log.error(`Can not get file for message: ${messageId}`)
1709
1715
  }
1710
- const url = await this._client?.downloadImage(imageInfo)
1716
+ const url = imageInfo.is_ww ? await this._client?.downloadWWMedia(imageInfo, 1) : await this._client?.downloadImage(imageInfo)
1711
1717
  return FileBox.fromUrl(url)
1712
1718
  }
1713
1719
 
@@ -1725,7 +1731,7 @@ class PuppetMatrix extends PUPPET.Puppet {
1725
1731
  if (!videoInfo.file_id) {
1726
1732
  log.error(`Can not get file for message: ${messageId}`)
1727
1733
  }
1728
- const url = await this._client?.downloadVideo(videoInfo)
1734
+ const url = videoInfo.is_ww ? await this._client?.downloadWWMedia(videoInfo, 2) : await this._client?.downloadVideo(videoInfo)
1729
1735
  log.info(PRE, '_getMessageVideoFileBox(): %s', url)
1730
1736
  return FileBox.fromUrl(url)
1731
1737
  }
@@ -1743,13 +1749,14 @@ class PuppetMatrix extends PUPPET.Puppet {
1743
1749
  }
1744
1750
  const appattach = appPayload.appattach
1745
1751
  const params = {
1752
+ auth_key: appattach?.authkey || '',
1746
1753
  aes_key: appattach?.aeskey || '',
1747
1754
  file_id: appattach?.attachid || '',
1748
1755
  file_name: appPayload.title,
1749
1756
  file_size: appattach?.totallen || '',
1750
1757
  }
1751
1758
 
1752
- const url = await this._client?.downloadFile(params)
1759
+ const url = appattach?.is_ww ? await this._client?.downloadWWMedia(params, 3) : await this._client?.downloadFile(params)
1753
1760
 
1754
1761
  if (!url) {
1755
1762
  log.error(`Can not get file url: ${JSON.stringify(params)}`)
@@ -1765,6 +1772,12 @@ class PuppetMatrix extends PUPPET.Puppet {
1765
1772
  }
1766
1773
  log.info('syncContact')
1767
1774
  await this._client!.syncContact()
1775
+ await this._client?.getContactList(1, 100)
1776
+ await this._client?.getOpenImContactList(1, 100)
1777
+ await delay(300)
1778
+ await this._client?.getGroupList(1, 100)
1779
+ await delay(300)
1780
+ await this._client?.getOfficeList(1, 100)
1768
1781
  }
1769
1782
 
1770
1783
  }