You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							192 lines
						
					
					
						
							5.9 KiB
						
					
					
				
			
		
		
	
	
							192 lines
						
					
					
						
							5.9 KiB
						
					
					
				| module Mobile
 | |
|   module Entities
 | |
|     class News < Grape::Entity
 | |
|       include Redmine::I18n
 | |
|       include ApiHelper
 | |
|       def self.news_expose(field)
 | |
|         expose field do |f,opt|
 | |
|           if f.is_a?(Hash) && f.key?(field)
 | |
|             f[field]
 | |
|           elsif f.is_a?(::News)
 | |
|             if f.respond_to?(field)
 | |
|               if field == :created_on
 | |
|                 format_time(f.send(field))
 | |
|               else
 | |
|                 f.send(field)
 | |
|               end
 | |
|             else
 | |
|               case field
 | |
|                 when :course_name
 | |
|                   get_course(f.course_id).name unless f.course_id == nil
 | |
|                 when :praise_count
 | |
|                   get_activity_praise_num(f)
 | |
|                 when :act_type
 | |
|                   'News'
 | |
|                 when :act_id
 | |
|                   f.id
 | |
|                 when :comment_count
 | |
|                   f.comments.count
 | |
|               end
 | |
|             end
 | |
|           elsif f.is_a?(::Comment)
 | |
|             case field
 | |
|               when :content
 | |
|                 f[:comments]
 | |
|               when :lasted_comment
 | |
|                 time_from_now f.created_on
 | |
|               when :act_id
 | |
|                 f.id
 | |
|               when :praise_count
 | |
|                 get_activity_praise_num(f)
 | |
|               when :act_type
 | |
|                 'Comment'
 | |
|             end
 | |
| 
 | |
|           elsif f.is_a?(Hash) && !f.key?(field)
 | |
|             n = f[:news]
 | |
|             comments = f[:comments]
 | |
|             if n.is_a?(::News)
 | |
|               if field == :created_on
 | |
|                 format_time(n.send(field)) if n.respond_to?(field)
 | |
|               else
 | |
|                 n.send(field) if n.respond_to?(field)
 | |
|               end
 | |
| 
 | |
|             end
 | |
| 
 | |
|           end
 | |
|         end
 | |
|       end
 | |
|       expose :id
 | |
|       #新闻标题
 | |
|       news_expose :title
 | |
| 
 | |
|       expose :user,using: Mobile::Entities::User do |f, opt|
 | |
|         obj = nil
 | |
|         if f.is_a?(::News) && f.respond_to?(:author)
 | |
|           obj = f.send(:author)
 | |
|         elsif f.is_a?(::Comment) && f.respond_to?(:author)
 | |
|           obj = f.send(:author)
 | |
|         elsif f.is_a?(Hash) && f.key?(:author)
 | |
|           obj = f[:author]
 | |
|         end
 | |
|         obj
 | |
|       end
 | |
|       news_expose :act_type
 | |
|       news_expose :act_id
 | |
|       #作者id
 | |
|       news_expose :author_id
 | |
|       #作者名
 | |
|       news_expose :author_name
 | |
|       #作者头像url
 | |
|       news_expose :author_img_url
 | |
|       #新闻内容
 | |
|       news_expose :description
 | |
|       #发布时间
 | |
|       news_expose :created_on
 | |
|       #评论数量
 | |
|       news_expose :comment_count
 | |
|       news_expose :praise_count
 | |
|       #课程名字
 | |
|       news_expose :course_name
 | |
|       news_expose :lasted_comment
 | |
| 
 | |
|       #评论
 | |
|       # expose :comments, using: Mobile::Entities::Comment do |f, opt|
 | |
|       #   if f.is_a?(Hash) && f.key?(:comments)
 | |
|       #     f[:comments]
 | |
|       #   elsif f.is_a?(::News) && f.respond_to?(:comments)
 | |
|       #     f.comments.reverse
 | |
|       #   end
 | |
|       # end
 | |
| 
 | |
|       news_expose :content
 | |
| 
 | |
|       expose :all_children, using: Mobile::Entities::News  do |f, opt|
 | |
|         #f[:journals_for_messages] if f.is_a?(Hash) && f.key?(:journals_for_messages)
 | |
|         if f.is_a?(::News)
 | |
|           # f.journals_for_messages.reverse
 | |
|           if !opt[:children] && opt[:type] == 0
 | |
|             opt[:children] = true
 | |
|             tStart = opt[:page]*5
 | |
|             tEnd = (opt[:page]+1)*5 - 1
 | |
| 
 | |
|             all_comments = f.comments.reorder("created_on desc")
 | |
|             all_comments[tStart..tEnd]
 | |
|           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_count, if: lambda { |instance, options| options[:user] } do |instance, options|
 | |
|         if instance.is_a?(::Comment)
 | |
|           parents_reply = []
 | |
|           parents_reply = get_reply_parents(parents_reply, instance)
 | |
|           parents_reply.count
 | |
|         end
 | |
|       end
 | |
| 
 | |
|       expose :parents_reply_bottom, using:Mobile::Entities::News do |f,opt|
 | |
|         if f.is_a? (::Comment)
 | |
|           #取二级回复的底楼层
 | |
|           parents_reply = []
 | |
|           parents_reply = get_reply_parents(parents_reply, f)
 | |
|           if parents_reply.count > 0 && !opt[:bottom]
 | |
|             if opt[:type] == 1
 | |
|               # opt[:bottom] = true
 | |
|               # parents_reply[opt[:page]..opt[:page]]
 | |
|             else
 | |
|               opt[:bottom] = true
 | |
|               parents_reply[0..0]
 | |
|             end
 | |
|           else
 | |
|             []
 | |
|           end
 | |
|         end
 | |
|       end
 | |
| 
 | |
|       expose :parents_reply_top, using:Mobile::Entities::News do |f,opt|
 | |
|         if f.is_a? (::Comment)
 | |
|           #取二级回复的顶楼层
 | |
|           parents_reply = []
 | |
|           parents_reply = get_reply_parents(parents_reply, f)
 | |
|           if parents_reply.count > 2 && !opt[:top]
 | |
|             if opt[:type] == 1
 | |
|               opt[:top] = true
 | |
|               tStart = (opt[:page]-1)*5+2
 | |
|               tEnd = (opt[:page])*5+2 - 1
 | |
| 
 | |
|               if tEnd >= parents_reply.count - 1
 | |
|                 tEnd = parents_reply.count - 2
 | |
|               end
 | |
| 
 | |
|               if tStart <= parents_reply.count - 2
 | |
|                 parents_reply = parents_reply.reverse[tStart..tEnd]
 | |
|                 parents_reply.reverse
 | |
|               else
 | |
|                 []
 | |
|               end
 | |
|             else
 | |
|               opt[:top] = true
 | |
|               parents_reply = parents_reply.reverse[0..1]
 | |
|               parents_reply.reverse
 | |
|             end
 | |
|           elsif parents_reply.count == 2 && !opt[:top]
 | |
|             opt[:top] = true
 | |
|             parents_reply = parents_reply.reverse[0..0]
 | |
|             parents_reply.reverse
 | |
|           else
 | |
|             []
 | |
|           end
 | |
|         end
 | |
|       end
 | |
|     end
 | |
|   end
 | |
| end |