From 7ed162fb91e99ee75d68b8fdbdf8073db1d52e1e Mon Sep 17 00:00:00 2001 From: Leafus Date: Sun, 15 Dec 2024 18:12:27 +0100 Subject: [PATCH] init --- main.js | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 7 ++++ 2 files changed, 102 insertions(+) create mode 100644 main.js create mode 100644 package.json diff --git a/main.js b/main.js new file mode 100644 index 0000000..d6cdc9b --- /dev/null +++ b/main.js @@ -0,0 +1,95 @@ +require('dotenv').config(); +const axios = require('axios'); +const readline = require('readline'); + +const USER_TOKEN = process.env.USER_TOKEN; +if (!USER_TOKEN) { + console.error('USER_TOKEN not found in .env file'); + process.exit(1); +} + +const INSTANCE_URL = process.env.INSTANCE_URL; +const API_BASE = `${INSTANCE_URL}/api`; + +async function deleteUser(userId) { + try { + await axios.post(`${API_BASE}/admin/accounts/delete`, { + i: USER_TOKEN, + userId: userId, + soft: false + }); + console.log(`Successfully deleted user: ${userId}`); + } catch (error) { + console.error(`Failed to delete user ${userId}:`, error.response?.data || error.message); + } +} + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +function askForConfirmation(message) { + return new Promise((resolve) => { + rl.question(message + ' (yes/no): ', (answer) => { + resolve(answer.toLowerCase() === 'yes'); + }); + }); +} + +async function main() { + try { + const response = await axios.post(`${API_BASE}/admin/show-users`, { + limit: 100, + i: USER_TOKEN, + origin: 'local', + state: 'all', + sort: '-createdAt', + }); + + const users = response.data; + const now = new Date(); + const fiveDaysAgo = new Date(now - 5 * 24 * 60 * 60 * 1000); + + const usersToDelete = users.filter(user => { + const createdAt = new Date(user.createdAt); + const updatedAt = new Date(user.updatedAt); + return user.notesCount === 0 && + createdAt < fiveDaysAgo && + updatedAt < fiveDaysAgo; + }); + + console.log(`Found ${usersToDelete.length} users matching criteria (no posts, not updated, and older than 5 days)`); + + console.log('\nUsers to be deleted:'); + usersToDelete.forEach(user => { + console.log(`- @${user.username} + Created: ${new Date(user.createdAt).toLocaleDateString()}, + Last Updated: ${new Date(user.updatedAt).toLocaleDateString()}, + Notes: ${user.notesCount}`); + }); + console.log(); + + const confirmed = await askForConfirmation(`Are you sure you want to delete ${usersToDelete.length} users?`); + + if (!confirmed) { + console.log('Operation cancelled by user'); + rl.close(); + return; + } + + for (const user of usersToDelete) { + console.log(`Processing user: ${user.username} (created: ${user.createdAt}, notes: ${user.notesCount})`); + await deleteUser(user.id); + await new Promise(resolve => setTimeout(resolve, 1000)); + } + + console.log('Finished processing users'); + rl.close(); + } catch (error) { + console.error('Error:', error.response?.data || error.message); + rl.close(); + } +} + +main(); diff --git a/package.json b/package.json new file mode 100644 index 0000000..ad2e08a --- /dev/null +++ b/package.json @@ -0,0 +1,7 @@ +{ + "dependencies": { + "axios": "^1.7.8", + "dotenv": "^16.4.5", + "readline": "^1.3.0" + } +} \ No newline at end of file