commit
a1395d69ef
@ -0,0 +1,152 @@
|
||||
#coding=utf-8
|
||||
|
||||
class AtController < ApplicationController
|
||||
respond_to :json
|
||||
|
||||
def show
|
||||
@logger = Logger.new(Rails.root.join('log', 'at.log').to_s)
|
||||
users = find_at_users(params[:type], params[:id])
|
||||
@users = users
|
||||
@users = users.uniq { |u| u.id }.delete_if { |u| u.id == User.current.id } if users
|
||||
end
|
||||
|
||||
private
|
||||
def find_at_users(type, id)
|
||||
@logger.info("#{type}, #{id}")
|
||||
case type
|
||||
when "Issue"
|
||||
find_issue(id)
|
||||
when 'Project'
|
||||
find_project(id)
|
||||
when 'Course'
|
||||
find_course(id)
|
||||
when 'Activity', 'CourseActivity', 'ForgeActivity','UserActivity', 'OrgActivity','PrincipalActivity'
|
||||
find_activity(id, type)
|
||||
when 'Attachment'
|
||||
find_attachment(id)
|
||||
when 'Message'
|
||||
find_message(id)
|
||||
when 'HomeworkCommon'
|
||||
find_homework(id)
|
||||
when 'Topic'
|
||||
find_topic(id)
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def find_topic(id)
|
||||
|
||||
end
|
||||
|
||||
def find_issue(id)
|
||||
#1. issues list persons
|
||||
#2. project persons
|
||||
issue = Issue.find(id)
|
||||
journals = issue.journals
|
||||
at_persons = journals.map(&:user) + issue.project.users
|
||||
at_persons.uniq { |u| u.id }.delete_if { |u| u.id == User.current.id }
|
||||
end
|
||||
|
||||
def find_project(id)
|
||||
at_persons = Project.find(id).users
|
||||
at_persons.delete_if { |u| u.id == User.current.id }
|
||||
end
|
||||
|
||||
def find_course(id)
|
||||
at_persons = Course.find(id).users
|
||||
at_persons.delete_if { |u| u.id == User.current.id }
|
||||
end
|
||||
|
||||
def find_activity(id, type)
|
||||
|
||||
## 基本上是本类型中的 加上所属类型的用户
|
||||
case type
|
||||
when 'Activity'
|
||||
activity = Activity.find(id)
|
||||
(find_at_users(activity.act_type, activity.act_id) ||[]) +
|
||||
(find_at_users(activity.activity_container_type, activity.activity_container_id) || [])
|
||||
when 'CourseActivity'
|
||||
activity = CourseActivity.find(id)
|
||||
(find_at_users(activity.course_act_type, activity.course_act_id) || []) + (find_course(activity.course.id) || [])
|
||||
when 'ForgeActivity'
|
||||
activity = ForgeActivity.find(id)
|
||||
(find_at_users(activity.forge_act_type, activity.forge_act_id) ||[]) +
|
||||
(find_project(activity.project_id) || [])
|
||||
when 'UserActivity'
|
||||
activity = UserActivity.find(id)
|
||||
(find_at_users(activity.act_type, activity.act_id) || []) +
|
||||
(find_at_users(activity.container_type, activity.container_id) || [])
|
||||
when 'OrgActivity'
|
||||
activity = OrgActivity.find(id)
|
||||
(find_at_users(activity.org_act_type, activity.org_act_id) || []) +
|
||||
(find_at_users(activity.container_type, activity.container_id) || [])
|
||||
when 'PrincipalActivity'
|
||||
activity = PrincipalActivity.find(id)
|
||||
find_at_users(activity.principal_act_type, activity.principal_act_id)
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
#作业应该是关联课程,取课程的用户列表
|
||||
def find_homework(id)
|
||||
homework = HomeworkCommon.find(id)
|
||||
find_course(homework.course_id)
|
||||
end
|
||||
|
||||
def find_attachment(id)
|
||||
attachment = Attachment.find(id)
|
||||
find_at_users(attachment.container_type, attachment.container_id)
|
||||
end
|
||||
|
||||
#Message
|
||||
def find_message(id)
|
||||
message = Message.find(id)
|
||||
at_persons = message.board.messages.map(&:author)
|
||||
(at_persons || []) + (find_project(message.board.project_id)||[])
|
||||
end
|
||||
|
||||
#News
|
||||
def find_news(id)
|
||||
find_project(News.find(id).project_id)
|
||||
end
|
||||
|
||||
#JournalsForMessage
|
||||
def find_journals_for_message(id)
|
||||
jounrnal = JournalsForMessage.find(id)
|
||||
find_at_users(jounrnal.jour_type, jounrnal.jour_id)
|
||||
end
|
||||
|
||||
#Poll
|
||||
def find_poll(id)
|
||||
end
|
||||
|
||||
#Journal
|
||||
def find_journal(id)
|
||||
journal = Journal.find(id)
|
||||
find_at_users(journal.journalized_type, journal.journalized_id)
|
||||
end
|
||||
|
||||
#Document
|
||||
def find_document(id)
|
||||
find_project(Document.find(id).project_id)
|
||||
end
|
||||
|
||||
#ProjectCreateInfo
|
||||
def find_project_create_info(id)
|
||||
|
||||
end
|
||||
|
||||
#Principal
|
||||
def find_principal(id)
|
||||
|
||||
end
|
||||
|
||||
#BlogComment
|
||||
def find_blog_comment(id)
|
||||
blog = BlogComment.find(id).blog
|
||||
blog.users
|
||||
end
|
||||
|
||||
end
|
@ -0,0 +1,102 @@
|
||||
#coding=utf-8
|
||||
|
||||
class AtMessage < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
belongs_to :sender, class_name: "User", foreign_key: "sender_id"
|
||||
attr_accessible :at_message, :container, :viewed, :user_id, :sender_id
|
||||
belongs_to :at_message, polymorphic: true
|
||||
belongs_to :container, polymorphic: true
|
||||
|
||||
has_many :message_alls, :class_name => 'MessageAll',:as =>:message, :dependent => :destroy
|
||||
validates :user_id, :sender_id, :at_message_id, :at_message_type, presence: true
|
||||
|
||||
after_create :add_user_message
|
||||
|
||||
scope :unviewed, ->(type, id){
|
||||
where(at_message_type: type, at_message_id:id, viewed: false)
|
||||
}
|
||||
|
||||
def viewed!
|
||||
update_attribute :viewed, true
|
||||
end
|
||||
|
||||
def at_valid?
|
||||
return true if at_message_type == 'Issue'
|
||||
return true if 'Journal' == at_message_type
|
||||
return true if 'JournalsForMessage' == at_message_type
|
||||
return true if 'Message' == at_message_type
|
||||
false
|
||||
end
|
||||
|
||||
def add_user_message
|
||||
if MessageAll.where(message_type: self.class.name,message_id: self.id).empty?
|
||||
self.message_alls << MessageAll.new(:user_id => self.user_id)
|
||||
end
|
||||
end
|
||||
|
||||
def subject
|
||||
case at_message_type
|
||||
when "Issue"
|
||||
"新建问题: " + at_message.subject
|
||||
when "Journal"
|
||||
"问题留言: " + at_message.journalized.subject
|
||||
when 'Message'
|
||||
if(at_message.topic?)
|
||||
"发布新帖: "
|
||||
else
|
||||
"回复帖子: "
|
||||
end + at_message.subject
|
||||
when 'JournalsForMessage'
|
||||
"作业: #{at_message.jour.name} 中留言"
|
||||
else
|
||||
logger.error "error type: #{at_message_type}"
|
||||
end
|
||||
end
|
||||
|
||||
def description
|
||||
case at_message_type
|
||||
when "Issue"
|
||||
at_message.description
|
||||
when "Journal"
|
||||
at_message.notes
|
||||
when 'Message'
|
||||
at_message.content
|
||||
when "JournalsForMessage"
|
||||
at_message.notes
|
||||
else
|
||||
logger.error "error type: #{at_message_type}"
|
||||
end
|
||||
end
|
||||
|
||||
def author
|
||||
case at_message_type
|
||||
when "Issue"
|
||||
at_message.author
|
||||
when "Journal"
|
||||
at_message.user
|
||||
when 'Message'
|
||||
at_message.author
|
||||
when 'JournalsForMessage'
|
||||
at_message.user
|
||||
else
|
||||
logger.error "error type: #{at_message_type}"
|
||||
end
|
||||
end
|
||||
|
||||
def url
|
||||
case at_message_type
|
||||
when "Issue"
|
||||
{controller: :issues, action: :show, id: at_message}
|
||||
when "Journal"
|
||||
{controller: :issues, action: :show, id: at_message.journalized}
|
||||
when 'Message'
|
||||
{controller: :boards, action: :show, project_id: at_message.board.project, id: at_message.board}
|
||||
when 'JournalsForMessage'
|
||||
{controller: :homework_common, action: :index, course: at_message.jour.course_id}
|
||||
else
|
||||
logger.error "error type: #{at_message_type}"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue