@juzi/wechaty 1.0.58 → 1.0.60

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 (53) hide show
  1. package/dist/cjs/src/package-json.js +4 -4
  2. package/dist/cjs/src/user-modules/contact-self.d.ts +2 -0
  3. package/dist/cjs/src/user-modules/contact-self.d.ts.map +1 -1
  4. package/dist/cjs/src/user-modules/contact-self.js +10 -0
  5. package/dist/cjs/src/user-modules/contact-self.js.map +1 -1
  6. package/dist/cjs/src/user-modules/contact.d.ts.map +1 -1
  7. package/dist/cjs/src/user-modules/contact.js +31 -34
  8. package/dist/cjs/src/user-modules/contact.js.map +1 -1
  9. package/dist/cjs/src/user-modules/message.d.ts.map +1 -1
  10. package/dist/cjs/src/user-modules/message.js +42 -4
  11. package/dist/cjs/src/user-modules/message.js.map +1 -1
  12. package/dist/cjs/src/user-modules/room.d.ts.map +1 -1
  13. package/dist/cjs/src/user-modules/room.js +31 -34
  14. package/dist/cjs/src/user-modules/room.js.map +1 -1
  15. package/dist/cjs/src/user-modules/room.spec.js +25 -0
  16. package/dist/cjs/src/user-modules/room.spec.js.map +1 -1
  17. package/dist/cjs/src/user-modules/tag-group.d.ts.map +1 -1
  18. package/dist/cjs/src/user-modules/tag-group.js +21 -1
  19. package/dist/cjs/src/user-modules/tag-group.js.map +1 -1
  20. package/dist/cjs/src/user-modules/tag.d.ts.map +1 -1
  21. package/dist/cjs/src/user-modules/tag.js +28 -15
  22. package/dist/cjs/src/user-modules/tag.js.map +1 -1
  23. package/dist/esm/src/package-json.js +4 -4
  24. package/dist/esm/src/user-modules/contact-self.d.ts +2 -0
  25. package/dist/esm/src/user-modules/contact-self.d.ts.map +1 -1
  26. package/dist/esm/src/user-modules/contact-self.js +10 -0
  27. package/dist/esm/src/user-modules/contact-self.js.map +1 -1
  28. package/dist/esm/src/user-modules/contact.d.ts.map +1 -1
  29. package/dist/esm/src/user-modules/contact.js +31 -34
  30. package/dist/esm/src/user-modules/contact.js.map +1 -1
  31. package/dist/esm/src/user-modules/message.d.ts.map +1 -1
  32. package/dist/esm/src/user-modules/message.js +42 -4
  33. package/dist/esm/src/user-modules/message.js.map +1 -1
  34. package/dist/esm/src/user-modules/room.d.ts.map +1 -1
  35. package/dist/esm/src/user-modules/room.js +31 -34
  36. package/dist/esm/src/user-modules/room.js.map +1 -1
  37. package/dist/esm/src/user-modules/room.spec.js +25 -0
  38. package/dist/esm/src/user-modules/room.spec.js.map +1 -1
  39. package/dist/esm/src/user-modules/tag-group.d.ts.map +1 -1
  40. package/dist/esm/src/user-modules/tag-group.js +21 -1
  41. package/dist/esm/src/user-modules/tag-group.js.map +1 -1
  42. package/dist/esm/src/user-modules/tag.d.ts.map +1 -1
  43. package/dist/esm/src/user-modules/tag.js +28 -15
  44. package/dist/esm/src/user-modules/tag.js.map +1 -1
  45. package/package.json +3 -3
  46. package/src/package-json.ts +4 -4
  47. package/src/user-modules/contact-self.ts +15 -0
  48. package/src/user-modules/contact.ts +34 -36
  49. package/src/user-modules/message.ts +43 -6
  50. package/src/user-modules/room.spec.ts +33 -0
  51. package/src/user-modules/room.ts +33 -36
  52. package/src/user-modules/tag-group.ts +21 -1
  53. package/src/user-modules/tag.ts +31 -18
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juzi/wechaty",
3
- "version": "1.0.58",
3
+ "version": "1.0.60",
4
4
  "description": "Wechaty is a RPA SDK for Chatbot Makers.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -109,7 +109,7 @@
