Skip to content

Commit

Permalink
Auto seal user when he send message too frequently (yinxin630#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
yinxin630 authored Sep 7, 2021
1 parent 1877363 commit e7161ec
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions packages/server/src/middlewares/frequency.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Socket } from 'socket.io';
import { getNewUserKey, Redis } from '@fiora/database/redis/initRedis';
import {
getNewUserKey,
getSealUserKey,
Redis,
} from '@fiora/database/redis/initRedis';

export const CALL_SERVICE_FREQUENTLY = '接口调用频繁, 请稍后再试';
export const CALL_SERVICE_FREQUENTLY = '发消息过于频繁, 请冷静一会再试';
export const NEW_USER_CALL_SERVICE_FREQUENTLY =
'接口调用失败, 你正处于萌新限制期, 请不要频繁操作';
'发消息过于频繁, 你还处于萌新期, 不要恶意刷屏, 先冷静一会再试';

const MaxCallPerMinutes = 20;
const NewUserMaxCallPerMinutes = 5;
Expand Down Expand Up @@ -46,10 +50,20 @@ export default function frequency(
(await Redis.has(getNewUserKey(socket.data.user)));
if (isNewUser && count >= newUserMaxCallPerMinutes) {
// new user limit
cb('接口调用失败, 你正处于萌新限制期, 请不要频繁操作');
cb(NEW_USER_CALL_SERVICE_FREQUENTLY);
await Redis.set(
getSealUserKey(socket.data.user),
socket.data.user,
Redis.Minute * 3,
);
} else if (count >= maxCallPerMinutes) {
// normal user limit
cb(CALL_SERVICE_FREQUENTLY);
await Redis.set(
getSealUserKey(socket.data.user),
socket.data.user,
Redis.Minute * 3,
);
} else {
callTimes[socketId] = count + 1;
next();
Expand Down

0 comments on commit e7161ec

Please sign in to comment.