diff --git a/app/assets/javascripts/app.coffee b/app/assets/javascripts/app.coffee index 2090aac2b1..cb25d50f17 100644 --- a/app/assets/javascripts/app.coffee +++ b/app/assets/javascripts/app.coffee @@ -37,9 +37,13 @@ window.App = type : likeable_type id : likeable_id success : (re) -> - if parseInt(re) >= 0 + likes_count = parseInt(re) + if likes_count >= 0 $(el).data("state","liked").attr("title", "取消喜欢") - $('span',el).text("#{re}人喜欢") + if likes_count == 0 + $('span',el).text("喜欢") + else + $('span',el).text("#{re}人喜欢") $("i.icon",el).attr("class","icon small_liked") else App.alert("抱歉,系统异常,提交失败。") @@ -50,9 +54,13 @@ window.App = data : type : likeable_type success : (re) -> - if parseInt(re) >= 0 + likes_count = parseInt(re) + if likes_count >= 0 $(el).data("state","").attr("title", "喜欢") - $('span',el).text("#{re}人喜欢") + if likes_count == 0 + $('span',el).text("喜欢") + else + $('span',el).text("#{re}人喜欢") $("i.icon",el).attr("class","icon small_like") else App.alert("抱歉,系统异常,提交失败。") @@ -90,7 +98,7 @@ $(document).ready -> App.initForDesktopView() $("abbr.timeago").timeago() - $(".alert").alert() + $(".alert").alert() $('.dropdown-toggle').dropdown() diff --git a/app/assets/stylesheets/topics.scss b/app/assets/stylesheets/topics.scss index 42ca508ece..739adf0f7c 100644 --- a/app/assets/stylesheets/topics.scss +++ b/app/assets/stylesheets/topics.scss @@ -39,6 +39,7 @@ text-align:right; height:16px; overflow:hidden; a:link, a:visited { + margin-left:5px; i { position:relative; margin-bottom:-1px; } text-decoration: none; } @@ -130,22 +131,31 @@ form .node_select { #replies { .total { padding-bottom:6px; color:#999; } .reply { + &.light { background:#F7F2FC; } + &.popular { background:#fffce9; } border-top:1px solid #DDD; padding:8px 10px; margin:0 -10px; .face { padding-right:8px; width:48px; margin-left:0; } .infos { padding-left:58px; } .info { margin-top:4px; color:#999;height:20px; a:link, - a:visited { color:#999; text-decoration: underline;} - .name { float:left; } - .time { float:right; text-align:right; } + a:visited { color:#999; } + .name { + float:left; + a { text-decoration:underline; margin-right:8px; } + } + .opts { + float:right; text-align:right; + a { margin-left:5px; } + a:hover { text-decoration:none; } + i.icon { margin-bottom:-1px; } + } a.reply_link img { vertical-align:middle;} } .body { img { max-width:622px;} } } - .reply.light { background:#F7F2FC;} } #reply.form { diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index 7b9de6429c..568932bd4e 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -17,7 +17,7 @@ def destroy def find_likeable @success = false @element_id = "likeable_#{params[:type]}_#{params[:id]}" - if not params[:type].in?(['Topic']) + if not params[:type].in?(['Topic','Reply']) render :text => "-1" return false end diff --git a/app/helpers/likes_helper.rb b/app/helpers/likes_helper.rb index 9397b37ee7..37ab428ae0 100644 --- a/app/helpers/likes_helper.rb +++ b/app/helpers/likes_helper.rb @@ -4,6 +4,10 @@ module LikesHelper def likeable_tag(likeable) return "" if likeable.blank? + label = "#{likeable.likes_count}人喜欢" + if likeable.likes_count == 0 + label = "喜欢" + end if current_user && likeable.liked_by_user?(current_user) title = "取消喜欢" state = "liked" @@ -13,7 +17,7 @@ def likeable_tag(likeable) state = "" icon = content_tag("i", "", :class => "icon small_like") end - like_label = raw "#{icon} #{likeable.likes_count}人喜欢" + like_label = raw "#{icon} #{label}" link_to(like_label,"#",:title => title, :rel => "twipsy", 'data-state' => state,'data-type' => likeable.class,'data-id' => likeable.id, :class => 'likeable', :onclick => "return App.likeable(this);") diff --git a/app/models/mongoid/likeable.rb b/app/models/mongoid/likeable.rb index c887d8546d..c9b01a6009 100644 --- a/app/models/mongoid/likeable.rb +++ b/app/models/mongoid/likeable.rb @@ -1,8 +1,16 @@ # coding: utf-8 module Mongoid module Likeable + extend ActiveSupport::Concern + + included do + field :liked_user_ids, :type => Array, :default => [] + field :likes_count, :type => Integer, :default => 0 + end + def liked_by_user?(user) - Like.where(:likeable_type => self.class, :likeable_id => self.id, :user_id => user.id).count > 0 + return false if user.blank? + self.liked_user_ids.include?(user.id) end end end diff --git a/app/models/reply.rb b/app/models/reply.rb index bdf33b2ded..07f8b59258 100644 --- a/app/models/reply.rb +++ b/app/models/reply.rb @@ -8,11 +8,13 @@ class Reply include Mongoid::SoftDelete include Mongoid::MarkdownBody include Mongoid::Mentionable + include Mongoid::Likeable field :body field :body_html field :source field :message_id + field :likes_count, :type => Integer, :default => 0 belongs_to :user, :inverse_of => :replies belongs_to :topic, :inverse_of => :replies @@ -59,6 +61,11 @@ def send_topic_reply_notification end end + # 是否热门 + def popular? + self.likes_count >= 5 + end + def destroy super notifications.delete_all diff --git a/app/models/topic.rb b/app/models/topic.rb index 984b084568..967ce49254 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -22,7 +22,6 @@ class Topic # 回复过的人的 ids 列表 field :follower_ids, :type => Array, :default => [] field :suggested_at, :type => DateTime - field :likes_count, :type => Integer, :default => 0 # 最后回复人的用户名 - cache 字段用于减少列表也的查询 field :last_reply_user_login # 节点名称 - cache 字段用于减少列表也的查询 diff --git a/app/models/user.rb b/app/models/user.rb index 1f944577dd..05e316b540 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -29,7 +29,6 @@ class User field :tagline field :topics_count, :type => Integer, :default => 0 field :replies_count, :type => Integer, :default => 0 - field :likes_count, :type => Integer, :default => 0 # 用户密钥,用于客户端验证 field :private_token field :favorite_topic_ids, :type => Array, :default => [] @@ -48,7 +47,6 @@ class User has_many :posts has_many :notifications, :class_name => 'Notification::Base', :dependent => :delete has_many :photos - has_many :likes def read_notifications(notifications) unread_ids = notifications.find_all{|notification| !notification.read?}.map(&:_id) @@ -191,16 +189,18 @@ def read_topic(topic) # 收藏东西 def like(likeable) - Like.find_or_create_by(:likeable_id => likeable.id, - :likeable_type => likeable.class, - :user_id => self.id) + return false if likeable.blank? + return false if likeable.liked_by_user?(self) + likeable.push(:liked_user_ids, self.id) + likeable.inc(:likes_count, 1) end # 取消收藏 def unlike(likeable) - Like.where(:likeable_id => likeable.id, - :likeable_type => likeable.class, - :user_id => self.id).destroy + return false if likeable.blank? + return false if not likeable.liked_by_user?(self) + likeable.pull(:liked_user_ids, self.id) + likeable.inc(:likes_count, -1) end # 收藏话题 diff --git a/app/views/replies/_reply.html.erb b/app/views/replies/_reply.html.erb index 41ba552a08..3f83b932ff 100644 --- a/app/views/replies/_reply.html.erb +++ b/app/views/replies/_reply.html.erb @@ -1,10 +1,13 @@ -