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

149 lines
6.8 KiB
Ruby
Raw Normal View History

2016-03-30 17:47:44 +08:00
# encoding: utf-8
module Mobile
module Entities
class Activity <Grape::Entity
include ApplicationHelper
include ApiHelper
def self.act_expose(f)
expose f do |ac,opt|
if ac.is_a?(Hash) && ac.key?(f)
ac[f]
elsif ac.is_a?(::UserActivity)
if ac.respond_to?(f)
ac.send(f)
else
case f
when :user_act
2016-05-19 15:26:23 +08:00
if ac.act_type == "Project"
2016-03-30 17:47:44 +08:00
ac unless ac.nil?
else
ac.act unless ac.nil? || ac.act.nil?
end
when :reply_count
# if ac.act_type == "HomeworkCommon"
# ac.nil? || ac.act.nil? ? 0 : ac.act.journals_for_messages.count
# elsif ac.act_type == "News"
# ac.nil? || ac.act.nil? ? 0 : ac.act.comments.count
# elsif ac.act_type == "Message" || ac.act_type == "BlogComment" || ac.act_type == "JournalsForMessage"
# ac.nil? || ac.act.nil? ? 0 : ac.act.children.count
# elsif ac.act_type == "Issue"
# ac.nil? || ac.act.nil? ? 0 : ac.act.journals.where("notes is not null and notes != ''").count
# end
all_comments = []
ac.nil? || ac.act.nil? ? 0 : get_all_children(all_comments, ac.act).count
2016-03-30 17:47:44 +08:00
when :subject
2016-04-08 15:59:23 +08:00
if ac.act_type == "HomeworkCommon"
ac.act.name unless ac.nil? || ac.act.nil?
elsif ac.act_type == "News" || ac.act_type == "BlogComment"
ac.act.title unless ac.nil? || ac.act.nil?
elsif ac.act_type == "Message" || ac.act_type == "Issue"
ac.act.subject unless ac.nil? || ac.act.nil?
elsif ac.act_type == "JournalsForMessage"
ac.act.private == 0 ? "留言" : "私信" unless ac.nil? || ac.act.nil?
2016-03-30 17:47:44 +08:00
end
when :description
2016-04-08 14:38:35 +08:00
if ac.act_type == "HomeworkCommon" || ac.act_type == "Issue" || ac.act_type == "News"
2016-05-26 17:18:58 +08:00
strip_html(ac.act.description) unless ac.nil? || ac.act.nil?
2016-04-08 14:38:35 +08:00
elsif ac.act_type == "Message" || ac.act_type == "BlogComment"
2016-05-26 17:18:58 +08:00
strip_html(ac.act.content) unless ac.nil? || ac.act.nil?
2016-04-08 14:38:35 +08:00
elsif ac.act_type == "JournalsForMessage"
2016-05-26 17:18:58 +08:00
strip_html(ac.act.notes) unless ac.nil? || ac.act.nil?
2016-03-30 17:47:44 +08:00
end
when :latest_update
time_from_now ac.updated_at unless ac.nil?
2016-04-07 16:40:21 +08:00
when :praise_count
2016-03-30 17:47:44 +08:00
if ac.act_type == "HomeworkCommon" || ac.act_type == "News" || ac.act_type == "Message" || ac.act_type == "BlogComment" || ac.act_type == "JournalsForMessage" || ac.act_type == "Issue"
ac.nil? || ac.act.nil? ? 0 : get_activity_praise_num(ac.act)
end
#when :homework_common_detail_manual
# if ac.act_type == "HomeworkCommon"
# ac.act.homework_detail_manual unless ac.nil? || ac.act.nil? || ac.act.homework_detail_manual.nil?
# end
when :course_project_name
if ac.container_type == "Course"
name = (get_course(ac.container_id)).name
name
elsif ac.container_type == "Project"
name = (get_project(ac.container_id)).name
name
elsif ac.container_type == "Blog"
"发表博客"
end
when :activity_type_name
if ac.container_type == "Course"
case ac.act_type
when "HomeworkCommon"
"课程作业"
when "News"
"课程通知"
when "Message"
"课程问答区"
when "Poll"
"课程问卷"
when "Course"
"课程"
end
elsif ac.container_type == "Project"
case ac.act_type
when "Issue"
2016-05-19 15:20:46 +08:00
"项目问题"
2016-03-30 17:47:44 +08:00
when "Message"
2016-04-07 16:57:40 +08:00
"项目讨论区"
2016-05-19 15:26:23 +08:00
when "Project"
2016-03-30 17:47:44 +08:00
"项目"
end
end
end
end
end
end
end
expose :act_type #缺陷/作业/讨论区/留言等类型
2016-04-01 14:58:41 +08:00
expose :act_id
2016-05-31 20:55:46 +08:00
expose :id
2016-03-30 17:47:44 +08:00
expose :container_type #课程/项目/博客/个人
expose :author, using: Mobile::Entities::User do |a, opt| #用户信息
if a.is_a? ::UserActivity
2016-05-19 15:26:23 +08:00
if a.act_type == "Project"
2016-03-30 17:47:44 +08:00
get_user(get_project(a.act_id).user_id)
2016-04-08 14:12:17 +08:00
elsif !a.act.nil?
if a.act_type == 'Issue' || a.act_type == 'News' || a.act_type == 'Message' || a.act_type == 'BlogComment'
2016-03-30 17:47:44 +08:00
a.act.author
2016-04-08 14:12:17 +08:00
elsif a.act_type == 'HomeworkCommon' || a.act_type == 'Poll' || a.act_type == 'JournalsForMessage'
a.act.user
elsif a.act_type == 'Course'
a.act.teacher
end
2016-03-30 17:47:44 +08:00
end
end
end
expose :homework_common_detail , using: Mobile::Entities::Whomework do |a, opt| #作业相关信息
if a.act_type == "HomeworkCommon"
a.act
end
end
expose :issue_detail, using: Mobile::Entities::Issue do |a, opt| #缺陷信息
if a.act_type == "Issue"
a.act
end
end
act_expose :reply_count #回复数
2016-04-07 16:40:21 +08:00
act_expose :praise_count #点赞数
2016-03-30 17:47:44 +08:00
#act_expose :user_act #某个动态
act_expose :subject #标题
act_expose :description #描述
act_expose :latest_update #最新更新时间
act_expose :course_project_name #课程/项目名字
act_expose :activity_type_name #课程问答区/项目缺陷等
2016-04-07 09:26:24 +08:00
expose :has_praise , if: lambda { |instance, options| options[:user] } do |instance, options|
2016-04-08 15:59:23 +08:00
if instance.act_type == "HomeworkCommon" || instance.act_type == "News" || instance.act_type == "Message" || instance.act_type == "BlogComment" || instance.act_type == "JournalsForMessage" || instance.act_type == "Issue"
has_praise = false
current_user = options[:user]
obj = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",instance.act_id,instance.act_type.to_s,current_user.id)
has_praise = obj.empty? ? false : true
has_praise
end
2016-04-07 09:26:24 +08:00
end
2016-03-30 17:47:44 +08:00
end
end
2016-03-30 10:32:45 +08:00
end