Commit 104f8d2e authored by 陈志良's avatar 陈志良

1

parent 5cd8c8ae
......@@ -2,6 +2,7 @@
var express = require('express'),
router = express.Router(),
returnCode = require('../../utils/returnCode'),
then = require('thenjs'),
_ = require('lodash');
var mongoose = require('mongoose');
......@@ -11,6 +12,7 @@ var forumThreadService = require('../../service/forumThreadService');
var forumUserThreadControlService = require('../../service/forumUserThreadControlService');
var forumTagService=require('../../service/forumTagService');
var forumCommentService=require('../../service/forumCommentService');
var forumThreadAttendService = require('../../service/forumThreadAttendService');
var httpService = require('../../service/httpService');
var user = require('../../utils/user');
......@@ -542,3 +544,59 @@ router.get('/info/:fid/mobileList', function(req, res, next) {
res.json(returnCode.WRONG_PARAM);
}
});
//我的评论
router.get('/info/myAttend', function(req, res, next) {
var user_id = user.getMobileUser(req);
var pageNo = req.query.pageNo,
pageSize = req.query.pageSize;
var total = 0;
then(function(cont){
var conditions = {
attend:user_id
}
forumThreadAttendService.findMyAttentByPage(conditions,pageNo, pageSize,'-updated',function(err,attends){
if(err){
cont(err);
}else{
if(attends && attends.datas.length>0){
total = attends.total;
var threadIds = [];
_.forEach(attends.datas,function(a){
threadIds.push(a.thread);
});
cont(null,threadIds);
}else{
res.json(_.assign({data:[],total:0}, returnCode.SUCCESS));
}
}
});
}).then(function(cont,threadIds){
forumUserThreadControlService.getUserThreadControlById(user.getMobileUser(req), function(err, doc) {
cont(err,doc,threadIds);
});
}).then(function(cont,results,threadIds){
var conditions = {};
if(results){
conditions._id ={
$in:threadIds,
$nin:results
}
}else{
conditions._id ={
$in:threadIds
}
}
forumThreadService.getAllThreadByFid(conditions, pageNo, pageSize,'', function(err, threads) {
cont(err,threads);
});
}).then(function(cont,threads){
threads.total = total;
res.json(_.assign(threads, returnCode.SUCCESS));
}).then(function(cont){
}).fail(function(cont,err){
console.error(err);
res.json(returnCode.BUSY);
})
});
......@@ -14,6 +14,7 @@ var forumShareLogService = require('../../service/forumShareLogService');
var forumCommentService = require('../../service/forumCommentService');
var forumUserThreadControlService = require('../../service/forumUserThreadControlService');
var forumLimitActionRefService = require('../../service/forumLimitActionRefService');
var forumThreadAttendService = require('../../service/forumThreadAttendService');
var commentTips = require('../../utils/commentTips');
var httpService = require('../../service/httpService');
......@@ -508,6 +509,7 @@ router.post('/thread/:tid/comment/add', function(req, res, next) {
} else {
if(user.getMobileUser(req) !== thread.from){//文章的作者不等于评论的作者才增加消息数
commentTips.incrTips(req.session.user.ent_code,thread.from);
forumThreadAttendService.threadAttend(thread._id,user.getMobileUser(req),function(err){});
}
var comments = thread.comments;
// console.log('=========');
......@@ -655,6 +657,7 @@ router.post('/thread/:tid/comment/create', function(req, res, next) {
} else {
if(user.getMobileUser(req) !== thread.from){//文章的作者不等于评论的作者才增加消息数
commentTips.incrTips(req.session.user.ent_code,thread.from);
forumThreadAttendService.threadAttend(thread._id,user.getMobileUser(req),function(err){});
}
var comments = thread.comments;
// console.log('=========');
......@@ -816,6 +819,7 @@ router.post('/thread/:tid/comment/:cid/create', function(req, res, next) {
} else {
if(user.getMobileUser(req) !== req.body.to){//被评论人 和评论人不是同一个
commentTips.incrTips(req.session.user.ent_code,req.body.to);
forumThreadAttendService.threadAttend(tid,user.getMobileUser(req),function(err){});
}
var comments = comment.comments;
var array = [];
......@@ -1336,5 +1340,3 @@ router.post('/thread/:tid/disable', function(req, res, next) {
}
});
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
//话题,文章
var ForumThreadAttendSchema = new Schema({
ent_code: {
type: Number,
require: true,
index: true
},
thread:{//文章
type: Schema.Types.ObjectId,
index: true,
ref: 'ForumThread',
require:true
},
info: {
type: Schema.Types.ObjectId,
require: true,
index: true,
ref: 'ForumInfo'
},
from: { //发帖者
type: Schema.Types.ObjectId,
require: true,
index: true,
ref: 'ForumUser'
},
attend: [{ //关注者
type: Schema.Types.ObjectId,
require: true,
index: true,
ref: 'ForumUser'
}],
total: { //关注数量
type: Number,
require: true,
default: 0
},
created: { //创建时间
type: Date,
required: true,
default:Date.now
},
updated:{ //最后关注时间
type: Date,
required: true,
default:Date.now
}
}, {
'collection': 'pisns_forum_thread_attend'
});
module.exports = mongoose.model('ForumThreadAttend', ForumThreadAttendSchema);
\ No newline at end of file
'use strict';
//模型
var mongoose = require('mongoose'),
then = require('thenjs');
var ForumThread = mongoose.model('ForumThread'),
ForumThreadAttend = mongoose.model('ForumThreadAttend'),
ForumComment = mongoose.model('ForumComment'),
ForumUser = mongoose.model('ForumUser');
var forumUserService = require('./forumUserService');
var forumCommentService = require('./forumCommentService');
//创建或更新
exports.threadAttend=function(tid,uid,callback){
then.parallel([
function(cont){
ForumThread.findById(tid,function(err,doc){
cont(err,doc);
});
},
function(cont){
ForumThreadAttend.findOne({thread:tid},function(err,doc){
cont(err,doc);
});
}
]).then(function(cont,results){
var thread = results[0],
attend = results[1];
if(!thread){
cont('thread not exits');
}else if(!attend){
//创建记录
var entity = {
thread:tid,
info:thread.info,
from:thread.from,
attend:[],
total:0
}
var forum = new ForumThreadAttend(entity);
forum.save(function(err, newAttend) {
cont(err,thread,newAttend);
});
}else{
cont(null,thread,attend);
}
}).then(function(cont,thread,attend){
ForumThreadAttend.findOne({_id:attend._id,attend:uid},function(err,doc){
cont(err,doc);
});
}).then(function(cont,attend){
if(attend){
callback(null,true);
}else{
ForumThreadAttend.findOneAndUpdate({thread:tid},{updated:new Date(),$push:{attend:uid}},function(err,doc){
callback(err,err?false:true);
});
}
}).fail(function(cont,err){
console.error(err);
callback(err);
});
};
//根据发帖者分页查询话题列表
exports.findMyAttentByPage = function(conditions,pageNo, pageSize,sort,callback) {
then(function(cont) {
ForumThreadAttend.find(conditions).count(cont);
}).then(function(cont, count) {
var skip = (pageNo - 1) * pageSize;
var limit = count - skip > pageSize ? pageSize : (count - skip);
ForumThreadAttend.find(conditions).skip(skip).limit(limit).sort(sort || '-updated').exec(function(err, docs) {
cont(err, count, docs);
});
}).then(function(cont, count, threads) {
var rsJson = {
result: true,
total: count,
datas: threads
};
callback(null, rsJson);
}).fail(function(cont, err) {
console.error(err);
var rsJson = {
result: false,
err: err
};
callback(err, rsJson);
});
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment