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 PERIOD = process.env.PERIOD; // Time in days: int 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 timeAgo = new Date(now - PERIOD * 24 * 60 * 60 * 1000); const usersToDelete = users.filter(user => { const createdAt = new Date(user.createdAt); const updatedAt = new Date(user.updatedAt); const excludedUsers = ['instance.actor', 'relay.actor']; return user.notesCount === 0 && createdAt < timeAgo && updatedAt < timeAgo && !excludedUsers.includes(user.username) }); 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();