socialforge/app/api/mobile/entities/message.rb

149 lines
4.6 KiB
Ruby
Raw Normal View History

2015-06-26 09:19:23 +08:00
module Mobile
module Entities
class Message < Grape::Entity
include ApplicationHelper
include ApiHelper
def self.message_expose(f)
expose f do |u,opt|
if u.is_a?(Hash) && u.key?(f)
u[f]
elsif u.is_a?(::Message)
if u.respond_to?(f)
if f == :created_on
format_time( u.send(f))
else
u.send(f)
end
else
2016-03-30 17:49:05 +08:00
case f
when :course_project_name
if u.board.project_id == -1
u.course.name
else
u.project.name
end
when :lasted_comment
time_from_now u.created_on
2016-04-07 16:40:21 +08:00
when :praise_count
2016-04-01 20:46:38 +08:00
get_activity_praise_num(u)
2016-04-07 16:40:21 +08:00
when :act_type
'Message'
when :act_id
u.id
when :comment_count
all_comments = []
get_all_children(all_comments, u).count
2016-08-04 10:00:55 +08:00
when :title
u.subject
2016-03-30 17:49:05 +08:00
end
2015-06-26 09:19:23 +08:00
end
end
end
end
expose :user, using: Mobile::Entities::User do |c, opt|
if c.is_a?(::Message)
c.author
end
end
2016-04-07 16:40:21 +08:00
message_expose :act_type
message_expose :act_id
2016-03-30 17:49:05 +08:00
message_expose :course_project_name
2015-06-26 09:19:23 +08:00
message_expose :board_id
message_expose :subject
2016-08-04 10:00:55 +08:00
message_expose :title
2015-06-26 09:19:23 +08:00
message_expose :content
message_expose :comment_count
2016-04-07 16:40:21 +08:00
message_expose :praise_count
2015-06-26 09:19:23 +08:00
message_expose :created_on
2016-05-13 14:43:22 +08:00
message_expose :locked
2015-06-26 09:19:23 +08:00
message_expose :id
2016-03-30 17:49:05 +08:00
message_expose :lasted_comment
expose :all_children,using:Mobile::Entities::Message do |c,opt|
2015-06-26 09:19:23 +08:00
if c.is_a? (::Message)
# c.children.reverse
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
2015-06-26 09:19:23 +08:00
end
end
2016-04-07 11:16:38 +08:00
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|
parents_reply = []
parents_reply = get_reply_parents_no_root(parents_reply, instance)
parents_reply.count
end
expose :parents_reply_bottom, using:Mobile::Entities::Message do |c,opt|
if c.is_a? (::Message)
#取二级回复的底楼层
parents_reply = []
parents_reply = get_reply_parents_no_root(parents_reply, c)
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::Message do |c,opt|
if c.is_a? (::Message)
#取二级回复的顶楼层
parents_reply = []
parents_reply = get_reply_parents_no_root(parents_reply, c)
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
2015-06-26 09:19:23 +08:00
end
end
end