109
109
  },
110
110
  "homepage": "https://github.com/wechaty/",
111
111
  "dependencies": {
112
- "@juzi/wechaty-puppet-service": "^1.0.61",
112
+ "@juzi/wechaty-puppet-service": "^1.0.64",
113
113
  "clone-class": "^1.1.1",
114
114
  "cmd-ts": "^0.10.0",
115
115
  "cockatiel": "^2.0.2",
@@ -132,7 +132,7 @@
132
132
  "@chatie/eslint-config": "^1.0.4",
133
133
  "@chatie/semver": "^0.4.7",
134
134
  "@chatie/tsconfig": "^4.6.3",
135
- "@juzi/wechaty-puppet": "^1.0.58",
135
+ "@juzi/wechaty-puppet": "^1.0.59",
136
136
  "@juzi/wechaty-puppet-mock": "^1.0.1",
137
137
  "@swc/core": "1.3.44",
138
138
  "@swc/helpers": "^0.3.6",
@@ -4,10 +4,10 @@
4
4
  */
5
5
  import type { PackageJsonWechaty } from './config'
6
6
 
7
- export const GIT_COMMIT_HASH: string = '7bb15dcc3563fbf710742a0c90443e9bd74da35b'
7
+ export const GIT_COMMIT_HASH: string = '47f0dca4c164f04760e66d514ee0cf668f5e9945'
8
8
  export const packageJson: PackageJsonWechaty = {
9
9
  "name": "@juzi/wechaty",
10
- "version": "1.0.58",
10
+ "version": "1.0.60",
11
11
  "description": "Wechaty is a RPA SDK for Chatbot Makers.",
12
12
  "type": "module",
13
13
  "exports": {
@@ -116,7 +116,7 @@ export const packageJson: PackageJsonWechaty = {
116
116
  },
117
117
  "homepage": "https://github.com/wechaty/",
118
118
  "dependencies": {
119
- "@juzi/wechaty-puppet-service": "^1.0.61",
119
+ "@juzi/wechaty-puppet-service": "^1.0.64",
120
120
  "clone-class": "^1.1.1",
121
121
  "cmd-ts": "^0.10.0",
122
122
  "cockatiel": "^2.0.2",
@@ -139,7 +139,7 @@ export const packageJson: PackageJsonWechaty = {
139
139
  "@chatie/eslint-config": "^1.0.4",
140
140
  "@chatie/semver": "^0.4.7",
141
141
  "@chatie/tsconfig": "^4.6.3",
142
- "@juzi/wechaty-puppet": "^1.0.58",
142
+ "@juzi/wechaty-puppet": "^1.0.59",
143
143
  "@juzi/wechaty-puppet-mock": "^1.0.1",
144
144
  "@swc/core": "1.3.44",
145
145
  "@swc/helpers": "^0.3.6",
@@ -36,6 +36,7 @@ import {
36
36
  } from './contact.js'
37
37
  import { validationMixin } from '../user-mixins/validation.js'
38
38
  import { poolifyMixin } from '../user-mixins/poolify.js'
39
+ import type { RoomInterface } from './room.js'
39
40
 
40
41
  const MixinBase = poolifyMixin(
41
42
  ContactImpl,
@@ -199,6 +200,20 @@ class ContactSelfMixin extends MixinBase {
199
200
  return this.wechaty.puppet.contactSelfSignature(signature).then(this.sync.bind(this))
200
201
  }
201
202
 
203
+ public async roomAlias (room: RoomInterface, alias?: string): Promise<void | string> {
204
+ log.verbose('ContactSelf', 'roomAlias()')
205
+
206
+ if (typeof alias === 'undefined') {
207
+ return room.alias(this)
208
+ }
209
+
210
+ if (!(await room.has(this))) {
211
+ throw new Error('cannot edit room alias because you are not in room')
212
+ }
213
+
214
+ return this.wechaty.puppet.contactSelfRoomAlias(room.id, alias)
215
+ }
216
+
202
217
  }
203
218
 
204
219
  class ContactSelfImpl extends validationMixin(ContactSelfMixin)<ContactSelfInterface>() {}
@@ -173,48 +173,46 @@ class ContactMixin extends MixinBase implements SayableSayer {
173
173
  ): Promise<ContactInterface[]> {
174
174
  log.verbose('Contact', 'findAll(%s)', JSON.stringify(query, stringifyFilter) || '')
175
175
 
176
- try {
177
- const contactIdList: string[] = await this.wechaty.puppet.contactSearch(query)
178
-
179
- let continuousErrorCount = 0
180
- let totalErrorCount = 0
181
- const idToContact = async (id: string) => {
182
- const result = await this.wechaty.Contact.find({ id }).catch(e => {
183
- this.wechaty.emitError(e)
184
- continuousErrorCount++
185
- totalErrorCount++
186
- if (continuousErrorCount > 5) {
187
- throw new Error('5 continuous errors!')
188
- }
189
- if (totalErrorCount > 100) {
190
- throw new Error('100 total errors!')
191
- }
192
- })
193
- continuousErrorCount = 0
194
- return result
195
- }
196
-
197
- /**
198
- * we need to use concurrencyExecuter to reduce the parallel number of the requests
199
- */
200
- const CONCURRENCY = 17
201
- const contactIterator = concurrencyExecuter(CONCURRENCY)(idToContact)(contactIdList)
176
+ const contactIdList: string[] = await this.wechaty.puppet.contactSearch(query)
202
177
 
203
- const contactList: ContactInterface[] = []
178
+ let continuousErrorCount = 0
179
+ let totalErrorCount = 0
180
+ const totalErrorThreshold = Math.round(contactIdList.length / 5)
204
181
 
205
- for await (const contact of contactIterator) {
206
- if (contact) {
207
- contactList.push(contact)
208
- }
182
+ const idToContact = async (id: string) => {
183
+ if (!this.wechaty.isLoggedIn) {
184
+ throw new Error('wechaty not logged in')
209
185
  }
186
+ const result = await this.wechaty.Contact.find({ id }).catch(e => {
187
+ this.wechaty.emitError(e)
188
+ continuousErrorCount++
189
+ totalErrorCount++
190
+ if (continuousErrorCount > 5) {
191
+ throw new Error('5 continuous errors!')
192
+ }
193
+ if (totalErrorCount > totalErrorThreshold) {
194
+ throw new Error(`${totalErrorThreshold} total errors!`)
195
+ }
196
+ })
197
+ continuousErrorCount = 0
198
+ return result
199
+ }
210
200
 
211
- return contactList
201
+ /**
202
+ * we need to use concurrencyExecuter to reduce the parallel number of the requests
203
+ */
204
+ const CONCURRENCY = 17
205
+ const contactIterator = concurrencyExecuter(CONCURRENCY)(idToContact)(contactIdList)
212
206
 
213
- } catch (e) {
214
- this.wechaty.emitError(e)
215
- log.error('Contact', 'this.wechaty.puppet.contactFindAll() rejected: %s', (e as Error).message)
216
- return [] // fail safe
207
+ const contactList: ContactInterface[] = []
208
+
209
+ for await (const contact of contactIterator) {
210
+ if (contact) {
211
+ contactList.push(contact)
212
+ }
217
213
  }
214
+
215
+ return contactList
218
216
  }
219
217
 
220
218
  // TODO
@@ -191,9 +191,28 @@ class MessageMixin extends MixinBase implements SayableSayer {
191
191
  const { contactIds = [], roomIds = [] } = await this.wechaty.puppet.getMessageBroadcastTarget()
192
192
 
193
193
  const CONCURRENCY = 17
194
- const idToRoom = async (id: string) =>
195
- this.wechaty.Room.find({ id })
196
- .catch(e => this.wechaty.emitError(e))
194
+
195
+ let roomContinuousErrorCount = 0
196
+ let roomTotalErrorCount = 0
197
+ const roomTotalErrorThreshold = Math.round(roomIds.length / 5)
198
+ const idToRoom = async (id: string) => {
199
+ if (!this.wechaty.isLoggedIn) {
200
+ throw new Error('wechaty not logged in')
201
+ }
202
+ const result = await this.wechaty.Room.find({ id }).catch(e => {
203
+ this.wechaty.emitError(e)
204
+ roomContinuousErrorCount++
205
+ roomTotalErrorCount++
206
+ if (roomContinuousErrorCount > 5) {
207
+ throw new Error('5 continuous errors!')
208
+ }
209
+ if (roomTotalErrorCount > roomTotalErrorThreshold) {
210
+ throw new Error(`${roomTotalErrorThreshold} total errors!`)
211
+ }
212
+ })
213
+ roomContinuousErrorCount = 0
214
+ return result
215
+ }
197
216
 
198
217
  const roomIterator = concurrencyExecuter(CONCURRENCY)(idToRoom)(roomIds)
199
218
 
@@ -205,9 +224,27 @@ class MessageMixin extends MixinBase implements SayableSayer {
205
224
  }
206
225
  }
207
226
 
208
- const idToContact = async (id: string) =>
209
- this.wechaty.Contact.find({ id })
210
- .catch(e => this.wechaty.emitError(e))
227
+ let contactContinuousErrorCount = 0
228
+ let contactTotalErrorCount = 0
229
+ const contactTotalErrorThreshold = Math.round(roomIds.length / 5)
230
+ const idToContact = async (id: string) => {
231
+ if (!this.wechaty.isLoggedIn) {
232
+ throw new Error('wechaty not logged in')
233
+ }
234
+ const result = await this.wechaty.Contact.find({ id }).catch(e => {
235
+ this.wechaty.emitError(e)
236
+ contactContinuousErrorCount++
237
+ contactTotalErrorCount++
238
+ if (contactContinuousErrorCount > 5) {
239
+ throw new Error('5 continuous errors!')
240
+ }
241
+ if (contactTotalErrorCount > contactTotalErrorThreshold) {
242
+ throw new Error(`${contactTotalErrorThreshold} total errors!`)
243
+ }
244
+ })
245
+ contactContinuousErrorCount = 0
246
+ return result
247
+ }
211
248
 
212
249
  const contactIterator = concurrencyExecuter(CONCURRENCY)(idToContact)(contactIds)
213
250
 
@@ -52,6 +52,13 @@ test('findAll()', async t => {
52
52
  topic: EXPECTED_ROOM_TOPIC,
53
53
  } as PUPPET.payloads.Room
54
54
  })
55
+ const mockContact = puppet.mocker.createContact({ name: 'bot' })
56
+ await puppet.mocker.login(mockContact)
57
+
58
+ const future = new Promise(resolve => {
59
+ wechaty.on('login', resolve)
60
+ })
61
+ await future
55
62
 
56
63
  const roomList = await wechaty.Room.findAll()
57
64
  t.equal(roomList.length, 1, 'should find 1 room')
@@ -60,6 +67,32 @@ test('findAll()', async t => {
60
67
  await wechaty.stop()
61
68
  })
62
69
 
70
+ test('room.findAll() not login test', async t => {
71
+ const EXPECTED_ROOM_ID = 'test-id'
72
+ const EXPECTED_ROOM_TOPIC = 'test-topic'
73
+ const EXPECTED_ROOM_ID_LIST = [ EXPECTED_ROOM_ID ]
74
+
75
+ const sandbox = sinon.createSandbox()
76
+
77
+ const puppet = new PuppetMock() as any
78
+ const wechaty = WechatyBuilder.build({ puppet })
79
+
80
+ await wechaty.start()
81
+
82
+ sandbox.stub(puppet, 'roomSearch').resolves(EXPECTED_ROOM_ID_LIST)
83
+ sandbox.stub(puppet, 'roomMemberList').resolves([])
84
+ sandbox.stub(puppet, 'roomPayload').callsFake(async () => {
85
+ await new Promise(resolve => setImmediate(resolve))
86
+ return {
87
+ topic: EXPECTED_ROOM_TOPIC,
88
+ } as PUPPET.payloads.Room
89
+ })
90
+
91
+ await t.rejects(wechaty.Room.findAll())
92
+
93
+ await wechaty.stop()
94
+ })
95
+
63
96
  test('room.say() smoke testing', async () => {
64
97
 
65
98
  const sandbox = sinon.createSandbox()
@@ -147,48 +147,45 @@ class RoomMixin extends MixinBase implements SayableSayer {
147
147
  ): Promise<RoomInterface[]> {
148
148
  log.verbose('Room', 'findAll(%s)', JSON.stringify(query, stringifyFilter) || '')
149
149
 
150
- try {
151
- const roomIdList = await this.wechaty.puppet.roomSearch(query)
152
-
153
- let continuousErrorCount = 0
154
- let totalErrorCount = 0
155
- const idToRoom = async (id: string) => {
156
- const result = await this.wechaty.Room.find({ id }).catch(e => {
157
- this.wechaty.emitError(e)
158
- continuousErrorCount++
159
- totalErrorCount++
160
- if (continuousErrorCount > 5) {
161
- throw new Error('5 continuous errors!')
162
- }
163
- if (totalErrorCount > 100) {
164
- throw new Error('100 total errors!')
165
- }
166
- })
167
- continuousErrorCount = 0
168
- return result
150
+ const roomIdList = await this.wechaty.puppet.roomSearch(query)
151
+
152
+ let continuousErrorCount = 0
153
+ let totalErrorCount = 0
154
+ const totalErrorThreshold = Math.round(roomIdList.length / 5)
155
+ const idToRoom = async (id: string) => {
156
+ if (!this.wechaty.isLoggedIn) {
157
+ throw new Error('wechaty not logged in')
169
158
  }
159
+ const result = await this.wechaty.Room.find({ id }).catch(e => {
160
+ this.wechaty.emitError(e)
161
+ continuousErrorCount++
162
+ totalErrorCount++
163
+ if (continuousErrorCount > 5) {
164
+ throw new Error('5 continuous errors!')
165
+ }
166
+ if (totalErrorCount > totalErrorThreshold) {
167
+ throw new Error(`${totalErrorThreshold} total errors!`)
168
+ }
169
+ })
170
+ continuousErrorCount = 0
171
+ return result
172
+ }
170
173
 
171
- /**
172
- * we need to use concurrencyExecuter to reduce the parallel number of the requests
173
- */
174
- const CONCURRENCY = 17
175
- const roomIterator = concurrencyExecuter(CONCURRENCY)(idToRoom)(roomIdList)
174
+ /**
175
+ * we need to use concurrencyExecuter to reduce the parallel number of the requests
176
+ */
177
+ const CONCURRENCY = 17
178
+ const roomIterator = concurrencyExecuter(CONCURRENCY)(idToRoom)(roomIdList)
176
179
 
177
- const roomList: RoomInterface[] = []
180
+ const roomList: RoomInterface[] = []
178
181
 
179
- for await (const room of roomIterator) {
180
- if (room) {
181
- roomList.push(room)
182
- }
182
+ for await (const room of roomIterator) {
183
+ if (room) {
184
+ roomList.push(room)
183
185
  }
184
-
185
- return roomList
186
-
187
- } catch (e) {
188
- this.wechaty.emitError(e)
189
- log.verbose('Room', 'findAll() rejected: %s', (e as Error).message)
190
- return [] as RoomInterface[] // fail safe
191
186
  }
187
+
188
+ return roomList
192
189
  }
193
190
 
194
191
  /**
@@ -67,7 +67,27 @@ class TagGroupMixin extends MixinBase {
67
67
  try {
68
68
  const tagGroupIds = await this.wechaty.puppet.tagGroupList()
69
69
 
70
- const idToTagGroup = async (id: string) => this.find({ id }).catch(e => this.wechaty.emitError(e))
70
+ let continuousErrorCount = 0
71
+ let totalErrorCount = 0
72
+ const totalErrorThreshold = Math.round(tagGroupIds.length / 5)
73
+ const idToTagGroup = async (id: string) => {
74
+ if (!this.wechaty.isLoggedIn) {
75
+ throw new Error('wechaty not logged in')
76
+ }
77
+ const result = this.find({ id }).catch(e => {
78
+ this.wechaty.emitError(e)
79
+ continuousErrorCount++
80
+ totalErrorCount++
81
+ if (continuousErrorCount > 5) {
82
+ throw new Error('5 continuous errors!')
83
+ }
84
+ if (totalErrorCount > totalErrorThreshold) {
85
+ throw new Error(`${totalErrorThreshold} total errors!`)
86
+ }
87
+ })
88
+ continuousErrorCount = 0
89
+ return result
90
+ }
71
91
 
72
92
  const CONCURRENCY = 17
73
93
  const tagGroupIterator = concurrencyExecuter(CONCURRENCY)(idToTagGroup)(tagGroupIds)
@@ -77,28 +77,41 @@ class TagMixin extends MixinBase {
77
77
  static async list (): Promise<TagInterface[]> {
78
78
  log.verbose('Tag', 'list()')
79
79
 
80
- try {
81
- const tagIdList = await this.wechaty.puppet.tagTagList()
82
-
83
- const idToTag = async (id: string) => this.find({ id }).catch(e => this.wechaty.emitError(e))
84
-
85
- const CONCURRENCY = 17
86
- const tagIterator = concurrencyExecuter(CONCURRENCY)(idToTag)(tagIdList)
87
-
88
- const tagList: TagInterface[] = []
89
- for await (const tag of tagIterator) {
90
- if (tag) {
91
- tagList.push(tag)
92
- }
80
+ const tagIdList = await this.wechaty.puppet.tagTagList()
81
+
82
+ let continuousErrorCount = 0
83
+ let totalErrorCount = 0
84
+ const totalErrorThreshold = Math.round(tagIdList.length / 5)
85
+ const idToTag = async (id: string) => {
86
+ if (!this.wechaty.isLoggedIn) {
87
+ throw new Error('wechaty not logged in')
93
88
  }
89
+ const result = this.find({ id }).catch(e => {
90
+ this.wechaty.emitError(e)
91
+ continuousErrorCount++
92
+ totalErrorCount++
93
+ if (continuousErrorCount > 5) {
94
+ throw new Error('5 continuous errors!')
95
+ }
96
+ if (totalErrorCount > totalErrorThreshold) {
97
+ throw new Error(`${totalErrorThreshold} total errors!`)
98
+ }
99
+ })
100
+ continuousErrorCount = 0
101
+ return result
102
+ }
94
103
 
95
- return tagList
104
+ const CONCURRENCY = 17
105
+ const tagIterator = concurrencyExecuter(CONCURRENCY)(idToTag)(tagIdList)
96
106
 
97
- } catch (e) {
98
- this.wechaty.emitError(e)
99
- log.error('Tag', 'list() exception: %s', (e as Error).message)
100
- return []
107
+ const tagList: TagInterface[] = []
108
+ for await (const tag of tagIterator) {
109
+ if (tag) {
110
+ tagList.push(tag)
111
+ }
101
112
  }
113
+
114
+ return tagList
102
115
  }
103
116
 
104
117
  static async find (filter: TagQueryFilter): Promise<TagInterface | undefined> {