Skip to content

Commit

Permalink
话题收藏
Browse files Browse the repository at this point in the history
  • Loading branch information
alsotang committed Mar 15, 2016
1 parent 5d8fbd7 commit 072cca9
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 51 deletions.
13 changes: 10 additions & 3 deletions controllers/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,20 @@ exports.index = function (req, res, next) {
}

var topic_id = req.params.tid;
var currentUser = req.session.user;

if (topic_id.length !== 24) {
return res.render404('此话题不存在或已被删除。');
}
var events = ['topic', 'other_topics', 'no_reply_topics'];
var ep = EventProxy.create(events, function (topic, other_topics, no_reply_topics) {
var events = ['topic', 'other_topics', 'no_reply_topics', 'is_collect'];
var ep = EventProxy.create(events,
function (topic, other_topics, no_reply_topics, is_collect) {
res.render('topic/index', {
topic: topic,
author_other_topics: other_topics,
no_reply_topics: no_reply_topics,
is_uped: isUped
is_uped: isUped,
is_collect: is_collect,
});
});

Expand Down Expand Up @@ -98,6 +102,8 @@ exports.index = function (req, res, next) {
}
}));
}));

TopicCollect.getTopicCollect(currentUser._id, topic_id, ep.done('is_collect'))
};

exports.create = function (req, res, next) {
Expand Down Expand Up @@ -355,6 +361,7 @@ exports.lock = function (req, res, next) {
// 收藏主题
exports.collect = function (req, res, next) {
var topic_id = req.body.topic_id;

Topic.getTopic(topic_id, function (err, topic) {
if (err) {
return next(err);
Expand Down
2 changes: 1 addition & 1 deletion middlewares/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports.adminRequired = function (req, res, next) {
* 需要登录
*/
exports.userRequired = function (req, res, next) {
if (!req.session || !req.session.user) {
if (!req.session || !req.session.user || !req.session.user._id) {
return res.status(403).send('forbidden!');
}

Expand Down
2 changes: 2 additions & 0 deletions models/topic_collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ var TopicCollectSchema = new Schema({
});

TopicCollectSchema.plugin(BaseModel);
TopicCollectSchema.index({user_id: 1});

mongoose.model('TopicCollect', TopicCollectSchema);
5 changes: 1 addition & 4 deletions public/stylesheets/style.less
Original file line number Diff line number Diff line change
Expand Up @@ -673,10 +673,6 @@ img.unread {
clear: left;
}

#collect_btn {

}

#backtotop {
width: 24px;
color: gray;
Expand Down Expand Up @@ -1249,3 +1245,4 @@ textarea.editor {
.cnode-app-download {
text-align: center;
}

88 changes: 46 additions & 42 deletions views/topic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,53 +70,57 @@
<% if (topic.tab) { %>
<span> 来自 <%= topic.tabName %></span>
<%}%>

<input class="span-common <%= is_collect ? '' : 'span-success' %> pull-right collect_btn" type="submit" value="<%= is_collect ? '取消收藏' : '收藏' %>" action="<%= is_collect ? 'de_collect' : 'collect' %>">

</div>
<% if (current_user) { %>
<div id="manage_topic">
<% if (current_user.is_admin) { %>

<a href='/topic/<%= topic._id %>/top' data-method="post">
<% if (topic.top) { %>
<i class="fa fa-lg fa-star-o" title='取消置顶'></i>
<a href='/topic/<%= topic._id %>/top' data-method="post">
<% if (topic.top) { %>
<i class="fa fa-lg fa-star-o" title='取消置顶'></i>
<% } else { %>
<i class="fa fa-lg fa-star" title='置顶'/></i>
<% } %>
</a>


<a href='/topic/<%= topic._id %>/good' data-method="post">
<% if (topic.good) { %>
<i class="fa fa-lg fa-heart-o" title="取消精华"></i>
<% } else { %>
<i class="fa fa-lg fa-heart" title="加精华"></i>
<% } %>
</a>

<a href='/topic/<%= topic._id %>/lock' data-method="post">
<% if (topic.lock) { %>
<i class="fa fa-lg fa-unlock" title='取消锁定'></i>
<% } else { %>
<i class="fa fa-lg fa-lock" title='锁定(不可再回复)'/></i>
<% } %>
</a>


<a href='/topic/<%= topic._id %>/edit'>
<i class="fa fa-lg fa-pencil-square-o" title='编辑'></i></a>
<a href='javascript:;'
data-id="<%= topic._id %>"
class='delete_topic_btn'>
<i class="fa fa-lg fa-trash" title='删除'></i></a>
<% } else { %>
<i class="fa fa-lg fa-star" title='置顶'/></i>
<% if (current_user._id.equals(topic.author_id)) { %>
<a href='/topic/<%= topic._id %>/edit'>
<i class="fa fa-lg fa-pencil-square-o" title='编辑'></i></a>
<a href='javascript:;'
data-id="<%= topic._id %>"
class='delete_topic_btn'>
<i class="fa fa-lg fa-trash" title='删除'></i></a>
<% } %>
</a>
<% } %>


<a href='/topic/<%= topic._id %>/good' data-method="post">
<% if (topic.good) { %>
<i class="fa fa-lg fa-heart-o" title="取消精华"></i>
<% } else { %>
<i class="fa fa-lg fa-heart" title="加精华"></i>
<% } %>
</a>

<a href='/topic/<%= topic._id %>/lock' data-method="post">
<% if (topic.lock) { %>
<i class="fa fa-lg fa-unlock" title='取消锁定'></i>
<% } else { %>
<i class="fa fa-lg fa-lock" title='锁定(不可再回复)'/></i>
<% } %>
</a>


<a href='/topic/<%= topic._id %>/edit'>
<i class="fa fa-lg fa-pencil-square-o" title='编辑'></i></a>
<a href='javascript:;'
data-id="<%= topic._id %>"
class='delete_topic_btn'>
<i class="fa fa-lg fa-trash" title='删除'></i></a>
<% } else { %>
<% if (current_user._id.equals(topic.author_id)) { %>
<a href='/topic/<%= topic._id %>/edit'>
<i class="fa fa-lg fa-pencil-square-o" title='编辑'></i></a>
<a href='javascript:;'
data-id="<%= topic._id %>"
class='delete_topic_btn'>
<i class="fa fa-lg fa-trash" title='删除'></i></a>
<% } %>
<% } %>
</div>
<% } %>
</div>
Expand Down Expand Up @@ -258,7 +262,7 @@
// END 评论回复

// 加入收藏
$('#collect_btn').click(function () {
$('.collect_btn').click(function () {
var $me = $(this);
var action = $me.attr('action');
var data = {
Expand All @@ -269,10 +273,10 @@
$.post('/topic/' + action, data, function (data) {
if (data.status === 'success') {
if (action == 'collect') {
$me.text('取消收藏');
$me.val('取消收藏');
$me.attr('action', 'de_collect');
} else {
$me.text('加入收藏');
$me.val('收藏');
$me.attr('action', 'collect');
}
$me.toggleClass('span-success');
Expand Down
2 changes: 1 addition & 1 deletion views/user/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<% if (user.collect_topic_count) {%>
<li>
<a class='dark' href="/user/<%= user.loginname %>/collections" target='_blank'>
<span class='big collect-topic-count'><%= user.collect_topic_count %></span>话题收藏
<span class='big collect-topic-count'><%= user.collect_topic_count %></span>个话题收藏
</a>
</li>
<%}%>
Expand Down

0 comments on commit 072cca9

Please sign in to comment.