Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
calidion committed Jun 25, 2016
1 parent ca42368 commit 5d0e529
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 68 deletions.
22 changes: 11 additions & 11 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ var plumber = require('gulp-plumber');
var coveralls = require('gulp-coveralls');
var less = require('gulp-less');

gulp.task('less', function() {
gulp.task('less', function () {
gulp.src('public/**/*.less')
.pipe(less())
.pipe(gulp.dest('public/'));
});

gulp.task('static', function() {
gulp.task('static', function () {
return gulp.src('lib/**/*.js')
.pipe(excludeGitignore())
.pipe(eslint({
"rules": {
rules: {
"camelcase": 0,
"space-before-function-paren": 0,
"object-curly-spacing": 0,
Expand Down Expand Up @@ -53,13 +53,13 @@ gulp.task('static', function() {
.pipe(eslint.failAfterError());
});

gulp.task('nsp', function(cb) {
gulp.task('nsp', function (cb) {
nsp({
package: path.resolve('package.json')
}, cb);
});

gulp.task('pre-test', function() {
gulp.task('pre-test', function () {
return gulp.src('lib/**/*.js')
.pipe(excludeGitignore())
.pipe(istanbul({
Expand All @@ -68,29 +68,29 @@ gulp.task('pre-test', function() {
.pipe(istanbul.hookRequire());
});

gulp.task('test', ['pre-test'], function(cb) {
gulp.task('test', ['pre-test'], function (cb) {
var mochaErr;

gulp.src(['test/**/*.js', 'test/**/*.test.js'])
.pipe(plumber())
.pipe(mocha({
reporter: 'spec'
}))
.on('error', function(err) {
.on('error', function (err) {
mochaErr = err;
throw err;
})
.pipe(istanbul.writeReports())
.on('end', function() {
.on('end', function () {
cb(mochaErr);
});
});

gulp.task('watch', function() {
gulp.task('watch', function () {
gulp.watch(['lib/**/*.js', 'test/**'], ['test']);
});

gulp.task('coveralls', ['test'], function() {
gulp.task('coveralls', ['test'], function () {
if (!process.env.CI) {
return;
}
Expand All @@ -100,6 +100,6 @@ gulp.task('coveralls', ['test'], function() {

// gulp.task('prepublish', ['nsp']);
gulp.task('prepublish');
gulp.task('default', ['less', 'static', 'test', 'coveralls'], function() {
gulp.task('default', ['less', 'static', 'test', 'coveralls'], function () {
process.exit();
});
24 changes: 12 additions & 12 deletions lib/api/v1/message.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var eventproxy = require('eventproxy');
var Message = require('../../proxy').Message;
var at = require('../../common/at');
var EventProxy = require('eventproxy');
var Message = require('../../proxy').Message;
var at = require('../../common/at');
var renderHelper = require('../../common/render_helper');
var _ = require('lodash');
var _ = require('lodash');

var index = function (req, res, next) {
var user_id = req.user._id;
var mdrender = req.query.mdrender === 'false' ? false : true;
var ep = new eventproxy();
var ep = new EventProxy();
ep.fail(next);

ep.all('has_read_messages', 'hasnot_read_messages', function (has_read_messages, hasnot_read_messages) {
Expand All @@ -21,20 +21,20 @@ var index = function (req, res, next) {

ep.all('has_read', 'unread', function (has_read, unread) {
[has_read, unread].forEach(function (msgs, idx) {
var epfill = new eventproxy();
var epfill = new EventProxy();
epfill.fail(next);
epfill.after('message_ready', msgs.length, function (docs) {
docs = docs.filter(function (doc) {
return !doc.is_invalid;
});
docs = docs.map(function (doc) {
doc.author = _.pick(doc.author, ['loginname', 'avatar_url']);
doc.topic = _.pick(doc.topic, ['id', 'author', 'title', 'last_reply_at']);
doc.reply = _.pick(doc.reply, ['id', 'content', 'ups', 'create_at']);
doc.topic = _.pick(doc.topic, ['id', 'author', 'title', 'last_reply_at']);
doc.reply = _.pick(doc.reply, ['id', 'content', 'ups', 'create_at']);
if (mdrender) {
doc.reply.content = renderHelper.markdown(at.linkUsers(doc.reply.content));
}
doc = _.pick(doc, ['id', 'type', 'has_read', 'author', 'topic', 'reply']);
doc = _.pick(doc, ['id', 'type', 'has_read', 'author', 'topic', 'reply']);

return doc;
});
Expand All @@ -55,7 +55,7 @@ exports.index = index;

var markAll = function (req, res, next) {
var user_id = req.user._id;
var ep = new eventproxy();
var ep = new EventProxy();
ep.fail(next);
Message.getUnreadMessageByUserId(user_id, ep.done('unread', function (docs) {
docs.forEach(function (doc) {
Expand All @@ -82,11 +82,11 @@ exports.markAll = markAll;
var count = function (req, res, next) {
var userId = req.user.id;

var ep = new eventproxy();
var ep = new EventProxy();
ep.fail(next);

Message.getMessagesCount(userId, ep.done(function (count) {
res.send({data: count});
res.send({ data: count });
}));
};

Expand Down
67 changes: 33 additions & 34 deletions lib/controllers/user.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
var User = require('../proxy').User;
var Topic = require('../proxy').Topic;
var Reply = require('../proxy').Reply;
var User = require('../proxy').User;
var Topic = require('../proxy').Topic;
var Reply = require('../proxy').Reply;
var TopicCollect = require('../proxy').TopicCollect;
var utility = require('utility');
var util = require('util');
var TopicModel = require('../models').Topic;
var ReplyModel = require('../models').Reply;
var tools = require('../common/tools');
var config = require('../config');
var EventProxy = require('eventproxy');
var validator = require('validator');
var utility = require('utility');
var _ = require('lodash');
var utility = require('utility');
var util = require('util');
var TopicModel = require('../models').Topic;
var ReplyModel = require('../models').Reply;
var tools = require('../common/tools');
var config = require('../config');
var EventProxy = require('eventproxy');
var validator = require('validator');
var _ = require('lodash');

exports.index = function (req, res, next) {
var user_name = req.params.name;
Expand Down Expand Up @@ -49,31 +48,31 @@ exports.index = function (req, res, next) {
proxy.assign('recent_topics', 'recent_replies', render);
proxy.fail(next);

var query = {author_id: user._id};
var opt = {limit: 5, sort: '-create_at'};
var query = { author_id: user._id };
var opt = { limit: 5, sort: '-create_at' };
Topic.getTopicsByQuery(query, opt, proxy.done('recent_topics'));

Reply.getRepliesByAuthorId(user._id, {limit: 20, sort: '-create_at'},
Reply.getRepliesByAuthorId(user._id, { limit: 20, sort: '-create_at' },
proxy.done(function (replies) {
var topic_ids = [];
for (var i = 0; i < replies.length; i++) {
if (topic_ids.indexOf(replies[i].topic_id.toString()) < 0) {
topic_ids.push(replies[i].topic_id.toString());
}
}
var query = {_id: {'$in': topic_ids}};
var opt = {limit: 5, sort: '-create_at'};
var query = { _id: { '$in': topic_ids } };
var opt = { limit: 5, sort: '-create_at' };
Topic.getTopicsByQuery(query, opt, proxy.done('recent_replies'));
}));
});
};

exports.listStars = function (req, res, next) {
User.getUsersByQuery({is_star: true}, {}, function (err, stars) {
User.getUsersByQuery({ is_star: true }, {}, function (err, stars) {
if (err) {
return next(err);
}
res.render('user/stars', {stars: stars});
res.render('user/stars', { stars: stars });
});
};

Expand Down Expand Up @@ -104,7 +103,7 @@ exports.setting = function (req, res, next) {
location: data.location,
signature: data.signature,
weibo: data.weibo,
accessToken: data.accessToken,
accessToken: data.accessToken
};
if (isSuccess) {
data2.success = msg;
Expand All @@ -131,7 +130,7 @@ exports.setting = function (req, res, next) {
if (err) {
return next(err);
}
req.session.user = user.toObject({virtual: true});
req.session.user = user.toObject({ virtual: true });
return res.redirect('/setting?save=success');
});
}));
Expand Down Expand Up @@ -226,8 +225,8 @@ exports.listCollectedTopics = function (req, res, next) {
};

exports.top100 = function (req, res, next) {
var opt = {limit: 100, sort: '-score'};
User.getUsersByQuery({is_block: false}, opt, function (err, tops) {
var opt = { limit: 100, sort: '-score' };
User.getUsersByQuery({ is_block: false }, opt, function (err, tops) {
if (err) {
return next(err);
}
Expand Down Expand Up @@ -262,8 +261,8 @@ exports.listTopics = function (req, res, next) {
proxy.assign('topics', 'pages', render);
proxy.fail(next);

var query = {'author_id': user._id};
var opt = {skip: (page - 1) * limit, limit: limit, sort: '-create_at'};
var query = { 'author_id': user._id };
var opt = { skip: (page - 1) * limit, limit: limit, sort: '-create_at' };
Topic.getTopicsByQuery(query, opt, proxy.done('topics'));

Topic.getCountByQuery(query, proxy.done(function (all_topics_count) {
Expand Down Expand Up @@ -297,14 +296,14 @@ exports.listReplies = function (req, res, next) {
proxy.assign('topics', 'pages', render);
proxy.fail(next);

var opt = {skip: (page - 1) * limit, limit: limit, sort: '-create_at'};
var opt = { skip: (page - 1) * limit, limit: limit, sort: '-create_at' };
Reply.getRepliesByAuthorId(user._id, opt, proxy.done(function (replies) {
// 获取所有有评论的主题
var topic_ids = replies.map(function (reply) {
return reply.topic_id;
});
topic_ids = _.uniq(topic_ids);
var query = {'_id': {'$in': topic_ids}};
var query = { '_id': { '$in': topic_ids } };
Topic.getTopicsByQuery(query, {}, proxy.done('topics'));
}));

Expand All @@ -329,7 +328,7 @@ exports.block = function (req, res, next) {
if (action === 'set_block') {
ep.all('block_user',
function (user) {
res.json({status: 'success'});
res.json({ status: 'success' });
});
user.is_block = true;
user.save(ep.done('block_user'));
Expand All @@ -338,7 +337,7 @@ exports.block = function (req, res, next) {
user.is_block = false;
user.save(ep.done(function () {

res.json({status: 'success'});
res.json({ status: 'success' });
}));
}
}));
Expand All @@ -356,13 +355,13 @@ exports.deleteAll = function (req, res, next) {
}
ep.all('del_topics', 'del_replys', 'del_ups',
function () {
res.json({status: 'success'});
res.json({ status: 'success' });
});
// 删除主题
TopicModel.update({author_id: user._id}, {$set: {deleted: true}}, {multi: true}, ep.done('del_topics'));
TopicModel.update({ author_id: user._id }, { $set: { deleted: true } }, { multi: true }, ep.done('del_topics'));
// 删除评论
ReplyModel.update({author_id: user._id}, {$set: {deleted: true}}, {multi: true}, ep.done('del_replys'));
ReplyModel.update({ author_id: user._id }, { $set: { deleted: true } }, { multi: true }, ep.done('del_replys'));
// 点赞数也全部干掉
ReplyModel.update({}, {$pull: {'ups': user._id}}, {multi: true}, ep.done('del_ups'));
ReplyModel.update({}, { $pull: { 'ups': user._id } }, { multi: true }, ep.done('del_ups'));
}));
};
5 changes: 2 additions & 3 deletions lib/middlewares/limit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var config = require('../config');
var cache = require('../common/cache');
var cache = require('../common/cache');
var moment = require('moment');

var SEPARATOR = '^_^@T_T';
Expand All @@ -9,7 +8,7 @@ var makePerDayLimiter = function (identityName, identityFn) {
return function (req, res, next) {
var identity = identityFn(req);
var YYYYMMDD = moment().format('YYYYMMDD');
var key = YYYYMMDD + SEPARATOR + identityName + SEPARATOR + name + SEPARATOR + identity;
var key = YYYYMMDD + SEPARATOR + identityName + SEPARATOR + name + SEPARATOR + identity;

cache.get(key, function (err, count) {
if (err) {
Expand Down
16 changes: 8 additions & 8 deletions lib/middlewares/proxy.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var urllib = require('url');
var urllib = require('url');
var request = require('request');
var logger = require('../common/logger')


var ALLOW_HOSTNAME = [
'avatars.githubusercontent.com', 'www.gravatar.com',
'gravatar.com', 'www.google-analytics.com',
'gravatar.com', 'www.google-analytics.com'
];
exports.proxy = function (req, res, next) {
exports.proxy = function (req, res) {
var url = decodeURIComponent(req.query.url);
var hostname = urllib.parse(url).hostname;

Expand All @@ -16,11 +16,11 @@ exports.proxy = function (req, res, next) {
}

request.get({
url: url,
headers: {
'If-Modified-Since': req.header('If-Modified-Since') || ''
}
})
url: url,
headers: {
'If-Modified-Since': req.header('If-Modified-Since') || ''
}
})
.on('response', function (response) {
res.set(response.headers);
})
Expand Down

0 comments on commit 5d0e529

Please sign in to comment.