|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const _ = require('lodash'); |
| 4 | +const utility = require('utility'); |
| 5 | + |
| 6 | +const Controller = require('egg').Controller; |
| 7 | + |
| 8 | +class UserController extends Controller { |
| 9 | + |
| 10 | + async index() { |
| 11 | + const { ctx, service, config } = this; |
| 12 | + const user_name = ctx.params.name; |
| 13 | + const user = await ctx.service.user.getUserByLoginName(user_name); |
| 14 | + if (!user) { |
| 15 | + ctx.status = 404; |
| 16 | + ctx.message = '这个用户不存在。'; |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + let query = { author_id: user._id }; |
| 21 | + const opt = { limit: 5, sort: '-create_at' }; |
| 22 | + const [ |
| 23 | + recent_topics, replies, |
| 24 | + ] = await Promise.all([ |
| 25 | + service.topic.getTopicsByQuery(query, opt), |
| 26 | + service.reply.getRepliesByAuthorId(user._id, { limit: 20, sort: '-create_at' }), |
| 27 | + ]); |
| 28 | + |
| 29 | + const topic_ids = [ new new Set(...replies.map(function(reply) { |
| 30 | + return reply.topic_id.toString(); |
| 31 | + }))() ].slice(0, 5); // 只显示最近5条 |
| 32 | + |
| 33 | + query = { _id: { $in: topic_ids } }; |
| 34 | + let recent_replies = await service.topic.getTopicsByQuery(query, {}); |
| 35 | + |
| 36 | + recent_replies = _.sortBy(recent_replies, function(topic) { |
| 37 | + return topic_ids.indexOf(topic._id.toString()); |
| 38 | + }); |
| 39 | + |
| 40 | + user.url = (function() { |
| 41 | + if (user.url && user.url.indexOf('http') !== 0) { |
| 42 | + return 'http://' + user.url; |
| 43 | + } |
| 44 | + return user.url; |
| 45 | + })(); |
| 46 | + |
| 47 | + // 如果用户没有激活,那么管理员可以帮忙激活 |
| 48 | + let token = ''; |
| 49 | + if (!user.active && ctx.session.user && ctx.session.user.is_admin) { |
| 50 | + token = utility.md5(user.email + user.pass + config.session_secret); |
| 51 | + } |
| 52 | + |
| 53 | + await ctx.render('user/index', { |
| 54 | + user, |
| 55 | + recent_topics, |
| 56 | + recent_replies, |
| 57 | + token, |
| 58 | + pageTitle: `@${user.loginname} 的个人主页`, |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + async listStars() { |
| 63 | + const { ctx, service } = this; |
| 64 | + const stars = await service.user.getUsersByQuery({ is_star: true }, {}); |
| 65 | + await ctx.render('user/stars', { stars }); |
| 66 | + } |
| 67 | + |
| 68 | + async top100() { |
| 69 | + const { ctx, service } = this; |
| 70 | + const opt = { limit: 100, sort: '-score' }; |
| 71 | + const tops = await service.user.getUsersByQuery({ is_block: false }, opt); |
| 72 | + await ctx.render('user/top100', { |
| 73 | + users: tops, |
| 74 | + pageTitle: 'top100', |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + async listCollectedTopics() { |
| 79 | + const { ctx, service } = this; |
| 80 | + const name = ctx.params.name; |
| 81 | + const page = Number(ctx.query.page) || 1; |
| 82 | + const limit = this.config.list_topic_count; |
| 83 | + |
| 84 | + const user = await service.user.getUserByLoginName(name); |
| 85 | + |
| 86 | + if (!user) { |
| 87 | + ctx.status = 404; |
| 88 | + ctx.message = '这个用户不存在。'; |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + const pages = Math.ceil(user.collect_topic_count / limit); |
| 93 | + const opt = { |
| 94 | + skip: (page - 1) * limit, |
| 95 | + limit, |
| 96 | + }; |
| 97 | + |
| 98 | + const collects = await service.topicCollect.getTopicCollectsByUserId(user._id, opt); |
| 99 | + const ids = collects.map(function(doc) { |
| 100 | + return String(doc.topic_id); |
| 101 | + }); |
| 102 | + const query = { _id: { $in: ids } }; |
| 103 | + let topics = await service.topic.getTopicsByQuery(query, {}); |
| 104 | + topics = _.sortBy(topics, function(topic) { |
| 105 | + return ids.indexOf(String(topic._id)); |
| 106 | + }); |
| 107 | + |
| 108 | + await ctx.render('user/collect_topics', { |
| 109 | + topics, |
| 110 | + current_page: page, |
| 111 | + pages, |
| 112 | + user, |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + async listTopics() { |
| 117 | + const { ctx, service, config } = this; |
| 118 | + const user_name = ctx.params.name; |
| 119 | + const page = Number(ctx.query.page) || 1; |
| 120 | + const limit = config.list_topic_count; |
| 121 | + |
| 122 | + const user = await service.user.getUserByLoginName(user_name); |
| 123 | + |
| 124 | + if (!user) { |
| 125 | + ctx.status = 404; |
| 126 | + ctx.message = '这个用户不存在。'; |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + const query = { author_id: user._id }; |
| 131 | + const opt = { skip: (page - 1) * limit, limit, sort: '-create_at' }; |
| 132 | + const [ topics, all_topics_count ] = await Promise.all([ |
| 133 | + service.topic.getTopicsByQuery(query, opt), |
| 134 | + service.topic.getCountByQuery(query), |
| 135 | + ]); |
| 136 | + const pages = Math.ceil(all_topics_count / limit); |
| 137 | + |
| 138 | + await ctx.render('user/topics', { |
| 139 | + user, |
| 140 | + topics, |
| 141 | + current_page: page, |
| 142 | + pages, |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + async listReplies() { |
| 147 | + const { ctx, service } = this; |
| 148 | + const user_name = ctx.params.name; |
| 149 | + const page = Number(ctx.query.page) || 1; |
| 150 | + const limit = 50; |
| 151 | + |
| 152 | + const user = await service.user.getUserByLoginName(user_name); |
| 153 | + if (!user) { |
| 154 | + ctx.status = 404; |
| 155 | + ctx.message = '这个用户不存在。'; |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + const opt = { skip: (page - 1) * limit, limit, sort: '-create_at' }; |
| 160 | + const replies = await service.reply.getRepliesByAuthorId(user._id, opt); |
| 161 | + const topic_ids = [ ...new Set([ ...replies.map(function(reply) { |
| 162 | + return reply.topic_id.toString(); |
| 163 | + }) ]) ]; |
| 164 | + // 获取所有有评论的主题 |
| 165 | + const query = { _id: { $in: topic_ids } }; |
| 166 | + let topics = await service.topic.getTopicsByQuery(query, {}); |
| 167 | + topics = _.sortBy(topics, function(topic) { |
| 168 | + return topic_ids.indexOf(topic._id.toString()); |
| 169 | + }); |
| 170 | + const count = await service.reply.getCountByAuthorId(user._id); |
| 171 | + const pages = Math.ceil(count / limit); |
| 172 | + |
| 173 | + await ctx.render('user/replies', { |
| 174 | + user, |
| 175 | + topics, |
| 176 | + current_page: page, |
| 177 | + pages, |
| 178 | + }); |
| 179 | + } |
| 180 | + |
| 181 | +} |
| 182 | + |
| 183 | +// var User = require('../proxy').User; |
| 184 | +// var Topic = require('../proxy').Topic; |
| 185 | +// var Reply = require('../proxy').Reply; |
| 186 | +// var TopicCollect = require('../proxy').TopicCollect; |
| 187 | +// var utility = require('utility'); |
| 188 | +// var util = require('util'); |
| 189 | +// var TopicModel = require('../models').Topic; |
| 190 | +// var ReplyModel = require('../models').Reply; |
| 191 | +// var tools = require('../common/tools'); |
| 192 | +// var config = require('../config'); |
| 193 | +// var EventProxy = require('eventproxy'); |
| 194 | +// var validator = require('validator'); |
| 195 | +// var _ = require('lodash'); |
| 196 | + |
| 197 | + |
| 198 | +// exports.showSetting = function (req, res, next) { |
| 199 | +// User.getUserById(req.session.user._id, function (err, user) { |
| 200 | +// if (err) { |
| 201 | +// return next(err); |
| 202 | +// } |
| 203 | +// if (req.query.save === 'success') { |
| 204 | +// user.success = '保存成功。'; |
| 205 | +// } |
| 206 | +// user.error = null; |
| 207 | +// return res.render('user/setting', user); |
| 208 | +// }); |
| 209 | +// }; |
| 210 | + |
| 211 | +// exports.setting = function (req, res, next) { |
| 212 | +// var ep = new EventProxy(); |
| 213 | +// ep.fail(next); |
| 214 | + |
| 215 | +// // 显示出错或成功信息 |
| 216 | +// function showMessage(msg, data, isSuccess) { |
| 217 | +// data = data || req.body; |
| 218 | +// var data2 = { |
| 219 | +// loginname: data.loginname, |
| 220 | +// email: data.email, |
| 221 | +// url: data.url, |
| 222 | +// location: data.location, |
| 223 | +// signature: data.signature, |
| 224 | +// weibo: data.weibo, |
| 225 | +// accessToken: data.accessToken, |
| 226 | +// }; |
| 227 | +// if (isSuccess) { |
| 228 | +// data2.success = msg; |
| 229 | +// } else { |
| 230 | +// data2.error = msg; |
| 231 | +// } |
| 232 | +// res.render('user/setting', data2); |
| 233 | +// } |
| 234 | + |
| 235 | +// // post |
| 236 | +// var action = req.body.action; |
| 237 | +// if (action === 'change_setting') { |
| 238 | +// var url = validator.trim(req.body.url); |
| 239 | +// var location = validator.trim(req.body.location); |
| 240 | +// var weibo = validator.trim(req.body.weibo); |
| 241 | +// var signature = validator.trim(req.body.signature); |
| 242 | + |
| 243 | +// User.getUserById(req.session.user._id, ep.done(function (user) { |
| 244 | +// user.url = url; |
| 245 | +// user.location = location; |
| 246 | +// user.signature = signature; |
| 247 | +// user.weibo = weibo; |
| 248 | +// user.save(function (err) { |
| 249 | +// if (err) { |
| 250 | +// return next(err); |
| 251 | +// } |
| 252 | +// req.session.user = user.toObject({virtual: true}); |
| 253 | +// return res.redirect('/setting?save=success'); |
| 254 | +// }); |
| 255 | +// })); |
| 256 | +// } |
| 257 | +// if (action === 'change_password') { |
| 258 | +// var old_pass = validator.trim(req.body.old_pass); |
| 259 | +// var new_pass = validator.trim(req.body.new_pass); |
| 260 | +// if (!old_pass || !new_pass) { |
| 261 | +// return res.send('旧密码或新密码不得为空'); |
| 262 | +// } |
| 263 | + |
| 264 | +// User.getUserById(req.session.user._id, ep.done(function (user) { |
| 265 | +// tools.bcompare(old_pass, user.pass, ep.done(function (bool) { |
| 266 | +// if (!bool) { |
| 267 | +// return showMessage('当前密码不正确。', user); |
| 268 | +// } |
| 269 | + |
| 270 | +// tools.bhash(new_pass, ep.done(function (passhash) { |
| 271 | +// user.pass = passhash; |
| 272 | +// user.save(function (err) { |
| 273 | +// if (err) { |
| 274 | +// return next(err); |
| 275 | +// } |
| 276 | +// return showMessage('密码已被修改。', user, true); |
| 277 | + |
| 278 | +// }); |
| 279 | +// })); |
| 280 | +// })); |
| 281 | +// })); |
| 282 | +// } |
| 283 | + |
| 284 | +// exports.toggleStar = function (req, res, next) { |
| 285 | +// var user_id = req.body.user_id; |
| 286 | +// User.getUserById(user_id, function (err, user) { |
| 287 | +// if (err) { |
| 288 | +// return next(err); |
| 289 | +// } |
| 290 | +// if (!user) { |
| 291 | +// return next(new Error('user is not exists')); |
| 292 | +// } |
| 293 | +// user.is_star = !user.is_star; |
| 294 | +// user.save(function (err) { |
| 295 | +// if (err) { |
| 296 | +// return next(err); |
| 297 | +// } |
| 298 | +// res.json({ status: 'success' }); |
| 299 | +// }); |
| 300 | +// }); |
| 301 | +// } |
| 302 | + |
| 303 | +// exports.block = function (req, res, next) { |
| 304 | +// var loginname = req.params.name; |
| 305 | +// var action = req.body.action; |
| 306 | + |
| 307 | +// var ep = EventProxy.create(); |
| 308 | +// ep.fail(next); |
| 309 | + |
| 310 | +// User.getUserByLoginName(loginname, ep.done(function (user) { |
| 311 | +// if (!user) { |
| 312 | +// return next(new Error('user is not exists')); |
| 313 | +// } |
| 314 | +// if (action === 'set_block') { |
| 315 | +// ep.all('block_user', |
| 316 | +// function (user) { |
| 317 | +// res.json({status: 'success'}); |
| 318 | +// }); |
| 319 | +// user.is_block = true; |
| 320 | +// user.save(ep.done('block_user')); |
| 321 | + |
| 322 | +// } else if (action === 'cancel_block') { |
| 323 | +// user.is_block = false; |
| 324 | +// user.save(ep.done(function () { |
| 325 | + |
| 326 | +// res.json({status: 'success'}); |
| 327 | +// })); |
| 328 | +// } |
| 329 | +// })); |
| 330 | +// }; |
| 331 | + |
| 332 | +// exports.deleteAll = function (req, res, next) { |
| 333 | +// var loginname = req.params.name; |
| 334 | + |
| 335 | +// var ep = EventProxy.create(); |
| 336 | +// ep.fail(next); |
| 337 | + |
| 338 | +// User.getUserByLoginName(loginname, ep.done(function (user) { |
| 339 | +// if (!user) { |
| 340 | +// return next(new Error('user is not exists')); |
| 341 | +// } |
| 342 | +// ep.all('del_topics', 'del_replys', 'del_ups', |
| 343 | +// function () { |
| 344 | +// res.json({status: 'success'}); |
| 345 | +// }); |
| 346 | +// // 删除主题 |
| 347 | +// TopicModel.update({author_id: user._id}, {$set: {deleted: true}}, {multi: true}, ep.done('del_topics')); |
| 348 | +// // 删除评论 |
| 349 | +// ReplyModel.update({author_id: user._id}, {$set: {deleted: true}}, {multi: true}, ep.done('del_replys')); |
| 350 | +// // 点赞数也全部干掉 |
| 351 | +// ReplyModel.update({}, {$pull: {'ups': user._id}}, {multi: true}, ep.done('del_ups')); |
| 352 | +// })); |
| 353 | +// }; |
| 354 | + |
| 355 | +module.exports = UserController; |
0 commit comments