Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion controllers/sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ exports.auth_user = function (req, res, next) {
return next(err);
}
user.messages_count = count;
req.session.user = user;
req.session.user = user.toObject({virtual: true});
res.locals.current_user = req.session.user;
return next();
});
Expand Down
90 changes: 57 additions & 33 deletions controllers/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
* Module dependencies.
*/

var _ = require('lodash');
var User = require('../proxy').User;
var Relation = require('../proxy').Relation;
var Topic = require('../proxy').Topic;
var config = require('../config').config;
var EventProxy = require('eventproxy');
Expand All @@ -20,9 +22,9 @@ setInterval(function () {
var limit = config.list_topic_count;
// 只缓存第一页, page = 1
var options = { skip: (1 - 1) * limit, limit: limit, sort: [ ['top', 'desc' ], [ 'last_reply_at', 'desc' ] ] };
var optionsStr = JSON.stringify(options);
var cacheKey = JSON.stringify([{}, options]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个地方不止有一个 cacheKey,所以还是叫 optionStr 吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个key现在包含了query和options,叫optionStr已经不合适了,我觉得在局部作用域内只有这一个cacheKey不会有命名冲突,等后面要加入其它缓存时再改名吧

Topic.getTopicsByQuery({}, options, function (err, topics) {
mcache.put(optionsStr, topics);
mcache.put(cacheKey, topics);
return topics;
});
}, 1000 * 5); // 五秒更新一次
Expand All @@ -42,21 +44,65 @@ exports.index = function (req, res, next) {
tops: tops,
no_reply_topics: no_reply_topics,
pages: pages,
site_links: config.site_links,
site_links: config.site_links
});
});
proxy.fail(next);

// 取主题
var options = { skip: (page - 1) * limit, limit: limit, sort: [ ['top', 'desc' ], [ 'last_reply_at', 'desc' ] ] };
var optionsStr = JSON.stringify(options);
if (mcache.get(optionsStr)) {
proxy.emit('topics', mcache.get(optionsStr));
proxy.on('blockings', function(blockIds) {
var query = {};
if (blockIds && blockIds.length > 0) {
query = {author_id: {$nin: blockIds}};
}

// 取主题
var options = { skip: (page - 1) * limit, limit: limit, sort: [ ['top', 'desc' ], [ 'last_reply_at', 'desc' ] ] };
var topicsCacheKey = JSON.stringify([query, options]);
if (mcache.get(topicsCacheKey)) {
proxy.emit('topics', mcache.get(topicsCacheKey));
} else {
Topic.getTopicsByQuery(query, options, proxy.done('topics', function (topics) {
return topics;
}));
}

// 取分页数据
var pagesCacheKey = JSON.stringify([query, 'pages']);
if (mcache.get(pagesCacheKey)) {
proxy.emit('pages', mcache.get(pagesCacheKey));
} else {
Topic.getCountByQuery(query, proxy.done(function (all_topics_count) {
var pages = Math.ceil(all_topics_count / limit);
mcache.put(pagesCacheKey, pages, 1000 * 60 * 1);
proxy.emit('pages', pages);
}));
}

// 取0回复的主题
if (mcache.get('no_reply_topics')) {
proxy.emit('no_reply_topics', mcache.get('no_reply_topics'));
} else {
Topic.getTopicsByQuery(
_.assign({ reply_count: 0 }, query),
{ limit: 5, sort: [ [ 'create_at', 'desc' ] ] },
proxy.done('no_reply_topics', function (no_reply_topics) {
mcache.put('no_reply_topics', no_reply_topics, 1000 * 60 * 1);
return no_reply_topics;
}));
}
});

if (req.session.user) {
Relation.getBlockings(
req.session.user._id,
proxy.done('blockings', function(rels) {
return _.pluck(rels, 'block_id');
})
);
} else {
Topic.getTopicsByQuery({}, options, proxy.done('topics', function (topics) {
return topics;
}));
proxy.emit('blockings');
}

// 取排行榜上的用户
if (mcache.get('tops')) {
proxy.emit('tops', mcache.get('tops'));
Expand All @@ -70,26 +116,4 @@ exports.index = function (req, res, next) {
})
);
}
// 取0回复的主题
if (mcache.get('no_reply_topics')) {
proxy.emit('no_reply_topics', mcache.get('no_reply_topics'));
} else {
Topic.getTopicsByQuery(
{ reply_count: 0 },
{ limit: 5, sort: [ [ 'create_at', 'desc' ] ] },
proxy.done('no_reply_topics', function (no_reply_topics) {
mcache.put('no_reply_topics', no_reply_topics, 1000 * 60 * 1);
return no_reply_topics;
}));
}
// 取分页数据
if (mcache.get('pages')) {
proxy.emit('pages', mcache.get('pages'));
} else {
Topic.getCountByQuery({}, proxy.done(function (all_topics_count) {
var pages = Math.ceil(all_topics_count / limit);
mcache.put('pages', pages, 1000 * 60 * 1);
proxy.emit('pages', pages);
}));
}
};
112 changes: 106 additions & 6 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ exports.index = function (req, res, next) {
recent_topics: recent_topics,
recent_replies: recent_replies,
relation: relation,
token: token,
token: token
});
};

