2017-07-03 17:48:27 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Module dependencies
|
|
|
|
|
*/
|
|
|
|
|
var path = require('path'),
|
|
|
|
|
config = require(path.resolve('./config/config')),
|
|
|
|
|
mongoose = require('mongoose'),
|
|
|
|
|
errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller')),
|
2017-07-14 12:29:57 +08:00
|
|
|
multer = require('multer'),
|
|
|
|
|
fs = require('fs'),
|
2017-07-12 10:10:34 +08:00
|
|
|
moment = require('moment'),
|
2017-07-03 17:48:27 +08:00
|
|
|
User = mongoose.model('User'),
|
|
|
|
|
Forum = mongoose.model('Forum'),
|
|
|
|
|
Topic = mongoose.model('Topic'),
|
2017-07-09 16:29:35 +08:00
|
|
|
Reply = mongoose.model('Reply'),
|
2017-07-12 12:12:13 +08:00
|
|
|
Thumb = mongoose.model('Thumb'),
|
2017-07-08 20:22:38 +08:00
|
|
|
async = require('async'),
|
|
|
|
|
traceLogCreate = require(path.resolve('./config/lib/tracelog')).create;
|
|
|
|
|
|
|
|
|
|
var traceConfig = config.meanTorrentConfig.trace;
|
2017-07-21 15:06:13 +08:00
|
|
|
var thumbsUpScore = config.meanTorrentConfig.score.thumbsUpScore;
|
2017-12-16 20:05:56 +08:00
|
|
|
var serverMessage = require(path.resolve('./config/lib/server-message'));
|
|
|
|
|
var serverNoticeConfig = config.meanTorrentConfig.serverNotice;
|
2017-07-03 17:48:27 +08:00
|
|
|
|
2017-09-13 10:51:52 +08:00
|
|
|
var mtDebug = require(path.resolve('./config/lib/debug'));
|
|
|
|
|
|
2017-07-03 17:48:27 +08:00
|
|
|
/**
|
|
|
|
|
* list forums
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.list = function (req, res) {
|
2017-07-11 18:42:36 +08:00
|
|
|
var findForumsList = function (callback) {
|
|
|
|
|
Forum.find()
|
|
|
|
|
.sort('category order -createdat')
|
|
|
|
|
.populate({
|
|
|
|
|
path: 'lastTopic',
|
|
|
|
|
populate: {
|
|
|
|
|
path: 'user lastUser',
|
2017-09-14 16:11:11 +08:00
|
|
|
select: 'username displayName profileImageURL isVip uploaded downloaded'
|
2017-07-11 18:42:36 +08:00
|
|
|
}
|
|
|
|
|
})
|
2017-09-14 16:11:11 +08:00
|
|
|
.populate('moderators', 'username displayName profileImageURL isVip uploaded downloaded')
|
2017-07-11 18:42:36 +08:00
|
|
|
.exec(function (err, forums) {
|
|
|
|
|
if (err) {
|
|
|
|
|
callback(err, null);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, forums);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var forumsTopicsCount = function (callback) {
|
|
|
|
|
Topic.aggregate({
|
|
|
|
|
$project: {
|
|
|
|
|
'forum': '$forum',
|
2017-08-11 15:55:52 +08:00
|
|
|
'year': {
|
|
|
|
|
'$year': '$createdAt'
|
|
|
|
|
},
|
|
|
|
|
'month': {
|
|
|
|
|
'$month': '$createdAt'
|
|
|
|
|
},
|
2017-07-11 18:42:36 +08:00
|
|
|
'day': {
|
|
|
|
|
'$dayOfMonth': '$createdAt'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
$match: {
|
2017-08-11 15:55:52 +08:00
|
|
|
year: moment.utc().year(),
|
|
|
|
|
month: moment.utc().month() + 1,
|
2017-07-12 10:48:30 +08:00
|
|
|
day: moment.utc().date()
|
2017-07-08 00:08:55 +08:00
|
|
|
}
|
2017-07-11 18:42:36 +08:00
|
|
|
}, {
|
|
|
|
|
$group: {
|
|
|
|
|
_id: '$forum',
|
|
|
|
|
count: {$sum: 1}
|
|
|
|
|
}
|
|
|
|
|
}).exec(function (err, counts) {
|
2017-07-03 17:48:27 +08:00
|
|
|
if (err) {
|
2017-07-11 18:42:36 +08:00
|
|
|
callback(err, null);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, counts);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var forumsRepliesCount = function (callback) {
|
|
|
|
|
Topic.aggregate({
|
|
|
|
|
$unwind: '$_replies'
|
|
|
|
|
}, {
|
|
|
|
|
$project: {
|
|
|
|
|
'forum': '$forum',
|
2017-08-11 15:55:52 +08:00
|
|
|
'year': {
|
|
|
|
|
'$year': '$_replies.createdAt'
|
|
|
|
|
},
|
|
|
|
|
'month': {
|
|
|
|
|
'$month': '$_replies.createdAt'
|
|
|
|
|
},
|
2017-07-11 18:42:36 +08:00
|
|
|
'day': {
|
|
|
|
|
'$dayOfMonth': '$_replies.createdAt'
|
2017-07-12 10:35:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
$match: {
|
2017-08-11 15:55:52 +08:00
|
|
|
year: moment.utc().year(),
|
|
|
|
|
month: moment.utc().month() + 1,
|
2017-07-12 10:35:06 +08:00
|
|
|
day: moment.utc().date()
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
$group: {
|
|
|
|
|
_id: '$forum',
|
|
|
|
|
count: {$sum: 1}
|
2017-07-11 18:42:36 +08:00
|
|
|
}
|
|
|
|
|
}).exec(function (err, counts) {
|
|
|
|
|
if (err) {
|
|
|
|
|
callback(err, null);
|
2017-07-08 00:08:55 +08:00
|
|
|
} else {
|
2017-07-11 18:42:36 +08:00
|
|
|
callback(null, counts);
|
2017-07-03 17:48:27 +08:00
|
|
|
}
|
|
|
|
|
});
|
2017-07-11 18:42:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async.parallel([findForumsList, forumsTopicsCount, forumsRepliesCount], function (err, results) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send(err);
|
|
|
|
|
} else {
|
|
|
|
|
res.json({
|
|
|
|
|
forumsList: results[0],
|
|
|
|
|
forumsTopicsCount: results[1],
|
|
|
|
|
forumsRepliesCount: results[2]
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-07-03 17:48:27 +08:00
|
|
|
};
|
2017-07-06 17:26:04 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* read forum
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.read = function (req, res) {
|
2017-12-04 16:39:37 +08:00
|
|
|
var forum = req.forum;
|
|
|
|
|
var user = req.user;
|
|
|
|
|
|
|
|
|
|
if (forum.vipOnly && !user.isVip && !user.isOper) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
redirect: 'forums.list'
|
|
|
|
|
});
|
|
|
|
|
} else if (forum.operOnly && !user.isOper) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
redirect: 'forums.list'
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.json(req.forum);
|
|
|
|
|
}
|
2017-07-06 17:26:04 +08:00
|
|
|
};
|
|
|
|
|
|
2017-07-19 15:56:40 +08:00
|
|
|
/**
|
|
|
|
|
* forumsSearch
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.forumsSearch = function (req, res) {
|
|
|
|
|
var condition = {};
|
|
|
|
|
var keysA = [];
|
|
|
|
|
var skip = 0;
|
|
|
|
|
var limit = 0;
|
|
|
|
|
|
|
|
|
|
if (req.body.skip !== undefined) {
|
|
|
|
|
skip = parseInt(req.body.skip, 10);
|
|
|
|
|
}
|
|
|
|
|
if (req.body.limit !== undefined) {
|
|
|
|
|
limit = parseInt(req.body.limit, 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (req.body.keys && req.body.keys.length > 0) {
|
|
|
|
|
var keysS = req.body.keys + '';
|
|
|
|
|
var keysT = keysS.split(' ');
|
|
|
|
|
|
|
|
|
|
keysT.forEach(function (it) {
|
|
|
|
|
var ti = new RegExp(it, 'i');
|
|
|
|
|
keysA.push(ti);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (req.body.forumId) {
|
2017-07-19 16:21:30 +08:00
|
|
|
condition.forum = req.body.forumId;
|
2017-07-19 15:56:40 +08:00
|
|
|
}
|
2017-07-19 16:21:30 +08:00
|
|
|
|
2017-07-19 15:56:40 +08:00
|
|
|
if (keysA.length > 0) {
|
|
|
|
|
condition.$or = [
|
|
|
|
|
{title: {'$all': keysA}},
|
|
|
|
|
{content: {'$all': keysA}},
|
2017-07-19 18:00:17 +08:00
|
|
|
{'_replies.content': {'$all': keysA}},
|
|
|
|
|
{'_attach.filename': {'$all': keysA}},
|
|
|
|
|
{'_replies._attach.filename': {'$all': keysA}}
|
2017-07-19 15:56:40 +08:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var countQuery = function (callback) {
|
|
|
|
|
Topic.count(condition, function (err, count) {
|
|
|
|
|
if (err) {
|
|
|
|
|
callback(err, null);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, count);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var findQuery = function (callback) {
|
|
|
|
|
Topic.find(condition)
|
|
|
|
|
.sort('-lastReplyAt -createdAt')
|
2017-09-14 16:11:11 +08:00
|
|
|
.populate('user', 'username displayName profileImageURL isVip uploaded downloaded')
|
|
|
|
|
.populate('lastUser', 'username displayName profileImageURL isVip uploaded downloaded')
|
2017-07-19 15:56:40 +08:00
|
|
|
.populate('forum', 'name category')
|
|
|
|
|
.skip(skip)
|
|
|
|
|
.limit(limit)
|
|
|
|
|
.exec(function (err, topics) {
|
|
|
|
|
if (err) {
|
|
|
|
|
callback(err, null);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, topics);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async.parallel([countQuery, findQuery], function (err, results) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send(err);
|
|
|
|
|
} else {
|
|
|
|
|
res.json({rows: results[1], total: results[0]});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-07-06 17:26:04 +08:00
|
|
|
/**
|
|
|
|
|
* listTopics
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.listTopics = function (req, res) {
|
2017-07-18 11:29:14 +08:00
|
|
|
var skip = 0;
|
|
|
|
|
var limit = 0;
|
|
|
|
|
|
|
|
|
|
if (req.query.skip !== undefined) {
|
|
|
|
|
skip = parseInt(req.query.skip, 10);
|
|
|
|
|
}
|
|
|
|
|
if (req.query.limit !== undefined) {
|
|
|
|
|
limit = parseInt(req.query.limit, 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var countQuery = function (callback) {
|
|
|
|
|
Topic.count({
|
|
|
|
|
forum: req.params.forumId
|
|
|
|
|
}, function (err, count) {
|
2017-07-06 17:26:04 +08:00
|
|
|
if (err) {
|
2017-07-18 11:29:14 +08:00
|
|
|
callback(err, null);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, count);
|
2017-07-06 17:26:04 +08:00
|
|
|
}
|
|
|
|
|
});
|
2017-07-18 11:29:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var findQuery = function (callback) {
|
|
|
|
|
Topic.find({
|
|
|
|
|
forum: req.params.forumId
|
|
|
|
|
})
|
|
|
|
|
.sort('-isTop -lastReplyAt -createdAt')
|
2017-09-14 16:11:11 +08:00
|
|
|
.populate('user', 'username displayName profileImageURL isVip uploaded downloaded')
|
|
|
|
|
.populate('lastUser', 'username displayName profileImageURL isVip uploaded downloaded')
|
2017-07-18 11:29:14 +08:00
|
|
|
.skip(skip)
|
|
|
|
|
.limit(limit)
|
|
|
|
|
.exec(function (err, topics) {
|
|
|
|
|
if (err) {
|
|
|
|
|
callback(err, null);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, topics);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async.parallel([countQuery, findQuery], function (err, results) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send(err);
|
|
|
|
|
} else {
|
|
|
|
|
res.json({rows: results[1], total: results[0]});
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-07-06 17:26:04 +08:00
|
|
|
};
|
2017-07-07 17:19:42 +08:00
|
|
|
|
2017-07-15 14:23:00 +08:00
|
|
|
/**
|
|
|
|
|
* globalTopics
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.globalTopics = function (req, res) {
|
2017-12-04 16:39:37 +08:00
|
|
|
var user = req.user;
|
|
|
|
|
|
2017-11-20 10:40:05 +08:00
|
|
|
Forum.find().exec(function (err, forums) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
var ids = forums.map(function (el) {
|
2017-12-04 16:39:37 +08:00
|
|
|
if (el.vipOnly && !user.isVip && !user.isOper) {
|
|
|
|
|
return undefined;
|
|
|
|
|
} else if (el.operOnly && !user.isOper) {
|
|
|
|
|
return undefined;
|
|
|
|
|
} else {
|
|
|
|
|
return el._id;
|
|
|
|
|
}
|
2017-11-20 10:40:05 +08:00
|
|
|
});
|
|
|
|
|
|
2017-12-04 16:39:37 +08:00
|
|
|
//console.log(ids);
|
2017-11-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
Topic.find({
|
|
|
|
|
isGlobal: true,
|
|
|
|
|
forum: {$in: ids}
|
|
|
|
|
})
|
|
|
|
|
.sort('-createdAt')
|
|
|
|
|
.populate('forum', 'name')
|
|
|
|
|
.populate('user', 'username displayName profileImageURL isVip uploaded downloaded')
|
|
|
|
|
.populate('lastUser', 'username displayName profileImageURL isVip uploaded downloaded')
|
|
|
|
|
.exec(function (err, topics) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
res.json(topics);
|
2017-07-15 14:23:00 +08:00
|
|
|
});
|
2017-11-20 10:40:05 +08:00
|
|
|
}
|
|
|
|
|
});
|
2017-07-15 14:23:00 +08:00
|
|
|
};
|
|
|
|
|
|
2017-07-07 17:19:42 +08:00
|
|
|
/**
|
|
|
|
|
* postNewTopic
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.postNewTopic = function (req, res) {
|
2017-07-16 14:44:22 +08:00
|
|
|
var user = req.user;
|
2017-07-07 17:19:42 +08:00
|
|
|
var forum = req.forum;
|
|
|
|
|
var topic = new Topic(req.body);
|
|
|
|
|
topic.forum = forum;
|
|
|
|
|
topic.user = req.user;
|
|
|
|
|
|
2017-07-14 12:29:57 +08:00
|
|
|
//move temp torrent file to dest directory
|
|
|
|
|
req.body._uImage.forEach(function (f) {
|
|
|
|
|
var oldPath = config.uploads.attach.file.temp + f.filename;
|
|
|
|
|
var newPath = config.uploads.attach.file.dest + f.filename;
|
|
|
|
|
move(oldPath, newPath, function (err) {
|
|
|
|
|
if (err) {
|
2017-09-13 10:51:52 +08:00
|
|
|
mtDebug.debugRed(err);
|
2017-07-14 12:29:57 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
req.body._attach.forEach(function (f) {
|
|
|
|
|
var oldPath = config.uploads.attach.file.temp + f.filename;
|
|
|
|
|
var newPath = config.uploads.attach.file.dest + f.filename;
|
|
|
|
|
move(oldPath, newPath, function (err) {
|
|
|
|
|
if (err) {
|
2017-09-13 10:51:52 +08:00
|
|
|
mtDebug.debugRed(err);
|
2017-07-14 12:29:57 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//replace content path
|
2017-10-31 17:45:23 +08:00
|
|
|
var tmp = config.uploads.attach.file.temp.substr(1);
|
|
|
|
|
var dst = config.uploads.attach.file.dest.substr(1);
|
|
|
|
|
|
|
|
|
|
var regex = new RegExp(tmp, 'g');
|
|
|
|
|
topic.content = topic.content.replace(regex, dst);
|
2017-07-14 12:29:57 +08:00
|
|
|
|
|
|
|
|
//save topic
|
2017-07-07 17:19:42 +08:00
|
|
|
topic.save(function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.json(topic);
|
2017-07-11 15:01:37 +08:00
|
|
|
|
|
|
|
|
forum.update({
|
|
|
|
|
$inc: {topicCount: 1},
|
|
|
|
|
lastTopic: topic
|
|
|
|
|
}).exec();
|
2017-07-16 14:44:22 +08:00
|
|
|
|
|
|
|
|
user.update({
|
|
|
|
|
$inc: {topics: 1}
|
|
|
|
|
}).exec();
|
2017-07-07 17:19:42 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
2017-07-08 01:57:31 +08:00
|
|
|
|
2017-07-14 12:29:57 +08:00
|
|
|
/**
|
|
|
|
|
* move file
|
|
|
|
|
* @param oldPath
|
|
|
|
|
* @param newPath
|
|
|
|
|
* @param callback
|
|
|
|
|
*/
|
|
|
|
|
function move(oldPath, newPath, callback) {
|
|
|
|
|
fs.rename(oldPath, newPath, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
if (err.code === 'EXDEV') {
|
|
|
|
|
copy();
|
|
|
|
|
} else {
|
|
|
|
|
callback(err);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function copy() {
|
|
|
|
|
var readStream = fs.createReadStream(oldPath);
|
|
|
|
|
var writeStream = fs.createWriteStream(newPath);
|
|
|
|
|
|
|
|
|
|
readStream.on('error', callback);
|
|
|
|
|
writeStream.on('error', callback);
|
|
|
|
|
|
|
|
|
|
readStream.on('close', function () {
|
|
|
|
|
fs.unlink(oldPath, callback);
|
|
|
|
|
});
|
|
|
|
|
readStream.pipe(writeStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-08 01:57:31 +08:00
|
|
|
/**
|
2017-07-08 18:52:21 +08:00
|
|
|
* read readTopic
|
2017-07-08 01:57:31 +08:00
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.readTopic = function (req, res) {
|
2017-07-09 16:29:35 +08:00
|
|
|
var topic = req.topic;
|
|
|
|
|
|
|
|
|
|
topic.update({
|
|
|
|
|
$inc: {viewCount: 1}
|
|
|
|
|
}).exec();
|
|
|
|
|
|
|
|
|
|
res.json(topic);
|
2017-07-08 01:57:31 +08:00
|
|
|
};
|
|
|
|
|
|
2017-07-08 18:52:21 +08:00
|
|
|
/**
|
|
|
|
|
* updateTopic
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.updateTopic = function (req, res) {
|
|
|
|
|
var forum = req.forum;
|
|
|
|
|
var topic = req.topic;
|
|
|
|
|
|
2017-07-16 15:45:52 +08:00
|
|
|
if (!canEdit(req.user, forum) && !isOwner(req.user, topic)) {
|
|
|
|
|
return res.status(403).json({
|
|
|
|
|
message: 'ERROR: User is not authorized'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-07 17:47:05 +08:00
|
|
|
topic.title = req.body.title;
|
2017-07-08 18:52:21 +08:00
|
|
|
topic.content = req.body.content;
|
|
|
|
|
topic.updatedAt = Date.now();
|
|
|
|
|
topic.updatedBy = req.user;
|
|
|
|
|
|
|
|
|
|
topic.save(function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.json(topic);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-07-11 11:48:28 +08:00
|
|
|
/**
|
|
|
|
|
* toggleTopicReadonly
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.toggleTopicReadonly = function (req, res) {
|
2017-07-16 15:45:52 +08:00
|
|
|
var forum = req.forum;
|
2017-07-11 11:48:28 +08:00
|
|
|
var topic = req.topic;
|
|
|
|
|
|
2017-07-16 15:45:52 +08:00
|
|
|
if (!canEdit(req.user, forum) && !isOwner(req.user, topic)) {
|
|
|
|
|
return res.status(403).json({
|
|
|
|
|
message: 'ERROR: User is not authorized'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 11:48:28 +08:00
|
|
|
topic.readOnly = !topic.readOnly;
|
|
|
|
|
|
|
|
|
|
topic.save(function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.json(topic);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-07-15 13:38:26 +08:00
|
|
|
/**
|
|
|
|
|
* toggleTopicTopStatus
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.toggleTopicTopStatus = function (req, res) {
|
2017-07-16 15:45:52 +08:00
|
|
|
var forum = req.forum;
|
2017-07-15 13:38:26 +08:00
|
|
|
var topic = req.topic;
|
|
|
|
|
|
2017-07-16 15:45:52 +08:00
|
|
|
if (!canEdit(req.user, forum)) {
|
|
|
|
|
return res.status(403).json({
|
|
|
|
|
message: 'ERROR: User is not authorized'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-15 13:38:26 +08:00
|
|
|
topic.isTop = !topic.isTop;
|
|
|
|
|
|
|
|
|
|
topic.save(function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.json(topic);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-07-15 14:23:00 +08:00
|
|
|
/**
|
|
|
|
|
* toggleTopicGlobalStatus
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.toggleTopicGlobalStatus = function (req, res) {
|
|
|
|
|
var topic = req.topic;
|
|
|
|
|
|
2017-09-14 15:18:14 +08:00
|
|
|
if (!req.user.isOper && !req.user.isAdmin) {
|
2017-07-16 15:45:52 +08:00
|
|
|
return res.status(403).json({
|
|
|
|
|
message: 'ERROR: User is not authorized'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-15 14:23:00 +08:00
|
|
|
topic.isGlobal = !topic.isGlobal;
|
|
|
|
|
|
|
|
|
|
topic.save(function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.json(topic);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-07-12 12:12:13 +08:00
|
|
|
/**
|
|
|
|
|
* thumbsUp
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.thumbsUp = function (req, res) {
|
2017-07-14 14:30:30 +08:00
|
|
|
var user = req.user;
|
2017-07-12 12:12:13 +08:00
|
|
|
var exist = false;
|
|
|
|
|
var topic = req.topic;
|
2017-12-16 20:05:56 +08:00
|
|
|
var replyUid = undefined;
|
2017-07-12 12:12:13 +08:00
|
|
|
var thumb = new Thumb();
|
|
|
|
|
thumb.user = req.user;
|
2017-07-21 15:06:13 +08:00
|
|
|
thumb.score = thumbsUpScore.topic;
|
2017-07-12 12:12:13 +08:00
|
|
|
|
2017-12-16 20:05:56 +08:00
|
|
|
|
2017-07-12 12:12:13 +08:00
|
|
|
if (req.query.replyId) {
|
|
|
|
|
topic._replies.forEach(function (r) {
|
|
|
|
|
if (r._id.equals(req.query.replyId)) {
|
|
|
|
|
//check if already exist
|
|
|
|
|
exist = false;
|
2017-12-16 20:05:56 +08:00
|
|
|
replyUid = r.user._id;
|
2017-07-21 15:20:28 +08:00
|
|
|
r._thumbs.forEach(function (sr) {
|
2017-07-12 12:12:13 +08:00
|
|
|
if (sr.user._id.equals(req.user._id)) {
|
|
|
|
|
exist = true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (exist) {
|
2017-07-14 14:30:30 +08:00
|
|
|
return res.status(422).send({
|
2017-11-30 17:50:20 +08:00
|
|
|
message: 'SERVER.ALREADY_THUMBS_UP'
|
2017-07-14 14:30:30 +08:00
|
|
|
});
|
2017-07-12 12:12:13 +08:00
|
|
|
} else {
|
2017-07-21 15:06:13 +08:00
|
|
|
if (req.user.score >= thumbsUpScore.topic) {
|
2017-07-21 15:20:28 +08:00
|
|
|
r._thumbs.push(thumb);
|
2017-07-14 14:30:30 +08:00
|
|
|
r.user.update({
|
2017-07-21 15:06:13 +08:00
|
|
|
$inc: {score: thumbsUpScore.topic}
|
2017-07-14 14:30:30 +08:00
|
|
|
}).exec();
|
2017-07-14 18:36:09 +08:00
|
|
|
save();
|
2017-12-16 20:05:56 +08:00
|
|
|
|
|
|
|
|
//add server message
|
|
|
|
|
if (serverNoticeConfig.action.forumReplyThumbsUp.enable) {
|
|
|
|
|
serverMessage.addMessage(replyUid, serverNoticeConfig.action.forumReplyThumbsUp.title, serverNoticeConfig.action.forumReplyThumbsUp.content, {
|
|
|
|
|
topic_title: topic.title,
|
|
|
|
|
forum_id: topic.forum,
|
|
|
|
|
topic_id: topic._id,
|
|
|
|
|
reply_id: req.query.replyId,
|
|
|
|
|
by_name: user.displayName,
|
|
|
|
|
by_id: user._id
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-07-14 14:30:30 +08:00
|
|
|
} else {
|
|
|
|
|
return res.status(422).send({
|
2017-11-30 17:50:20 +08:00
|
|
|
message: 'SERVER.SCORE_NOT_ENOUGH'
|
2017-07-14 14:30:30 +08:00
|
|
|
});
|
|
|
|
|
}
|
2017-07-12 12:12:13 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
//check if already exist
|
|
|
|
|
exist = false;
|
2017-07-21 15:20:28 +08:00
|
|
|
topic._thumbs.forEach(function (sr) {
|
2017-07-12 12:12:13 +08:00
|
|
|
if (sr.user._id.equals(req.user._id)) {
|
|
|
|
|
exist = true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (exist) {
|
2017-07-14 14:30:30 +08:00
|
|
|
return res.status(422).send({
|
2017-11-30 17:50:20 +08:00
|
|
|
message: 'SERVER.ALREADY_THUMBS_UP'
|
2017-07-14 14:30:30 +08:00
|
|
|
});
|
2017-07-12 12:12:13 +08:00
|
|
|
} else {
|
2017-07-21 15:06:13 +08:00
|
|
|
if (req.user.score >= thumbsUpScore.topic) {
|
2017-07-21 15:20:28 +08:00
|
|
|
topic._thumbs.push(thumb);
|
2017-07-14 14:30:30 +08:00
|
|
|
topic.user.update({
|
2017-07-21 15:06:13 +08:00
|
|
|
$inc: {score: thumbsUpScore.topic}
|
2017-07-14 14:30:30 +08:00
|
|
|
}).exec();
|
2017-07-14 18:36:09 +08:00
|
|
|
save();
|
2017-12-16 20:05:56 +08:00
|
|
|
|
|
|
|
|
//add server message
|
|
|
|
|
if (serverNoticeConfig.action.forumTopicThumbsUp.enable) {
|
|
|
|
|
serverMessage.addMessage(topic.user._id, serverNoticeConfig.action.forumTopicThumbsUp.title, serverNoticeConfig.action.forumTopicThumbsUp.content, {
|
|
|
|
|
topic_title: topic.title,
|
|
|
|
|
forum_id: topic.forum,
|
|
|
|
|
topic_id: topic._id,
|
|
|
|
|
by_name: user.displayName,
|
|
|
|
|
by_id: user._id
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-07-14 14:30:30 +08:00
|
|
|
} else {
|
|
|
|
|
return res.status(422).send({
|
2017-11-30 17:50:20 +08:00
|
|
|
message: 'SERVER.SCORE_NOT_ENOUGH'
|
2017-07-14 14:30:30 +08:00
|
|
|
});
|
|
|
|
|
}
|
2017-07-12 12:12:13 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-14 18:36:09 +08:00
|
|
|
function save() {
|
|
|
|
|
topic.save(function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.json(topic);
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-07-14 14:30:30 +08:00
|
|
|
|
2017-07-14 18:36:09 +08:00
|
|
|
user.update({
|
2017-07-21 15:06:13 +08:00
|
|
|
$inc: {score: -thumbsUpScore.topic}
|
2017-07-14 18:36:09 +08:00
|
|
|
}).exec();
|
|
|
|
|
}
|
2017-07-12 12:12:13 +08:00
|
|
|
};
|
|
|
|
|
|
2017-07-08 20:22:38 +08:00
|
|
|
/**
|
|
|
|
|
* deleteTopic
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.deleteTopic = function (req, res) {
|
|
|
|
|
var forum = req.forum;
|
|
|
|
|
var topic = req.topic;
|
2017-07-11 15:01:37 +08:00
|
|
|
var rcount = topic.replyCount;
|
2017-07-08 20:22:38 +08:00
|
|
|
|
2017-07-16 15:45:52 +08:00
|
|
|
if (!canEdit(req.user, forum) && !isOwner(req.user, topic)) {
|
|
|
|
|
return res.status(403).json({
|
|
|
|
|
message: 'ERROR: User is not authorized'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-16 14:44:22 +08:00
|
|
|
topic._replies.forEach(function (r) {
|
|
|
|
|
r.user.update({
|
|
|
|
|
$inc: {replies: -1}
|
|
|
|
|
}).exec();
|
|
|
|
|
});
|
|
|
|
|
|
2017-07-08 20:22:38 +08:00
|
|
|
topic.remove(function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.json(topic);
|
|
|
|
|
|
2017-07-11 14:54:26 +08:00
|
|
|
Topic.findOne({
|
|
|
|
|
forum: forum._id
|
|
|
|
|
})
|
|
|
|
|
.sort('-lastReplyAt -createdAt')
|
|
|
|
|
.exec(function (err, topic) {
|
|
|
|
|
if (!err) {
|
|
|
|
|
forum.update({
|
2017-07-11 15:01:37 +08:00
|
|
|
$inc: {topicCount: -1, replyCount: -rcount},
|
2017-07-11 14:54:26 +08:00
|
|
|
lastTopic: topic
|
|
|
|
|
}).exec();
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-07-08 20:22:38 +08:00
|
|
|
|
2017-07-16 14:44:22 +08:00
|
|
|
topic.user.update({
|
|
|
|
|
$inc: {topics: -1}
|
|
|
|
|
}).exec();
|
|
|
|
|
|
2017-12-16 22:25:02 +08:00
|
|
|
//add server message
|
|
|
|
|
if (serverNoticeConfig.action.forumTopicDeleted.enable && !topic.user._id.equals(req.user._id)) {
|
|
|
|
|
serverMessage.addMessage(topic.user._id, serverNoticeConfig.action.forumTopicDeleted.title, serverNoticeConfig.action.forumTopicDeleted.content, {
|
|
|
|
|
topic_title: topic.title,
|
|
|
|
|
forum_id: topic.forum,
|
|
|
|
|
topic_id: topic._id,
|
|
|
|
|
by_name: req.user.displayName,
|
|
|
|
|
by_id: req.user._id
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 14:54:26 +08:00
|
|
|
//create trace log
|
|
|
|
|
traceLogCreate(req, traceConfig.action.forumDeleteTopic, {
|
|
|
|
|
forum: forum._id,
|
|
|
|
|
topic: topic._id
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-07-08 20:22:38 +08:00
|
|
|
};
|
|
|
|
|
|
2017-07-09 16:29:35 +08:00
|
|
|
/**
|
|
|
|
|
* postNewReply
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.postNewReply = function (req, res) {
|
2017-07-16 14:44:22 +08:00
|
|
|
var user = req.user;
|
2017-07-09 16:29:35 +08:00
|
|
|
var forum = req.forum;
|
|
|
|
|
var topic = req.topic;
|
|
|
|
|
var reply = new Reply(req.body);
|
|
|
|
|
reply.user = req.user;
|
|
|
|
|
|
2017-07-14 12:29:57 +08:00
|
|
|
//replace content path
|
|
|
|
|
var regex = new RegExp('/modules/forums/client/attach/temp/', 'g');
|
|
|
|
|
reply.content = reply.content.replace(regex, '/modules/forums/client/attach/');
|
|
|
|
|
|
2017-07-09 16:29:35 +08:00
|
|
|
topic._replies.push(reply);
|
|
|
|
|
topic.replyCount++;
|
2017-07-09 20:21:19 +08:00
|
|
|
topic.lastReplyAt = Date.now();
|
|
|
|
|
topic.lastUser = req.user;
|
2017-07-09 16:29:35 +08:00
|
|
|
|
2017-07-14 12:29:57 +08:00
|
|
|
//move temp torrent file to dest directory
|
|
|
|
|
req.body._uImage.forEach(function (f) {
|
|
|
|
|
var oldPath = config.uploads.attach.file.temp + f.filename;
|
|
|
|
|
var newPath = config.uploads.attach.file.dest + f.filename;
|
|
|
|
|
move(oldPath, newPath, function (err) {
|
|
|
|
|
if (err) {
|
2017-09-13 10:51:52 +08:00
|
|
|
mtDebug.debugRed(err);
|
2017-07-14 12:29:57 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
req.body._attach.forEach(function (f) {
|
|
|
|
|
var oldPath = config.uploads.attach.file.temp + f.filename;
|
|
|
|
|
var newPath = config.uploads.attach.file.dest + f.filename;
|
|
|
|
|
move(oldPath, newPath, function (err) {
|
|
|
|
|
if (err) {
|
2017-09-13 10:51:52 +08:00
|
|
|
mtDebug.debugRed(err);
|
2017-07-14 12:29:57 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//save topic
|
2017-07-09 16:29:35 +08:00
|
|
|
topic.save(function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.json(topic);
|
2017-07-11 15:01:37 +08:00
|
|
|
|
2017-07-16 14:44:22 +08:00
|
|
|
user.update({
|
|
|
|
|
$inc: {replies: 1}
|
|
|
|
|
}).exec();
|
|
|
|
|
|
2017-07-11 15:01:37 +08:00
|
|
|
forum.update({
|
|
|
|
|
$inc: {replyCount: 1},
|
|
|
|
|
lastTopic: topic
|
|
|
|
|
}).exec();
|
2017-12-16 20:05:56 +08:00
|
|
|
|
|
|
|
|
//add server message
|
|
|
|
|
if (serverNoticeConfig.action.forumTopicNewReply.enable && !topic.user._id.equals(user._id)) {
|
|
|
|
|
serverMessage.addMessage(topic.user._id, serverNoticeConfig.action.forumTopicNewReply.title, serverNoticeConfig.action.forumTopicNewReply.content, {
|
|
|
|
|
topic_title: topic.title,
|
|
|
|
|
forum_id: topic.forum,
|
|
|
|
|
topic_id: topic._id,
|
|
|
|
|
reply_id: reply._id,
|
|
|
|
|
by_name: user.displayName,
|
|
|
|
|
by_id: user._id
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-07-09 16:29:35 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* updateReply
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.updateReply = function (req, res) {
|
2017-07-16 15:45:52 +08:00
|
|
|
var forum = req.forum;
|
2017-07-09 17:54:31 +08:00
|
|
|
var topic = req.topic;
|
|
|
|
|
|
2017-07-16 15:45:52 +08:00
|
|
|
topic._replies.forEach(function (r) {
|
|
|
|
|
if (r._id.equals(req.params.replyId)) {
|
|
|
|
|
|
|
|
|
|
if (!canEdit(req.user, forum) && !isOwner(req.user, r)) {
|
|
|
|
|
return res.status(403).json({
|
|
|
|
|
message: 'ERROR: User is not authorized'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.content = req.body.content;
|
|
|
|
|
r.updatedAt = Date.now();
|
|
|
|
|
r.updatedBy = req.user;
|
2017-07-09 17:54:31 +08:00
|
|
|
|
|
|
|
|
topic.save(function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.json(topic);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-07-09 16:29:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* deleteReply
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.deleteReply = function (req, res) {
|
2017-07-09 17:29:12 +08:00
|
|
|
var forum = req.forum;
|
|
|
|
|
var topic = req.topic;
|
2017-12-16 22:25:02 +08:00
|
|
|
var replyUid = undefined;
|
2017-07-09 17:29:12 +08:00
|
|
|
|
2017-07-16 14:44:22 +08:00
|
|
|
topic._replies.forEach(function (r) {
|
|
|
|
|
if (r._id.equals(req.params.replyId)) {
|
2017-07-16 15:45:52 +08:00
|
|
|
|
|
|
|
|
if (!canEdit(req.user, forum) && !isOwner(req.user, r)) {
|
|
|
|
|
return res.status(403).json({
|
|
|
|
|
message: 'ERROR: User is not authorized'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-16 22:25:02 +08:00
|
|
|
replyUid = r.user._id;
|
2017-07-16 14:44:22 +08:00
|
|
|
topic._replies.pull(r);
|
2017-07-09 17:29:12 +08:00
|
|
|
topic.replyCount--;
|
|
|
|
|
topic.save(function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return res.status(422).send({
|
|
|
|
|
message: errorHandler.getErrorMessage(err)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.json(topic);
|
2017-07-11 15:01:37 +08:00
|
|
|
|
2017-07-16 14:44:22 +08:00
|
|
|
r.user.update({
|
|
|
|
|
$inc: {replies: -1}
|
|
|
|
|
}).exec();
|
|
|
|
|
|
2017-07-11 15:01:37 +08:00
|
|
|
forum.update({
|
|
|
|
|
$inc: {replyCount: -1}
|
|
|
|
|
}).exec();
|
|
|
|
|
|
2017-12-16 22:25:02 +08:00
|
|
|
//add server message
|
|
|
|
|
if (serverNoticeConfig.action.forumReplyDeleted.enable && !replyUid.equals(req.user._id)) {
|
|
|
|
|
serverMessage.addMessage(replyUid, serverNoticeConfig.action.forumReplyDeleted.title, serverNoticeConfig.action.forumReplyDeleted.content, {
|
|
|
|
|
topic_title: topic.title,
|
|
|
|
|
forum_id: topic.forum,
|
|
|
|
|
topic_id: topic._id,
|
|
|
|
|
reply_id: req.params.replyId,
|
|
|
|
|
by_name: req.user.displayName,
|
|
|
|
|
by_id: req.user._id
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 15:01:37 +08:00
|
|
|
//create trace log
|
|
|
|
|
traceLogCreate(req, traceConfig.action.forumDeleteReply, {
|
|
|
|
|
forum: forum._id,
|
|
|
|
|
topic: topic._id,
|
|
|
|
|
reply: req.params.replyId
|
|
|
|
|
});
|
2017-07-09 17:29:12 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-07-09 16:29:35 +08:00
|
|
|
};
|
|
|
|
|
|
2017-07-14 12:29:57 +08:00
|
|
|
/**
|
|
|
|
|
* attachUpload
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.attachUpload = function (req, res) {
|
|
|
|
|
var user = req.user;
|
|
|
|
|
var createUploadAttachFilename = require(path.resolve('./config/lib/multer')).createUploadAttachFilename;
|
|
|
|
|
var getUploadAttachDestination = require(path.resolve('./config/lib/multer')).getUploadAttachDestination;
|
|
|
|
|
var attachInfo = {};
|
|
|
|
|
|
|
|
|
|
var storage = multer.diskStorage({
|
|
|
|
|
destination: getUploadAttachDestination,
|
|
|
|
|
filename: createUploadAttachFilename
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var upload = multer({
|
|
|
|
|
storage: storage,
|
|
|
|
|
//fileFilter: fileFilter,
|
|
|
|
|
limits: config.uploads.attach.file.limits
|
|
|
|
|
}).single('newAttachFile');
|
|
|
|
|
|
|
|
|
|
if (user) {
|
|
|
|
|
uploadFile()
|
|
|
|
|
.then(function () {
|
|
|
|
|
res.status(200).send(attachInfo);
|
|
|
|
|
})
|
|
|
|
|
.catch(function (err) {
|
|
|
|
|
res.status(422).send(err);
|
2017-09-13 10:51:52 +08:00
|
|
|
mtDebug.debugRed(err);
|
2017-07-14 12:29:57 +08:00
|
|
|
|
|
|
|
|
if (req.file && req.file.filename) {
|
|
|
|
|
var newfile = config.uploads.attach.file.temp + req.file.filename;
|
|
|
|
|
if (fs.existsSync(newfile)) {
|
2017-09-13 10:51:52 +08:00
|
|
|
mtDebug.debugRed(err);
|
|
|
|
|
mtDebug.debugRed('ERROR: DELETE TEMP ATTACH FILE: ' + newfile);
|
2017-07-14 12:29:57 +08:00
|
|
|
fs.unlinkSync(newfile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.status(401).send({
|
|
|
|
|
message: 'User is not signed in'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function uploadFile() {
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
|
upload(req, res, function (uploadError) {
|
|
|
|
|
if (uploadError) {
|
|
|
|
|
var message = errorHandler.getErrorMessage(uploadError);
|
|
|
|
|
|
|
|
|
|
if (uploadError.code === 'LIMIT_FILE_SIZE') {
|
|
|
|
|
message = 'Attach file too large. Maximum size allowed is ' + (config.uploads.attach.file.limits.fileSize / (1024 * 1024)).toFixed(2) + ' Mb files.';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reject(message);
|
|
|
|
|
} else {
|
|
|
|
|
attachInfo.filename = req.file.filename;
|
|
|
|
|
resolve();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* attachDownload
|
|
|
|
|
* @param req
|
|
|
|
|
* @param res
|
|
|
|
|
*/
|
|
|
|
|
exports.attachDownload = function (req, res) {
|
|
|
|
|
var topic = req.topic;
|
|
|
|
|
var filePath = undefined;
|
2017-07-15 15:28:46 +08:00
|
|
|
var fileName = undefined;
|
2017-07-14 12:29:57 +08:00
|
|
|
|
|
|
|
|
if (req.params.replyId) {
|
|
|
|
|
topic._replies.forEach(function (rep) {
|
|
|
|
|
if (rep._id.equals(req.params.replyId)) {
|
|
|
|
|
rep._attach.forEach(function (at) {
|
|
|
|
|
if (at._id.equals(req.query.attachId)) {
|
|
|
|
|
at.downCount++;
|
|
|
|
|
filePath = config.uploads.attach.file.dest + at.filename;
|
2017-07-15 15:28:46 +08:00
|
|
|
fileName = at.filename;
|
2017-07-14 12:29:57 +08:00
|
|
|
|
|
|
|
|
topic.markModified('_replies');
|
|
|
|
|
topic.save();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
topic._attach.forEach(function (at) {
|
|
|
|
|
if (at._id.equals(req.query.attachId)) {
|
|
|
|
|
at.downCount++;
|
|
|
|
|
filePath = config.uploads.attach.file.dest + at.filename;
|
2017-07-15 15:28:46 +08:00
|
|
|
fileName = at.filename;
|
2017-07-14 12:29:57 +08:00
|
|
|
|
|
|
|
|
topic.save();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-07-15 15:28:46 +08:00
|
|
|
return downFile(filePath, fileName);
|
2017-07-14 12:29:57 +08:00
|
|
|
|
2017-07-15 15:28:46 +08:00
|
|
|
function downFile(filePath, filename) {
|
2017-07-14 12:29:57 +08:00
|
|
|
if (!filePath) {
|
|
|
|
|
res.status(422).send({
|
|
|
|
|
message: 'FILE_DOES_NOT_FINDED'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs.exists(filePath, function (exists) {
|
|
|
|
|
if (exists) {
|
|
|
|
|
var stat = fs.statSync(filePath);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
//res.set('Content-Type', 'application/x-bittorrent');
|
2017-07-15 15:28:46 +08:00
|
|
|
res.set('Content-Disposition', 'attachment; filename=' + encodeURI(filename));
|
2017-07-14 12:29:57 +08:00
|
|
|
res.set('Content-Length', stat.size);
|
|
|
|
|
|
|
|
|
|
fs.createReadStream(filePath).pipe(res);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
res.status(422).send({
|
|
|
|
|
message: 'DOWNLOAD_FAILED'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
res.status(422).send({
|
|
|
|
|
message: 'FILE_DOES_NOT_EXISTS'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-07-08 01:57:31 +08:00
|
|
|
/**
|
|
|
|
|
* Invitation middleware
|
|
|
|
|
*/
|
|
|
|
|
exports.topicById = function (req, res, next, id) {
|
|
|
|
|
|
|
|
|
|
if (!mongoose.Types.ObjectId.isValid(id)) {
|
|
|
|
|
return res.status(400).send({
|
|
|
|
|
message: 'Topic is invalid'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Topic.findById(id)
|
2017-11-08 16:45:31 +08:00
|
|
|
.populate('user', 'username displayName profileImageURL isVip uploaded downloaded score signature')
|
2017-09-14 16:11:11 +08:00
|
|
|
.populate('lastUser', 'username displayName profileImageURL isVip uploaded downloaded')
|
|
|
|
|
.populate('updatedBy', 'username displayName profileImageURL isVip uploaded downloaded')
|
|
|
|
|
.populate('_thumbs.user', 'username displayName profileImageURL isVip uploaded downloaded')
|
2017-11-08 16:45:31 +08:00
|
|
|
.populate('_replies.user', 'username displayName profileImageURL isVip uploaded downloaded signature')
|
2017-09-14 16:11:11 +08:00
|
|
|
.populate('_replies.updatedBy', 'username displayName profileImageURL isVip uploaded downloaded')
|
|
|
|
|
.populate('_replies._thumbs.user', 'username displayName profileImageURL isVip uploaded downloaded')
|
2017-07-08 01:57:31 +08:00
|
|
|
.exec(function (err, topic) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
} else if (!topic) {
|
|
|
|
|
return res.status(404).send({
|
|
|
|
|
message: 'No topic with that identifier has been found'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
req.topic = topic;
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
};
|
2017-07-16 15:45:52 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* canEdit
|
|
|
|
|
* @param u, req.user
|
|
|
|
|
* @param f, forum
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
|
|
|
|
function canEdit(u, f) {
|
2017-09-14 15:18:14 +08:00
|
|
|
if (u.isOper) {
|
2017-07-16 16:03:55 +08:00
|
|
|
return true;
|
2017-09-14 15:18:14 +08:00
|
|
|
} else if (u.isAdmin) {
|
2017-07-16 15:45:52 +08:00
|
|
|
return true;
|
|
|
|
|
} else if (isModerator(f)) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isModerator(f) {
|
|
|
|
|
if (f) {
|
|
|
|
|
var isM = false;
|
|
|
|
|
f.moderators.forEach(function (m) {
|
2017-07-16 16:03:55 +08:00
|
|
|
if (m._id.equals(u._id)) {
|
2017-07-16 15:45:52 +08:00
|
|
|
isM = true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return isM;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* isOwner
|
|
|
|
|
* @param o, topic or reply object
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
|
|
|
|
function isOwner(u, o) {
|
|
|
|
|
if (o) {
|
2017-07-16 16:03:55 +08:00
|
|
|
if (o.user._id.equals(u._id)) {
|
2017-07-16 15:45:52 +08:00
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|