Permalink
Browse files

增加屏蔽用户功能

  • Loading branch information...
1 parent 87a59e3 commit 7dfb25845147c108754497f342b02955238521d8 @alsotang alsotang committed Jan 10, 2014
Showing with 76 additions and 4 deletions.
  1. +2 −0 app.js
  2. +1 −1 controllers/reply.js
  3. +0 −1 controllers/sign.js
  4. +1 −1 controllers/topic.js
  5. +33 −0 controllers/user.js
  6. +9 −0 middlewares/auth.js
  7. +1 −0 models/user.js
  8. +1 −0 routes.js
  9. +1 −1 test/support/support.js
  10. +27 −0 views/user/index.html
View
2 app.js
@@ -18,6 +18,7 @@ var User = Models.User;
var GitHubStrategy = require('passport-github').Strategy;
var githubStrategyMiddleware = require('./middlewares/github_strategy');
var routes = require('./routes');
+var auth = require('./middlewares/auth');
var maxAge = 3600000 * 24 * 30;
var staticDir = path.join(__dirname, 'public');
@@ -62,6 +63,7 @@ app.configure(function () {
app.use(passport.initialize());
// custom middleware
app.use(require('./controllers/sign').auth_user);
+ app.use(auth.blockUser());
app.use('/upload/', express.static(config.upload_dir, { maxAge: maxAge }));
// old image url: http://host/user_data/images/xxxx
app.use('/user_data/', express.static(path.join(__dirname, 'public', 'user_data'), { maxAge: maxAge }));
View
@@ -47,7 +47,7 @@ exports.add = function (req, res, next) {
user.score += 5;
user.reply_count += 1;
user.save();
- req.session.user.score += 5;
+ req.session.user = user;
ep.emit('score_saved');
}));
});
View
@@ -318,7 +318,6 @@ exports.auth_user = function (req, res, next) {
}
user.messages_count = count;
req.session.user = user;
- req.session.user.avatar_url = user.avatar_url;
res.local('current_user', req.session.user);
return next();
});
View
@@ -178,7 +178,7 @@ exports.put = function (req, res, next) {
user.score += 5;
user.topic_count += 1;
user.save();
- req.session.user.score += 5;
+ req.session.user = user;
proxy.emit('score_saved');
}));
View
@@ -1,7 +1,10 @@
var User = require('../proxy').User;
+var UserModel = require('../models').User;
var Tag = require('../proxy').Tag;
var Topic = require('../proxy').Topic;
+var TopicModel = require('../models').Topic;
var Reply = require('../proxy').Reply;
+var ReplyModel = require('../models').Reply;
var Relation = require('../proxy').Relation;
var TopicCollect = require('../proxy').TopicCollect;
var TagCollect = require('../proxy').TagCollect;
@@ -579,3 +582,33 @@ exports.list_replies = function (req, res, next) {
}
});
};
+
+exports.block = function (req, res, next) {
+ var userName = req.params.name;
+ User.getUserByName(userName, function (err, user) {
+ if (err) {
+ return next(err);
+ }
+ if (req.body.action === 'set_block') {
+ var ep = EventProxy.create();
+ ep.fail(next);
+ ep.all('block_user', 'del_topics', 'del_replys',
+ function (user, topics, replys) {
+ res.json({status: 'success'});
+ });
+ user.is_block = true;
+ user.save(ep.done('block_user'));
+ TopicModel.remove({author_id: user._id}, ep.done('del_topics'));
+ ReplyModel.remove({author_id: user._id}, ep.done('del_replys'));
+ } else if (req.body.action === 'cancel_block') {
+ user.is_block = false;
+ user.save(function (err) {
+ if (err) {
+ return next(err);
+ }
+ res.json({status: 'success'});
+ });
+ }
+
+ });
+};
View
@@ -31,3 +31,12 @@ exports.signinRequired = function (req, res, next) {
}
next();
};
+
+exports.blockUser = function () {
+ return function (req, res, next) {
+ if (req.session.user && req.session.user.is_block) {
+ return res.send('您被屏蔽了。');
+ }
+ next();
+ };
+}
View
@@ -16,6 +16,7 @@ var UserSchema = new Schema({
avatar: { type: String },
githubId: { type: String, index: true },
githubUsername: {type: String},
+ is_block: {type: Boolean, default: false},
score: { type: Number, default: 0 },
topic_count: { type: Number, default: 0 },
View
@@ -67,6 +67,7 @@ module.exports = function (app) {
app.post('/user/un_follow', user.un_follow);
app.post('/user/set_star', user.toggle_star);
app.post('/user/cancel_star', user.toggle_star);
+ app.post('/user/:name/block', auth.adminRequired, user.block);
// message
app.post('/messages/mark_read', message.mark_read);
View
@@ -4,7 +4,7 @@ var Topic = require('../../proxy/topic');
exports.createUser = function (callback) {
var key = new Date().getTime() + '_' + Math.random();
- User.newAndSave('JacksonTian', 'Jackson' + key, 'pass', 'jackson' + key + '@domain.com', '', false, callback);
+ User.newAndSave('jackson' + key, 'jackson' + key, 'pass', 'jackson' + key + '@domain.com', '', false, callback);
};
exports.createTopic = function (authorId, callback) {
View
@@ -70,6 +70,13 @@
<% } else { %>
<button class='btn' id='set_star_btn' action='cancel_star'>取消达人</button>
<% } %>
+
+ <% if (!user.is_block) { %>
+ <button class='btn' id='set_block_btn' action='set_block'>屏蔽用户</button>
+ <% } else { %>
+ <button class='btn' id='set_block_btn' action='cancel_block'>取消屏蔽用户</button>
+ <% } %>
+
<br/><br/>
Email (Seen by Administrator): <a href="mailto:<%= user.email %>"><%= user.email %></a>
<% if (!user.active) { %>
@@ -134,6 +141,26 @@
}
}, 'json');
});
+
+ $('#set_block_btn').click(function () {
+ var $me = $(this);
+ var action = $me.attr('action');
+ var params = {
+ _csrf: '<%- csrf %>',
+ action: action
+ };
+ $.post('/user/<%- user.name %>/block', params, function (data) {
+ if (data.status === 'success') {
+ if (action === 'set_block') {
+ $me.html('取消屏蔽用户');
+ $me.attr('action', 'cancel_block');
+ } else if (action === 'cancel_block') {
+ $me.html('屏蔽用户');
+ $me.attr('action', 'set_block');
+ }
+ }
+ }, 'json');
+ })
});
</script>
<% } %>

0 comments on commit 7dfb258

Please sign in to comment.