@juzi/wechaty 1.0.24 → 1.0.25
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.
- package/dist/cjs/src/package-json.js +4 -4
- package/dist/cjs/src/schemas/wechaty-events.d.ts +5 -2
- package/dist/cjs/src/schemas/wechaty-events.d.ts.map +1 -1
- package/dist/cjs/src/schemas/wechaty-events.js.map +1 -1
- package/dist/cjs/src/user-modules/contact.d.ts.map +1 -1
- package/dist/cjs/src/user-modules/contact.js +5 -15
- package/dist/cjs/src/user-modules/contact.js.map +1 -1
- package/dist/cjs/src/user-modules/tag-group.d.ts +2 -1
- package/dist/cjs/src/user-modules/tag-group.d.ts.map +1 -1
- package/dist/cjs/src/user-modules/tag-group.js +24 -14
- package/dist/cjs/src/user-modules/tag-group.js.map +1 -1
- package/dist/cjs/src/user-modules/tag.d.ts +6 -7
- package/dist/cjs/src/user-modules/tag.d.ts.map +1 -1
- package/dist/cjs/src/user-modules/tag.js +41 -42
- package/dist/cjs/src/user-modules/tag.js.map +1 -1
- package/dist/cjs/src/wechaty/wechaty-base.d.ts +7 -7
- package/dist/cjs/src/wechaty-mixins/gerror-mixin.d.ts +1 -1
- package/dist/cjs/src/wechaty-mixins/io-mixin.d.ts +2 -2
- package/dist/cjs/src/wechaty-mixins/login-mixin.d.ts +5 -5
- package/dist/cjs/src/wechaty-mixins/misc-mixin.d.ts +5 -5
- package/dist/cjs/src/wechaty-mixins/plugin-mixin.d.ts +6 -6
- package/dist/cjs/src/wechaty-mixins/puppet-mixin.d.ts +32 -6
- package/dist/cjs/src/wechaty-mixins/puppet-mixin.d.ts.map +1 -1
- package/dist/cjs/src/wechaty-mixins/puppet-mixin.js +31 -31
- package/dist/cjs/src/wechaty-mixins/puppet-mixin.js.map +1 -1
- package/dist/cjs/src/wechaty-mixins/wechatify-user-module-mixin.d.ts +1 -1
- package/dist/esm/src/package-json.js +4 -4
- package/dist/esm/src/schemas/wechaty-events.d.ts +5 -2
- package/dist/esm/src/schemas/wechaty-events.d.ts.map +1 -1
- package/dist/esm/src/schemas/wechaty-events.js.map +1 -1
- package/dist/esm/src/user-modules/contact.d.ts.map +1 -1
- package/dist/esm/src/user-modules/contact.js +5 -15
- package/dist/esm/src/user-modules/contact.js.map +1 -1
- package/dist/esm/src/user-modules/tag-group.d.ts +2 -1
- package/dist/esm/src/user-modules/tag-group.d.ts.map +1 -1
- package/dist/esm/src/user-modules/tag-group.js +24 -14
- package/dist/esm/src/user-modules/tag-group.js.map +1 -1
- package/dist/esm/src/user-modules/tag.d.ts +6 -7
- package/dist/esm/src/user-modules/tag.d.ts.map +1 -1
- package/dist/esm/src/user-modules/tag.js +42 -43
- package/dist/esm/src/user-modules/tag.js.map +1 -1
- package/dist/esm/src/wechaty/wechaty-base.d.ts +7 -7
- package/dist/esm/src/wechaty-mixins/gerror-mixin.d.ts +1 -1
- package/dist/esm/src/wechaty-mixins/io-mixin.d.ts +2 -2
- package/dist/esm/src/wechaty-mixins/login-mixin.d.ts +5 -5
- package/dist/esm/src/wechaty-mixins/misc-mixin.d.ts +5 -5
- package/dist/esm/src/wechaty-mixins/plugin-mixin.d.ts +6 -6
- package/dist/esm/src/wechaty-mixins/puppet-mixin.d.ts +32 -6
- package/dist/esm/src/wechaty-mixins/puppet-mixin.d.ts.map +1 -1
- package/dist/esm/src/wechaty-mixins/puppet-mixin.js +31 -31
- package/dist/esm/src/wechaty-mixins/puppet-mixin.js.map +1 -1
- package/dist/esm/src/wechaty-mixins/wechatify-user-module-mixin.d.ts +1 -1
- package/package.json +3 -3
- package/src/package-json.ts +4 -4
- package/src/schemas/wechaty-events.ts +4 -1
- package/src/user-modules/contact.ts +5 -15
- package/src/user-modules/tag-group.ts +27 -15
- package/src/user-modules/tag.ts +44 -43
- package/src/wechaty-mixins/puppet-mixin.ts +36 -40
package/src/user-modules/tag.ts
CHANGED
|
@@ -18,12 +18,11 @@
|
|
|
18
18
|
*
|
|
19
19
|
*/
|
|
20
20
|
import * as PUPPET from '@juzi/wechaty-puppet'
|
|
21
|
-
import type {
|
|
22
|
-
import { getTagKey } from '@juzi/wechaty-puppet/helpers'
|
|
21
|
+
import type { TagQueryFilter } from '@juzi/wechaty-puppet/dist/esm/src/schemas/tag.js'
|
|
23
22
|
|
|
24
23
|
import type { Constructor } from 'clone-class'
|
|
25
24
|
import { concurrencyExecuter } from 'rx-queue'
|
|
26
|
-
import {
|
|
25
|
+
import { log } from '../config.js'
|
|
27
26
|
import { poolifyMixin } from '../user-mixins/poolify.js'
|
|
28
27
|
|
|
29
28
|
import { validationMixin } from '../user-mixins/validation.js'
|
|
@@ -48,43 +47,43 @@ class TagMixin extends MixinBase {
|
|
|
48
47
|
*
|
|
49
48
|
*/
|
|
50
49
|
payload?: PUPPET.payloads.Tag
|
|
51
|
-
public readonly id: string
|
|
52
|
-
public readonly groupId?: string
|
|
53
50
|
|
|
54
51
|
/**
|
|
55
52
|
* @hideconstructor
|
|
56
53
|
*/
|
|
57
54
|
constructor (
|
|
58
|
-
public readonly
|
|
55
|
+
public readonly id: string,
|
|
59
56
|
) {
|
|
60
57
|
super()
|
|
61
|
-
this.groupId = this.key.split(FOUR_PER_EM_SPACE)[0]
|
|
62
|
-
this.id = this.key.split(FOUR_PER_EM_SPACE)[1]!
|
|
63
58
|
log.silly('Tag', 'constructor()')
|
|
64
59
|
}
|
|
65
60
|
|
|
66
61
|
type (): PUPPET.types.Tag {
|
|
67
|
-
return (this.payload && this.payload.type) || PUPPET.types.Tag.
|
|
62
|
+
return (this.payload && this.payload.type) || PUPPET.types.Tag.Unspecific
|
|
68
63
|
}
|
|
69
64
|
|
|
70
65
|
name (): string {
|
|
71
66
|
return (this.payload && this.payload.name) || ''
|
|
72
67
|
}
|
|
73
68
|
|
|
69
|
+
groupId (): string {
|
|
70
|
+
return (this.payload && this.payload.groupId) || ''
|
|
71
|
+
}
|
|
72
|
+
|
|
74
73
|
async group (): Promise<TagGroupInterface | undefined> {
|
|
75
|
-
return this.groupId ? this.wechaty.TagGroup.find(this.groupId) : undefined
|
|
74
|
+
return this.payload?.groupId ? this.wechaty.TagGroup.find({ id: this.payload.groupId }) : undefined
|
|
76
75
|
}
|
|
77
76
|
|
|
78
|
-
static async list (
|
|
79
|
-
log.verbose('Tag', 'list(
|
|
77
|
+
static async list (): Promise<TagInterface[]> {
|
|
78
|
+
log.verbose('Tag', 'list()')
|
|
80
79
|
|
|
81
80
|
try {
|
|
82
|
-
const
|
|
81
|
+
const tagIdList = await this.wechaty.puppet.tagTagList()
|
|
83
82
|
|
|
84
|
-
const
|
|
83
|
+
const idToTag = async (id: string) => this.find({ id }).catch(e => this.wechaty.emitError(e))
|
|
85
84
|
|
|
86
85
|
const CONCURRENCY = 17
|
|
87
|
-
const tagIterator = concurrencyExecuter(CONCURRENCY)(
|
|
86
|
+
const tagIterator = concurrencyExecuter(CONCURRENCY)(idToTag)(tagIdList)
|
|
88
87
|
|
|
89
88
|
const tagList: TagInterface[] = []
|
|
90
89
|
for await (const tag of tagIterator) {
|
|
@@ -102,18 +101,30 @@ class TagMixin extends MixinBase {
|
|
|
102
101
|
}
|
|
103
102
|
}
|
|
104
103
|
|
|
105
|
-
static async find (
|
|
106
|
-
log.silly('Tag', 'find(%s)', JSON.stringify(
|
|
104
|
+
static async find (filter: TagQueryFilter): Promise<TagInterface | undefined> {
|
|
105
|
+
log.silly('Tag', 'find(%s)', JSON.stringify(filter))
|
|
107
106
|
|
|
108
|
-
|
|
107
|
+
if (filter.id) {
|
|
108
|
+
const tag = (this.wechaty.Tag as any as typeof TagImpl).load(filter.id)
|
|
109
109
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
110
|
+
try {
|
|
111
|
+
await tag.ready()
|
|
112
|
+
} catch (e) {
|
|
113
|
+
this.wechaty.emitError(e)
|
|
114
|
+
return undefined
|
|
115
|
+
}
|
|
116
|
+
return tag
|
|
115
117
|
}
|
|
116
|
-
|
|
118
|
+
|
|
119
|
+
if (filter.name) {
|
|
120
|
+
const tags = (await this.wechaty.Tag.list()).filter(t => t.name() === filter.name)
|
|
121
|
+
if (tags.length > 0) {
|
|
122
|
+
return tags[0]
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return undefined
|
|
127
|
+
// TODO: use a puppet method to find tag, like how contact and room do it
|
|
117
128
|
}
|
|
118
129
|
|
|
119
130
|
/**
|
|
@@ -124,10 +135,7 @@ class TagMixin extends MixinBase {
|
|
|
124
135
|
* await tag.sync()
|
|
125
136
|
*/
|
|
126
137
|
async sync (): Promise<void> {
|
|
127
|
-
await this.wechaty.puppet.tagPayloadDirty(
|
|
128
|
-
id: this.id,
|
|
129
|
-
groupId: this.groupId,
|
|
130
|
-
}))
|
|
138
|
+
await this.wechaty.puppet.tagPayloadDirty(this.id)
|
|
131
139
|
await this.ready(true)
|
|
132
140
|
}
|
|
133
141
|
|
|
@@ -149,7 +157,7 @@ class TagMixin extends MixinBase {
|
|
|
149
157
|
async ready (
|
|
150
158
|
forceSync = false,
|
|
151
159
|
): Promise<void> {
|
|
152
|
-
log.silly('Tag', 'ready() @ %s with Tag key="%s"', this.wechaty.puppet, this.
|
|
160
|
+
log.silly('Tag', 'ready() @ %s with Tag key="%s"', this.wechaty.puppet, this.id)
|
|
153
161
|
|
|
154
162
|
if (!forceSync && this.isReady()) { // already ready
|
|
155
163
|
log.silly('Tag', 'ready() isReady() true')
|
|
@@ -157,10 +165,7 @@ class TagMixin extends MixinBase {
|
|
|
157
165
|
}
|
|
158
166
|
|
|
159
167
|
try {
|
|
160
|
-
this.payload = await this.wechaty.puppet.tagPayload(
|
|
161
|
-
id: this.id,
|
|
162
|
-
groupId: this.groupId,
|
|
163
|
-
})
|
|
168
|
+
this.payload = await this.wechaty.puppet.tagPayload(this.id)
|
|
164
169
|
|
|
165
170
|
} catch (e) {
|
|
166
171
|
this.wechaty.emitError(e)
|
|
@@ -175,8 +180,7 @@ class TagMixin extends MixinBase {
|
|
|
175
180
|
async contactList (): Promise<ContactInterface[]> {
|
|
176
181
|
log.verbose('Tag', 'contactList() for tag : %s', this)
|
|
177
182
|
|
|
178
|
-
const
|
|
179
|
-
const contactIds = await this.wechaty.puppet.tagTagContactList(tag)
|
|
183
|
+
const contactIds = await this.wechaty.puppet.tagTagContactList(this.id)
|
|
180
184
|
const contactPromises = contactIds.map(id => this.wechaty.Contact.find({ id })) as Promise<ContactInterface>[]
|
|
181
185
|
return Promise.all(contactPromises)
|
|
182
186
|
}
|
|
@@ -184,23 +188,22 @@ class TagMixin extends MixinBase {
|
|
|
184
188
|
async tag (contacts: ContactInterface | ContactInterface[]): Promise<void> {
|
|
185
189
|
log.verbose('Tag', 'tag(%s) for tag : %s', contacts, this)
|
|
186
190
|
|
|
187
|
-
const tag = { id: this.id, groupId: this.groupId } as TagIdentifier
|
|
188
191
|
let contactIds: string[]
|
|
189
192
|
if (Array.isArray(contacts)) {
|
|
190
193
|
contactIds = contacts.map(c => c.id)
|
|
191
194
|
} else {
|
|
192
195
|
contactIds = [contacts.id]
|
|
193
196
|
}
|
|
194
|
-
await this.wechaty.puppet.tagContactTagAdd([
|
|
197
|
+
await this.wechaty.puppet.tagContactTagAdd([this.id], contactIds)
|
|
195
198
|
}
|
|
196
199
|
|
|
197
200
|
static async createTag (name: string, tagGroup?: TagGroupInterface): Promise<TagInterface | void> {
|
|
198
201
|
log.verbose('Tag', 'createTag(%s, %s)', tagGroup, name)
|
|
199
202
|
|
|
200
203
|
try {
|
|
201
|
-
const
|
|
202
|
-
if (
|
|
203
|
-
const newTag = await this.find(
|
|
204
|
+
const tagId = await this.wechaty.puppet.tagTagAdd(name, tagGroup?.name())
|
|
205
|
+
if (tagId) {
|
|
206
|
+
const newTag = await this.find({ id: tagId })
|
|
204
207
|
return newTag
|
|
205
208
|
}
|
|
206
209
|
} catch (e) {
|
|
@@ -212,10 +215,8 @@ class TagMixin extends MixinBase {
|
|
|
212
215
|
static async deleteTag (tagInstance: TagInterface): Promise<void> {
|
|
213
216
|
log.verbose('Tag', 'deleteTag(%s, %s)', tagInstance)
|
|
214
217
|
|
|
215
|
-
const tagIdentifier = { id: tagInstance.id, groupId: tagInstance.groupId } as TagIdentifier
|
|
216
|
-
|
|
217
218
|
try {
|
|
218
|
-
await this.wechaty.puppet.tagTagDelete(
|
|
219
|
+
await this.wechaty.puppet.tagTagDelete(tagInstance.id)
|
|
219
220
|
} catch (e) {
|
|
220
221
|
this.wechaty.emitError(e)
|
|
221
222
|
log.error('Tag', 'deleteTag() exception: %s', (e as Error).message)
|
|
@@ -29,7 +29,6 @@ import type { GErrorMixin } from './gerror-mixin.js'
|
|
|
29
29
|
import type { IoMixin } from './io-mixin.js'
|
|
30
30
|
import { ContactImportantFields, ContactUpdatableValuePair, InfoUpdateInterface, RoomImportantFields, RoomUpdatableValuePair } from '../schemas/update.js'
|
|
31
31
|
import { checkUntilChanged, diffPayload } from '../pure-functions/update.js'
|
|
32
|
-
import { getTagKey } from '@juzi/wechaty-puppet/helpers'
|
|
33
32
|
|
|
34
33
|
const PUPPET_MEMORY_NAME = 'puppet'
|
|
35
34
|
|
|
@@ -432,37 +431,22 @@ const puppetMixin = <MixinBase extends WechatifyUserModuleMixin & GErrorMixin &
|
|
|
432
431
|
|
|
433
432
|
case 'tag':
|
|
434
433
|
puppet.on('tag', async payload => {
|
|
435
|
-
switch (payload.
|
|
434
|
+
switch (payload.type) {
|
|
436
435
|
case PUPPET.types.TagEvent.TagCreate: {
|
|
437
|
-
const newTagPromises = payload.
|
|
438
|
-
this.Tag.find({
|
|
439
|
-
id: tag.tagId,
|
|
440
|
-
groupId: tag.tagGroupId,
|
|
441
|
-
}),
|
|
442
|
-
)
|
|
436
|
+
const newTagPromises = payload.idList.map(id => this.Tag.find({ id }))
|
|
443
437
|
const newTags = await Promise.all(newTagPromises)
|
|
444
|
-
this.emit('tag', payload.
|
|
438
|
+
this.emit('tag', payload.type, newTags)
|
|
445
439
|
break
|
|
446
440
|
}
|
|
447
441
|
case PUPPET.types.TagEvent.TagDelete: {
|
|
448
|
-
const deletedTagPromises = payload.
|
|
449
|
-
this.Tag.find({
|
|
450
|
-
id: tag.tagId,
|
|
451
|
-
groupId: tag.tagGroupId,
|
|
452
|
-
}),
|
|
453
|
-
)
|
|
442
|
+
const deletedTagPromises = payload.idList.map(id => this.Tag.find({ id }))
|
|
454
443
|
const deletedTags = await Promise.all(deletedTagPromises)
|
|
455
|
-
this.emit('tag', payload.
|
|
444
|
+
this.emit('tag', payload.type, deletedTags)
|
|
456
445
|
// TODO: bind tag-delete to tag instance
|
|
457
446
|
break
|
|
458
447
|
}
|
|
459
448
|
case PUPPET.types.TagEvent.TagRename: {
|
|
460
|
-
const renamedTagPromises = payload.
|
|
461
|
-
this.Tag.find({
|
|
462
|
-
id: tag.tagId,
|
|
463
|
-
groupId: tag.tagGroupId,
|
|
464
|
-
}),
|
|
465
|
-
)
|
|
449
|
+
const renamedTagPromises = payload.idList.map(id => this.Tag.find({ id }))
|
|
466
450
|
const renamedTags = (await Promise.all(renamedTagPromises)) as TagInterface[]
|
|
467
451
|
await Promise.all(renamedTags.map(async tag => {
|
|
468
452
|
const oldName = tag.name()
|
|
@@ -471,33 +455,43 @@ const puppetMixin = <MixinBase extends WechatifyUserModuleMixin & GErrorMixin &
|
|
|
471
455
|
return tag.name() === oldName
|
|
472
456
|
})
|
|
473
457
|
if (!result) {
|
|
474
|
-
log.warn('WechatyPuppetMixin', 'tagRenameEvent still get old name after %s retries for tag %s', PUPPET_PAYLOAD_SYNC_MAX_RETRY, tag.
|
|
458
|
+
log.warn('WechatyPuppetMixin', 'tagRenameEvent still get old name after %s retries for tag %s', PUPPET_PAYLOAD_SYNC_MAX_RETRY, tag.id)
|
|
475
459
|
}
|
|
476
460
|
}))
|
|
477
|
-
this.emit('tag', payload.
|
|
461
|
+
this.emit('tag', payload.type, renamedTags)
|
|
478
462
|
// TODO: bind tag-rename to tag instance
|
|
479
463
|
break
|
|
480
464
|
}
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
465
|
+
default:
|
|
466
|
+
throw new Error('tagEventType ' + payload.type + ' unsupported!')
|
|
467
|
+
}
|
|
468
|
+
})
|
|
469
|
+
break
|
|
470
|
+
|
|
471
|
+
case 'tag-group':
|
|
472
|
+
puppet.on('tag-group', async payload => {
|
|
473
|
+
switch (payload.type) {
|
|
474
|
+
|
|
475
|
+
case PUPPET.types.TagGroupEvent.TagGroupCreate: {
|
|
476
|
+
const newTagGroupPromises = payload.idList.map(id =>
|
|
477
|
+
this.TagGroup.find({ id }),
|
|
484
478
|
)
|
|
485
479
|
const newTagGroups = await Promise.all(newTagGroupPromises)
|
|
486
|
-
this.emit('tag', payload.
|
|
480
|
+
this.emit('tag-group', payload.type, newTagGroups)
|
|
487
481
|
break
|
|
488
482
|
}
|
|
489
|
-
case PUPPET.types.
|
|
490
|
-
const deletedTagGroupPromises = payload.
|
|
491
|
-
this.TagGroup.find(
|
|
483
|
+
case PUPPET.types.TagGroupEvent.TagGroupDelete: {
|
|
484
|
+
const deletedTagGroupPromises = payload.idList.map(id =>
|
|
485
|
+
this.TagGroup.find({ id }),
|
|
492
486
|
)
|
|
493
487
|
const deletedTagGroups = await Promise.all(deletedTagGroupPromises)
|
|
494
|
-
this.emit('tag', payload.
|
|
488
|
+
this.emit('tag-group', payload.type, deletedTagGroups)
|
|
495
489
|
break
|
|
496
490
|
// TODO: bind tagGroup-delete to tagGroup instance
|
|
497
491
|
}
|
|
498
|
-
case PUPPET.types.
|
|
499
|
-
const renamedTagGroupPromises = payload.
|
|
500
|
-
this.TagGroup.find(
|
|
492
|
+
case PUPPET.types.TagGroupEvent.TagGroupRename: {
|
|
493
|
+
const renamedTagGroupPromises = payload.idList.map(id =>
|
|
494
|
+
this.TagGroup.find({ id }),
|
|
501
495
|
)
|
|
502
496
|
const renamedTagGroups = (await Promise.all(renamedTagGroupPromises)) as TagGroupInterface[]
|
|
503
497
|
await Promise.all(renamedTagGroups.map(async tagGroup => {
|
|
@@ -510,10 +504,12 @@ const puppetMixin = <MixinBase extends WechatifyUserModuleMixin & GErrorMixin &
|
|
|
510
504
|
log.warn('WechatyPuppetMixin', 'tagGroupRenameEvent still get old name after %s retries for tagGroup %s', PUPPET_PAYLOAD_SYNC_MAX_RETRY, tagGroup.id)
|
|
511
505
|
}
|
|
512
506
|
}))
|
|
513
|
-
this.emit('tag', payload.
|
|
507
|
+
this.emit('tag-group', payload.type, renamedTagGroups)
|
|
514
508
|
// TODO: bind tagGroup-rename to tagGroup instance
|
|
515
509
|
break
|
|
516
510
|
}
|
|
511
|
+
default:
|
|
512
|
+
throw new Error('tagGroupEventType ' + payload.type + ' unsupported!')
|
|
517
513
|
}
|
|
518
514
|
|
|
519
515
|
})
|
|
@@ -551,10 +547,10 @@ const puppetMixin = <MixinBase extends WechatifyUserModuleMixin & GErrorMixin &
|
|
|
551
547
|
for (const difference of importantDifferences) {
|
|
552
548
|
switch (difference?.key) {
|
|
553
549
|
case 'tags': {
|
|
554
|
-
const oldTagsSet = new Set(difference.oldValue
|
|
555
|
-
const newTagsSet = new Set(difference.newValue
|
|
556
|
-
const addedTags = difference.newValue?.filter(ele => !oldTagsSet.has(
|
|
557
|
-
const removedTags = difference.oldValue?.filter(ele => !newTagsSet.has(
|
|
550
|
+
const oldTagsSet = new Set(difference.oldValue)
|
|
551
|
+
const newTagsSet = new Set(difference.newValue)
|
|
552
|
+
const addedTags = difference.newValue?.filter(ele => !oldTagsSet.has(ele)).map(ele => this.Tag.find({ id: ele })) || []
|
|
553
|
+
const removedTags = difference.oldValue?.filter(ele => !newTagsSet.has(ele)).map(ele => this.Tag.find({ id: ele })) || []
|
|
558
554
|
if (addedTags.length > 0) {
|
|
559
555
|
this.emit('contact-tag-add', contact, addedTags)
|
|
560
556
|
}
|