Expand Down Expand Up @@ -208,7 +208,7 @@ exports.setting = function (req, res, next) {
};

exports.follow = function (req, res, next) {
var follow_id = req.body.follow_id;
var follow_id = req.body.target_id;
User.getUserById(follow_id, function (err, user) {
if (err) {
return next(err);
Expand All @@ -221,13 +221,13 @@ exports.follow = function (req, res, next) {
res.json({status: 'success'});
});
proxy.fail(next);
Relation.getRelation(req.session.user._id, user._id, proxy.done(function (doc) {
Relation.getFollowRelation(req.session.user._id, user._id, proxy.done(function (doc) {
if (doc) {
return proxy.emit('relation_saved');
}

// 新建关系并保存
Relation.newAndSave(req.session.user._id, user._id);
Relation.newAndSaveFollowRelation(req.session.user._id, user._id);
proxy.emit('relation_saved');

User.getUserById(req.session.user._id, proxy.done(function (me) {
Expand All @@ -251,7 +251,7 @@ exports.un_follow = function (req, res, next) {
res.send('forbidden!');
return;
}
var follow_id = req.body.follow_id;
var follow_id = req.body.target_id;
User.getUserById(follow_id, function (err, user) {
if (err) {
return next(err);
Expand Down Expand Up @@ -292,6 +292,82 @@ exports.un_follow = function (req, res, next) {
});
};

exports.user_block = function (req, res, next) {
var block_id = req.body.target_id;
User.getUserById(block_id, function (err, user) {
if (err) {
return next(err);
}
if (!user) {
res.json({status: 'failed'});
}

var proxy = EventProxy.create('relation_saved', 'message_saved', function () {
res.json({status: 'success'});
});
proxy.fail(next);
Relation.getBlockRelation(req.session.user._id, user._id, proxy.done(function (doc) {
if (doc) {
return proxy.emit('relation_saved');
}

// 新建关系并保存
Relation.newAndSaveBlockRelation(req.session.user._id, user._id);
proxy.emit('relation_saved');

User.getUserById(req.session.user._id, proxy.done(function (me) {
me.blocking_count += 1;
me.save();
}));

req.session.user.blocking_count += 1;
}));

message.sendBlockMessage(block_id, req.session.user._id);
proxy.emit('message_saved');
});
};

exports.user_unblock = function (req, res, next) {
if (!req.session || !req.session.user) {
res.send('forbidden!');
return;
}
var block_id = req.body.target_id;
User.getUserById(block_id, function (err, user) {
if (err) {
return next(err);
}
if (!user) {
res.json({status: 'failed'});
return;
}
// 删除关系
Relation.remove(req.session.user._id, user._id, function (err) {
if (err) {
return next(err);
}
res.json({status: 'success'});
});

User.getUserById(req.session.user._id, function (err, me) {
if (err) {
return next(err);
}
me.blocking_count -= 1;
if (me.blocking_count < 0) {
me.blocking_count = 0;
}
me.save();
});

req.session.user.blocking_count -= 1;
if (req.session.user.blocking_count < 0) {
req.session.user.blocking_count = 0;
}
});
};

exports.toggle_star = function (req, res, next) {
if (!req.session.user || !req.session.user.is_admin) {
res.send('forbidden!');
Expand Down Expand Up @@ -410,7 +486,7 @@ exports.get_followers = function (req, res, next) {
}
var proxy = new EventProxy();
proxy.fail(next);
Relation.getRelationsByUserId(user._id, proxy.done(function (docs) {
Relation.getFollowers(user._id, proxy.done(function (docs) {
var ids = [];
for (var i = 0; i < docs.length; i++) {
ids.push(docs[i].user_id);
Expand All @@ -422,6 +498,30 @@ exports.get_followers = function (req, res, next) {
});
};

exports.get_blockings = function (req, res, next) {
var name = req.params.name;
User.getUserByName(name, function (err, user) {
if (err || !user) {
return next(err);
}
Relation.getBlockings(user._id, function (err, docs) {
if (err) {
return next(err);
}
var ids = [];
for (var i = 0; i < docs.length; i++) {
ids.push(docs[i].block_id);
}
User.getUsersByIds(ids, function (err, users) {
if (err) {
return next(err);
}
res.render('user/blockings', { users: users, user: user });
});
});
});
};

exports.top100 = function (req, res, next) {
var opt = {limit: 100, sort: [['score', 'desc']]};
User.getUsersByQuery({'$or': [
Expand Down
1 change: 1 addition & 0 deletions models/relation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var ObjectId = Schema.ObjectId;
var RelationSchema = new Schema({
user_id: { type: ObjectId },
follow_id: { type: ObjectId },
block_id: { type: ObjectId },
create_at: { type: Date, default: Date.now }
});

Expand Down
1 change: 1 addition & 0 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var UserSchema = new Schema({
reply_count: { type: Number, default: 0 },
follower_count: { type: Number, default: 0 },
following_count: { type: Number, default: 0 },
blocking_count: { type: Number, default: 0 },
collect_tag_count: { type: Number, default: 0 },
collect_topic_count: { type: Number, default: 0 },
create_at: { type: Date, default: Date.now },
Expand Down
Loading