forked from RocketChat/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PATCH: AR-3490 Deactivate idle accounts
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
apps/meteor/app/seeking-alpha/deactivate-idle-accounts/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters