2017-07-16 00:08:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const db = require('../db');
|
2019-01-26 18:50:51 +08:00
|
|
|
const consts = require('../consts');
|
2017-07-16 00:08:33 +08:00
|
|
|
|
|
|
|
// SUBSCRIBE "path/to/mailbox"
|
|
|
|
module.exports = server => (path, session, callback) => {
|
|
|
|
server.logger.debug(
|
|
|
|
{
|
|
|
|
tnx: 'subscribe',
|
|
|
|
cid: session.id
|
|
|
|
},
|
|
|
|
'[%s] SUBSCRIBE to "%s"',
|
|
|
|
session.id,
|
|
|
|
path
|
|
|
|
);
|
2017-12-10 07:19:50 +08:00
|
|
|
db.database.collection('mailboxes').findOneAndUpdate(
|
|
|
|
{
|
|
|
|
user: session.user.id,
|
|
|
|
path
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
subscribed: true
|
|
|
|
}
|
|
|
|
},
|
2019-01-24 05:38:29 +08:00
|
|
|
{
|
2019-01-26 18:50:51 +08:00
|
|
|
maxTimeMS: consts.DB_MAX_TIME_MAILBOXES
|
2019-01-24 05:38:29 +08:00
|
|
|
},
|
2017-12-10 07:19:50 +08:00
|
|
|
(err, item) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2017-07-16 00:08:33 +08:00
|
|
|
|
2017-12-10 07:19:50 +08:00
|
|
|
if (!item || !item.value) {
|
|
|
|
// was not able to acquire a lock
|
|
|
|
return callback(null, 'NONEXISTENT');
|
|
|
|
}
|
2017-07-16 00:08:33 +08:00
|
|
|
|
2017-12-10 07:19:50 +08:00
|
|
|
callback(null, true);
|
|
|
|
}
|
|
|
|
);
|
2017-07-16 00:08:33 +08:00
|
|
|
};
|