Skip to content

Commit

Permalink
PATCH: AR-3490 Deactivate idle accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
nmagedman committed Jan 27, 2025
1 parent 6e51e94 commit d8d86e6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
48 changes: 48 additions & 0 deletions apps/meteor/app/seeking-alpha/deactivate-idle-accounts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-disable prettier/prettier */
// Deactivate idle, unused accounts.
// We pay upstream for active users (`db.users.countDocuments({active: true})`),
// however many users don’t make use of chat.
// RocketChatAuth will still list them as active, so they’ll automatically
// reactivate here in RC once they click "Launch Chat".


import { cronJobs } from '@rocket.chat/cron';
import { Logger } from '@rocket.chat/logger';
import { Users } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor';

import { today, nDaysBeforeDate } from '../utils/datetime_functions';


const CRON_JOB_NAME = 'seeking-alpha-deactivate-idle-accounts';
const CRON_JOB_SCHEDULE = '*/5 * * * *'; // '15 10 * * *';
const MAX_IDLE_DAYS = 90;

const LOG = new Logger(CRON_JOB_NAME);

Meteor.startup(async () => {
await cronJobs.add(CRON_JOB_NAME, CRON_JOB_SCHEDULE, async () => {
try {
LOG.info("Starting...");
await perform();
LOG.info("Finished!");
} catch (e: any) {
LOG.error(`ERROR: CRONJOB: ${CRON_JOB_NAME}:`, e.message);
}
});
});

async function perform() {
const cutoffDate = nDaysBeforeDate(MAX_IDLE_DAYS, today());

const numDeactivatableUsers = await Users.col.countDocuments({
active: true,
createdAt: { $lt: cutoffDate },
$or: [
{ lastLogin: { $lt: cutoffDate } },
{ lastLogin: { $exists: false } },
],
});

await LOG.info(`${numDeactivatableUsers} users can be deactivated!`);
}
1 change: 1 addition & 0 deletions apps/meteor/server/importPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ import '../app/livechat/server';
import '../app/authentication/server';
import '../app/voip/server/startup';
import '../app/seeking-alpha/terms-of-use-violations';
import '../app/seeking-alpha/deactivate-idle-accounts';

0 comments on commit d8d86e6

Please sign in to comment.