From 087b7801396b95095bc5108cc0263731ec63c69b Mon Sep 17 00:00:00 2001 From: Federico Scarpa Date: Mon, 20 Sep 2021 13:45:04 -0300 Subject: [PATCH 01/18] Create events handlers --- lib/mumuki/laboratory/events/events.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/mumuki/laboratory/events/events.rb b/lib/mumuki/laboratory/events/events.rb index 1f85ca2365..2fffe7dde7 100644 --- a/lib/mumuki/laboratory/events/events.rb +++ b/lib/mumuki/laboratory/events/events.rb @@ -3,4 +3,13 @@ event 'AssignmentManuallyEvaluated' do |payload| Assignment.evaluate_manually! payload.deep_symbolize_keys[:assignment] end + + event 'CreateMassiveNotifications' do |payload| + body = payload.with_indifferent_access + Notification.create_massive_notifications_for!(body[:notification], body[:uids]) + end + + event 'SendNotificationEmail' do |payload| + Notification.notify_via_email! payload.with_indifferent_access[:notification_id] + end end From b69c7bd55261506bf8b44dd798b2c4f08197f495 Mon Sep 17 00:00:00 2001 From: Federico Scarpa Date: Wed, 22 Sep 2021 14:05:22 -0300 Subject: [PATCH 02/18] Add Jobs Eventes handlers --- lib/mumuki/laboratory/events/events.rb | 9 --------- lib/mumuki/laboratory/jobs/jobs.rb | 11 +++++++++++ lib/tasks/events.rake | 7 +------ lib/tasks/jobs.rake | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 lib/mumuki/laboratory/jobs/jobs.rb create mode 100644 lib/tasks/jobs.rake diff --git a/lib/mumuki/laboratory/events/events.rb b/lib/mumuki/laboratory/events/events.rb index 2fffe7dde7..1f85ca2365 100644 --- a/lib/mumuki/laboratory/events/events.rb +++ b/lib/mumuki/laboratory/events/events.rb @@ -3,13 +3,4 @@ event 'AssignmentManuallyEvaluated' do |payload| Assignment.evaluate_manually! payload.deep_symbolize_keys[:assignment] end - - event 'CreateMassiveNotifications' do |payload| - body = payload.with_indifferent_access - Notification.create_massive_notifications_for!(body[:notification], body[:uids]) - end - - event 'SendNotificationEmail' do |payload| - Notification.notify_via_email! payload.with_indifferent_access[:notification_id] - end end diff --git a/lib/mumuki/laboratory/jobs/jobs.rb b/lib/mumuki/laboratory/jobs/jobs.rb new file mode 100644 index 0000000000..422add1a20 --- /dev/null +++ b/lib/mumuki/laboratory/jobs/jobs.rb @@ -0,0 +1,11 @@ +Mumukit::Nuntius::JobConsumer.handle do + # Emitted by assigment manual evaluation in classroom + job 'CreateMassiveNotifications' do |payload| + body = payload.with_indifferent_access + Notification.create_massive_notifications_for!(body[:notification], body[:uids]) + end + + job 'SendNotificationEmail' do |payload| + Notification.notify_via_email! payload.with_indifferent_access[:notification_id] + end +end diff --git a/lib/tasks/events.rake b/lib/tasks/events.rake index e3a4521c60..833388a191 100644 --- a/lib/tasks/events.rake +++ b/lib/tasks/events.rake @@ -1,10 +1,5 @@ -module Mumukit::Nuntius::EventConsumer - def self.handled_events - @@handlers.keys - end -end - logger = Mumukit::Nuntius::Logger + namespace :laboratory do namespace :events do task listen: :environment do diff --git a/lib/tasks/jobs.rake b/lib/tasks/jobs.rake new file mode 100644 index 0000000000..c475f56299 --- /dev/null +++ b/lib/tasks/jobs.rake @@ -0,0 +1,14 @@ +logger = Mumukit::Nuntius::Logger + +namespace :laboratory do + namespace :jobs do + task listen: :environment do + logger.info 'Loading job handlers....' + require_relative '../mumuki/laboratory/jobs/jobs' + logger.info "Loaded handlers #{Mumukit::Nuntius::JobConsumer.handled_jobs}!" + + logger.info 'Listening to jobs...' + Mumukit::Nuntius::JobConsumer.start! + end + end +end From 9dbaad5e8107cfb18dbb99e047dfba869ac665ae Mon Sep 17 00:00:00 2001 From: Federico Scarpa Date: Fri, 24 Sep 2021 14:07:25 -0300 Subject: [PATCH 03/18] Add custom notifications templates --- .../notifications/_custom_notification.html.erb | 2 ++ .../previews/_custom_notification.html.erb | 2 ++ .../notifications/_custom_notification.html.erb | 12 ++++++++++++ 3 files changed, 16 insertions(+) create mode 100644 app/views/notifications/_custom_notification.html.erb create mode 100644 app/views/notifications/previews/_custom_notification.html.erb create mode 100644 app/views/user_mailer/notifications/_custom_notification.html.erb diff --git a/app/views/notifications/_custom_notification.html.erb b/app/views/notifications/_custom_notification.html.erb new file mode 100644 index 0000000000..e4d9bda38b --- /dev/null +++ b/app/views/notifications/_custom_notification.html.erb @@ -0,0 +1,2 @@ +<% target = notification.target %> +<%= target.custom_content_plain_text %> diff --git a/app/views/notifications/previews/_custom_notification.html.erb b/app/views/notifications/previews/_custom_notification.html.erb new file mode 100644 index 0000000000..5c59e70923 --- /dev/null +++ b/app/views/notifications/previews/_custom_notification.html.erb @@ -0,0 +1,2 @@ +<% target = notification.target %> +<%= notification_text_preview_item :envelope, target.custom_title, notification.organization.url_for('/user/notifications') %> diff --git a/app/views/user_mailer/notifications/_custom_notification.html.erb b/app/views/user_mailer/notifications/_custom_notification.html.erb new file mode 100644 index 0000000000..1389746b22 --- /dev/null +++ b/app/views/user_mailer/notifications/_custom_notification.html.erb @@ -0,0 +1,12 @@ +<%= target = @notification.target %> +<% if target.custom_content_html.present? %> + <%= render inline: target.custom_content_html %> +<% else %> + <%= render partial: 'user_mailer/mail_template', locals: { + title: t("mailer.title.custom"), + subtitle: '', + text: target.custom_content_plain_text, + button: t("mailer.button.custom"), + url: @organization.url_for('/user/notifications') + } %> +<% end %> From 9eb008b1bbfddaeab073b45c9d7eab7af6aa189c Mon Sep 17 00:00:00 2001 From: Federico Scarpa Date: Fri, 24 Sep 2021 14:07:59 -0300 Subject: [PATCH 04/18] Jobs now user CustomNotification model instead of Notification --- lib/mumuki/laboratory/jobs/jobs.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/mumuki/laboratory/jobs/jobs.rb b/lib/mumuki/laboratory/jobs/jobs.rb index 422add1a20..e27b70e534 100644 --- a/lib/mumuki/laboratory/jobs/jobs.rb +++ b/lib/mumuki/laboratory/jobs/jobs.rb @@ -1,11 +1,12 @@ Mumukit::Nuntius::JobConsumer.handle do # Emitted by assigment manual evaluation in classroom - job 'CreateMassiveNotifications' do |payload| + job 'CustomNotificationCreated' do |payload| body = payload.with_indifferent_access - Notification.create_massive_notifications_for!(body[:notification], body[:uids]) + CustomNotification.notify_users_to_add!(body[:custom_notification_id], body[:uids]) end - job 'SendNotificationEmail' do |payload| - Notification.notify_via_email! payload.with_indifferent_access[:notification_id] + job 'CustomNotificationUserAdded' do |payload| + body = payload.with_indifferent_access + CustomNotification.add_user_and_notify_via_email!(body[:custom_notification_id], body[:uid]) end end From 67c3d382e3e7e6b7d1c326df915f46a4b2641ed6 Mon Sep 17 00:00:00 2001 From: Federico Scarpa Date: Fri, 24 Sep 2021 15:36:05 -0300 Subject: [PATCH 05/18] Gemfile dependencies --- Gemfile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index a31f62b19a..83275fd5a1 100644 --- a/Gemfile +++ b/Gemfile @@ -10,8 +10,6 @@ gem 'mini_racer', '~> 0.4' gem 'uglifier', '~> 2.7' gem 'sanitize', '~> 6.0' -gem 'mumuki-domain', github: 'mumuki/mumuki-domain', branch: 'feature-time-left-for-exam' - group :test do gem 'rspec-rails', '~> 3.6' gem 'factory_bot_rails', '~> 5.0' @@ -37,3 +35,7 @@ group :development, :test do gem 'pry-stack_explorer' gem 'binding_of_caller' end +# +gem 'mumuki-domain', github: 'mumuki/mumuki-domain', branch: 'feature-async-massive-notifications' +# +gem 'mumukit-nuntius', github: 'mumuki/mumukit-nuntius', branch: 'feature-async-massive-notifications' From 877e6dca1b9c973624d9064c068db824306db98a Mon Sep 17 00:00:00 2001 From: Federico Scarpa Date: Fri, 24 Sep 2021 17:15:20 -0300 Subject: [PATCH 06/18] Fix custom notification views --- app/views/notifications/_custom_notification.html.erb | 3 +-- .../notifications/previews/_custom_notification.html.erb | 3 +-- .../user_mailer/notifications/_custom_notification.html.erb | 6 +++--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/app/views/notifications/_custom_notification.html.erb b/app/views/notifications/_custom_notification.html.erb index e4d9bda38b..de09faa097 100644 --- a/app/views/notifications/_custom_notification.html.erb +++ b/app/views/notifications/_custom_notification.html.erb @@ -1,2 +1 @@ -<% target = notification.target %> -<%= target.custom_content_plain_text %> +<%= notification.target.body_html %> diff --git a/app/views/notifications/previews/_custom_notification.html.erb b/app/views/notifications/previews/_custom_notification.html.erb index 5c59e70923..74a121057a 100644 --- a/app/views/notifications/previews/_custom_notification.html.erb +++ b/app/views/notifications/previews/_custom_notification.html.erb @@ -1,2 +1 @@ -<% target = notification.target %> -<%= notification_text_preview_item :envelope, target.custom_title, notification.organization.url_for('/user/notifications') %> +<%= notification_text_preview_item :envelope, notification.target.title, notification.organization.url_for('/user/notifications') %> diff --git a/app/views/user_mailer/notifications/_custom_notification.html.erb b/app/views/user_mailer/notifications/_custom_notification.html.erb index 1389746b22..4a8de29149 100644 --- a/app/views/user_mailer/notifications/_custom_notification.html.erb +++ b/app/views/user_mailer/notifications/_custom_notification.html.erb @@ -1,11 +1,11 @@ <%= target = @notification.target %> -<% if target.custom_content_html.present? %> - <%= render inline: target.custom_content_html %> +<% if target.custom_html.present? %> + <%= render inline: target.custom_html %> <% else %> <%= render partial: 'user_mailer/mail_template', locals: { title: t("mailer.title.custom"), subtitle: '', - text: target.custom_content_plain_text, + text: target.body_html, button: t("mailer.button.custom"), url: @organization.url_for('/user/notifications') } %> From 0b5fd2725be9f037cac0dec11e8f4bfd910b0873 Mon Sep 17 00:00:00 2001 From: Federico Scarpa Date: Fri, 24 Sep 2021 17:15:44 -0300 Subject: [PATCH 07/18] Update UserMailer previews --- .../_custom_notification.html.erb | 2 +- .../_custom_notification.html.erb | 4 +- spec/mailers/previews/user_mailer_preview.rb | 49 ++++++++++++------- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/app/views/notifications/_custom_notification.html.erb b/app/views/notifications/_custom_notification.html.erb index de09faa097..3c17a3483d 100644 --- a/app/views/notifications/_custom_notification.html.erb +++ b/app/views/notifications/_custom_notification.html.erb @@ -1 +1 @@ -<%= notification.target.body_html %> +<%= notification.target.body_html.html_safe %> diff --git a/app/views/user_mailer/notifications/_custom_notification.html.erb b/app/views/user_mailer/notifications/_custom_notification.html.erb index 4a8de29149..906961ede7 100644 --- a/app/views/user_mailer/notifications/_custom_notification.html.erb +++ b/app/views/user_mailer/notifications/_custom_notification.html.erb @@ -1,11 +1,11 @@ -<%= target = @notification.target %> +<% target = @notification.target %> <% if target.custom_html.present? %> <%= render inline: target.custom_html %> <% else %> <%= render partial: 'user_mailer/mail_template', locals: { title: t("mailer.title.custom"), subtitle: '', - text: target.body_html, + text: target.body_html.html_safe, button: t("mailer.button.custom"), url: @organization.url_for('/user/notifications') } %> diff --git a/spec/mailers/previews/user_mailer_preview.rb b/spec/mailers/previews/user_mailer_preview.rb index 2d02f66803..950e026026 100644 --- a/spec/mailers/previews/user_mailer_preview.rb +++ b/spec/mailers/previews/user_mailer_preview.rb @@ -1,41 +1,47 @@ # Preview all emails at http://localhost:3000/rails/mailers/user_mailer class UserMailerPreview < ActionMailer::Preview - # Preview this email at http://localhost:3000/rails/mailers/user_mailer/we_miss_you_notification + # Preview this email at http://localhost:3000/rails/mailers/user_mailer/we_miss_you_reminder def we_miss_you_reminder UserMailer.we_miss_you_reminder user, 1 end - def custom_content_plain_text_notification - UserMailer.notification notification subject: :custom, - custom_content_plain_text: 'This is the text of the mail. Awesome!', - custom_title: 'This is the title!' - end - - def custom_content_html_notification - UserMailer.notification notification subject: :custom, - custom_content_html: 'This is the text of the mail. Awesome!', - custom_title: 'This is the title!' - end - + # Preview this email at http://localhost:3000/rails/mailers/user_mailer/certificate_preview def certificate_preview UserMailer.certificate certificate end + # Preview this email at http://localhost:3000/rails/mailers/user_mailer/exam_registration_preview def exam_registration_preview - notification = notification target: exam_registration, subject: :exam_registration + notification = notification target: exam_registration UserMailer.notification notification end + # Preview this email at http://localhost:3000/rails/mailers/user_mailer/exam_authorization_request_approved def exam_authorization_request_approved - notification = notification target: exam_authorization_request('approved'), subject: :exam_authorization_request_updated + notification = notification target: exam_authorization_request('approved') UserMailer.notification notification end + # Preview this email at http://localhost:3000/rails/mailers/user_mailer/exam_authorization_request_rejected def exam_authorization_request_rejected - notification = notification target: exam_authorization_request('rejected'), subject: :exam_authorization_request_updated + notification = notification target: exam_authorization_request('rejected') + + UserMailer.notification notification + end + + # Preview this email at http://localhost:3000/rails/mailers/user_mailer/custom_content_plain_text_notification + def custom_content_plain_text_notification + notification = notification target: custom_notification + + UserMailer.notification notification target: custom_notification + end + + # Preview this email at http://localhost:3000/rails/mailers/user_mailer/custom_content_html_notification + def custom_content_html_notification + notification = notification target: custom_notification('This is the text of the mail. Awesome!') UserMailer.notification notification end @@ -59,6 +65,14 @@ def exam_authorization_request(status) organization: organization end + def custom_notification(custom_html = '') + CustomNotification.new id: 1, + organization: organization, + title: 'This is the title!', + body_html: 'This is the text of the mail. Awesome!', + custom_html: custom_html + end + def exam Exam.new organization: organization, start_time: 5.minute.ago, @@ -89,7 +103,6 @@ def certificate end def notification(**hash) - Notification.new({user: user, - organization: organization}.merge hash) + Notification.new({user: user, organization: organization}.merge hash) end end From 53caaa39f3da4617ab819de3e272658274f97c90 Mon Sep 17 00:00:00 2001 From: Federico Scarpa Date: Mon, 27 Sep 2021 15:19:00 -0300 Subject: [PATCH 08/18] Add limit 10 to notifications dropdown --- app/controllers/application_controller.rb | 1 + app/views/notifications/_dropdown.html.erb | 15 +++++++++++---- .../laboratory/controllers/notifications.rb | 4 ++++ lib/mumuki/laboratory/locales/en.yml | 1 + lib/mumuki/laboratory/locales/es-CL.yml | 1 + lib/mumuki/laboratory/locales/es.yml | 1 + lib/mumuki/laboratory/locales/pt.yml | 1 + 7 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 227ff00c87..517234a24e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -37,6 +37,7 @@ class ApplicationController < ActionController::Base :login_button, :notifications_count, :has_notifications?, + :has_many_notifications?, :user_notifications, :subject, :should_choose_organization?, diff --git a/app/views/notifications/_dropdown.html.erb b/app/views/notifications/_dropdown.html.erb index 8a3f2fb4c3..9e6c2cd9d1 100644 --- a/app/views/notifications/_dropdown.html.erb +++ b/app/views/notifications/_dropdown.html.erb @@ -3,10 +3,17 @@ <%= notifications_count %> -