forked from capability-boosters-dev/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
address_book.rb
85 lines (73 loc) · 2.65 KB
/
address_book.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
83
84
85
# frozen_string_literal: true
#
# Copyright (C) 2016 - present 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/>.
# manually require since ::MessageableUser satisfies
# AddressBook::MessageableUser and prevents the autoload
require_relative 'address_book/messageable_user'
# see AddressBook::Base for primary documentation of the interface
module AddressBook
STRATEGIES = {
'messageable_user' => { implementation: AddressBook::MessageableUser, label: lambda{ I18n.t('MessageableUser library') } }.freeze,
}.freeze
DEFAULT_STRATEGY = 'messageable_user'
def self.registry
RequestStore.store[:address_books] ||= {}
end
# choose the implementation of address book according to the plugin setting
def self.strategy
DEFAULT_STRATEGY
end
def self.implementation
return STRATEGIES[strategy][:implementation]
end
# instantiates an address book for the sender
def self.for(sender)
registry[sender] ||= implementation.new(sender)
end
# partitions the list of recipients into user ids and context asset strings
def self.partition_recipients(recipients)
users = ::MessageableUser.individual_recipients(recipients)
contexts = ::MessageableUser.context_recipients(recipients)
return users, contexts
end
# filters the list of users to only those that are "available" (but not
# necessarily known to any particular sender)
def self.available(users)
GuardRail.activate(:secondary) do
::MessageableUser.available.where(id: users).to_a
end
end
def self.decompose_context(context_code)
context_code &&
context_code =~ ::MessageableUser::Calculator::CONTEXT_RECIPIENT &&
Regexp.last_match.to_a[1..-1]
end
def self.valid_context?(context_code)
decompose_context(context_code).present?
end
def self.load_context(context_code)
context_type, context_id = decompose_context(context_code)
return nil unless context_id
context_class =
case context_type
when 'course' then Course
when 'section' then CourseSection
when 'group' then Group
end
context_class.find(context_id)
end
end