Skip to content

Commit

Permalink
Chore: Migrate retention-policy to ts (#25582)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman authored and ggazzo committed May 24, 2022
1 parent 31df6df commit 43303bc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
import { SyncedCron } from 'meteor/littledata:synced-cron';
import { IRoomWithRetentionPolicy } from '@rocket.chat/core-typings';

import { settings } from '../../settings/server';
import { Rooms } from '../../models/server';
import { cleanRoomHistory } from '../../lib';
import { cleanRoomHistory } from '../../lib/server';

let types = [];

const oldest = new Date('0001-01-01T00:00:00Z');
interface IParser {
cron(val: string): string;
}

const maxTimes = {
c: 0,
p: 0,
d: 0,
};

const toDays = (d) => d * 1000 * 60 * 60 * 24;
let types: (keyof typeof maxTimes)[] = [];

const oldest = new Date('0001-01-01T00:00:00Z');

const toDays = (d: number): number => d * 1000 * 60 * 60 * 24;

function job() {
function job(): void {
const now = new Date();
const filesOnly = settings.get('RetentionPolicy_FilesOnly');
const excludePinned = settings.get('RetentionPolicy_DoNotPrunePinned');
const ignoreDiscussion = settings.get('RetentionPolicy_DoNotPruneDiscussion');
const ignoreThreads = settings.get('RetentionPolicy_DoNotPruneThreads');
const filesOnly = settings.get<boolean>('RetentionPolicy_FilesOnly');
const excludePinned = settings.get<boolean>('RetentionPolicy_DoNotPrunePinned');
const ignoreDiscussion = settings.get<boolean>('RetentionPolicy_DoNotPruneDiscussion');
const ignoreThreads = settings.get<boolean>('RetentionPolicy_DoNotPruneThreads');

// get all rooms with default values
types.forEach((type) => {
Expand All @@ -35,7 +40,7 @@ function job() {
'retention.overrideGlobal': { $ne: true },
},
{ fields: { _id: 1 } },
).forEach(({ _id: rid }) => {
).forEach(({ _id: rid }: IRoomWithRetentionPolicy) => {
cleanRoomHistory({
rid,
latest,
Expand All @@ -52,7 +57,7 @@ function job() {
'retention.enabled': { $eq: true },
'retention.overrideGlobal': { $eq: true },
'retention.maxAge': { $gte: 0 },
}).forEach((room) => {
}).forEach((room: IRoomWithRetentionPolicy) => {
const { maxAge = 30, filesOnly, excludePinned, ignoreThreads } = room.retention;
const latest = new Date(now.getTime() - toDays(maxAge));
cleanRoomHistory({
Expand All @@ -67,7 +72,7 @@ function job() {
});
}

function getSchedule(precision) {
function getSchedule(precision: '0' | '1' | '2' | '3'): string {
switch (precision) {
case '0':
return '*/30 * * * *'; // 30 minutes
Expand All @@ -82,8 +87,8 @@ function getSchedule(precision) {

const pruneCronName = 'Prune old messages by retention policy';

function deployCron(precision) {
const schedule = (parser) => parser.cron(precision);
function deployCron(precision: string): void {
const schedule = (parser: IParser): string => parser.cron(precision);

SyncedCron.remove(pruneCronName);
SyncedCron.add({
Expand Down Expand Up @@ -129,7 +134,7 @@ settings.watchMultiple(
maxTimes.d = settings.get('RetentionPolicy_MaxAge_DMs');

const precision =
(settings.get('RetentionPolicy_Advanced_Precision') && settings.get('RetentionPolicy_Advanced_Precision_Cron')) ||
(settings.get<boolean>('RetentionPolicy_Advanced_Precision') && settings.get<string>('RetentionPolicy_Advanced_Precision_Cron')) ||
getSchedule(settings.get('RetentionPolicy_Precision'));

return deployCron(precision);
Expand Down
9 changes: 9 additions & 0 deletions packages/core-typings/src/IRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,12 @@ export type RoomAdminFieldsType =
| 'broadcast'
| 'uids'
| 'avatarETag';

export interface IRoomWithRetentionPolicy extends IRoom {
retention: {
maxAge: number;
filesOnly: boolean;
excludePinned: boolean;
ignoreThreads: boolean;
};
}

0 comments on commit 43303bc

Please sign in to comment.