2017-07-30 23:07:35 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const Joi = require('joi');
|
|
|
|
const ObjectID = require('mongodb').ObjectID;
|
2018-10-12 16:13:54 +08:00
|
|
|
const tools = require('../tools');
|
|
|
|
const roles = require('../roles');
|
2020-07-20 01:51:06 +08:00
|
|
|
const { sessSchema, sessIPSchema, booleanSchema } = require('../schemas');
|
2020-10-09 16:08:33 +08:00
|
|
|
const { publish, AUTOREPLY_USER_DISABLED, AUTOREPLY_USER_ENABLED } = require('../events');
|
2017-07-30 23:07:35 +08:00
|
|
|
|
|
|
|
module.exports = (db, server) => {
|
2018-10-12 16:13:54 +08:00
|
|
|
server.put(
|
|
|
|
'/users/:user/autoreply',
|
|
|
|
tools.asyncifyJson(async (req, res, next) => {
|
|
|
|
res.charSet('utf-8');
|
|
|
|
|
|
|
|
const schema = Joi.object().keys({
|
2020-07-16 16:31:58 +08:00
|
|
|
user: Joi.string().hex().lowercase().length(24).required(),
|
2020-07-31 18:36:07 +08:00
|
|
|
status: booleanSchema,
|
|
|
|
name: Joi.string().allow('').trim().max(128),
|
|
|
|
subject: Joi.string().allow('').trim().max(128),
|
2018-10-12 16:13:54 +08:00
|
|
|
text: Joi.string()
|
2020-07-31 18:36:07 +08:00
|
|
|
.allow('')
|
2018-10-12 16:13:54 +08:00
|
|
|
.trim()
|
|
|
|
.max(128 * 1024),
|
|
|
|
html: Joi.string()
|
2020-07-31 18:36:07 +08:00
|
|
|
.allow('')
|
2018-10-12 16:13:54 +08:00
|
|
|
.trim()
|
|
|
|
.max(128 * 1024),
|
2020-07-16 16:31:58 +08:00
|
|
|
start: Joi.date().empty('').allow(false),
|
|
|
|
end: Joi.date().empty('').allow(false),
|
2020-07-20 01:51:06 +08:00
|
|
|
sess: sessSchema,
|
|
|
|
ip: sessIPSchema
|
2017-07-30 23:07:35 +08:00
|
|
|
});
|
|
|
|
|
2020-07-20 01:51:06 +08:00
|
|
|
const result = schema.validate(req.params, {
|
2018-10-12 16:13:54 +08:00
|
|
|
abortEarly: false,
|
|
|
|
convert: true
|
|
|
|
});
|
2018-01-24 17:29:12 +08:00
|
|
|
|
2018-10-12 16:13:54 +08:00
|
|
|
if (result.error) {
|
2019-07-31 21:05:59 +08:00
|
|
|
res.status(400);
|
2018-10-12 16:13:54 +08:00
|
|
|
res.json({
|
|
|
|
error: result.error.message,
|
2020-07-16 16:31:58 +08:00
|
|
|
code: 'InputValidationError',
|
|
|
|
details: tools.validationErrors(result)
|
2018-10-12 16:13:54 +08:00
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
2017-07-30 23:07:35 +08:00
|
|
|
|
2018-10-12 16:13:54 +08:00
|
|
|
// permissions check
|
|
|
|
if (req.user && req.user === result.value.user) {
|
|
|
|
req.validate(roles.can(req.role).updateOwn('autoreplies'));
|
|
|
|
} else {
|
|
|
|
req.validate(roles.can(req.role).updateAny('autoreplies'));
|
|
|
|
}
|
|
|
|
|
2020-07-31 18:36:07 +08:00
|
|
|
let user = new ObjectID(result.value.user);
|
|
|
|
result.value.user = user;
|
2017-11-15 21:59:37 +08:00
|
|
|
|
2020-07-31 18:36:07 +08:00
|
|
|
if (typeof result.value.status === 'boolean') {
|
|
|
|
const r = await db.users.collection('users').updateOne({ _id: user }, { $set: { autoreply: result.value.status } });
|
|
|
|
if (!r.matchedCount) {
|
2021-05-20 19:47:20 +08:00
|
|
|
res.status(404);
|
2020-07-31 18:36:07 +08:00
|
|
|
res.json({
|
|
|
|
error: 'Unknown user',
|
|
|
|
code: 'UserNotFound'
|
|
|
|
});
|
|
|
|
return next();
|
2018-10-12 16:13:54 +08:00
|
|
|
}
|
2020-10-09 16:08:33 +08:00
|
|
|
if (r.modifiedCount) {
|
|
|
|
await publish(db.redis, {
|
|
|
|
ev: result.value.status ? AUTOREPLY_USER_ENABLED : AUTOREPLY_USER_DISABLED,
|
|
|
|
user
|
|
|
|
});
|
|
|
|
}
|
2020-07-31 18:36:07 +08:00
|
|
|
} else {
|
|
|
|
const userData = await db.users.collection('users').findOne({ _id: user }, { projection: { _id: true, autoreply: true } });
|
|
|
|
if (!userData) {
|
2021-05-20 19:47:20 +08:00
|
|
|
res.status(404);
|
2020-07-31 18:36:07 +08:00
|
|
|
res.json({
|
|
|
|
error: 'Unknown user',
|
|
|
|
code: 'UserNotFound'
|
|
|
|
});
|
|
|
|
return next();
|
2018-10-12 16:13:54 +08:00
|
|
|
}
|
2017-07-30 23:07:35 +08:00
|
|
|
}
|
2018-10-12 16:13:54 +08:00
|
|
|
|
2020-07-31 18:36:07 +08:00
|
|
|
const r = await db.database.collection('autoreplies').updateOne({ user }, { $set: result.value }, { upsert: true });
|
2017-07-30 23:07:35 +08:00
|
|
|
|
2018-10-12 16:13:54 +08:00
|
|
|
res.json({
|
|
|
|
success: true,
|
2021-01-07 15:41:48 +08:00
|
|
|
id: ((r.upsertedId && r.upsertedId._id) || '').toString()
|
2017-07-30 23:07:35 +08:00
|
|
|
});
|
2018-10-12 16:13:54 +08:00
|
|
|
|
|
|
|
return next();
|
|
|
|
})
|
|
|
|
);
|
2017-07-30 23:07:35 +08:00
|
|
|
|
2018-10-12 16:13:54 +08:00
|
|
|
server.get(
|
|
|
|
'/users/:user/autoreply',
|
|
|
|
tools.asyncifyJson(async (req, res, next) => {
|
|
|
|
res.charSet('utf-8');
|
|
|
|
|
|
|
|
const schema = Joi.object().keys({
|
2020-07-16 16:31:58 +08:00
|
|
|
user: Joi.string().hex().lowercase().length(24).required(),
|
2020-07-20 01:51:06 +08:00
|
|
|
sess: sessSchema,
|
|
|
|
ip: sessIPSchema
|
2017-07-30 23:07:35 +08:00
|
|
|
});
|
|
|
|
|
2020-07-20 01:51:06 +08:00
|
|
|
const result = schema.validate(req.params, {
|
2018-10-12 16:13:54 +08:00
|
|
|
abortEarly: false,
|
|
|
|
convert: true
|
|
|
|
});
|
2017-07-30 23:07:35 +08:00
|
|
|
|
2018-10-12 16:13:54 +08:00
|
|
|
if (result.error) {
|
2019-07-31 21:05:59 +08:00
|
|
|
res.status(400);
|
2017-07-30 23:07:35 +08:00
|
|
|
res.json({
|
2018-10-12 16:13:54 +08:00
|
|
|
error: result.error.message,
|
2020-07-16 16:31:58 +08:00
|
|
|
code: 'InputValidationError',
|
|
|
|
details: tools.validationErrors(result)
|
2017-07-30 23:07:35 +08:00
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2018-10-12 16:13:54 +08:00
|
|
|
// permissions check
|
|
|
|
if (req.user && req.user === result.value.user) {
|
|
|
|
req.validate(roles.can(req.role).readOwn('autoreplies'));
|
|
|
|
} else {
|
|
|
|
req.validate(roles.can(req.role).readAny('autoreplies'));
|
|
|
|
}
|
|
|
|
|
|
|
|
let user = new ObjectID(result.value.user);
|
|
|
|
|
|
|
|
let entry = await db.database.collection('autoreplies').findOne({ user });
|
|
|
|
|
2017-07-30 23:07:35 +08:00
|
|
|
entry = entry || {};
|
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
status: !!entry.status,
|
2018-01-24 17:29:12 +08:00
|
|
|
name: entry.name || '',
|
2017-07-30 23:07:35 +08:00
|
|
|
subject: entry.subject || '',
|
2017-11-15 21:59:37 +08:00
|
|
|
text: entry.text || '',
|
|
|
|
html: entry.html || '',
|
2018-10-12 16:13:54 +08:00
|
|
|
start: entry.start || false,
|
|
|
|
end: entry.end || false
|
2017-07-30 23:07:35 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return next();
|
2018-10-12 16:13:54 +08:00
|
|
|
})
|
|
|
|
);
|
2017-07-30 23:07:35 +08:00
|
|
|
|
2018-10-12 16:13:54 +08:00
|
|
|
server.del(
|
|
|
|
'/users/:user/autoreply',
|
|
|
|
tools.asyncifyJson(async (req, res, next) => {
|
|
|
|
res.charSet('utf-8');
|
|
|
|
|
|
|
|
const schema = Joi.object().keys({
|
2020-07-16 16:31:58 +08:00
|
|
|
user: Joi.string().hex().lowercase().length(24).required(),
|
2020-07-20 01:51:06 +08:00
|
|
|
sess: sessSchema,
|
|
|
|
ip: sessIPSchema
|
2017-07-30 23:07:35 +08:00
|
|
|
});
|
|
|
|
|
2020-07-20 01:51:06 +08:00
|
|
|
const result = schema.validate(req.params, {
|
2018-10-12 16:13:54 +08:00
|
|
|
abortEarly: false,
|
|
|
|
convert: true
|
|
|
|
});
|
2017-07-30 23:07:35 +08:00
|
|
|
|
2018-10-12 16:13:54 +08:00
|
|
|
if (result.error) {
|
2019-07-31 21:05:59 +08:00
|
|
|
res.status(400);
|
2017-07-30 23:07:35 +08:00
|
|
|
res.json({
|
2018-10-12 16:13:54 +08:00
|
|
|
error: result.error.message,
|
2020-07-16 16:31:58 +08:00
|
|
|
code: 'InputValidationError',
|
|
|
|
details: tools.validationErrors(result)
|
2017-07-30 23:07:35 +08:00
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2018-10-12 16:13:54 +08:00
|
|
|
// permissions check
|
|
|
|
if (req.user && req.user === result.value.user) {
|
|
|
|
req.validate(roles.can(req.role).deleteOwn('autoreplies'));
|
|
|
|
} else {
|
|
|
|
req.validate(roles.can(req.role).deleteAny('autoreplies'));
|
|
|
|
}
|
2017-07-30 23:07:35 +08:00
|
|
|
|
2018-10-12 16:13:54 +08:00
|
|
|
let user = new ObjectID(result.value.user);
|
2017-07-30 23:07:35 +08:00
|
|
|
|
2020-10-09 16:08:33 +08:00
|
|
|
let r = await db.users.collection('users').updateOne({ _id: user }, { $set: { autoreply: false } });
|
|
|
|
if (r.modifiedCount) {
|
|
|
|
await publish(db.redis, {
|
|
|
|
ev: AUTOREPLY_USER_DISABLED,
|
|
|
|
user
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-12 16:13:54 +08:00
|
|
|
await db.database.collection('autoreplies').deleteOne({ user });
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
success: true
|
2017-07-30 23:07:35 +08:00
|
|
|
});
|
2018-10-12 16:13:54 +08:00
|
|
|
|
|
|
|
return next();
|
|
|
|
})
|
|
|
|
);
|
2017-07-30 23:07:35 +08:00
|
|
|
};
|