2016-03-30 17:47:44 +08:00
#coding=utf-8
module Mobile
module Apis
class Activities < Grape :: API
resources :activities do
desc " get user activities "
2016-04-01 20:46:38 +08:00
params do
requires :page , type : Integer
2016-04-02 08:05:49 +08:00
requires :openid , type : String
2016-04-01 20:46:38 +08:00
end
2016-04-02 07:39:46 +08:00
post do
2016-04-02 08:08:03 +08:00
user = UserWechat . find_by_openid ( params [ :openid ] ) . user
2016-04-08 15:59:23 +08:00
= begin
2016-03-30 17:47:44 +08:00
shield_project_ids = ShieldActivity . where ( " container_type='User' and container_id= #{ user . id } and shield_type='Project' " ) . map ( & :shield_id )
shield_course_ids = ShieldActivity . where ( " container_type='User' and container_id= #{ user . id } and shield_type='Course' " ) . map ( & :shield_id )
2016-04-01 20:46:38 +08:00
page = params [ :page ] ? params [ :page ] : 0
2016-03-30 17:47:44 +08:00
user_project_ids = ( user . projects . visible . map { | project | project . id } - shield_project_ids ) . empty? ? " (-1) " : " ( " + ( user . projects . visible . map { | project | project . id } - shield_project_ids ) . join ( " , " ) + " ) "
user_course_ids = ( user . courses . visible . map { | course | course . id } - shield_course_ids ) . empty? ? " (-1) " : " ( " + ( user . courses . visible . map { | course | course . id } - shield_course_ids ) . join ( " , " ) + " ) "
course_types = " ('Message','News','HomeworkCommon','Poll','Course') "
project_types = " ('Message','Issue','ProjectCreateInfo') "
principal_types = " JournalsForMessage "
blog_ids = " ( " + user . blog . id . to_s + " , " + ( ( User . watched_by ( user . id ) . count == 0 ) ? '0' :User . watched_by ( user . id ) . map { | u | u . blog . id } . join ( ',' ) ) + " ) "
activities = UserActivity . where ( " (container_type = 'Project' and container_id in #{ user_project_ids } and act_type in #{ project_types } ) " +
" or (container_type = 'Course' and container_id in #{ user_course_ids } and act_type in #{ course_types } ) " +
" or (container_type = 'Principal' and act_type= ' #{ principal_types } ' and container_id = #{ user . id } ) " +
2016-04-01 20:46:38 +08:00
" or (container_type = 'Blog' and act_type= 'BlogComment' and container_id in #{ blog_ids } ) " ) . order ( 'updated_at desc' )
2016-04-08 15:59:23 +08:00
= end
shield_project_ids = ShieldActivity . select ( " shield_id " ) . where ( " container_type='User' and container_id= #{ user . id } and shield_type='Project' " ) . map ( & :shield_id )
shield_course_ids = ShieldActivity . select ( " shield_id " ) . where ( " container_type='User' and container_id= #{ user . id } and shield_type='Course' " ) . map ( & :shield_id )
page = params [ :page ] ? params [ :page ] : 0
2016-05-31 08:57:08 +08:00
user_project_ids = ( user . projects . visible . map { | project | project . id } - shield_project_ids ) . empty? ? " (-1) " : " ( " + ( user . projects . visible . map { | project | project . id } - shield_project_ids ) . join ( " , " ) + " ) "
user_course_ids = ( user . courses . visible . where ( " is_delete = 0 " ) . map { | course | course . id } - shield_course_ids ) . empty? ? " (-1) " : " ( " + ( user . courses . visible . where ( " is_delete = 0 " ) . map { | course | course . id } - shield_course_ids ) . join ( " , " ) + " ) "
2016-04-08 15:59:23 +08:00
course_types = " ('Message','News','HomeworkCommon','Poll','Course') "
2016-05-19 15:26:23 +08:00
project_types = " ('Message','Issue','Project') "
2016-04-08 15:59:23 +08:00
principal_types = " JournalsForMessage "
watched_user_ids = User . watched_by ( user . id ) . count == 0 ? " " : ( " , " + User . watched_by ( user . id ) . map { | u | u . id . to_s } . join ( ',' ) )
user_ids = " ( " + user . id . to_s + watched_user_ids + " ) "
2016-05-31 08:57:08 +08:00
watched_user_blog_ids = Blog . select ( " id " ) . where ( " author_id in #{ user_ids } " ) . count == 0 ? " " :Blog . select ( " id " ) . where ( " author_id in #{ user_ids } " ) . map { | blog | blog . id } . join ( " , " )
2016-04-08 15:59:23 +08:00
blog_ids = " ( " + watched_user_blog_ids + " ) "
activities = UserActivity . where ( " (container_type = 'Project' and container_id in #{ user_project_ids } and act_type in #{ project_types } ) " +
" or (container_type = 'Course' and container_id in #{ user_course_ids } and act_type in #{ course_types } ) " +
" or (container_type = 'Principal' and act_type= ' #{ principal_types } ' and container_id = #{ user . id } ) " +
" or (container_type = 'Blog' and act_type= 'BlogComment' and container_id in #{ blog_ids } ) " ) . order ( 'updated_at desc' )
2016-04-01 20:46:38 +08:00
all_count = activities . count
activities = activities . limit ( 10 ) . offset ( page * 10 )
count = activities . count
2016-04-07 09:26:24 +08:00
present :data , activities , with : Mobile :: Entities :: Activity , user : user
2016-04-01 20:46:38 +08:00
present :all_count , all_count
present :count , count
present :page , page
2016-03-30 17:47:44 +08:00
present :status , 0
end
end
end
end
2016-03-30 10:32:45 +08:00
end