Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/room anti spam #83

Merged
merged 4 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion src/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
})

Expand Down
40 changes: 40 additions & 0 deletions src/user-modules/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions src/user-modules/wecom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -18,6 +19,34 @@ class WecomMixin extends wechatifyMixinBase() {
)
}

static async getRoomAntiSpamStrategyList (): Promise<RoomAntiSpamStrategy[]> {
return this.wechaty.puppet.getRoomAntiSpamStrategyList()
}

static async getRoomAntiSpamStrategyEffectRoomList (
strategyId: string,
): Promise<string[]> {
return this.wechaty.puppet.getRoomAntiSpamStrategyEffectRoomList(strategyId)
}

static async applyRoomAntiSpamStrategy (
strategyId: string,
roomIds: string[],
active: boolean,
): Promise<void> {
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
*/
Expand Down
Loading