diff --git a/package.json b/package.json index 8ccb6586f..88c743313 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@juzi/wechaty", - "version": "1.0.88", + "version": "1.0.89", "description": "Wechaty is a RPA SDK for Chatbot Makers.", "type": "module", "exports": { @@ -109,7 +109,7 @@ }, "homepage": "https://github.com/wechaty/", "dependencies": { - "@juzi/wechaty-puppet-service": "^1.0.88", + "@juzi/wechaty-puppet-service": "^1.0.89", "clone-class": "^1.1.1", "cmd-ts": "^0.10.0", "cockatiel": "^2.0.2", @@ -132,7 +132,7 @@ "@chatie/eslint-config": "^1.0.4", "@chatie/semver": "^0.4.7", "@chatie/tsconfig": "^4.6.3", - "@juzi/wechaty-puppet": "^1.0.78", + "@juzi/wechaty-puppet": "^1.0.79", "@juzi/wechaty-puppet-mock": "^1.0.1", "@swc/core": "1.3.44", "@swc/helpers": "^0.3.6", diff --git a/src/io.ts b/src/io.ts index c34eb038d..a4a6f09de 100644 --- a/src/io.ts +++ b/src/io.ts @@ -163,11 +163,14 @@ export class Io { this.ws = await this.initWebSocket() this.options.wechaty.on('login', () => { this.scanPayload = undefined }) - this.options.wechaty.on('scan', (qrcode, status) => { + this.options.wechaty.on('scan', (qrcode, status, data, type, date) => { this.scanPayload = { ...this.scanPayload, qrcode, status, + type, + data, + timestamp: date.valueOf(), } }) diff --git a/src/user-modules/room.ts b/src/user-modules/room.ts index d361617e1..ea30b9d38 100644 --- a/src/user-modules/room.ts +++ b/src/user-modules/room.ts @@ -255,6 +255,46 @@ class RoomMixin extends MixinBase implements SayableSayer { return undefined } + static async batchLoadRooms (roomIdList: string[]) { + let continuousErrorCount = 0 + let totalErrorCount = 0 + const totalErrorThreshold = Math.round(roomIdList.length / 5) + + const idToRoom = async (id: string) => { + if (!this.wechaty.isLoggedIn) { + throw new Error('wechaty not logged in') + } + const result = await this.wechaty.Room.find({ id }).catch(e => { + this.wechaty.emitError(e) + continuousErrorCount++ + totalErrorCount++ + if (continuousErrorCount > 5) { + throw new Error('5 continuous errors!') + } + if (totalErrorCount > totalErrorThreshold) { + throw new Error(`${totalErrorThreshold} total errors!`) + } + }) + continuousErrorCount = 0 + return result + } + + /** + * we need to use concurrencyExecuter to reduce the parallel number of the requests + */ + const CONCURRENCY = 17 + const roomIterator = concurrencyExecuter(CONCURRENCY)(idToRoom)(roomIdList) + + const roomList: RoomInterface[] = [] + + for await (const room of roomIterator) { + if (room) { + roomList.push(room) + } + } + return roomList + } + /** const roomList: RoomInterface[] = [] * @ignore diff --git a/src/user-modules/wecom.ts b/src/user-modules/wecom.ts index 4837ac5e8..9a62cb3bb 100644 --- a/src/user-modules/wecom.ts +++ b/src/user-modules/wecom.ts @@ -5,6 +5,7 @@ import { validationMixin } from '../user-mixins/validation.js' import { wechatifyMixinBase, } from '../user-mixins/wechatify.js' +import type { RoomAntiSpamStrategy } from '@juzi/wechaty-puppet/types' class WecomMixin extends wechatifyMixinBase() { @@ -18,6 +19,34 @@ class WecomMixin extends wechatifyMixinBase() { ) } + static async getRoomAntiSpamStrategyList (): Promise { + return this.wechaty.puppet.getRoomAntiSpamStrategyList() + } + + static async getRoomAntiSpamStrategyEffectRoomList ( + strategyId: string, + ): Promise { + return this.wechaty.puppet.getRoomAntiSpamStrategyEffectRoomList(strategyId) + } + + static async applyRoomAntiSpamStrategy ( + strategyId: string, + roomIds: string[], + active: boolean, + ): Promise { + const rawRoomIdSet = new Set(roomIds) + const rooms = (await this.wechaty.Room.batchLoadRooms(Array.from(rawRoomIdSet))).filter(room => room.owner()?.self()) + + const actualRoomIdSet = new Set(rooms.map(room => room.id)) + const filteredRoomIds = Array.from(rawRoomIdSet).filter(id => !actualRoomIdSet.has(id)) + + if (filteredRoomIds.length) { + log.warn(`these rooms cannot be applied with anti-spam strategy: ${filteredRoomIds}`) + } + + return this.wechaty.puppet.applyRoomAntiSpamStrategy(strategyId, Array.from(actualRoomIdSet), active) + } + /* * @hideconstructor */