mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
The scheduled notification-tray cleanup logged `Exception in removed observeChanges callback: Error: Removed nonexistent document …`. The crash itself came from the old `cottz:publish-relations` package (since replaced by `reywood:publish-composite`), but the cleanup that provoked it still fired one **un-awaited** `removeNotification()` per expired notification — a separate `Users.update` `$pull` each time — so a user with K stale notifications produced K writes to the Users collection, and every publication that republishes user documents re-ran its observers K times in quick bursts (the churn that surfaced the removed-document error), while any rejected write went unhandled. The cleanup now scans only users that have notifications and prunes each user's stale notifications in a single awaited `$pull … $in`. It also removes an activity's notifications only when *every* entry for that activity is read and past its removal age (so a freshly re-created unread notification sharing an activity id is not dropped), and guards missing/invalid `read` timestamps. Covered by `tests/notificationCleanup.test.cjs`. Thanks to xet7. Fixes #5685
131 lines
4.2 KiB
JavaScript
131 lines
4.2 KiB
JavaScript
'use strict';
|
|
|
|
// Plain-Node unit test (no Meteor) for expiredNotificationActivityIds().
|
|
// Run: node tests/notificationCleanup.test.cjs
|
|
//
|
|
// Regression guard for #5685 ("Exception in removed observeChanges callback:
|
|
// Error: Removed nonexistent document" during notification_cleanup). The cleanup
|
|
// now computes, per user, the set of activity ids whose notifications are all
|
|
// read and past the removal age, then prunes them in a single `$pull … $in`.
|
|
// This helper is that decision: it must select fully-expired activities, skip
|
|
// unread / not-yet-aged / invalid-date / re-created ones, and de-duplicate.
|
|
|
|
const assert = require('assert');
|
|
const {
|
|
expiredNotificationActivityIds,
|
|
} = require('../models/lib/notificationCleanup');
|
|
|
|
let passed = 0;
|
|
function test(name, fn) {
|
|
fn();
|
|
passed += 1;
|
|
console.log(' ok -', name);
|
|
}
|
|
|
|
const NOW = new Date('2025-02-12T12:00:00Z');
|
|
const daysAgo = n => new Date(NOW.getTime() - n * 24 * 60 * 60 * 1000);
|
|
const REMOVE_AGE = 2; // days after "read" before a notification is removable
|
|
|
|
// --- POSITIVE: read + aged past the window is removable ---------------------
|
|
test('a read notification older than the removal age is removed', () => {
|
|
const ids = expiredNotificationActivityIds(
|
|
[{ activity: 'a1', read: daysAgo(3) }],
|
|
REMOVE_AGE,
|
|
NOW,
|
|
);
|
|
assert.deepStrictEqual(ids, ['a1']);
|
|
});
|
|
|
|
test('multiple expired activities are all returned, de-duplicated', () => {
|
|
const ids = expiredNotificationActivityIds(
|
|
[
|
|
{ activity: 'a1', read: daysAgo(5) },
|
|
{ activity: 'a2', read: daysAgo(10) },
|
|
{ activity: 'a1', read: daysAgo(4) }, // same activity, also expired
|
|
],
|
|
REMOVE_AGE,
|
|
NOW,
|
|
);
|
|
assert.deepStrictEqual(ids.sort(), ['a1', 'a2']);
|
|
// de-duplicated: 'a1' appears once
|
|
assert.strictEqual(ids.filter(x => x === 'a1').length, 1);
|
|
});
|
|
|
|
test('exactly at the removal boundary is removable (<=)', () => {
|
|
// read 2 days ago, removeAge 2 -> removeDate == now -> removable
|
|
const ids = expiredNotificationActivityIds(
|
|
[{ activity: 'edge', read: daysAgo(2) }],
|
|
REMOVE_AGE,
|
|
NOW,
|
|
);
|
|
assert.deepStrictEqual(ids, ['edge']);
|
|
});
|
|
|
|
// --- NEGATIVE: things that must NOT be removed ------------------------------
|
|
test('an unread notification (read=null) is never removed', () => {
|
|
const ids = expiredNotificationActivityIds(
|
|
[{ activity: 'a1', read: null }],
|
|
REMOVE_AGE,
|
|
NOW,
|
|
);
|
|
assert.deepStrictEqual(ids, []);
|
|
});
|
|
|
|
test('a read notification still within the age window is kept', () => {
|
|
const ids = expiredNotificationActivityIds(
|
|
[{ activity: 'a1', read: daysAgo(1) }], // 1 day < 2 day window
|
|
REMOVE_AGE,
|
|
NOW,
|
|
);
|
|
assert.deepStrictEqual(ids, []);
|
|
});
|
|
|
|
test('#5685: an activity with a re-created unread entry is NOT removed', () => {
|
|
// Same activity: one read+expired entry, one fresh unread entry. Pulling by
|
|
// activity id would wrongly drop the unread one, so the activity is kept.
|
|
const ids = expiredNotificationActivityIds(
|
|
[
|
|
{ activity: 'a1', read: daysAgo(9) }, // old, expired
|
|
{ activity: 'a1', read: null }, // re-notified, unread
|
|
],
|
|
REMOVE_AGE,
|
|
NOW,
|
|
);
|
|
assert.deepStrictEqual(ids, []);
|
|
});
|
|
|
|
test('a notification with an invalid read date is not removed', () => {
|
|
const ids = expiredNotificationActivityIds(
|
|
[{ activity: 'a1', read: 'not-a-date' }],
|
|
REMOVE_AGE,
|
|
NOW,
|
|
);
|
|
assert.deepStrictEqual(ids, []);
|
|
});
|
|
|
|
test('entries without an activity id are ignored', () => {
|
|
const ids = expiredNotificationActivityIds(
|
|
[{ read: daysAgo(9) }, null, { activity: '', read: daysAgo(9) }],
|
|
REMOVE_AGE,
|
|
NOW,
|
|
);
|
|
assert.deepStrictEqual(ids, []);
|
|
});
|
|
|
|
test('non-array / empty input yields an empty list without throwing', () => {
|
|
assert.deepStrictEqual(expiredNotificationActivityIds(undefined, REMOVE_AGE, NOW), []);
|
|
assert.deepStrictEqual(expiredNotificationActivityIds(null, REMOVE_AGE, NOW), []);
|
|
assert.deepStrictEqual(expiredNotificationActivityIds([], REMOVE_AGE, NOW), []);
|
|
});
|
|
|
|
test('an invalid `now` is handled defensively (no removals)', () => {
|
|
const ids = expiredNotificationActivityIds(
|
|
[{ activity: 'a1', read: daysAgo(9) }],
|
|
REMOVE_AGE,
|
|
new Date('nonsense'),
|
|
);
|
|
assert.deepStrictEqual(ids, []);
|
|
});
|
|
|
|
console.log(`\n${passed} tests passed`);
|