@juzi/wechaty 1.0.21 → 1.0.24

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 (106) hide show
  1. package/dist/cjs/src/config.d.ts +2 -0
  2. package/dist/cjs/src/config.d.ts.map +1 -1
  3. package/dist/cjs/src/config.js +3 -1
  4. package/dist/cjs/src/config.js.map +1 -1
  5. package/dist/cjs/src/package-json.js +4 -4
  6. package/dist/cjs/src/package-json.js.map +1 -1
  7. package/dist/cjs/src/pure-functions/update.d.ts +2 -1
  8. package/dist/cjs/src/pure-functions/update.d.ts.map +1 -1
  9. package/dist/cjs/src/pure-functions/update.js +18 -6
  10. package/dist/cjs/src/pure-functions/update.js.map +1 -1
  11. package/dist/cjs/src/schemas/contact-events.d.ts +14 -2
  12. package/dist/cjs/src/schemas/contact-events.d.ts.map +1 -1
  13. package/dist/cjs/src/schemas/contact-events.js.map +1 -1
  14. package/dist/cjs/src/schemas/room-events.d.ts +4 -1
  15. package/dist/cjs/src/schemas/room-events.d.ts.map +1 -1
  16. package/dist/cjs/src/schemas/room-events.js +1 -0
  17. package/dist/cjs/src/schemas/room-events.js.map +1 -1
  18. package/dist/cjs/src/schemas/update.d.ts +30 -11
  19. package/dist/cjs/src/schemas/update.d.ts.map +1 -1
  20. package/dist/cjs/src/schemas/update.js +32 -0
  21. package/dist/cjs/src/schemas/update.js.map +1 -1
  22. package/dist/cjs/src/schemas/wechaty-events.d.ts +26 -2
  23. package/dist/cjs/src/schemas/wechaty-events.d.ts.map +1 -1
  24. package/dist/cjs/src/schemas/wechaty-events.js +7 -0
  25. package/dist/cjs/src/schemas/wechaty-events.js.map +1 -1
  26. package/dist/cjs/src/user-modules/contact.d.ts.map +1 -1
  27. package/dist/cjs/src/user-modules/contact.js +7 -10
  28. package/dist/cjs/src/user-modules/contact.js.map +1 -1
  29. package/dist/cjs/src/user-modules/tag-group.d.ts +54 -20
  30. package/dist/cjs/src/user-modules/tag-group.d.ts.map +1 -1
  31. package/dist/cjs/src/user-modules/tag-group.js +103 -44
  32. package/dist/cjs/src/user-modules/tag-group.js.map +1 -1
  33. package/dist/cjs/src/user-modules/tag.d.ts +57 -22
  34. package/dist/cjs/src/user-modules/tag.d.ts.map +1 -1
  35. package/dist/cjs/src/user-modules/tag.js +140 -48
  36. package/dist/cjs/src/user-modules/tag.js.map +1 -1
  37. package/dist/cjs/src/wechaty/wechaty-base.d.ts +7 -7
  38. package/dist/cjs/src/wechaty-mixins/gerror-mixin.d.ts +1 -1
  39. package/dist/cjs/src/wechaty-mixins/io-mixin.d.ts +2 -2
  40. package/dist/cjs/src/wechaty-mixins/login-mixin.d.ts +5 -5
  41. package/dist/cjs/src/wechaty-mixins/misc-mixin.d.ts +5 -5
  42. package/dist/cjs/src/wechaty-mixins/plugin-mixin.d.ts +6 -6
  43. package/dist/cjs/src/wechaty-mixins/puppet-mixin.d.ts +4 -4
  44. package/dist/cjs/src/wechaty-mixins/puppet-mixin.d.ts.map +1 -1
  45. package/dist/cjs/src/wechaty-mixins/puppet-mixin.js +152 -13
  46. package/dist/cjs/src/wechaty-mixins/puppet-mixin.js.map +1 -1
  47. package/dist/cjs/src/wechaty-mixins/wechatify-user-module-mixin.d.ts +1 -1
  48. package/dist/esm/src/config.d.ts +2 -0
  49. package/dist/esm/src/config.d.ts.map +1 -1
  50. package/dist/esm/src/config.js +2 -0
  51. package/dist/esm/src/config.js.map +1 -1
  52. package/dist/esm/src/package-json.js +4 -4
  53. package/dist/esm/src/package-json.js.map +1 -1
  54. package/dist/esm/src/pure-functions/update.d.ts +2 -1
  55. package/dist/esm/src/pure-functions/update.d.ts.map +1 -1
  56. package/dist/esm/src/pure-functions/update.js +16 -5
  57. package/dist/esm/src/pure-functions/update.js.map +1 -1
  58. package/dist/esm/src/schemas/contact-events.d.ts +14 -2
  59. package/dist/esm/src/schemas/contact-events.d.ts.map +1 -1
  60. package/dist/esm/src/schemas/contact-events.js.map +1 -1
  61. package/dist/esm/src/schemas/room-events.d.ts +4 -1
  62. package/dist/esm/src/schemas/room-events.d.ts.map +1 -1
  63. package/dist/esm/src/schemas/room-events.js +1 -0
  64. package/dist/esm/src/schemas/room-events.js.map +1 -1
  65. package/dist/esm/src/schemas/update.d.ts +30 -11
  66. package/dist/esm/src/schemas/update.d.ts.map +1 -1
  67. package/dist/esm/src/schemas/update.js +8 -1
  68. package/dist/esm/src/schemas/update.js.map +1 -1
  69. package/dist/esm/src/schemas/wechaty-events.d.ts +26 -2
  70. package/dist/esm/src/schemas/wechaty-events.d.ts.map +1 -1
  71. package/dist/esm/src/schemas/wechaty-events.js +7 -0
  72. package/dist/esm/src/schemas/wechaty-events.js.map +1 -1
  73. package/dist/esm/src/user-modules/contact.d.ts.map +1 -1
  74. package/dist/esm/src/user-modules/contact.js +7 -10
  75. package/dist/esm/src/user-modules/contact.js.map +1 -1
  76. package/dist/esm/src/user-modules/tag-group.d.ts +54 -20
  77. package/dist/esm/src/user-modules/tag-group.d.ts.map +1 -1
  78. package/dist/esm/src/user-modules/tag-group.js +104 -45
  79. package/dist/esm/src/user-modules/tag-group.js.map +1 -1
  80. package/dist/esm/src/user-modules/tag.d.ts +57 -22
  81. package/dist/esm/src/user-modules/tag.d.ts.map +1 -1
  82. package/dist/esm/src/user-modules/tag.js +119 -50
  83. package/dist/esm/src/user-modules/tag.js.map +1 -1
  84. package/dist/esm/src/wechaty/wechaty-base.d.ts +7 -7
  85. package/dist/esm/src/wechaty-mixins/gerror-mixin.d.ts +1 -1
  86. package/dist/esm/src/wechaty-mixins/io-mixin.d.ts +2 -2
  87. package/dist/esm/src/wechaty-mixins/login-mixin.d.ts +5 -5
  88. package/dist/esm/src/wechaty-mixins/misc-mixin.d.ts +5 -5
  89. package/dist/esm/src/wechaty-mixins/plugin-mixin.d.ts +6 -6
  90. package/dist/esm/src/wechaty-mixins/puppet-mixin.d.ts +4 -4
  91. package/dist/esm/src/wechaty-mixins/puppet-mixin.d.ts.map +1 -1
  92. package/dist/esm/src/wechaty-mixins/puppet-mixin.js +153 -14
  93. package/dist/esm/src/wechaty-mixins/puppet-mixin.js.map +1 -1
  94. package/dist/esm/src/wechaty-mixins/wechatify-user-module-mixin.d.ts +1 -1
  95. package/package.json +3 -3
  96. package/src/config.ts +3 -0
  97. package/src/package-json.ts +4 -4
  98. package/src/pure-functions/update.ts +20 -8
  99. package/src/schemas/contact-events.ts +22 -3
  100. package/src/schemas/room-events.ts +4 -0
  101. package/src/schemas/update.ts +42 -13
  102. package/src/schemas/wechaty-events.ts +77 -43
  103. package/src/user-modules/contact.ts +7 -10
  104. package/src/user-modules/tag-group.ts +135 -53
  105. package/src/user-modules/tag.ts +124 -57
  106. package/src/wechaty-mixins/puppet-mixin.ts +171 -15
