Files
wekan/server/autoArchiveCards.js
T
Lauri Ojansivu 22c62b7b39 Archive cards automatically after a chosen number of days without activity.
Board Settings gains Nextcloud Deck's auto-archive: a board admin sets a
number of days from 1 to 3650, and an hourly job archives the board's cards
whose last activity is older than that, with their subtasks and an
archivedCard activity attributed to the board creator, as scheduled rules
are. Empty means off, templates are never archived, and each run takes at
most 200 cards per board. The same date drives the card-aging fade, so a
card fades before it goes. New strings are English, pending Transifex.

Thanks to xet7 !
2026-09-29 16:40:48 +03:00

73 lines
2.5 KiB
JavaScript

// Nextcloud Deck-style auto-archive (models/lib/autoArchive.js): every hour,
// archive the cards of boards with "Archive cards after N days without
// activity" set that nobody has touched for that long. Same quave:synced-cron
// infrastructure as server/checklistResetSchedule.js.
import { Meteor } from 'meteor/meteor';
import { DDP } from 'meteor/ddp';
import Boards from '/models/boards';
import Cards from '/models/cards';
import { SyncedCron } from '/server/cron/syncedCron';
const {
AUTO_ARCHIVE_BATCH, AUTO_ARCHIVE_BOARD_SELECTOR, normalizeAutoArchiveDays, autoArchiveCardSelector,
} = require('/models/lib/autoArchive');
// The archive is recorded as the board creator's, the same attribution the
// scheduled rules (server/scheduledRules.js) use for what the board itself
// does on a timer.
async function asUser(userId, fn) {
if (userId && typeof DDP._CurrentMethodInvocation?.withValue === 'function') {
return DDP._CurrentMethodInvocation.withValue({ userId }, fn);
}
return fn();
}
export async function autoArchiveBoard(board, now = new Date()) {
const days = normalizeAutoArchiveDays(board.autoArchiveInactiveDays);
if (!days) return 0;
const cards = await Cards.find(autoArchiveCardSelector(board._id, days, now), {
sort: { dateLastActivity: 1 }, limit: AUTO_ARCHIVE_BATCH,
}).fetchAsync();
let archived = 0;
for (const card of cards) {
try {
// card.archive() also archives its subtasks and records archivedCard.
// eslint-disable-next-line no-await-in-loop
await asUser(board.createdBy, () => card.archive());
archived += 1;
} catch (e) {
// One card that cannot be archived must not stop the rest.
// eslint-disable-next-line no-console
console.error('autoArchiveCards: could not archive card', card._id, e.message);
}
}
return archived;
}
export async function scanAutoArchive(now = new Date()) {
const boards = await Boards.find(AUTO_ARCHIVE_BOARD_SELECTOR, {
fields: { _id: 1, autoArchiveInactiveDays: 1, createdBy: 1 },
}).fetchAsync();
for (const board of boards) {
// eslint-disable-next-line no-await-in-loop
await autoArchiveBoard(board, now);
}
}
Meteor.startup(() => {
try {
SyncedCron.add({
name: 'wekan-card-auto-archive',
schedule(parser) {
return parser.text('every 1 hour');
},
job() {
return scanAutoArchive(new Date());
},
});
} catch (e) {
// eslint-disable-next-line no-console
console.error('autoArchiveCards: failed to register cron job', e);
}
});