Skip to content

Commit

Permalink
在topic页面增加用户关注自动判断
Browse files Browse the repository at this point in the history
还有一些小更新,如更改用户排行榜样式,前20位增加不同显示
  • Loading branch information
kennyz committed Mar 13, 2012
1 parent 4fa8572 commit c7f4530
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ config.js
node_modules
.naeindex
public/user_data

.monitor
Binary file modified History.md
Binary file not shown.
3 changes: 2 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ app.configure('production', function(){
routes(app);

app.listen(config.port);
//console.log("NodeClub listening on port %d in %s mode", app.address().port, app.settings.env);
console.log("NodeClub listening on port %d in %s mode", app.address().port, app.settings.env);
console.log("God bless love....");
console.log("You can debug your app with http://localhost:"+app.address().port);
2 changes: 1 addition & 1 deletion config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports.config = {

// site settings
site_headers: [
'<meta name="author" content="慕远@TaoBao" />',
'<meta name="author" content="EDP@TAOBAO" />',
],
host: 'http://127.0.0.1', // host 结尾不要添加'/'
site_logo: '', // default is `name`
Expand Down
3 changes: 2 additions & 1 deletion controllers/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ exports.index = function(req,res,next){
});
var recent_tags = tags.slice(0,5);

res.render('index',{tags:all_tags,topics:topics,current_page:page,list_topic_count:limit,hot_tags:hot_tags,recent_tags:recent_tags,
//res.render('index',{tags:all_tags,topics:topics,current_page:page,list_topic_count:limit,hot_tags:hot_tags,recent_tags:recent_tags,
res.render('index',{tags:all_tags,topics:topics,current_page:page,list_topic_count:limit,recent_tags:recent_tags,
hot_topics:hot_topics,stars:stars,tops:tops,no_reply_topics:no_reply_topics,pages:pages});
};

Expand Down
25 changes: 20 additions & 5 deletions controllers/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var Tag = models.Tag;
var Topic = models.Topic;
var TopicTag = models.TopicTag;
var TopicCollect = models.TopicCollect;
var Relation = models.Relation;
var check = require('validator').check;
var sanitize = require('validator').sanitize;
var at_ctrl = require('./at');
Expand All @@ -33,12 +34,13 @@ exports.index = function(req, res, next) {
if (topic_id.length !== 24) {
return res.render('notify/notify', { error: '此话题不存在或已被删除。' });
}
var events = [ 'topic', 'other_topics', 'no_reply_topics', '@user' ];
var ep = EventProxy.create(events, function(topic, other_topics, no_reply_topics) {
var events = [ 'topic', 'other_topics', 'no_reply_topics', 'get_relation', '@user' ];
var ep = EventProxy.create(events, function(topic, other_topics, no_reply_topics, relation) {
res.render('topic/index', {
topic: topic,
author_other_topics: other_topics,
no_reply_topics: no_reply_topics
no_reply_topics: no_reply_topics,
relation : relation
});
});
ep.on('error', function(err) {
Expand All @@ -59,6 +61,7 @@ exports.index = function(req, res, next) {
return res.render('notify/notify', { error: message });
}


at_ctrl.link_at_who(topic.content, function(err, content) {
if (err) return ep.emit('error', err);
topic.content = content;
Expand Down Expand Up @@ -91,12 +94,23 @@ exports.index = function(req, res, next) {
}
});

//get author's relationship
if(!req.session.user._id){
ep.emit('get_relation',null);
}else{

Relation.findOne({user_id:req.session.user._id, follow_id: topic.author_id},function(err,relation){
if (err) return ep.emit('error', err);
ep.emit('get_relation',relation);
});
}

// get author other topics
var options = { limit: 5, sort: [ [ 'last_reply_at', 'desc' ] ]};
var query = { author_id: topic.author_id, _id: { '$nin': [ topic._id ] } };
get_topics_by_query(query, options, function(err,topics){
if (err) return ep.emit('error', err);
ep.emit('other_topics', topics);
ep.emit('other_topics', topics);
});

// get no reply topics
Expand Down Expand Up @@ -545,12 +559,13 @@ function get_full_topic(id,cb){
return cb(null, '话题的作者丢了。');
}
proxy.trigger('author',author);
});
});

reply_ctrl.get_replies_by_topic_id(topic._id,function(err,replies){
if(err) return cb(err);
proxy.trigger('replies',replies);
});

});

}
Expand Down
3 changes: 2 additions & 1 deletion controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var EventProxy = require('eventproxy').EventProxy;

var check = require('validator').check,
sanitize = require('validator').sanitize;

var crypto = require('crypto');

exports.index = function(req,res,next){
Expand Down Expand Up @@ -63,6 +63,7 @@ exports.index = function(req,res,next){
}else{
Relation.findOne({user_id:req.session.user._id,follow_id:user._id},function(err,doc){
if(err) return next(err);

proxy.trigger('relation',doc);
});
}
Expand Down
6 changes: 3 additions & 3 deletions views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<div id='content'>
<div class='panel'>
<div class='moon'>
<img src='/images/tag_icon&16.png' title='标签'/>
<div class='moon'>
<% if (locals.current_user && current_user.is_admin) { %>
<img src='/images/tag_icon&16.png' title='标签'/>
<span class='fr'><a href='/tags/edit'><img src='/images/wrench_icon&16.png' title='编辑标签' /></a></span>
<% } %>
</div>
Expand All @@ -18,7 +18,7 @@
<div class='header'>
<img src='/images/spechbubble_2_icon&16.png' title='话题'/>
<% if (locals.current_user) { %>
<a href='/topic/create' class='fr'><button class='btn' id='create_topic_btn'>发布话题</button></a>
<a href='/topic/create' class='fr'><button class='btn btn-success fr' id='create_topic_btn'>发布话题</button></a>
<% } %>
</div>
<% if (locals.topics && topics.length > 0) { %>
Expand Down
2 changes: 1 addition & 1 deletion views/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
</div>
<div class='sep10'></div>
<div class='col_fade'>
&copy; 2012 <br>版本: <%= config.version %> <br>
&copy; 2012 <br>本社区为开源系统,版本: <%= config.version %> ,欢迎贡献代码<br>
<a href='http://cnodejs.net' target='_blank'> NAE </a><%= config.name %> 提供动力
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion views/reply/reply2.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</span>
<span class='fr'>
<% if(locals.current_user){ %>
<a href='javascript:void(0);' class='dark reply2_at_btn'>@</a>
<a href='javascript:void(0);' class='dark reply2_at_btn'>@回复</a>
<% } %>
</span>

Expand Down
38 changes: 20 additions & 18 deletions views/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@
</div>
<% } %>
</div>

<% if (locals.tops) { %>
<div class='sep10'></div>
<div class='panel'>
<div class='header'>
<span class='col_fade'>积分榜</span>
</div>
<div class='inner'>
<% if (tops.length > 0) { %>
<ol>
<%- partial('user/top', { collection: tops, as: 'user' }) %>
</ol>
<a class='dark' href='/users/top100'>TOP 100 &gt;&gt;</a>
<% } else { %>
<p></p>
<% } %>
</div>
</div>
<% } %>

<% if (locals.hot_tags) { %>
<div class='sep10'></div>
<div class='panel'>
Expand Down Expand Up @@ -68,24 +88,6 @@
</div>
<% } %>

<% if (locals.tops) { %>
<div class='sep10'></div>
<div class='panel'>
<div class='header'>
<span class='col_fade'>积分榜</span>
</div>
<div class='inner'>
<% if (tops.length > 0) { %>
<ol>
<%- partial('user/top', { collection: tops, as: 'user' }) %>
</ol>
<a class='dark' href='/users/top100'>Top100»</a>
<% } else { %>
<p></p>
<% } %>
</div>
</div>
<% } %>

<% if (locals.stars) { %>
<div class='sep10'></div>
Expand Down
43 changes: 42 additions & 1 deletion views/user/card.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,61 @@
<% if(user.avatar){ %>
<a href='/user/<%= user.name %>'><img class='user_avatar' src='<%= user.avatar %>' title='<%= user.name %>' /></a>
<% }else{ %>
<a href='/user/<%= user.name %>'><img class='user_avatar' src='/images/user_icon&48.png' title='<%= user.name %>' /></a>
<a href='/user/<%= user.name %>'><img class='user_avatar' src='<%- config.site_static_host %>/images/user_icon&48.png' title='<%= user.name %>' /></a>
<% } %>
<span class='sp10'></span>
<span class='user_name'><a class='dark' href='/user/<%= user.name %>'><%= user.name %></a></span>

<% if (locals.current_user && current_user._id != user._id) { %>
<% if (!locals.relation) { %>
<button class='btn btn-success fr' id='follow_btn' action='follow'>加入关注</button>
<% } else { %>
<button class='btn fr' id='follow_btn' action='un_follow'>取消关注</button>
<% } %>
<% } %>

<div class='board cl'>
<div class='floor'><a href='/my/tags/'><span class='big'><%= user.collect_tag_count %></span> 标签收藏</a></div>
<div class='floor'><a href='/my/topics'><span class='big'><%= user.collect_topic_count %></span> 话题收藏</a></div>
<div class='floor'>
<a href='/my/following'><span class='big'><%= user.following_count %></span> 关注</a>
<a href='/my/follower'><span class='big'><%= user.follower_count %></span> 粉丝</a>



</div>

<% if(user.messages_count > 0){ %>
<div class='floor'><a href='/my/messages'><span class='big messages_count'><%= user.messages_count %></span> 消息</a></div>
<% } %>
</div>
</div>
</div>

<% if (locals.current_user) { %>
<script type='text/javascript'>
$(document).ready(function(){

$('#follow_btn').click(function(){
var $me = $(this);
var action = $me.attr('action');
var params = {
follow_id: '<%= user._id %>',
_csrf: '<%- csrf %>'
};
$.post('/user/' + action, params, function(data) {
if (data.status === 'success') {
if (action === 'follow') {
$me.html('取消关注');
$me.attr('action','un_follow');
} else {
$me.html('加入关注');
$me.attr('action','follow');
}
$me.toggleClass('btn-success');
}
}, 'json');
});
});
</script>
<% } %>
2 changes: 2 additions & 0 deletions views/user/top100.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<thead>
<th>#</th>
<th>用户名</th>
<th>粉丝数</th>
<th>文章数</th>
<th>积分</th>
</thead>
<tbody>
Expand Down
19 changes: 17 additions & 2 deletions views/user/top100_user.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<tr>
<td><%= indexInCollection+1 %></td>
<td><a href='/user/<%= user.name %>'><%= user.name %></a></td>
<% if(indexInCollection<20) { %>
<td><b><%= indexInCollection+1 %></b></td>
<td>
<% if(user.avatar){ %>
<a href='/user/<%= user.name %>'><img class='user_avatar' src='<%= user.avatar %>' title='<%= user.name %>' /></a>
<% }else{ %>
<a href='/user/<%= user.name %>'><img class='user_avatar' src='<%- config.site_static_host %>/images/user_icon&48.png' title='<%= user.name %>' /></a>
<% } %>
<span class='sp10'></span>
<a href='/user/<%= user.name %>'><%= user.name %></a></td>
<% } else { %>
<td><%= indexInCollection+1 %></td>
<td><a href='/user/<%= user.name %>'><%= user.name %></a></td>
<% } %>

<td><%= user.follower_count %></td>
<td><%= user.topic_count %></td>
<td><%= user.score %></td>
</tr>

0 comments on commit c7f4530

Please sign in to comment.