@@ -18,60 +18,69 @@
18
18
  *
19
19
  */
20
20
  import type * as PUPPET from '@juzi/wechaty-puppet'
21
+ import type { TagIdentifier } from '@juzi/wechaty-puppet/filters'
21
22
 
22
23
  import type { Constructor } from 'clone-class'
24
+ import { concurrencyExecuter } from 'rx-queue'
23
25
  import { log } from '../config.js'
26
+ import { poolifyMixin } from '../user-mixins/poolify.js'
24
27
 
25
28
  import { validationMixin } from '../user-mixins/validation.js'
26
29
  import {
27
- wechatifyMixinBase,
30
+ wechatifyMixin,
28
31
  } from '../user-mixins/wechatify.js'
29
32
  import type { TagInterface } from './tag.js'
30
33
 
31
- class TagGroupMixin extends wechatifyMixinBase() {
34
+ const MixinBase = wechatifyMixin(
35
+ poolifyMixin(
36
+ Object,
37
+ )<TagGroupImplInterface>(),
38
+ )
39
+
40
+ class TagGroupMixin extends MixinBase {
32
41
 
33
42
  /**
34
43
  *
35
- * Create
44
+ * Instance properties
45
+ * @ignore
36
46
  *
37
47
  */
38
- static create (payload: PUPPET.payloads.TagGroup): TagGroupInterface {
39
- log.verbose('TagGroup', 'create()')
40
-
41
- return new this(payload)
42
- }
48
+ payload?: PUPPET.payloads.TagGroup
43
49
 
44
50
  /**
45
51
  * @hideconstructor
46
52
  */
47
53
  constructor (
48
- public readonly payload: PUPPET.payloads.TagGroup,
54
+ public readonly id: string,
49
55
  ) {
50
56
  super()
51
57
  log.silly('TagGroup', 'constructor()')
52
58
  }
53
59
 
54
- id (): string {
55
- return this.payload.id
56
- }
57
-
58
60
  name (): string {
59
- return this.payload.name
61
+ return (this.payload && this.payload.name) || ''
60
62
  }
61
63
 
62
- private static pool: TagGroupInterface[] = []
64
+ static async list (): Promise<TagGroupInterface[]> {
65
+ log.verbose('TagGroup', 'list()')
63
66
 
64
- static async list (forceSync = false): Promise<TagGroupInterface[]> {
65
- log.verbose('TagGroup', 'list(%s)', forceSync)
67
+ try {
68
+ const tagGroupIds = await this.wechaty.puppet.tagGroupList()
66
69
 
67
- if (this.pool.length > 0 && !forceSync) {
68
- return this.pool
69
- }
70
+ const idToTagGroup = async (id: string) => this.find(id).catch(e => this.wechaty.emitError(e))
71
+
72
+ const CONCURRENCY = 17
73
+ const tagGroupIterator = concurrencyExecuter(CONCURRENCY)(idToTagGroup)(tagGroupIds)
74
+
75
+ const tagGroupList: TagGroupInterface[] = []
76
+ for await (const tagGroup of tagGroupIterator) {
77
+ if (tagGroup) {
78
+ tagGroupList.push(tagGroup)
79
+ }
80
+ }
81
+
82
+ return tagGroupList
70
83
 
71
- try {
72
- const payloads = await this.wechaty.puppet.tagGroupList()
73
- this.pool = payloads.map(payload => new this(payload))
74
- return this.pool
75
84
  } catch (e) {
76
85
  this.wechaty.emitError(e)
77
86
  log.error('TagGroup', 'list() exception: %s', (e as Error).message)
@@ -79,31 +88,13 @@ class TagGroupMixin extends wechatifyMixinBase() {
79
88
  }
80
89
  }
81
90
 
82
- static async sync (): Promise<void> {
83
- log.verbose('TagGroup', 'sync()')
84
-
85
- await this.list(true)
86
- }
87
-
88
- static load (tagGroupId: string): TagGroupInterface | undefined {
89
- log.verbose('TagGroup', 'load(%s)', tagGroupId)
90
-
91
- for (const item of this.pool) {
92
- if (item.id() === tagGroupId) {
93
- return item
94
- }
95
- }
96
- return undefined
97
- }
98
-
99
91
  static async createTagGroup (name: string): Promise<TagGroupInterface | void> {
100
92
  log.verbose('TagGroup', 'createTagGroup(%s, %s)', name)
101
93
 
102
94
  try {
103
- const payload = await this.wechaty.puppet.tagGroupAdd(name)
104
- if (payload) {
105
- const newTagGroup = new this(payload)
106
- this.pool.push(newTagGroup)
95
+ const groupId = await this.wechaty.puppet.tagGroupAdd(name)
96
+ if (groupId) {
97
+ const newTagGroup = await this.find(groupId)
107
98
  return newTagGroup
108
99
  }
109
100
  } catch (e) {
@@ -116,8 +107,7 @@ class TagGroupMixin extends wechatifyMixinBase() {
116
107
  log.verbose('TagGroup', 'deleteTagGroup(%s)', tagGroup)
117
108
 
118
109
  try {
119
- await this.wechaty.puppet.tagGroupDelete(tagGroup.id())
120
- this.pool.splice(this.pool.indexOf(tagGroup), 1)
110
+ await this.wechaty.puppet.tagGroupDelete(tagGroup.id)
121
111
  } catch (e) {
122
112
  this.wechaty.emitError(e)
123
113
  log.error('TagGroup', 'deleteTagGroup() exception: %s', (e as Error).message)
@@ -125,25 +115,117 @@ class TagGroupMixin extends wechatifyMixinBase() {
125
115
  }
126
116
 
127
117
  async tags (): Promise<TagInterface[]> {
128
- return (await this.wechaty.Tag.list()).filter(tag => tag.groupId() === this.id())
118
+ log.verbose('TagGroup', 'tags(%s)', this)
119
+ try {
120
+ const tagIdentifierList = await this.wechaty.puppet.tagGroupTagList(this.id)
121
+
122
+ const identifierToTag = async (tag: TagIdentifier) => this.wechaty.Tag.find(tag).catch(e => this.wechaty.emitError(e))
123
+
124
+ const CONCURRENCY = 17
125
+ const tagIterator = concurrencyExecuter(CONCURRENCY)(identifierToTag)(tagIdentifierList)
126
+
127
+ const tagList: TagInterface[] = []
128
+ for await (const tag of tagIterator) {
129
+ if (tag) {
130
+ tagList.push(tag)
131
+ }
132
+ }
133
+ return tagList
134
+
135
+ } catch (e) {
136
+ this.wechaty.emitError(e)
137
+ log.error('TagGroup', 'list() exception: %s', (e as Error).message)
138
+ return []
139
+ }
140
+
141
+ }
142
+
143
+ static async find (id: string): Promise<TagGroupInterface | undefined> {
144
+ log.silly('TagGroup', 'find(%s)', id)
145
+
146
+ const tagGroup = (this.wechaty.TagGroup as any as typeof TagGroupImpl).load(id)
147
+
148
+ try {
149
+ await tagGroup.ready()
150
+ } catch (e) {
151
+ this.wechaty.emitError(e)
152
+ return undefined
153
+ }
154
+ return tagGroup
155
+ }
156
+
157
+ /**
158
+ * Force reload data for TagGroup, Sync data from low-level API again.
159
+ *
160
+ * @returns {Promise<this>}
161
+ * @example
162
+ * await tagGroup.sync()
163
+ */
164
+ async sync (): Promise<void> {
165
+ await this.wechaty.puppet.tagGroupPayloadDirty(this.id)
166
+ await this.ready(true)
167
+ }
168
+
169
+ /**
170
+ * @ignore
171
+ */
172
+ isReady (): boolean {
173
+ return !!(this.payload)
174
+ }
175
+
176
+ /**
177
+ * `ready()` is For FrameWork ONLY!
178
+ *
179
+ * Please not to use `ready()` at the user land.
180
+ * If you want to sync data, use `sync()` instead.
181
+ *
182
+ * @ignore
183
+ */
184
+ async ready (
185
+ forceSync = false,
186
+ ): Promise<void> {
187
+ log.silly('TagGroup', 'ready() @ %s with TagGroup="%s"', this.wechaty.puppet, this.id)
188
+
189
+ if (!forceSync && this.isReady()) { // already ready
190
+ log.silly('TagGroup', 'ready() isReady() true')
191
+ return
192
+ }
193
+
194
+ try {
195
+ this.payload = await this.wechaty.puppet.tagGroupPayload(this.id)
196
+ } catch (e) {
197
+ this.wechaty.emitError(e)
198
+ log.verbose('TagGroup', 'ready() this.wechaty.puppet.tagGroupPayload(%s) exception: %s',
199
+ this.id,
200
+ (e as Error).message,
201
+ )
202
+ throw e
203
+ }
129
204
  }
130
205
 
131
206
  override toString () {
132
- return `<TagGroup#${this.name() || this.id()}>`
207
+ return `<TagGroup#${this.name() || this.id}>`
133
208
  }
134
209
 
135
210
  }
136
211
 
137
- class TagGroupImpl extends validationMixin(TagGroupMixin)<TagGroupInterface>() { }
138
- interface TagGroupInterface extends TagGroupImpl { }
212
+ class TagGroupImplBase extends validationMixin(TagGroupMixin)<TagGroupImplInterface>() { }
213
+ interface TagGroupImplInterface extends TagGroupImplBase { }
214
+
215
+ type TagGroupProtectedProperty =
216
+ | 'ready'
217
+
218
+ type TagGroupInterface = Omit<TagGroupImplInterface, TagGroupProtectedProperty>
219
+ class TagGroupImpl extends validationMixin(TagGroupImplBase)<TagGroupInterface>() { }
139
220
 
140
221
  type TagGroupConstructor = Constructor<
141
- TagGroupInterface,
142
- typeof TagGroupImpl
222
+ TagGroupImplInterface,
223
+ Omit<typeof TagGroupImpl, 'load'>
143
224
  >
144
225
 
145
226
  export type {
146
227
  TagGroupConstructor,
228
+ TagGroupProtectedProperty,
147
229
  TagGroupInterface,
148
230
  }
149
231
  export {
@@ -17,75 +17,84 @@
17
17
  * limitations under the License.
18
18
  *
19
19
  */
20
- import type * as PUPPET from '@juzi/wechaty-puppet'
20
+ import * as PUPPET from '@juzi/wechaty-puppet'
21
21
  import type { TagIdentifier } from '@juzi/wechaty-puppet/filters'
22
+ import { getTagKey } from '@juzi/wechaty-puppet/helpers'
22
23
 
23
24
  import type { Constructor } from 'clone-class'
24
- import { log } from '../config.js'
25
+ import { concurrencyExecuter } from 'rx-queue'
26
+ import { FOUR_PER_EM_SPACE, log } from '../config.js'
27
+ import { poolifyMixin } from '../user-mixins/poolify.js'
25
28
 
26
29
  import { validationMixin } from '../user-mixins/validation.js'
27
30
  import {
28
- wechatifyMixinBase,
31
+ wechatifyMixin,
29
32
  } from '../user-mixins/wechatify.js'
30
33
  import type { ContactInterface } from './contact.js'
31
34
  import type { TagGroupInterface } from './tag-group.js'
32
35
 
33
- class TagMixin extends wechatifyMixinBase() {
36
+ const MixinBase = wechatifyMixin(
37
+ poolifyMixin(
38
+ Object,
39
+ )<TagImplInterface>(),
40
+ )
41
+
42
+ class TagMixin extends MixinBase {
34
43
 
35
44
  /**
36
45
  *
37
- * Create
46
+ * Instance properties
47
+ * @ignore
38
48
  *
39
49
  */
40
- static create (payload: PUPPET.payloads.Tag): TagInterface {
41
- log.verbose('Tag', 'create()')
42
-
43
- return new this(payload)
44
- }
50
+ payload?: PUPPET.payloads.Tag
51
+ public readonly id: string
52
+ public readonly groupId?: string
45
53
 
46
54
  /**
47
55
  * @hideconstructor
48
56
  */
49
57
  constructor (
50
- public readonly payload: PUPPET.payloads.Tag,
58
+ public readonly key: string,
51
59
  ) {
52
60
  super()
61
+ this.groupId = this.key.split(FOUR_PER_EM_SPACE)[0]
62
+ this.id = this.key.split(FOUR_PER_EM_SPACE)[1]!
53
63
  log.silly('Tag', 'constructor()')
54
64
  }
55
65
 
56
- id (): string {
57
- return this.payload.id
58
- }
59
-
60
66
  type (): PUPPET.types.Tag {
61
- return this.payload.type
62
- }
63
-
64
- groupId (): string {
65
- return this.payload.groupId || ''
67
+ return (this.payload && this.payload.type) || PUPPET.types.Tag.Personal
66
68
  }
67
69
 
68
70
  name (): string {
69
- return this.payload.name
71
+ return (this.payload && this.payload.name) || ''
70
72
  }
71
73
 
72
- group (): TagGroupInterface | undefined {
73
- return this.wechaty.TagGroup.load(this.groupId())
74
+ async group (): Promise<TagGroupInterface | undefined> {
75
+ return this.groupId ? this.wechaty.TagGroup.find(this.groupId) : undefined
74
76
  }
75
77
 
76
- private static pool: TagInterface[] = []
77
-
78
78
  static async list (forceSync = false): Promise<TagInterface[]> {
79
79
  log.verbose('Tag', 'list(%s)', forceSync)
80
80
 
81
- if (this.pool.length > 0 && !forceSync) {
82
- return this.pool
83
- }
84
-
85
81
  try {
86
- const payloads = await this.wechaty.puppet.tagTagList()
87
- this.pool = payloads.map(payload => new this(payload))
88
- return this.pool
82
+ const tagIdentifierList = await this.wechaty.puppet.tagTagList()
83
+
84
+ const identifierToTag = async (identifier: TagIdentifier) => this.find(identifier).catch(e => this.wechaty.emitError(e))
85
+
86
+ const CONCURRENCY = 17
87
+ const tagIterator = concurrencyExecuter(CONCURRENCY)(identifierToTag)(tagIdentifierList)
88
+
89
+ const tagList: TagInterface[] = []
90
+ for await (const tag of tagIterator) {
91
+ if (tag) {
92
+ tagList.push(tag)
93
+ }
94
+ }
95
+
96
+ return tagList
97
+
89
98
  } catch (e) {
90
99
  this.wechaty.emitError(e)
91
100
  log.error('Tag', 'list() exception: %s', (e as Error).message)
@@ -93,27 +102,80 @@ class TagMixin extends wechatifyMixinBase() {
93
102
  }
94
103
  }
95
104
 
96
- static async sync (): Promise<void> {
97
- log.verbose('Tag', 'sync()')
105
+ static async find (identifier: TagIdentifier): Promise<TagInterface | undefined> {
106
+ log.silly('Tag', 'find(%s)', JSON.stringify(identifier))
107
+
108
+ const tag = (this.wechaty.Tag as any as typeof TagImpl).load(getTagKey(identifier))
98
109
 
99
- await this.list(true)
110
+ try {
111
+ await tag.ready()
112
+ } catch (e) {
113
+ this.wechaty.emitError(e)
114
+ return undefined
115
+ }
116
+ return tag
100
117
  }
101
118
 
102
- static load (tag: TagIdentifier): TagInterface | undefined {
103
- log.verbose('TagGroup', 'load(%s)', tag)
119
+ /**
120
+ * Force reload data for Tag, Sync data from low-level API again.
121
+ *
122
+ * @returns {Promise<this>}
123
+ * @example
124
+ * await tag.sync()
125
+ */
126
+ async sync (): Promise<void> {
127
+ await this.wechaty.puppet.tagPayloadDirty(getTagKey({
128
+ id: this.id,
129
+ groupId: this.groupId,
130
+ }))
131
+ await this.ready(true)
132
+ }
104
133
 
105
- for (const item of this.pool) {
106
- if (item.id() === tag.id && (item.groupId() === tag.groupId || (!item.groupId() && !tag.groupId))) {
107
- return item
108
- }
134
+ /**
135
+ * @ignore
136
+ */
137
+ isReady (): boolean {
138
+ return !!(this.payload && this.payload.name)
139
+ }
140
+
141
+ /**
142
+ * `ready()` is For FrameWork ONLY!
143
+ *
144
+ * Please not to use `ready()` at the user land.
145
+ * If you want to sync data, use `sync()` instead.
146
+ *
147
+ * @ignore
148
+ */
149
+ async ready (
150
+ forceSync = false,
151
+ ): Promise<void> {
152
+ log.silly('Tag', 'ready() @ %s with Tag key="%s"', this.wechaty.puppet, this.key)
153
+
154
+ if (!forceSync && this.isReady()) { // already ready
155
+ log.silly('Tag', 'ready() isReady() true')
156
+ return
157
+ }
158
+
159
+ try {
160
+ this.payload = await this.wechaty.puppet.tagPayload({
161
+ id: this.id,
162
+ groupId: this.groupId,
163
+ })
164
+
165
+ } catch (e) {
166
+ this.wechaty.emitError(e)
167
+ log.verbose('Tag', 'ready() this.wechaty.puppet.tagPayload(%s) exception: %s',
168
+ this.id,
169
+ (e as Error).message,
170
+ )
171
+ throw e
109
172
  }
110
- return undefined
111
173
  }
112
174
 
113
175
  async contactList (): Promise<ContactInterface[]> {
114
176
  log.verbose('Tag', 'contactList() for tag : %s', this)
115
177
 
116
- const tag = { id: this.id(), groupId: this.groupId() } as TagIdentifier
178
+ const tag = { id: this.id, groupId: this.groupId } as TagIdentifier
117
179
  const contactIds = await this.wechaty.puppet.tagTagContactList(tag)
118
180
  const contactPromises = contactIds.map(id => this.wechaty.Contact.find({ id })) as Promise<ContactInterface>[]
119
181
  return Promise.all(contactPromises)
@@ -122,7 +184,7 @@ class TagMixin extends wechatifyMixinBase() {
122
184
  async tag (contacts: ContactInterface | ContactInterface[]): Promise<void> {
123
185
  log.verbose('Tag', 'tag(%s) for tag : %s', contacts, this)
124
186
 
125
- const tag = { id: this.id(), groupId: this.groupId() } as TagIdentifier
187
+ const tag = { id: this.id, groupId: this.groupId } as TagIdentifier
126
188
  let contactIds: string[]
127
189
  if (Array.isArray(contacts)) {
128
190
  contactIds = contacts.map(c => c.id)
@@ -136,26 +198,24 @@ class TagMixin extends wechatifyMixinBase() {
136
198
  log.verbose('Tag', 'createTag(%s, %s)', tagGroup, name)
137
199
 
138
200
  try {
139
- const payload = await this.wechaty.puppet.tagTagAdd(name, tagGroup?.name())
140
- if (payload) {
141
- const newTag = new this(payload)
142
- this.pool.push(newTag)
201
+ const tagIdentifier = await this.wechaty.puppet.tagTagAdd(name, tagGroup?.name())
202
+ if (tagIdentifier) {
203
+ const newTag = await this.find(tagIdentifier)
143
204
  return newTag
144
205
  }
145
206
  } catch (e) {
146
207
  this.wechaty.emitError(e)
147
- log.error('Contact', 'createTag() exception: %s', (e as Error).message)
208
+ log.error('Tag', 'createTag() exception: %s', (e as Error).message)
148
209
  }
149
210
  }
150
211
 
151
212
  static async deleteTag (tagInstance: TagInterface): Promise<void> {
152
213
  log.verbose('Tag', 'deleteTag(%s, %s)', tagInstance)
153
214
 
154
- const tag = { id: tagInstance.id(), groupId: tagInstance.groupId() } as TagIdentifier
215
+ const tagIdentifier = { id: tagInstance.id, groupId: tagInstance.groupId } as TagIdentifier
155
216
 
156
217
  try {
157
- await this.wechaty.puppet.tagTagDelete(tag)
158
- this.pool.splice(this.pool.indexOf(tagInstance), 1)
218
+ await this.wechaty.puppet.tagTagDelete(tagIdentifier)
159
219
  } catch (e) {
160
220
  this.wechaty.emitError(e)
161
221
  log.error('Tag', 'deleteTag() exception: %s', (e as Error).message)
@@ -163,21 +223,28 @@ class TagMixin extends wechatifyMixinBase() {
163
223
  }
164
224
 
165
225
  override toString () {
166
- return `<Tag#${this.name() || this.id()}>`
226
+ return `<Tag#${this.name() || this.id}>`
167
227
  }
168
228
 
169
229
  }
170
230
 
171
- class TagImpl extends validationMixin(TagMixin)<TagInterface>() {}
172
- interface TagInterface extends TagImpl {}
231
+ class TagImplBase extends validationMixin(TagMixin)<TagImplInterface>() {}
232
+ interface TagImplInterface extends TagImplBase {}
233
+
234
+ type TagProtectedProperty =
235
+ | 'ready'
236
+
237
+ type TagInterface = Omit<TagImplInterface, TagProtectedProperty>
238
+ class TagImpl extends validationMixin(TagImplBase)<TagInterface>() {}
173
239
 
174
240
  type TagConstructor = Constructor<
175
- TagInterface,
176
- typeof TagImpl
241
+ TagImplInterface,
242
+ Omit<typeof TagImpl, 'load'>
177
243
  >
178
244
 
179
245
  export type {
180
246
  TagConstructor,
247
+ TagProtectedProperty,
181
248
  TagInterface,
182
249
  }
183
250
  export {