2011-02-01 09:57:29 +08:00
|
|
|
#
|
|
|
|
# Copyright (C) 2011 Instructure, Inc.
|
|
|
|
#
|
|
|
|
# This file is part of Canvas.
|
|
|
|
#
|
|
|
|
# Canvas is free software: you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
# Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License along
|
|
|
|
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
module SendToInbox
|
|
|
|
|
|
|
|
module SendToInboxClassMethods
|
|
|
|
def self.extended(klass)
|
2014-07-24 01:14:22 +08:00
|
|
|
klass.send(:class_attribute, :send_to_inbox_block)
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
2011-06-17 02:56:11 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
def on_create_send_to_inboxes(&block)
|
|
|
|
self.send_to_inbox_block = block
|
|
|
|
after_create :queue_create_inbox_items
|
|
|
|
end
|
|
|
|
end
|
2011-06-17 02:56:11 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
module SendToInboxInstanceMethods
|
|
|
|
def queue_create_inbox_items
|
2011-06-17 02:56:11 +08:00
|
|
|
send_later_if_production(:create_inbox_items)
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
2011-06-17 02:56:11 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
def create_inbox_items
|
|
|
|
self.extend TextHelper
|
|
|
|
block = self.class.send_to_inbox_block
|
|
|
|
inbox_results = self.instance_eval(&block) || {}
|
|
|
|
inbox_results[:body_teaser] = if inbox_results[:body]
|
2014-04-11 01:37:36 +08:00
|
|
|
CanvasTextHelper.truncate_text(inbox_results[:body], :max_length => 255)
|
2011-02-01 09:57:29 +08:00
|
|
|
elsif inbox_results[:html_body]
|
2014-04-11 01:37:36 +08:00
|
|
|
HtmlTextHelper.strip_and_truncate(inbox_results[:html_body] || "", :max_length => 255)
|
2011-02-01 09:57:29 +08:00
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
2011-06-17 02:56:11 +08:00
|
|
|
@inbox_item_recipient_ids = (Array(inbox_results[:recipients]) || []).map{|i| User.infer_id(i) rescue nil}.compact
|
2011-02-01 09:57:29 +08:00
|
|
|
sender_id = User.infer_id(inbox_results[:sender]) rescue nil
|
|
|
|
@inbox_item_recipient_ids.each do |user_id|
|
|
|
|
if user_id
|
|
|
|
InboxItem.create(
|
|
|
|
:user_id => user_id,
|
|
|
|
:asset => self,
|
2014-04-11 01:37:36 +08:00
|
|
|
:subject => CanvasTextHelper.truncate_text(inbox_results[:subject] || I18n.t('lib.send_to_inbox.default_subject', "No Subject"), :max_length => 255),
|
2011-02-01 09:57:29 +08:00
|
|
|
:body_teaser => inbox_results[:body_teaser],
|
|
|
|
:sender_id => sender_id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue => e
|
2011-05-12 00:31:07 +08:00
|
|
|
ErrorReport.log_exception(:default, e, {
|
2011-02-01 09:57:29 +08:00
|
|
|
:message => "SendToInbox failure",
|
2011-06-17 02:56:11 +08:00
|
|
|
})
|
2011-02-01 09:57:29 +08:00
|
|
|
nil
|
|
|
|
end
|
2011-05-12 00:31:07 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
def inbox_item_recipient_ids
|
|
|
|
@inbox_item_recipient_ids
|
|
|
|
end
|
2011-06-17 02:56:11 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
2011-06-17 02:56:11 +08:00
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
def self.included(klass)
|
|
|
|
klass.send :include, SendToInboxInstanceMethods
|
|
|
|
klass.extend SendToInboxClassMethods
|
|
|
|
end
|
|
|
|
end
|