2015-01-22 17:21:15 +08:00
|
|
|
module Mobile
|
|
|
|
module Entities
|
2015-01-23 11:42:35 +08:00
|
|
|
#普通留言
|
2015-01-22 17:21:15 +08:00
|
|
|
class Jours < Grape::Entity
|
|
|
|
include Redmine::I18n
|
|
|
|
def self.jours_expose(field)
|
|
|
|
expose field do |f,opt|
|
|
|
|
if f.is_a?(Hash) && f.key?(field)
|
|
|
|
f[field]
|
|
|
|
elsif f.is_a?(::JournalsForMessage) && f.respond_to?(field)
|
|
|
|
if field == :created_on
|
|
|
|
format_time(f.send(field))
|
|
|
|
else
|
|
|
|
f.send(field)
|
|
|
|
end
|
2015-05-28 17:51:50 +08:00
|
|
|
else
|
2016-03-30 17:49:05 +08:00
|
|
|
case field
|
|
|
|
when :lasted_comment
|
|
|
|
time_from_now f.created_on
|
2016-07-22 15:37:25 +08:00
|
|
|
when :comment_count
|
2016-07-19 10:50:36 +08:00
|
|
|
# f.children.count
|
|
|
|
all_comments = []
|
|
|
|
get_all_children(all_comments, f).count
|
2016-04-07 16:40:21 +08:00
|
|
|
when :praise_count
|
2016-04-06 11:40:05 +08:00
|
|
|
get_activity_praise_num(f)
|
2016-04-07 16:40:21 +08:00
|
|
|
when :act_type
|
|
|
|
'JournalsForMessage'
|
|
|
|
when :act_id
|
|
|
|
f.id
|
2016-07-22 15:37:25 +08:00
|
|
|
when :content
|
|
|
|
f.notes
|
2015-05-28 17:51:50 +08:00
|
|
|
end
|
2015-01-22 17:21:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-04-07 16:40:21 +08:00
|
|
|
|
|
|
|
jours_expose :act_type
|
|
|
|
jours_expose :act_id
|
2015-01-23 11:42:35 +08:00
|
|
|
jours_expose :id
|
2015-02-05 17:32:19 +08:00
|
|
|
jours_expose :jour_type
|
|
|
|
jours_expose :jour_id
|
2015-01-22 17:21:15 +08:00
|
|
|
expose :user,using: Mobile::Entities::User do |f, opt|
|
|
|
|
f.user
|
|
|
|
end
|
|
|
|
jours_expose :created_on
|
2016-03-30 17:49:05 +08:00
|
|
|
jours_expose :lasted_comment
|
2016-07-22 15:37:25 +08:00
|
|
|
jours_expose :content
|
2015-01-23 11:42:35 +08:00
|
|
|
jours_expose :m_reply_id
|
2015-02-05 17:32:19 +08:00
|
|
|
jours_expose :m_parent_id
|
2016-07-22 15:37:25 +08:00
|
|
|
jours_expose :comment_count
|
2016-04-07 16:40:21 +08:00
|
|
|
jours_expose :praise_count
|
2015-05-28 17:51:50 +08:00
|
|
|
expose :course,using:Mobile::Entities::Course do |f,opt|
|
|
|
|
if f.is_a?(::JournalsForMessage) && f[:jour_type] == "Course"
|
|
|
|
f.course
|
|
|
|
end
|
|
|
|
end
|
2015-01-23 11:42:35 +08:00
|
|
|
expose :reply_user,using: Mobile::Entities::User do |f, opt|
|
|
|
|
f.at_user
|
|
|
|
end
|
2016-07-22 15:37:25 +08:00
|
|
|
expose :all_children,using: Mobile::Entities::Jours do |f, opt|
|
2015-01-23 11:42:35 +08:00
|
|
|
if f.is_a?(::JournalsForMessage)
|
2016-07-22 15:37:25 +08:00
|
|
|
# f.children.reverse
|
|
|
|
if !opt[:children] && opt[:comment_type].nil?
|
|
|
|
if f.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, f)[tStart..tEnd]
|
|
|
|
all_comments
|
|
|
|
end
|
|
|
|
end
|
2015-01-23 11:42:35 +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
|
2016-07-22 15:37:25 +08:00
|
|
|
|
|
|
|
expose :parents_count, if: lambda { |instance, options| options[:user] } do |instance, options|
|
|
|
|
parents_reply = []
|
|
|
|
|
|
|
|
if options[:comment_type].nil?
|
|
|
|
parents_reply = get_reply_parents_no_root(parents_reply, instance)
|
|
|
|
elsif options[:comment_type] == "homework"
|
|
|
|
parents_reply = get_reply_parents(parents_reply, instance)
|
|
|
|
end
|
|
|
|
parents_reply.count
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :parents_reply_bottom, using:Mobile::Entities::Jours do |f,opt|
|
|
|
|
if f.is_a? (::JournalsForMessage)
|
|
|
|
#取二级回复的底楼层
|
|
|
|
parents_reply = []
|
|
|
|
if opt[:comment_type].nil?
|
|
|
|
parents_reply = get_reply_parents_no_root(parents_reply, f)
|
|
|
|
elsif opt[:comment_type] == "homework"
|
|
|
|
parents_reply = get_reply_parents(parents_reply, f)
|
|
|
|
end
|
2016-08-03 16:31:33 +08:00
|
|
|
if parents_reply.count > 0 && !opt[:bottom]
|
2016-07-22 15:37:25 +08:00
|
|
|
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::Jours do |f,opt|
|
|
|
|
if f.is_a? (::JournalsForMessage)
|
|
|
|
#取二级回复的顶楼层
|
|
|
|
parents_reply = []
|
|
|
|
|
|
|
|
if opt[:comment_type].nil?
|
|
|
|
parents_reply = get_reply_parents_no_root(parents_reply, f)
|
|
|
|
elsif opt[:comment_type] == "homework"
|
|
|
|
parents_reply = get_reply_parents(parents_reply, f)
|
|
|
|
end
|
2016-08-03 16:31:33 +08:00
|
|
|
if parents_reply.count > 2 && !opt[:top]
|
2016-07-22 15:37:25 +08:00
|
|
|
if opt[:type] == 1
|
2016-08-03 16:31:33 +08:00
|
|
|
opt[:top] = true
|
2016-07-22 15:37:25 +08:00
|
|
|
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
|
2016-08-03 16:31:33 +08:00
|
|
|
elsif parents_reply.count == 2 && !opt[:top]
|
|
|
|
opt[:top] = true
|
|
|
|
parents_reply = parents_reply.reverse[0..0]
|
|
|
|
parents_reply.reverse
|
2016-07-22 15:37:25 +08:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-22 17:21:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|