forked from danny-avila/LibreChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathban-user.js
90 lines (75 loc) · 2.17 KB
/
ban-user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const path = require('path');
require('module-alias')({ base: path.resolve(__dirname, '..', 'api') });
const { askQuestion, silentExit } = require('./helpers');
const banViolation = require('~/cache/banViolation');
const User = require('~/models/User');
const connect = require('./connect');
(async () => {
await connect();
console.purple('---------------------');
console.purple('Ban a user account!');
console.purple('---------------------');
let email = '';
let duration = '';
if (process.argv.length >= 4) {
// Check if there are enough command-line arguments.
email = process.argv[2];
duration = parseInt(process.argv[3]); // Parse the duration as an integer.
} else {
console.orange('Usage: npm run ban-user <email> <duration>');
console.orange('Note: if you do not pass in the arguments, you will be prompted for them.');
console.purple('--------------------------');
}
if (!email) {
email = await askQuestion('Email:');
}
if (!duration) {
const durationInMinutes = await askQuestion('Duration (in minutes):');
duration = parseInt(durationInMinutes) * 60000;
}
if (isNaN(duration) || duration <= 0) {
console.red('Error: Invalid duration!');
silentExit(1);
}
if (!email.includes('@')) {
console.red('Error: Invalid email address!');
silentExit(1);
}
const user = await User.findOne({ email }).lean();
if (!user) {
console.red('Error: No user with that email was found!');
silentExit(1);
} else {
console.purple(`Found user: ${user.email}`);
}
const req = {};
const res = {
clearCookie: () => {},
status: function () {
return this;
},
json: function () {
return this;
},
};
const errorMessage = {
type: 'concurrent',
violation_count: 20,
user_id: user._id,
prev_count: 0,
duration: duration,
};
await banViolation(req, res, errorMessage);
silentExit(0);
})();
process.on('uncaughtException', (err) => {
if (!err.message.includes('fetch failed')) {
console.error('There was an uncaught error:');
console.error(err);
}
if (err.message.includes('fetch failed')) {
return;
} else {
process.exit(1);
}
});