activity_notification 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE/bug_report.md +22 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +17 -0
- data/.github/pull_request_template.md +13 -0
- data/.gitignore +10 -3
- data/.travis.yml +6 -5
- data/CHANGELOG.md +22 -0
- data/Gemfile +8 -2
- data/Procfile +1 -1
- data/README.md +153 -1510
- data/activity_notification.gemspec +4 -1
- data/app/channels/activity_notification/notification_api_channel.rb +12 -0
- data/app/channels/activity_notification/notification_api_with_devise_channel.rb +46 -0
- data/app/channels/activity_notification/notification_channel.rb +1 -1
- data/app/channels/activity_notification/notification_with_devise_channel.rb +1 -1
- data/app/controllers/activity_notification/apidocs_controller.rb +75 -0
- data/app/controllers/activity_notification/notifications_api_controller.rb +143 -0
- data/app/controllers/activity_notification/notifications_api_with_devise_controller.rb +7 -0
- data/app/controllers/activity_notification/notifications_controller.rb +79 -53
- data/app/controllers/activity_notification/subscriptions_api_controller.rb +197 -0
- data/app/controllers/activity_notification/subscriptions_api_with_devise_controller.rb +7 -0
- data/app/controllers/activity_notification/subscriptions_controller.rb +78 -69
- data/app/views/activity_notification/notifications/default/_default.html.erb +18 -18
- data/app/views/activity_notification/notifications/default/_default_without_grouping.html.erb +14 -14
- data/app/views/activity_notification/notifications/default/index.html.erb +6 -6
- data/app/views/activity_notification/optional_targets/default/action_cable_channel/_default.html.erb +176 -0
- data/app/views/activity_notification/subscriptions/default/_form.html.erb +1 -1
- data/app/views/activity_notification/subscriptions/default/_notification_keys.html.erb +3 -31
- data/app/views/activity_notification/subscriptions/default/_subscription.html.erb +7 -7
- data/app/views/activity_notification/subscriptions/default/index.html.erb +11 -7
- data/bin/deploy_on_heroku.sh +3 -1
- data/docs/CODE_OF_CONDUCT.md +76 -0
- data/docs/CONTRIBUTING.md +36 -0
- data/docs/Functions.md +1130 -0
- data/docs/Setup.md +674 -0
- data/docs/Testing.md +148 -0
- data/gemfiles/Gemfile.rails-4.2 +3 -0
- data/gemfiles/Gemfile.rails-5.0 +3 -0
- data/gemfiles/Gemfile.rails-5.1 +3 -0
- data/gemfiles/Gemfile.rails-5.2 +3 -0
- data/gemfiles/{Gemfile.rails-6.0.rc → Gemfile.rails-6.0} +5 -3
- data/lib/activity_notification.rb +13 -1
- data/lib/activity_notification/apis/notification_api.rb +29 -92
- data/lib/activity_notification/apis/subscription_api.rb +20 -8
- data/lib/activity_notification/apis/swagger.rb +6 -0
- data/lib/activity_notification/config.rb +41 -21
- data/lib/activity_notification/controllers/common_api_controller.rb +30 -0
- data/lib/activity_notification/controllers/common_controller.rb +44 -20
- data/lib/activity_notification/controllers/concerns/swagger/error_responses.rb +55 -0
- data/lib/activity_notification/controllers/concerns/swagger/notifications_api.rb +273 -0
- data/lib/activity_notification/controllers/concerns/swagger/notifications_parameters.rb +92 -0
- data/lib/activity_notification/controllers/concerns/swagger/subscriptions_api.rb +405 -0
- data/lib/activity_notification/controllers/concerns/swagger/subscriptions_parameters.rb +50 -0
- data/lib/activity_notification/controllers/devise_authentication_controller.rb +7 -6
- data/lib/activity_notification/gem_version.rb +14 -0
- data/lib/activity_notification/helpers/errors.rb +2 -0
- data/lib/activity_notification/helpers/view_helpers.rb +4 -0
- data/lib/activity_notification/mailers/helpers.rb +17 -10
- data/lib/activity_notification/models/concerns/notifiable.rb +26 -10
- data/lib/activity_notification/models/concerns/subscriber.rb +12 -1
- data/lib/activity_notification/models/concerns/swagger/error_schema.rb +36 -0
- data/lib/activity_notification/models/concerns/swagger/notification_schema.rb +209 -0
- data/lib/activity_notification/models/concerns/swagger/subscription_schema.rb +162 -0
- data/lib/activity_notification/models/concerns/target.rb +36 -10
- data/lib/activity_notification/models/notification.rb +1 -0
- data/lib/activity_notification/models/subscription.rb +1 -0
- data/lib/activity_notification/optional_targets/action_cable_api_channel.rb +69 -0
- data/lib/activity_notification/optional_targets/action_cable_channel.rb +68 -0
- data/lib/activity_notification/optional_targets/base.rb +7 -13
- data/lib/activity_notification/orm/active_record/notification.rb +17 -1
- data/lib/activity_notification/orm/active_record/subscription.rb +1 -1
- data/lib/activity_notification/orm/dynamoid.rb +28 -0
- data/lib/activity_notification/orm/dynamoid/extension.rb +79 -1
- data/lib/activity_notification/orm/dynamoid/notification.rb +1 -1
- data/lib/activity_notification/orm/dynamoid/subscription.rb +1 -1
- data/lib/activity_notification/orm/mongoid.rb +22 -0
- data/lib/activity_notification/orm/mongoid/notification.rb +17 -1
- data/lib/activity_notification/orm/mongoid/subscription.rb +1 -1
- data/lib/activity_notification/rails/routes.rb +132 -48
- data/lib/activity_notification/renderable.rb +13 -2
- data/lib/activity_notification/roles/acts_as_notifiable.rb +38 -20
- data/lib/activity_notification/version.rb +1 -1
- data/lib/generators/activity_notification/controllers_generator.rb +2 -1
- data/lib/generators/templates/activity_notification.rb +8 -0
- data/lib/generators/templates/controllers/notifications_api_controller.rb +31 -0
- data/lib/generators/templates/controllers/notifications_api_with_devise_controller.rb +31 -0
- data/lib/generators/templates/controllers/notifications_controller.rb +1 -37
- data/lib/generators/templates/controllers/notifications_with_devise_controller.rb +1 -45
- data/lib/generators/templates/controllers/subscriptions_api_controller.rb +61 -0
- data/lib/generators/templates/controllers/subscriptions_api_with_devise_controller.rb +61 -0
- data/lib/generators/templates/controllers/subscriptions_controller.rb +14 -37
- data/lib/generators/templates/controllers/subscriptions_with_devise_controller.rb +14 -45
- data/lib/generators/templates/models/README +8 -4
- data/lib/generators/templates/models/notification.rb +1 -1
- data/lib/generators/templates/models/subscription.rb +1 -1
- data/package.json +8 -0
- data/spec/channels/notification_api_channel_shared_examples.rb +59 -0
- data/spec/channels/notification_api_channel_spec.rb +51 -0
- data/spec/channels/notification_api_with_devise_channel_spec.rb +78 -0
- data/spec/concerns/apis/notification_api_spec.rb +37 -2
- data/spec/concerns/models/notifiable_spec.rb +64 -0
- data/spec/concerns/models/subscriber_spec.rb +13 -16
- data/spec/concerns/models/target_spec.rb +32 -0
- data/spec/concerns/renderable_spec.rb +2 -2
- data/spec/controllers/controller_spec_utility.rb +136 -0
- data/spec/controllers/notifications_api_controller_shared_examples.rb +506 -0
- data/spec/controllers/notifications_api_controller_spec.rb +19 -0
- data/spec/controllers/notifications_api_with_devise_controller_spec.rb +60 -0
- data/spec/controllers/notifications_controller_shared_examples.rb +54 -79
- data/spec/controllers/notifications_controller_spec.rb +1 -2
- data/spec/controllers/notifications_with_devise_controller_spec.rb +3 -12
- data/spec/controllers/subscriptions_api_controller_shared_examples.rb +750 -0
- data/spec/controllers/subscriptions_api_controller_spec.rb +19 -0
- data/spec/controllers/subscriptions_api_with_devise_controller_spec.rb +60 -0
- data/spec/controllers/subscriptions_controller_shared_examples.rb +94 -121
- data/spec/controllers/subscriptions_controller_spec.rb +1 -2
- data/spec/controllers/subscriptions_with_devise_controller_spec.rb +3 -12
- data/spec/helpers/view_helpers_spec.rb +4 -11
- data/spec/mailers/mailer_spec.rb +41 -0
- data/spec/models/notification_spec.rb +17 -0
- data/spec/models/subscription_spec.rb +0 -13
- data/spec/optional_targets/action_cable_api_channel_spec.rb +37 -0
- data/spec/optional_targets/action_cable_channel_spec.rb +44 -0
- data/spec/optional_targets/amazon_sns_spec.rb +0 -2
- data/spec/optional_targets/slack_spec.rb +0 -2
- data/spec/rails_app/Rakefile +9 -0
- data/spec/rails_app/app/assets/config/manifest.js +3 -0
- data/spec/rails_app/app/assets/images/.keep +0 -0
- data/spec/rails_app/app/controllers/admins_controller.rb +21 -0
- data/spec/rails_app/app/controllers/application_controller.rb +1 -1
- data/spec/rails_app/app/controllers/articles_controller.rb +6 -3
- data/spec/rails_app/app/controllers/spa_controller.rb +7 -0
- data/spec/rails_app/app/controllers/users/notifications_controller.rb +0 -65
- data/spec/rails_app/app/controllers/users/notifications_with_devise_controller.rb +0 -73
- data/spec/rails_app/app/controllers/users/subscriptions_controller.rb +0 -77
- data/spec/rails_app/app/controllers/users/subscriptions_with_devise_controller.rb +0 -85
- data/spec/rails_app/app/controllers/users_controller.rb +21 -0
- data/spec/rails_app/app/javascript/App.vue +104 -0
- data/spec/rails_app/app/javascript/components/DeviseTokenAuth.vue +83 -0
- data/spec/rails_app/app/javascript/components/Top.vue +99 -0
- data/spec/rails_app/app/javascript/components/notifications/Index.vue +200 -0
- data/spec/rails_app/app/javascript/components/notifications/Notification.vue +133 -0
- data/spec/rails_app/app/javascript/components/notifications/NotificationContent.vue +122 -0
- data/spec/rails_app/app/javascript/components/subscriptions/Index.vue +279 -0
- data/spec/rails_app/app/javascript/components/subscriptions/NewSubscription.vue +112 -0
- data/spec/rails_app/app/javascript/components/subscriptions/NotificationKey.vue +141 -0
- data/spec/rails_app/app/javascript/components/subscriptions/Subscription.vue +226 -0
- data/spec/rails_app/app/javascript/config/development.js +5 -0
- data/spec/rails_app/app/javascript/config/environment.js +7 -0
- data/spec/rails_app/app/javascript/config/production.js +5 -0
- data/spec/rails_app/app/javascript/config/test.js +5 -0
- data/spec/rails_app/app/javascript/packs/application.js +18 -0
- data/spec/rails_app/app/javascript/packs/spa.js +11 -0
- data/spec/rails_app/app/javascript/store/auth.js +37 -0
- data/spec/rails_app/app/models/admin.rb +16 -15
- data/spec/rails_app/app/models/article.rb +26 -21
- data/spec/rails_app/app/models/comment.rb +24 -71
- data/spec/rails_app/app/models/user.rb +43 -20
- data/spec/rails_app/app/views/activity_notification/notifications/default/article/_update.html.erb +146 -0
- data/spec/rails_app/app/views/articles/index.html.erb +51 -7
- data/spec/rails_app/app/views/articles/show.html.erb +1 -1
- data/spec/rails_app/app/views/layouts/_header.html.erb +8 -10
- data/spec/rails_app/app/views/spa/index.html.erb +2 -0
- data/spec/rails_app/babel.config.js +72 -0
- data/spec/rails_app/bin/webpack +18 -0
- data/spec/rails_app/bin/webpack-dev-server +18 -0
- data/spec/rails_app/config/application.rb +15 -2
- data/spec/rails_app/config/environment.rb +2 -1
- data/spec/rails_app/config/environments/development.rb +5 -0
- data/spec/rails_app/config/environments/production.rb +3 -0
- data/spec/rails_app/config/environments/test.rb +5 -0
- data/spec/rails_app/config/initializers/activity_notification.rb +8 -0
- data/spec/rails_app/config/initializers/devise_token_auth.rb +55 -0
- data/spec/rails_app/config/initializers/mysql.rb +9 -0
- data/spec/rails_app/config/locales/activity_notification.en.yml +2 -2
- data/spec/rails_app/config/routes.rb +33 -1
- data/spec/rails_app/config/webpack/development.js +5 -0
- data/spec/rails_app/config/webpack/environment.js +7 -0
- data/spec/rails_app/config/webpack/loaders/vue.js +6 -0
- data/spec/rails_app/config/webpack/production.js +5 -0
- data/spec/rails_app/config/webpack/test.js +5 -0
- data/spec/rails_app/config/webpacker.yml +97 -0
- data/spec/rails_app/db/migrate/20191201000000_add_tokens_to_users.rb +10 -0
- data/spec/rails_app/db/schema.rb +4 -1
- data/spec/rails_app/db/seeds.rb +1 -0
- data/spec/rails_app/lib/custom_optional_targets/raise_error.rb +14 -0
- data/spec/rails_app/package.json +23 -0
- data/spec/rails_app/postcss.config.js +12 -0
- data/spec/roles/acts_as_group_spec.rb +0 -2
- data/spec/roles/acts_as_notifiable_spec.rb +1 -3
- data/spec/roles/acts_as_notifier_spec.rb +0 -2
- data/spec/roles/acts_as_target_spec.rb +0 -4
- data/spec/spec_helper.rb +7 -15
- data/spec/version_spec.rb +31 -0
- metadata +187 -13
@@ -0,0 +1,197 @@
|
|
1
|
+
module ActivityNotification
|
2
|
+
# Controller to manage subscriptions API.
|
3
|
+
class SubscriptionsApiController < SubscriptionsController
|
4
|
+
# Include Swagger API reference
|
5
|
+
include Swagger::SubscriptionsApi
|
6
|
+
# Include CommonApiController to select target and define common methods
|
7
|
+
include CommonApiController
|
8
|
+
protect_from_forgery except: [:create]
|
9
|
+
before_action :set_subscription, except: [:index, :create, :find, :optional_target_names]
|
10
|
+
before_action ->{ validate_param(:key) }, only: [:find, :optional_target_names]
|
11
|
+
|
12
|
+
# Returns subscription index of the target.
|
13
|
+
#
|
14
|
+
# GET /:target_type/:target_id/subscriptions
|
15
|
+
# @overload index(params)
|
16
|
+
# @param [Hash] params Request parameter options for subscription index
|
17
|
+
# @option params [String] :filter (nil) Filter option to load subscription index by their configuration status (Nothing as all, 'configured' or 'unconfigured')
|
18
|
+
# @option params [String] :limit (nil) Limit to query for subscriptions
|
19
|
+
# @option params [String] :reverse ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
|
20
|
+
# @option params [String] :filtered_by_key (nil) Key of the subscription for filter
|
21
|
+
# @return [JSON] configured_count: count of subscription index, subscriptions: subscription index, unconfigured_count: count of unconfigured notification keys, unconfigured_notification_keys: unconfigured notification keys
|
22
|
+
def index
|
23
|
+
super
|
24
|
+
json_response = { configured_count: @subscriptions.size, subscriptions: @subscriptions } if @subscriptions
|
25
|
+
json_response = (json_response || {}).merge(unconfigured_count: @notification_keys.size, unconfigured_notification_keys: @notification_keys) if @notification_keys
|
26
|
+
render json: json_response
|
27
|
+
end
|
28
|
+
|
29
|
+
# Creates new subscription.
|
30
|
+
#
|
31
|
+
# POST /:target_type/:target_id/subscriptions
|
32
|
+
# @overload create(params)
|
33
|
+
# @param [Hash] params Request parameters
|
34
|
+
# @option params [String] :subscription Subscription parameters
|
35
|
+
# @option params [String] :subscription[:key] Key of the subscription
|
36
|
+
# @option params [String] :subscription[:subscribing] (nil) Whether the target will subscribe to the notification
|
37
|
+
# @option params [String] :subscription[:subscribing_to_email] (nil) Whether the target will subscribe to the notification email
|
38
|
+
# @return [JSON] Created subscription
|
39
|
+
def create
|
40
|
+
render_invalid_parameter("Parameter is missing or the value is empty: subscription") and return if params[:subscription].blank?
|
41
|
+
optional_target_names = (params[:subscription][:optional_targets] || {}).keys.select { |key| !key.to_s.start_with?("subscribing_to_") }
|
42
|
+
optional_target_names.each do |optional_target_name|
|
43
|
+
subscribing_param = params[:subscription][:optional_targets][optional_target_name][:subscribing]
|
44
|
+
params[:subscription][:optional_targets]["subscribing_to_#{optional_target_name}"] = subscribing_param unless subscribing_param.nil?
|
45
|
+
end
|
46
|
+
super
|
47
|
+
render status: 201, location: { action: :show, id: @subscription }, json: subscription_json
|
48
|
+
end
|
49
|
+
|
50
|
+
# Finds and shows a subscription from specified key.
|
51
|
+
#
|
52
|
+
# GET /:target_type/:target_id/subscriptions/find
|
53
|
+
# @overload index(params)
|
54
|
+
# @param [Hash] params Request parameter options for subscription index
|
55
|
+
# @option params [required, String] :key (nil) Key of the subscription
|
56
|
+
# @return [JSON] Found single subscription
|
57
|
+
def find
|
58
|
+
super
|
59
|
+
render json: subscription_json if @subscription
|
60
|
+
end
|
61
|
+
|
62
|
+
# Finds and returns configured optional_target names from specified key.
|
63
|
+
#
|
64
|
+
# GET /:target_type/:target_id/subscriptions/optional_target_names
|
65
|
+
# @overload index(params)
|
66
|
+
# @param [Hash] params Request parameter options for subscription index
|
67
|
+
# @option params [required, String] :key (nil) Key of the subscription
|
68
|
+
# @return [JSON] Configured optional_target names
|
69
|
+
def optional_target_names
|
70
|
+
latest_notification = @target.notifications.filtered_by_key(params[:key]).latest
|
71
|
+
latest_notification ?
|
72
|
+
render(json: { configured_count: latest_notification.optional_target_names.length, optional_target_names: latest_notification.optional_target_names }) :
|
73
|
+
render_resource_not_found("Couldn't find notification with this target and 'key'=#{params[:key]}")
|
74
|
+
end
|
75
|
+
|
76
|
+
# Shows a subscription.
|
77
|
+
#
|
78
|
+
# GET /:target_type/:target_id/subscriptions/:id
|
79
|
+
# @overload show(params)
|
80
|
+
# @param [Hash] params Request parameters
|
81
|
+
# @return [JSON] Found single subscription
|
82
|
+
def show
|
83
|
+
super
|
84
|
+
render json: subscription_json
|
85
|
+
end
|
86
|
+
|
87
|
+
# Deletes a subscription.
|
88
|
+
#
|
89
|
+
# DELETE /:target_type/:target_id/subscriptions/:id
|
90
|
+
#
|
91
|
+
# @overload destroy(params)
|
92
|
+
# @param [Hash] params Request parameters
|
93
|
+
# @return [JSON] 204 No Content
|
94
|
+
def destroy
|
95
|
+
super
|
96
|
+
head 204
|
97
|
+
end
|
98
|
+
|
99
|
+
# Updates a subscription to subscribe to the notifications.
|
100
|
+
#
|
101
|
+
# PUT /:target_type/:target_id/subscriptions/:id/subscribe
|
102
|
+
# @overload open(params)
|
103
|
+
# @param [Hash] params Request parameters
|
104
|
+
# @option params [String] :with_email_subscription ('true') Whether the subscriber also subscribes notification email
|
105
|
+
# @option params [String] :with_optional_targets ('true') Whether the subscriber also subscribes optional targets
|
106
|
+
# @return [JSON] Updated subscription
|
107
|
+
def subscribe
|
108
|
+
super
|
109
|
+
validate_and_render_subscription
|
110
|
+
end
|
111
|
+
|
112
|
+
# Updates a subscription to unsubscribe to the notifications.
|
113
|
+
#
|
114
|
+
# PUT /:target_type/:target_id/subscriptions/:id/unsubscribe
|
115
|
+
# @overload open(params)
|
116
|
+
# @param [Hash] params Request parameters
|
117
|
+
# @return [JSON] Updated subscription
|
118
|
+
def unsubscribe
|
119
|
+
super
|
120
|
+
validate_and_render_subscription
|
121
|
+
end
|
122
|
+
|
123
|
+
# Updates a subscription to subscribe to the notification email.
|
124
|
+
#
|
125
|
+
# PUT /:target_type/:target_id/subscriptions/:id/subscribe_email
|
126
|
+
# @overload open(params)
|
127
|
+
# @param [Hash] params Request parameters
|
128
|
+
# @return [JSON] Updated subscription
|
129
|
+
def subscribe_to_email
|
130
|
+
super
|
131
|
+
validate_and_render_subscription
|
132
|
+
end
|
133
|
+
|
134
|
+
# Updates a subscription to unsubscribe to the notification email.
|
135
|
+
#
|
136
|
+
# PUT /:target_type/:target_id/subscriptions/:id/unsubscribe_email
|
137
|
+
# @overload open(params)
|
138
|
+
# @param [Hash] params Request parameters
|
139
|
+
# @return [JSON] Updated subscription
|
140
|
+
def unsubscribe_to_email
|
141
|
+
super
|
142
|
+
validate_and_render_subscription
|
143
|
+
end
|
144
|
+
|
145
|
+
# Updates a subscription to subscribe to the specified optional target.
|
146
|
+
#
|
147
|
+
# PUT /:target_type/:target_id/subscriptions/:id/subscribe_to_optional_target
|
148
|
+
# @overload open(params)
|
149
|
+
# @param [Hash] params Request parameters
|
150
|
+
# @option params [required, String] :optional_target_name (nil) Class name of the optional target implementation (e.g. 'amazon_sns', 'slack')
|
151
|
+
# @return [JSON] Updated subscription
|
152
|
+
def subscribe_to_optional_target
|
153
|
+
super
|
154
|
+
validate_and_render_subscription
|
155
|
+
end
|
156
|
+
|
157
|
+
# Updates a subscription to unsubscribe to the specified optional target.
|
158
|
+
#
|
159
|
+
# PUT /:target_type/:target_id/subscriptions/:id/unsubscribe_to_optional_target
|
160
|
+
# @overload open(params)
|
161
|
+
# @param [Hash] params Request parameters
|
162
|
+
# @option params [required, String] :optional_target_name (nil) Class name of the optional target implementation (e.g. 'amazon_sns', 'slack')
|
163
|
+
# @return [JSON] Updated subscription
|
164
|
+
def unsubscribe_to_optional_target
|
165
|
+
super
|
166
|
+
validate_and_render_subscription
|
167
|
+
end
|
168
|
+
|
169
|
+
protected
|
170
|
+
|
171
|
+
# Returns include option for subscription JSON
|
172
|
+
# @api protected
|
173
|
+
def subscription_json_include_option
|
174
|
+
[:target].freeze
|
175
|
+
end
|
176
|
+
|
177
|
+
# Returns methods option for subscription JSON
|
178
|
+
# @api protected
|
179
|
+
def subscription_json_methods_option
|
180
|
+
[].freeze
|
181
|
+
end
|
182
|
+
|
183
|
+
# Returns JSON of @subscription
|
184
|
+
# @api protected
|
185
|
+
def subscription_json
|
186
|
+
@subscription.as_json(include: subscription_json_include_option, methods: subscription_json_methods_option)
|
187
|
+
end
|
188
|
+
|
189
|
+
# Validate @subscription and render JSON of @subscription
|
190
|
+
# @api protected
|
191
|
+
def validate_and_render_subscription
|
192
|
+
raise RecordInvalidError, @subscription.errors.full_messages.first if @subscription.invalid?
|
193
|
+
render json: subscription_json
|
194
|
+
end
|
195
|
+
|
196
|
+
end
|
197
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
module ActivityNotification
|
2
|
+
# Controller to manage subscriptions API with Devise authentication.
|
3
|
+
class SubscriptionsApiWithDeviseController < SubscriptionsApiController
|
4
|
+
include DeviseTokenAuth::Concerns::SetUserByToken
|
5
|
+
include DeviseAuthenticationController
|
6
|
+
end
|
7
|
+
end
|
@@ -3,54 +3,62 @@ module ActivityNotification
|
|
3
3
|
class SubscriptionsController < ActivityNotification.config.parent_controller.constantize
|
4
4
|
# Include CommonController to select target and define common methods
|
5
5
|
include CommonController
|
6
|
-
before_action :set_subscription, except: [:index, :create]
|
7
|
-
before_action :
|
6
|
+
before_action :set_subscription, except: [:index, :create, :find]
|
7
|
+
before_action ->{ validate_param(:key) }, only: [:find]
|
8
|
+
before_action ->{ validate_param(:optional_target_name) }, only: [:subscribe_to_optional_target, :unsubscribe_to_optional_target]
|
8
9
|
|
9
10
|
# Shows subscription index of the target.
|
10
11
|
#
|
11
12
|
# GET /:target_type/:target_id/subscriptions
|
12
13
|
# @overload index(params)
|
13
14
|
# @param [Hash] params Request parameter options for subscription index
|
14
|
-
# @option params [String] :filter (nil) Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
|
15
|
+
# @option params [String] :filter (nil) Filter option to load subscription index by their configuration status (Nothing as all, 'configured' or 'unconfigured')
|
15
16
|
# @option params [String] :limit (nil) Limit to query for subscriptions
|
16
|
-
# @option params [String] :reverse ('false')
|
17
|
+
# @option params [String] :reverse ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
|
17
18
|
# @option params [String] :filtered_by_key (nil) Key of the subscription for filter
|
18
|
-
# @return [
|
19
|
+
# @return [Response] HTML view of subscription index
|
19
20
|
def index
|
20
21
|
set_index_options
|
21
22
|
load_index if params[:reload].to_s.to_boolean(true)
|
22
|
-
respond_to do |format|
|
23
|
-
format.html # index.html.erb
|
24
|
-
format.json { render json: { subscriptions: @subscriptions, unconfigured_notification_keys: @notification_keys } }
|
25
|
-
end
|
26
23
|
end
|
27
24
|
|
28
|
-
# Creates
|
29
|
-
#
|
30
|
-
# POST /:target_type/:target_id/subscriptions
|
25
|
+
# Creates new subscription.
|
31
26
|
#
|
27
|
+
# PUT /:target_type/:target_id/subscriptions
|
32
28
|
# @overload create(params)
|
33
29
|
# @param [Hash] params Request parameters
|
34
30
|
# @option params [String] :subscription Subscription parameters
|
35
31
|
# @option params [String] :subscription[:key] Key of the subscription
|
36
|
-
# @option params [String] :subscription[:subscribing] (nil)
|
37
|
-
# @option params [String] :subscription[:subscribing_to_email] (nil)
|
38
|
-
# @option params [String] :filter (nil)
|
39
|
-
# @option params [String] :limit (nil)
|
40
|
-
# @option params [String] :reverse ('false')
|
41
|
-
# @option params [String] :filtered_by_key (nil)
|
42
|
-
# @return [
|
32
|
+
# @option params [String] :subscription[:subscribing] (nil) Whether the target will subscribe to the notification
|
33
|
+
# @option params [String] :subscription[:subscribing_to_email] (nil) Whether the target will subscribe to the notification email
|
34
|
+
# @option params [String] :filter (nil) Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
|
35
|
+
# @option params [String] :limit (nil) Limit to query for subscriptions
|
36
|
+
# @option params [String] :reverse ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
|
37
|
+
# @option params [String] :filtered_by_key (nil) Key of the subscription for filter
|
38
|
+
# @return [Response] JavaScript view for ajax request or redirects to back as default
|
43
39
|
def create
|
44
|
-
@target.create_subscription(subscription_params)
|
40
|
+
@subscription = @target.create_subscription(subscription_params)
|
45
41
|
return_back_or_ajax
|
46
42
|
end
|
47
43
|
|
44
|
+
# Finds and shows a subscription from specified key.
|
45
|
+
#
|
46
|
+
# GET /:target_type/:target_id/subscriptions/find
|
47
|
+
# @overload index(params)
|
48
|
+
# @param [Hash] params Request parameter options for subscription index
|
49
|
+
# @option params [required, String] :key (nil) Key of the subscription
|
50
|
+
# @return [Response] HTML view as default or JSON of subscription index with json format parameter
|
51
|
+
def find
|
52
|
+
@subscription = @target.find_subscription(params[:key])
|
53
|
+
@subscription ? redirect_to_subscription_path : render_resource_not_found("Couldn't find subscription with this target and 'key'=#{params[:key]}")
|
54
|
+
end
|
55
|
+
|
48
56
|
# Shows a subscription.
|
49
57
|
#
|
50
58
|
# GET /:target_type/:target_id/subscriptions/:id
|
51
59
|
# @overload show(params)
|
52
60
|
# @param [Hash] params Request parameters
|
53
|
-
# @return [
|
61
|
+
# @return [Response] HTML view as default
|
54
62
|
def show
|
55
63
|
set_index_options
|
56
64
|
end
|
@@ -58,109 +66,108 @@ module ActivityNotification
|
|
58
66
|
# Deletes a subscription.
|
59
67
|
#
|
60
68
|
# DELETE /:target_type/:target_id/subscriptions/:id
|
61
|
-
#
|
62
69
|
# @overload destroy(params)
|
63
70
|
# @param [Hash] params Request parameters
|
64
71
|
# @option params [String] :filter (nil) Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
|
65
72
|
# @option params [String] :limit (nil) Limit to query for subscriptions
|
66
|
-
# @option params [String] :reverse ('false')
|
73
|
+
# @option params [String] :reverse ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
|
67
74
|
# @option params [String] :filtered_by_key (nil) Key of the subscription for filter
|
68
|
-
# @return [
|
75
|
+
# @return [Response] JavaScript view for ajax request or redirects to back as default
|
69
76
|
def destroy
|
70
77
|
@subscription.destroy
|
71
78
|
return_back_or_ajax
|
72
79
|
end
|
73
80
|
|
74
|
-
#
|
81
|
+
# Updates a subscription to subscribe to notifications.
|
75
82
|
#
|
76
|
-
#
|
83
|
+
# PUT /:target_type/:target_id/subscriptions/:id/subscribe
|
77
84
|
# @overload open(params)
|
78
85
|
# @param [Hash] params Request parameters
|
79
|
-
# @option params [String] :with_email_subscription ('true')
|
80
|
-
# @option params [String] :with_optional_targets ('true')
|
86
|
+
# @option params [String] :with_email_subscription ('true') Whether the subscriber also subscribes notification email
|
87
|
+
# @option params [String] :with_optional_targets ('true') Whether the subscriber also subscribes optional targets
|
81
88
|
# @option params [String] :filter (nil) Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
|
82
89
|
# @option params [String] :limit (nil) Limit to query for subscriptions
|
83
|
-
# @option params [String] :reverse ('false')
|
90
|
+
# @option params [String] :reverse ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
|
84
91
|
# @option params [String] :filtered_by_key (nil) Key of the subscription for filter
|
85
|
-
# @return [
|
92
|
+
# @return [Response] JavaScript view for ajax request or redirects to back as default
|
86
93
|
def subscribe
|
87
94
|
@subscription.subscribe(with_email_subscription: params[:with_email_subscription].to_s.to_boolean(true),
|
88
95
|
with_optional_targets: params[:with_optional_targets].to_s.to_boolean(true))
|
89
96
|
return_back_or_ajax
|
90
97
|
end
|
91
98
|
|
92
|
-
#
|
99
|
+
# Updates a subscription to unsubscribe to the notifications.
|
93
100
|
#
|
94
|
-
#
|
101
|
+
# PUT /:target_type/:target_id/subscriptions/:id/unsubscribe
|
95
102
|
# @overload open(params)
|
96
103
|
# @param [Hash] params Request parameters
|
97
104
|
# @option params [String] :filter (nil) Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
|
98
105
|
# @option params [String] :limit (nil) Limit to query for subscriptions
|
99
|
-
# @option params [String] :reverse ('false')
|
106
|
+
# @option params [String] :reverse ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
|
100
107
|
# @option params [String] :filtered_by_key (nil) Key of the subscription for filter
|
101
|
-
# @return [
|
108
|
+
# @return [Response] JavaScript view for ajax request or redirects to back as default
|
102
109
|
def unsubscribe
|
103
110
|
@subscription.unsubscribe
|
104
111
|
return_back_or_ajax
|
105
112
|
end
|
106
113
|
|
107
|
-
#
|
114
|
+
# Updates a subscription to subscribe to the notification email.
|
108
115
|
#
|
109
|
-
#
|
116
|
+
# PUT /:target_type/:target_id/subscriptions/:id/subscribe_email
|
110
117
|
# @overload open(params)
|
111
118
|
# @param [Hash] params Request parameters
|
112
119
|
# @option params [String] :filter (nil) Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
|
113
120
|
# @option params [String] :limit (nil) Limit to query for subscriptions
|
114
|
-
# @option params [String] :reverse ('false')
|
121
|
+
# @option params [String] :reverse ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
|
115
122
|
# @option params [String] :filtered_by_key (nil) Key of the subscription for filter
|
116
|
-
# @return [
|
123
|
+
# @return [Response] JavaScript view for ajax request or redirects to back as default
|
117
124
|
def subscribe_to_email
|
118
125
|
@subscription.subscribe_to_email
|
119
126
|
return_back_or_ajax
|
120
127
|
end
|
121
128
|
|
122
|
-
#
|
129
|
+
# Updates a subscription to unsubscribe to the notification email.
|
123
130
|
#
|
124
|
-
#
|
131
|
+
# PUT /:target_type/:target_id/subscriptions/:id/unsubscribe_email
|
125
132
|
# @overload open(params)
|
126
133
|
# @param [Hash] params Request parameters
|
127
134
|
# @option params [String] :filter (nil) Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
|
128
135
|
# @option params [String] :limit (nil) Limit to query for subscriptions
|
129
|
-
# @option params [String] :reverse ('false')
|
136
|
+
# @option params [String] :reverse ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
|
130
137
|
# @option params [String] :filtered_by_key (nil) Key of the subscription for filter
|
131
|
-
# @return [
|
138
|
+
# @return [Response] JavaScript view for ajax request or redirects to back as default
|
132
139
|
def unsubscribe_to_email
|
133
140
|
@subscription.unsubscribe_to_email
|
134
141
|
return_back_or_ajax
|
135
142
|
end
|
136
143
|
|
137
|
-
#
|
144
|
+
# Updates a subscription to subscribe to the specified optional target.
|
138
145
|
#
|
139
|
-
#
|
146
|
+
# PUT /:target_type/:target_id/subscriptions/:id/subscribe_to_optional_target
|
140
147
|
# @overload open(params)
|
141
148
|
# @param [Hash] params Request parameters
|
142
|
-
# @option params [required, String] :optional_target_name (nil)
|
143
|
-
# @option params [String] :filter (nil)
|
144
|
-
# @option params [String] :limit (nil)
|
145
|
-
# @option params [String] :reverse ('false')
|
146
|
-
# @option params [String] :filtered_by_key (nil)
|
147
|
-
# @return [
|
149
|
+
# @option params [required, String] :optional_target_name (nil) Class name of the optional target implementation (e.g. 'amazon_sns', 'slack')
|
150
|
+
# @option params [String] :filter (nil) Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
|
151
|
+
# @option params [String] :limit (nil) Limit to query for subscriptions
|
152
|
+
# @option params [String] :reverse ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
|
153
|
+
# @option params [String] :filtered_by_key (nil) Key of the subscription for filter
|
154
|
+
# @return [Response] JavaScript view for ajax request or redirects to back as default
|
148
155
|
def subscribe_to_optional_target
|
149
156
|
@subscription.subscribe_to_optional_target(params[:optional_target_name])
|
150
157
|
return_back_or_ajax
|
151
158
|
end
|
152
159
|
|
153
|
-
#
|
160
|
+
# Updates a subscription to unsubscribe to the specified optional target.
|
154
161
|
#
|
155
|
-
#
|
162
|
+
# PUT /:target_type/:target_id/subscriptions/:id/unsubscribe_to_optional_target
|
156
163
|
# @overload open(params)
|
157
164
|
# @param [Hash] params Request parameters
|
158
|
-
# @option params [required, String] :optional_target_name (nil)
|
159
|
-
# @option params [String] :filter (nil)
|
160
|
-
# @option params [String] :limit (nil)
|
161
|
-
# @option params [String] :reverse ('false')
|
162
|
-
# @option params [String] :filtered_by_key (nil)
|
163
|
-
# @return [
|
165
|
+
# @option params [required, String] :optional_target_name (nil) Class name of the optional target implementation (e.g. 'amazon_sns', 'slack')
|
166
|
+
# @option params [String] :filter (nil) Filter option to load subscription index (Nothing as all, 'configured' or 'unconfigured')
|
167
|
+
# @option params [String] :limit (nil) Limit to query for subscriptions
|
168
|
+
# @option params [String] :reverse ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
|
169
|
+
# @option params [String] :filtered_by_key (nil) Key of the subscription for filter
|
170
|
+
# @return [Response] JavaScript view for ajax request or redirects to back as default
|
164
171
|
def unsubscribe_to_optional_target
|
165
172
|
@subscription.unsubscribe_to_optional_target(params[:optional_target_name])
|
166
173
|
return_back_or_ajax
|
@@ -177,11 +184,14 @@ module ActivityNotification
|
|
177
184
|
|
178
185
|
# Only allow a trusted parameter "white list" through.
|
179
186
|
def subscription_params
|
180
|
-
|
181
|
-
|
182
|
-
|
187
|
+
if params[:subscription].present?
|
188
|
+
optional_target_keys = (params[:subscription][:optional_targets] || {}).keys.select { |key| key.to_s.start_with?("subscribing_to_") }
|
189
|
+
optional_target_keys.each do |optional_target_key|
|
190
|
+
boolean_value = params[:subscription][:optional_targets][optional_target_key].respond_to?(:to_boolean) ? params[:subscription][:optional_targets][optional_target_key].to_boolean : !!params[:subscription][:optional_targets][optional_target_key]
|
191
|
+
params[:subscription][:optional_targets][optional_target_key] = boolean_value
|
192
|
+
end
|
183
193
|
end
|
184
|
-
params.require(:subscription).permit(:key, :subscribing, :subscribing_to_email, optional_targets:
|
194
|
+
params.require(:subscription).permit(:key, :subscribing, :subscribing_to_email, optional_targets: optional_target_keys)
|
185
195
|
end
|
186
196
|
|
187
197
|
# Sets options to load subscription index from request parameters.
|
@@ -212,6 +222,12 @@ module ActivityNotification
|
|
212
222
|
end
|
213
223
|
end
|
214
224
|
|
225
|
+
# Redirect to subscription path
|
226
|
+
# @api protected
|
227
|
+
def redirect_to_subscription_path
|
228
|
+
redirect_to action: :show, id: @subscription
|
229
|
+
end
|
230
|
+
|
215
231
|
# Returns controller path.
|
216
232
|
# This method is called from target_view_path method and can be overridden.
|
217
233
|
# @api protected
|
@@ -220,12 +236,5 @@ module ActivityNotification
|
|
220
236
|
"activity_notification/subscriptions"
|
221
237
|
end
|
222
238
|
|
223
|
-
# Validate optional_target_name param and return HTTP 400 unless it presents.
|
224
|
-
def validate_optional_target_param
|
225
|
-
if params[:optional_target_name].blank?
|
226
|
-
render plain: "400 Bad Request: Missing parameter", status: 400
|
227
|
-
end
|
228
|
-
end
|
229
|
-
|
230
239
|
end
|
231
240
|
end
|