forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_to_inbox.rb
82 lines (74 loc) · 2.71 KB
/
send_to_inbox.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#
# 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)
if CANVAS_RAILS2
klass.send(:class_inheritable_accessor, :send_to_inbox_block)
else
klass.send(:class_attribute, :send_to_inbox_block)
end
end
def on_create_send_to_inboxes(&block)
self.send_to_inbox_block = block
after_create :queue_create_inbox_items
end
end
module SendToInboxInstanceMethods
def queue_create_inbox_items
send_later_if_production(:create_inbox_items)
end
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]
truncate_text(inbox_results[:body], :max_length => 255)
elsif inbox_results[:html_body]
strip_and_truncate(inbox_results[:html_body] || "", :max_length => 255)
else
""
end
@inbox_item_recipient_ids = (Array(inbox_results[:recipients]) || []).map{|i| User.infer_id(i) rescue nil}.compact
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,
:subject => truncate_text(inbox_results[:subject] || I18n.t('lib.send_to_inbox.default_subject', "No Subject"), :max_length => 255),
:body_teaser => inbox_results[:body_teaser],
:sender_id => sender_id
)
end
end
rescue => e
ErrorReport.log_exception(:default, e, {
:message => "SendToInbox failure",
})
nil
end
def inbox_item_recipient_ids
@inbox_item_recipient_ids
end
end
def self.included(klass)
klass.send :include, SendToInboxInstanceMethods
klass.extend SendToInboxClassMethods
end
end