@juzi/wechaty 1.0.21 → 1.0.22

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 (72) hide show
  1. package/dist/cjs/src/package-json.js +3 -3
  2. package/dist/cjs/src/package-json.js.map +1 -1
  3. package/dist/cjs/src/pure-functions/update.d.ts +1 -1
  4. package/dist/cjs/src/pure-functions/update.d.ts.map +1 -1
  5. package/dist/cjs/src/pure-functions/update.js +6 -5
  6. package/dist/cjs/src/pure-functions/update.js.map +1 -1
  7. package/dist/cjs/src/schemas/contact-events.d.ts +14 -2
  8. package/dist/cjs/src/schemas/contact-events.d.ts.map +1 -1
  9. package/dist/cjs/src/schemas/contact-events.js.map +1 -1
  10. package/dist/cjs/src/schemas/room-events.d.ts +4 -1
  11. package/dist/cjs/src/schemas/room-events.d.ts.map +1 -1
  12. package/dist/cjs/src/schemas/room-events.js +1 -0
  13. package/dist/cjs/src/schemas/room-events.js.map +1 -1
  14. package/dist/cjs/src/schemas/update.d.ts +30 -11
  15. package/dist/cjs/src/schemas/update.d.ts.map +1 -1
  16. package/dist/cjs/src/schemas/update.js +32 -0
  17. package/dist/cjs/src/schemas/update.js.map +1 -1
  18. package/dist/cjs/src/schemas/wechaty-events.d.ts +23 -2
  19. package/dist/cjs/src/schemas/wechaty-events.d.ts.map +1 -1
  20. package/dist/cjs/src/schemas/wechaty-events.js +7 -0
  21. package/dist/cjs/src/schemas/wechaty-events.js.map +1 -1
  22. package/dist/cjs/src/wechaty/wechaty-base.d.ts +7 -7
  23. package/dist/cjs/src/wechaty-mixins/gerror-mixin.d.ts +1 -1
  24. package/dist/cjs/src/wechaty-mixins/io-mixin.d.ts +2 -2
  25. package/dist/cjs/src/wechaty-mixins/login-mixin.d.ts +5 -5
  26. package/dist/cjs/src/wechaty-mixins/misc-mixin.d.ts +5 -5
  27. package/dist/cjs/src/wechaty-mixins/plugin-mixin.d.ts +6 -6
  28. package/dist/cjs/src/wechaty-mixins/puppet-mixin.d.ts +4 -4
  29. package/dist/cjs/src/wechaty-mixins/puppet-mixin.d.ts.map +1 -1
  30. package/dist/cjs/src/wechaty-mixins/puppet-mixin.js +73 -14
  31. package/dist/cjs/src/wechaty-mixins/puppet-mixin.js.map +1 -1
  32. package/dist/cjs/src/wechaty-mixins/wechatify-user-module-mixin.d.ts +1 -1
  33. package/dist/esm/src/package-json.js +3 -3
  34. package/dist/esm/src/package-json.js.map +1 -1
  35. package/dist/esm/src/pure-functions/update.d.ts +1 -1
  36. package/dist/esm/src/pure-functions/update.d.ts.map +1 -1
  37. package/dist/esm/src/pure-functions/update.js +6 -5
  38. package/dist/esm/src/pure-functions/update.js.map +1 -1
  39. package/dist/esm/src/schemas/contact-events.d.ts +14 -2
  40. package/dist/esm/src/schemas/contact-events.d.ts.map +1 -1
  41. package/dist/esm/src/schemas/contact-events.js.map +1 -1
  42. package/dist/esm/src/schemas/room-events.d.ts +4 -1
  43. package/dist/esm/src/schemas/room-events.d.ts.map +1 -1
  44. package/dist/esm/src/schemas/room-events.js +1 -0
  45. package/dist/esm/src/schemas/room-events.js.map +1 -1
  46. package/dist/esm/src/schemas/update.d.ts +30 -11
  47. package/dist/esm/src/schemas/update.d.ts.map +1 -1
  48. package/dist/esm/src/schemas/update.js +8 -1
  49. package/dist/esm/src/schemas/update.js.map +1 -1
  50. package/dist/esm/src/schemas/wechaty-events.d.ts +23 -2
  51. package/dist/esm/src/schemas/wechaty-events.d.ts.map +1 -1
  52. package/dist/esm/src/schemas/wechaty-events.js +7 -0
  53. package/dist/esm/src/schemas/wechaty-events.js.map +1 -1
  54. package/dist/esm/src/wechaty/wechaty-base.d.ts +7 -7
  55. package/dist/esm/src/wechaty-mixins/gerror-mixin.d.ts +1 -1
  56. package/dist/esm/src/wechaty-mixins/io-mixin.d.ts +2 -2
  57. package/dist/esm/src/wechaty-mixins/login-mixin.d.ts +5 -5
  58. package/dist/esm/src/wechaty-mixins/misc-mixin.d.ts +5 -5
  59. package/dist/esm/src/wechaty-mixins/plugin-mixin.d.ts +6 -6
  60. package/dist/esm/src/wechaty-mixins/puppet-mixin.d.ts +4 -4
  61. package/dist/esm/src/wechaty-mixins/puppet-mixin.d.ts.map +1 -1
  62. package/dist/esm/src/wechaty-mixins/puppet-mixin.js +73 -14
  63. package/dist/esm/src/wechaty-mixins/puppet-mixin.js.map +1 -1
  64. package/dist/esm/src/wechaty-mixins/wechatify-user-module-mixin.d.ts +1 -1
  65. package/package.json +2 -2
  66. package/src/package-json.ts +3 -3
  67. package/src/pure-functions/update.ts +9 -8
  68. package/src/schemas/contact-events.ts +22 -3
  69. package/src/schemas/room-events.ts +4 -0
  70. package/src/schemas/update.ts +42 -13
  71. package/src/schemas/wechaty-events.ts +73 -43
  72. package/src/wechaty-mixins/puppet-mixin.ts +75 -15
