From deea1c012bebf2d4fe86ea30e55ee8370f12aa51 Mon Sep 17 00:00:00 2001 From: alsotang Date: Sun, 26 Oct 2014 18:11:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E7=A6=BB=E5=B1=8F=E8=94=BD=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=92=8C=E5=88=A0=E9=99=A4=E5=8F=91=E8=A8=80=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/user.js | 32 +++++++++++++++++++++++--------- test/api/v1/tools.test.js | 4 ++-- test/api/v1/topic.test.js | 2 +- test/controllers/user.test.js | 26 ++++++++++++++++++++++++++ views/user/index.html | 21 +++++++++++++++++++-- web_router.js | 1 + 6 files changed, 72 insertions(+), 14 deletions(-) diff --git a/controllers/user.js b/controllers/user.js index a5e06ee..5609638 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -338,20 +338,13 @@ exports.block = function (req, res, next) { return next(new Error('user is not exists')); } if (action === 'set_block') { - ep.all('block_user', 'del_topics', 'del_replys', - function (user, topics, replys) { + ep.all('block_user', + function (user) { 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')); - // ep.emit('del_topics'); - // ep.emit('del_replys'); - // END 防止误操作,平时都注释 - } else if (action === 'cancel_block') { user.is_block = false; user.save(ep.done(function () { @@ -361,3 +354,24 @@ exports.block = function (req, res, next) { } })); }; + +exports.deleteAll = function (req, res, next) { + var loginname = req.params.name; + + var ep = EventProxy.create(); + ep.fail(next); + + User.getUserByLoginName(loginname, ep.done(function (user) { + if (!user) { + return next(new Error('user is not exists')); + } + ep.all('del_topics', 'del_replys', 'del_ups', + function () { + res.json({status: 'success'}); + }); + TopicModel.remove({author_id: user._id}, ep.done('del_topics')); + ReplyModel.remove({author_id: user._id}, ep.done('del_replys')); + // 点赞数也全部干掉 + ReplyModel.update({}, {$pull: {'ups': user._id}}, {multi: true}, ep.done('del_ups')); + })); +}; diff --git a/test/api/v1/tools.test.js b/test/api/v1/tools.test.js index 25f8aae..d8b8f6b 100644 --- a/test/api/v1/tools.test.js +++ b/test/api/v1/tools.test.js @@ -21,8 +21,8 @@ describe('test/api/v1/tools.test.js', function () { }) .end(function (err, res) { should.not.exists(err); - res.status.should.equal(302); - res.headers.location.should.equal('/api/v1/user/' + mockUser.loginname); + res.status.should.equal(200); + res.body.loginname.should.equal(mockUser.loginname); done(); }) }) diff --git a/test/api/v1/topic.test.js b/test/api/v1/topic.test.js index 75ed81d..fc99855 100644 --- a/test/api/v1/topic.test.js +++ b/test/api/v1/topic.test.js @@ -6,7 +6,7 @@ var should = require('should'); var support = require('../../support/support'); -describe.only('test/api/v1/topic.test.js', function () { +describe('test/api/v1/topic.test.js', function () { var mockUser, mockTopic; before(function (done) { support.createUser(function (err, user) { diff --git a/test/controllers/user.test.js b/test/controllers/user.test.js index 4b63bdf..9f85ff3 100644 --- a/test/controllers/user.test.js +++ b/test/controllers/user.test.js @@ -16,6 +16,7 @@ var support = require('../support/support'); var _ = require('lodash'); var pedding = require('pedding'); var UserProxy = require('../../proxy/user'); +var ReplyModel = require('../../models').Reply; describe('test/controllers/user.test.js', function () { var testUser; @@ -260,4 +261,29 @@ describe('test/controllers/user.test.js', function () { }) }) }) + + describe('#delete_all', function () { + it('should delele all ups', function (done) { + support.createUser(function (err, user) { + var userId = user._id; + ReplyModel.findOne(function (err, reply) { + reply.ups.push(userId); + reply.save(function (err, reply) { + reply.ups.should.containEql(userId) + + request.post('/user/' + user.loginname + '/delete_all') + .set('Cookie', support.adminUserCookie) + .expect(200, function (err, res) { + res.body.should.eql({ status: 'success' }); + + ReplyModel.findOne({_id: reply._id}, function (err, reply) { + reply.ups.should.not.containEql(userId) + done(); + }) + }) + }) + }) + }) + }) + }) }); diff --git a/views/user/index.html b/views/user/index.html index c525881..a925ee9 100644 --- a/views/user/index.html +++ b/views/user/index.html @@ -62,11 +62,13 @@ <% } %> <% if (!user.is_block) { %> - 屏蔽用户并删其所有帖 + 屏蔽用户 <% } else { %> 取消屏蔽用户 <% } %> + 删除所有发言 +

Email (Seen by Administrator): <%= user.email %> <% if (!user.active) { %> @@ -144,7 +146,7 @@ _csrf: '<%- csrf %>', action: action }; - if (action === 'set_block' && !confirm('确定要屏蔽该用户吗?此操作不可逆且会删除该用户的所有主题和回复!')) { + if (action === 'set_block' && !confirm('确定要屏蔽该用户吗?')) { return; } $.post('/user/<%- user.loginname %>/block', params, function (data) { @@ -159,6 +161,21 @@ } }, 'json'); }) + + $('#delete_all').click(function () { + var $me = $(this); + var params = { + _csrf: '<%- csrf %>', + }; + if (!confirm('确定要删除吗?此操作不可逆')) { + return; + } + $.post('/user/<%- user.loginname %>/delete_all', params, function (data) { + if (data.status === 'success') { + alert('操作成功'); + } + }, 'json'); + }) }); <% } %> diff --git a/web_router.js b/web_router.js index 22c9d8b..75d9072 100644 --- a/web_router.js +++ b/web_router.js @@ -61,6 +61,7 @@ router.get('/user/:name/replies', user.list_replies); // 用户参与的所有 router.post('/user/set_star', auth.adminRequired, user.toggle_star); // 把某用户设为达人 router.post('/user/cancel_star', auth.adminRequired, user.toggle_star); // 取消某用户的达人身份 router.post('/user/:name/block', auth.adminRequired, user.block); // 禁言某用户 +router.post('/user/:name/delete_all', auth.adminRequired, user.deleteAll); // 删除某用户所有发言 // message controler router.get('/my/messages', auth.userRequired, message.index); // 用户个人的所有消息页