do not COPY if QUOTA is full

This commit is contained in:
Andris Reinman 2020-09-08 10:00:17 +03:00
parent 977c36a0d5
commit c1abce1363
2 changed files with 237 additions and 216 deletions

View file

@ -88,9 +88,9 @@ module.exports = {
TEMP_PASS_WINDOW: 24 * 3600 * 1000,
// mongdb query TTL limits
DB_MAX_TIME_USERS: 1 * 1000,
DB_MAX_TIME_MAILBOXES: 800,
DB_MAX_TIME_MESSAGES: 60 * 1000,
DB_MAX_TIME_USERS: 3 * 1000,
DB_MAX_TIME_MAILBOXES: 3 * 1000,
DB_MAX_TIME_MESSAGES: 2 * 60 * 1000,
// what is the max username part after wildcard
MAX_ALLOWED_WILDCARD_LENGTH: 32,

View file

@ -18,250 +18,271 @@ module.exports = (server, messageHandler) => (mailbox, update, session, callback
update.destination
);
db.database.collection('mailboxes').findOne(
db.users.collection('users').findOne(
{
_id: mailbox
_id: session.user.id
},
{
maxTimeMS: consts.DB_MAX_TIME_MAILBOXES
maxTimeMS: consts.DB_MAX_TIME_USERS
},
(err, mailboxData) => {
(err, userData) => {
if (err) {
return callback(err);
}
if (!mailboxData) {
return callback(null, 'NONEXISTENT');
if (!userData) {
return callback(new Error('User not found'));
}
if (userData.quota && userData.storageUsed > userData.quota) {
return callback(false, 'OVERQUOTA');
}
db.database.collection('mailboxes').findOne(
{
user: session.user.id,
path: update.destination
_id: mailbox
},
{
maxTimeMS: consts.DB_MAX_TIME_MAILBOXES
},
(err, targetData) => {
(err, mailboxData) => {
if (err) {
return callback(err);
}
if (!targetData) {
return callback(null, 'TRYCREATE');
if (!mailboxData) {
return callback(null, 'NONEXISTENT');
}
let cursor = db.database
.collection('messages')
.find({
mailbox: mailboxData._id,
uid: tools.checkRangeQuery(update.messages)
}) // no projection as we need to copy the entire message
.sort({ uid: 1 })
.maxTimeMS(consts.DB_MAX_TIME_MESSAGES);
let copiedMessages = 0;
let copiedStorage = 0;
let updateQuota = next => {
if (!copiedMessages) {
return next();
}
db.users.collection('users').findOneAndUpdate(
{
_id: mailboxData.user
},
{
$inc: {
storageUsed: copiedStorage
}
},
{
returnOriginal: false,
projection: {
storageUsed: true
},
maxTimeMS: consts.DB_MAX_TIME_USERS
},
(...args) => {
let r = args && args[1];
if (r && r.value) {
server.loggelf({
short_message: '[QUOTA] +',
_mail_action: 'quota',
_user: mailboxData.user,
_inc: copiedStorage,
_copied_messages: copiedMessages,
_storage_used: r.value.storageUsed,
_mailbox: targetData._id,
_sess: session && session.id
});
}
next();
}
);
};
let sourceUid = [];
let destinationUid = [];
let processNext = () => {
cursor.next((err, messageData) => {
db.database.collection('mailboxes').findOne(
{
user: session.user.id,
path: update.destination
},
{
maxTimeMS: consts.DB_MAX_TIME_MAILBOXES
},
(err, targetData) => {
if (err) {
return updateQuota(() => callback(err));
return callback(err);
}
if (!targetData) {
return callback(null, 'TRYCREATE');
}
if (!messageData) {
return cursor.close(() => {
updateQuota(() => {
server.notifier.fire(session.user.id, targetData.path);
return callback(null, true, {
uidValidity: targetData.uidValidity,
sourceUid,
destinationUid
});
});
});
}
let cursor = db.database
.collection('messages')
.find({
mailbox: mailboxData._id,
uid: tools.checkRangeQuery(update.messages)
}) // no projection as we need to copy the entire message
.sort({ uid: 1 })
.maxTimeMS(consts.DB_MAX_TIME_MESSAGES);
// this query points to current message
let existingQuery = {
mailbox: messageData.mailbox,
uid: messageData.uid,
_id: messageData._id
};
let copiedMessages = 0;
let copiedStorage = 0;
// Copying is not done in bulk to minimize risk of going out of sync with incremental UIDs
sourceUid.unshift(messageData.uid);
db.database.collection('mailboxes').findOneAndUpdate(
{
_id: targetData._id
},
{
$inc: {
uidNext: 1
}
},
{
projection: {
uidNext: true,
modifyIndex: true
let updateQuota = next => {
if (!copiedMessages) {
return next();
}
db.users.collection('users').findOneAndUpdate(
{
_id: mailboxData.user
},
returnOriginal: true,
maxTimeMS: consts.DB_MAX_TIME_MAILBOXES
},
(err, item) => {
if (err) {
return cursor.close(() => {
updateQuota(() => callback(err));
});
}
if (!item || !item.value) {
// mailbox not found
return cursor.close(() => {
updateQuota(() => callback(null, 'TRYCREATE'));
});
}
let uidNext = item.value.uidNext;
let modifyIndex = item.value.modifyIndex;
destinationUid.unshift(uidNext);
messageData._id = new ObjectID();
messageData.mailbox = targetData._id;
messageData.uid = uidNext;
// retention settings
messageData.exp = !!targetData.retention;
messageData.rdate = Date.now() + (targetData.retention || 0);
messageData.modseq = modifyIndex; // reset message modseq to whatever it is for the mailbox right now
messageData.searchable = true;
let junk = false;
if (targetData.specialUse === '\\Junk' && !messageData.junk) {
messageData.junk = true;
junk = 1;
} else if (targetData.specialUse !== '\\Trash' && messageData.junk) {
delete messageData.junk;
junk = -1;
}
if (!messageData.meta) {
messageData.meta = {};
}
if (!messageData.meta.events) {
messageData.meta.events = [];
}
messageData.meta.events.push({
action: 'IMAPCOPY',
time: new Date()
});
db.database.collection('messages').updateOne(
existingQuery,
{
$set: {
// indicate that we do not need to archive this message when deleted
copied: true
}
{
$inc: {
storageUsed: copiedStorage
}
},
{
returnOriginal: false,
projection: {
storageUsed: true
},
{ w: 'majority' },
() => {
db.database.collection('messages').insertOne(messageData, { w: 'majority' }, (err, r) => {
if (err) {
return cursor.close(() => {
updateQuota(() => callback(err));
});
}
maxTimeMS: consts.DB_MAX_TIME_USERS
},
(...args) => {
let r = args && args[1];
if (!r || !r.insertedCount) {
return processNext();
}
copiedMessages++;
copiedStorage += Number(messageData.size) || 0;
let attachmentIds = Object.keys(messageData.mimeTree.attachmentMap || {}).map(
key => messageData.mimeTree.attachmentMap[key]
);
if (!attachmentIds.length) {
let entry = {
command: 'EXISTS',
uid: messageData.uid,
message: messageData._id,
unseen: messageData.unseen
};
if (junk) {
entry.junk = junk;
}
return server.notifier.addEntries(targetData, entry, processNext);
}
messageHandler.attachmentStorage.updateMany(attachmentIds, 1, messageData.magic, err => {
if (err) {
// should we care about this error?
}
let entry = {
command: 'EXISTS',
uid: messageData.uid,
message: messageData._id,
unseen: messageData.unseen
};
if (junk) {
entry.junk = junk;
}
server.notifier.addEntries(targetData, entry, processNext);
});
if (r && r.value) {
server.loggelf({
short_message: '[QUOTA] +',
_mail_action: 'quota',
_user: mailboxData.user,
_inc: copiedStorage,
_copied_messages: copiedMessages,
_storage_used: r.value.storageUsed,
_mailbox: targetData._id,
_sess: session && session.id
});
}
next();
}
);
};
let sourceUid = [];
let destinationUid = [];
let processNext = () => {
cursor.next((err, messageData) => {
if (err) {
return updateQuota(() => callback(err));
}
if (!messageData) {
return cursor.close(() => {
updateQuota(() => {
server.notifier.fire(session.user.id, targetData.path);
return callback(null, true, {
uidValidity: targetData.uidValidity,
sourceUid,
destinationUid
});
});
});
}
// this query points to current message
let existingQuery = {
mailbox: messageData.mailbox,
uid: messageData.uid,
_id: messageData._id
};
// Copying is not done in bulk to minimize risk of going out of sync with incremental UIDs
sourceUid.unshift(messageData.uid);
db.database.collection('mailboxes').findOneAndUpdate(
{
_id: targetData._id
},
{
$inc: {
uidNext: 1
}
},
{
projection: {
uidNext: true,
modifyIndex: true
},
returnOriginal: true,
maxTimeMS: consts.DB_MAX_TIME_MAILBOXES
},
(err, item) => {
if (err) {
return cursor.close(() => {
updateQuota(() => callback(err));
});
}
if (!item || !item.value) {
// mailbox not found
return cursor.close(() => {
updateQuota(() => callback(null, 'TRYCREATE'));
});
}
let uidNext = item.value.uidNext;
let modifyIndex = item.value.modifyIndex;
destinationUid.unshift(uidNext);
messageData._id = new ObjectID();
messageData.mailbox = targetData._id;
messageData.uid = uidNext;
// retention settings
messageData.exp = !!targetData.retention;
messageData.rdate = Date.now() + (targetData.retention || 0);
messageData.modseq = modifyIndex; // reset message modseq to whatever it is for the mailbox right now
messageData.searchable = true;
let junk = false;
if (targetData.specialUse === '\\Junk' && !messageData.junk) {
messageData.junk = true;
junk = 1;
} else if (targetData.specialUse !== '\\Trash' && messageData.junk) {
delete messageData.junk;
junk = -1;
}
if (!messageData.meta) {
messageData.meta = {};
}
if (!messageData.meta.events) {
messageData.meta.events = [];
}
messageData.meta.events.push({
action: 'IMAPCOPY',
time: new Date()
});
db.database.collection('messages').updateOne(
existingQuery,
{
$set: {
// indicate that we do not need to archive this message when deleted
copied: true
}
},
{ w: 'majority' },
() => {
db.database.collection('messages').insertOne(messageData, { w: 'majority' }, (err, r) => {
if (err) {
return cursor.close(() => {
updateQuota(() => callback(err));
});
}
if (!r || !r.insertedCount) {
return processNext();
}
copiedMessages++;
copiedStorage += Number(messageData.size) || 0;
let attachmentIds = Object.keys(messageData.mimeTree.attachmentMap || {}).map(
key => messageData.mimeTree.attachmentMap[key]
);
if (!attachmentIds.length) {
let entry = {
command: 'EXISTS',
uid: messageData.uid,
message: messageData._id,
unseen: messageData.unseen
};
if (junk) {
entry.junk = junk;
}
return server.notifier.addEntries(targetData, entry, processNext);
}
messageHandler.attachmentStorage.updateMany(attachmentIds, 1, messageData.magic, err => {
if (err) {
// should we care about this error?
}
let entry = {
command: 'EXISTS',
uid: messageData.uid,
message: messageData._id,
unseen: messageData.unseen
};
if (junk) {
entry.junk = junk;
}
server.notifier.addEntries(targetData, entry, processNext);
});
});
}
);
}
);
}
);
});
};
processNext();
});
};
processNext();
}
);
}
);
}