module Mobile module Entities class BlogComment < Grape::Entity include ApplicationHelper include ApiHelper def self.blog_comment_expose(f) expose f do |u,opt| if u.is_a?(Hash) && u.key?(f) u[f] elsif u.is_a?(::BlogComment) if u.respond_to?(f) if f == :created_at format_time( u.send(f)) else u.send(f) end else case f when :praise_count get_activity_praise_num(u) when :lasted_comment time_from_now(u.created_at) when :act_type 'BlogComment' when :act_id u.id when :comment_count # u.children.count all_comments = [] get_all_children(all_comments, u).count end end end end end expose :user, using: Mobile::Entities::User do |c, opt| if c.is_a?(::BlogComment) c.author end end blog_comment_expose :act_type blog_comment_expose :act_id blog_comment_expose :blog_id blog_comment_expose :title blog_comment_expose :content blog_comment_expose :comment_count blog_comment_expose :created_at blog_comment_expose :lasted_comment blog_comment_expose :id blog_comment_expose :locked blog_comment_expose :praise_count expose :blog_comment_children, using:Mobile::Entities::BlogComment do |c,opt| if c.is_a? (::BlogComment) ##自己的父回复为空 才有子回复 if !opt[:children] if c.parent.nil? && opt[:type] == 0 opt[:children] = true all_comments = [] tStart = opt[:page]*5 tEnd = (opt[:page]+1)*5 - 1 all_comments = get_all_children(all_comments, c)[tStart..tEnd] all_comments end end end end expose :has_praise, if: lambda { |instance, options| options[:user] } do |instance, options| has_praise = false current_user = options[:user] obj = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",instance.id,instance.class.to_s,current_user.id) has_praise = obj.empty? ? false : true has_praise end expose :parents_reply_bottom, using:Mobile::Entities::BlogComment do |c,opt| if c.is_a? (::BlogComment) #取二级回复的底楼层 if (opt[:type] == 1 && !opt[:bottom]) opt[:bottom] = true parents_reply = [] parents_reply = c.parent.nil? ? [] : parents_reply << c.parent #get_reply_parents_no_root(parents_reply, c)[0] parents_reply end end end expose :parents_reply_top, using:Mobile::Entities::BlogComment do |c,opt| if c.is_a? (::BlogComment) #取二级回复的顶楼层 if (opt[:type] == 1 && !opt[:top]) opt[:top] = true parents_reply = [] parents_reply = get_reply_parents_no_root(parents_reply, c) tStart = opt[:page]*2 tEnd = (opt[:page]+1)*2 - 1 parents_reply = parents_reply.reverse[tStart,tEnd] parents_reply end end end expose :parents_count, if: lambda { |instance, options| options[:user] } do |instance, options| parents_reply = [] parents_reply = get_reply_parents_no_root(parents_reply, instance) parents_reply.count end end end end