@@ -11,7 +11,7 @@ import type {
11
11
  StateSwitchInterface,
12
12
  } from 'state-switch'
13
13
 
14
- import { config } from '../config.js'
14
+ import { config, FOUR_PER_EM_SPACE } from '../config.js'
15
15
  import { timestampToDate } from '../pure-functions/timestamp-to-date.js'
16
16
  import type {
17
17
  ContactImpl,
@@ -25,7 +25,7 @@ import type {
25
25
 
26
26
  import type { GErrorMixin } from './gerror-mixin.js'
27
27
  import type { IoMixin } from './io-mixin.js'
28
- import type { InfoUpdateInterface } from '../schemas/update.js'
28
+ import { ContactImportantFields, ContactUpdatableValuePair, InfoUpdateInterface, RoomImportantFields, RoomUpdatableValuePair } from '../schemas/update.js'
29
29
  import { diffPayload } from '../pure-functions/update.js'
30
30
 
31
31
  const PUPPET_MEMORY_NAME = 'puppet'
@@ -280,7 +280,7 @@ const puppetMixin = <MixinBase extends WechatifyUserModuleMixin & GErrorMixin &
280
280
  return
281
281
  }
282
282
 
283
- this.emit('message', msg)
283
+ this.emit('message', 'msg')
284
284
 
285
285
  const room = msg.room()
286
286
  const listener = msg.listener()
@@ -443,15 +443,58 @@ const puppetMixin = <MixinBase extends WechatifyUserModuleMixin & GErrorMixin &
443
443
  const oldPayload = JSON.parse(JSON.stringify(contact?.payload || {}))
444
444
  await contact?.ready(true)
445
445
  const newPayload = JSON.parse(JSON.stringify(contact?.payload || {}))
446
- const updateEvent: InfoUpdateInterface = {
447
- type: payloadType,
448
- id: payloadId,
449
- updates: diffPayload(oldPayload, newPayload),
450
- }
451
- if (updateEvent.updates.length > 0) {
446
+
447
+ const differences = diffPayload<PUPPET.payloads.Contact>(oldPayload, newPayload)
448
+ const importangDifferences = differences.filter(ele => ele && ContactImportantFields.some(key => key === ele.key))
449
+ const regularDifferences = differences.filter(ele => ele && !ContactImportantFields.some(key => key === ele.key)) as ContactUpdatableValuePair[]
450
+ if (regularDifferences.length > 0) {
451
+ const updateEvent: InfoUpdateInterface = {
452
+ type: payloadType,
453
+ id: payloadId,
454
+ updates: regularDifferences,
455
+ }
452
456
  this.emit('update', updateEvent)
453
457
  contact?.emit('update', updateEvent)
454
458
  }
459
+ for (const difference of importangDifferences) {
460
+ switch (difference?.key) {
461
+ case 'tags': {
462
+ const oldTagsSet = new Set(difference.oldValue?.map(ele => ele.groupId + FOUR_PER_EM_SPACE + ele.id))
463
+ const newTagsSet = new Set(difference.newValue?.map(ele => ele.groupId + FOUR_PER_EM_SPACE + ele.id))
464
+ const addedTags = difference.newValue?.filter(ele => !oldTagsSet.has(ele.groupId + FOUR_PER_EM_SPACE + ele.id)).map(ele => this.Tag.load(ele)) || []
465
+ const removedTags = difference.oldValue?.filter(ele => !newTagsSet.has(ele.groupId + FOUR_PER_EM_SPACE + ele.id)).map(ele => this.Tag.load(ele)) || []
466
+ if (addedTags.length > 0) {
467
+ this.emit('contact-tag-add', contact, addedTags)
468
+ }
469
+ if (removedTags.length > 0) {
470
+ this.emit('contact-tag-removed', contact, removedTags)
471
+ }
472
+ break
473
+ }
474
+ case 'name': {
475
+ this.emit('contact-name', contact, difference.newValue || '', difference.oldValue || '')
476
+ contact?.emit('name', difference.newValue || '', difference.oldValue || '')
477
+ break
478
+ }
479
+ case 'alias': {
480
+ this.emit('contact-alias', contact, difference.newValue || '', difference.oldValue || '')
481
+ contact?.emit('alias', difference.newValue || '', difference.oldValue || '')
482
+ break
483
+ }
484
+ case 'phone': {
485
+ this.emit('contact-phone', contact, difference.newValue || [], difference.oldValue || [])
486
+ contact?.emit('phone', difference.newValue || [], difference.oldValue || [])
487
+ break
488
+ }
489
+ case 'description': {
490
+ this.emit('contact-description', contact, difference.newValue || '', difference.oldValue || '')
491
+ contact?.emit('description', difference.newValue || '', difference.oldValue || '')
492
+ break
493
+ }
494
+ default:
495
+ log.warn('WechatyPuppetMixin', 'puppet dirty unsupported difference type: %s', JSON.stringify(difference))
496
+ }
497
+ }
455
498
  break
456
499
  }
457
500
  case PUPPET.types.Payload.Room: {
@@ -459,15 +502,32 @@ const puppetMixin = <MixinBase extends WechatifyUserModuleMixin & GErrorMixin &
459
502
  const oldPayload = JSON.parse(JSON.stringify(room?.payload || {}))
460
503
  await room?.ready(true)
461
504
  const newPayload = JSON.parse(JSON.stringify(room?.payload || {}))
462
- const updateEvent: InfoUpdateInterface = {
463
- type: payloadType,
464
- id: payloadId,
465
- updates: diffPayload(oldPayload, newPayload),
466
- }
467
- if (updateEvent.updates.length > 0) {
505
+
506
+ const differences = diffPayload<PUPPET.payloads.Room>(oldPayload, newPayload)
507
+ const importangDifferences = differences.filter(ele => ele && RoomImportantFields.some(key => key === ele.key))
508
+ const regularDifferences = differences.filter(ele => ele && !RoomImportantFields.some(key => key === ele.key)) as RoomUpdatableValuePair[]
509
+ if (regularDifferences.length > 0) {
510
+ const updateEvent: InfoUpdateInterface = {
511
+ type: payloadType,
512
+ id: payloadId,
513
+ updates: regularDifferences,
514
+ }
468
515
  this.emit('update', updateEvent)
469
516
  room?.emit('update', updateEvent)
470
517
  }
518
+ for (const difference of importangDifferences) {
519
+ switch (difference?.key) {
520
+ case 'ownerId': {
521
+ const oldOwner = (await this.Contact.find({ id: difference.oldValue }))!
522
+ const newOwner = (await this.Contact.find({ id: difference.newValue }))!
523
+ this.emit('room-owner', room, newOwner, oldOwner)
524
+ room?.emit('owner', newOwner, oldOwner)
525
+ break
526
+ }
527
+ default:
528
+ log.warn('WechatyPuppetMixin', 'puppet dirty unsupported difference type: %s', JSON.stringify(difference))
529
+ }
530
+ }
471
531
  break
472
532
  }
473
533
  case PUPPET.types.Payload.RoomMember: {