From e94f3d7a106b28f2506be1a4a43040872f0a5c8e Mon Sep 17 00:00:00 2001 From: Leonardo Crauss Daronco Date: Thu, 8 Dec 2016 18:33:19 -0200 Subject: [PATCH] Send an email to the user when a recording is done processing Had to add an email attribute to users to store their email to send the notification. Will only send it for user rooms since they are the only ones we know who to notify. --- app/controllers/bbb_controller.rb | 6 +++- app/jobs/recording_ready_email_job.rb | 25 +++++++++++++++ app/lib/bbb_api.rb | 9 +++--- app/mailers/notification_mailer.rb | 25 +++++++++++++++ app/models/user.rb | 9 ++++++ .../recording_ready_email.html.erb | 11 +++++++ config/application.rb | 31 +++++++++++++++++++ config/locales/en-us.yml | 6 ++++ .../20161208185458_add_email_to_user.rb | 22 +++++++++++++ db/schema.rb | 4 ++- sample.env | 14 +++++++++ .../previews/notification_mailer_preview.rb | 6 ++++ 12 files changed, 161 insertions(+), 7 deletions(-) create mode 100644 app/jobs/recording_ready_email_job.rb create mode 100644 app/mailers/notification_mailer.rb create mode 100644 app/views/notification_mailer/recording_ready_email.html.erb create mode 100644 db/migrate/20161208185458_add_email_to_user.rb create mode 100644 test/mailers/previews/notification_mailer_preview.rb diff --git a/app/controllers/bbb_controller.rb b/app/controllers/bbb_controller.rb index b837ced2..c70ada69 100644 --- a/app/controllers/bbb_controller.rb +++ b/app/controllers/bbb_controller.rb @@ -170,9 +170,13 @@ class BbbController < ApplicationController rec_info = rec_info[:recordings].first RecordingCreatedJob.perform_later(token, parse_recording_for_view(rec_info)) + # send an email to the owner of this recording, if defined + owner = User.find_by(encrypted_id: token) + RecordingReadyEmailJob.perform_later(owner) if owner.present? + # TODO: remove the webhook now that the meeting and recording are done # remove only if the meeting is not running, otherwise the hook is needed - # if ENV['GREENLIGHT_USE_WEBHOOKS'] + # if Rails.configuration.use_webhooks # webhook_remove("#{base_url}/callback") # end else diff --git a/app/jobs/recording_ready_email_job.rb b/app/jobs/recording_ready_email_job.rb new file mode 100644 index 00000000..465f0f38 --- /dev/null +++ b/app/jobs/recording_ready_email_job.rb @@ -0,0 +1,25 @@ +# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/. +# +# Copyright (c) 2016 BigBlueButton Inc. and by respective authors (see below). +# +# This program is free software; you can redistribute it and/or modify it under the +# terms of the GNU Lesser General Public License as published by the Free Software +# Foundation; either version 3.0 of the License, or (at your option) any later +# version. +# +# BigBlueButton 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 Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with BigBlueButton; if not, see . + +class RecordingReadyEmailJob < ApplicationJob + queue_as :default + + def perform(user) + if user.email.present? + NotificationMailer.recording_ready_email(user).deliver + end + end +end diff --git a/app/lib/bbb_api.rb b/app/lib/bbb_api.rb index 2f0d923b..35f3c450 100644 --- a/app/lib/bbb_api.rb +++ b/app/lib/bbb_api.rb @@ -77,13 +77,12 @@ module BbbApi "meta_#{BbbApi::META_LISTED}": false, "meta_#{BbbApi::META_TOKEN}": meeting_token } + meeting_options.merge!( + { "meta_#{BbbApi::META_HOOK_URL}": options[:hook_url] } + ) if options[:hook_url] - if ENV['GREENLIGHT_USE_WEBHOOKS'] + if Rails.configuration.use_webhooks webhook_register(options[:hook_url], meeting_id) - else - meeting_options.merge!( - { "meta_#{BbbApi::META_HOOK_URL}": options[:hook_url] } - ) if options[:hook_url] end # Create the meeting diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb new file mode 100644 index 00000000..9c73a0bd --- /dev/null +++ b/app/mailers/notification_mailer.rb @@ -0,0 +1,25 @@ +# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/. +# +# Copyright (c) 2016 BigBlueButton Inc. and by respective authors (see below). +# +# This program is free software; you can redistribute it and/or modify it under the +# terms of the GNU Lesser General Public License as published by the Free Software +# Foundation; either version 3.0 of the License, or (at your option) any later +# version. +# +# BigBlueButton 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 Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with BigBlueButton; if not, see . + +class NotificationMailer < ActionMailer::Base + default from: Rails.configuration.smtp_from + + def recording_ready_email(user) + @user = user + @room_url = resource_url(resource: "rooms", id: user.encrypted_id) + mail(to: user.email, subject: t('.subject')) + end +end diff --git a/app/models/user.rb b/app/models/user.rb index c1a77b10..01a16ce4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -21,6 +21,7 @@ class User < ApplicationRecord def self.from_omniauth(auth_hash) user = find_or_initialize_by(uid: auth_hash['uid'], provider: auth_hash['provider']) user.username = self.send("#{auth_hash['provider']}_username", auth_hash) rescue nil + user.email = self.send("#{auth_hash['provider']}_email", auth_hash) rescue nil user.name = auth_hash['info']['name'] user.save! user @@ -30,10 +31,18 @@ class User < ApplicationRecord auth_hash['info']['nickname'] end + def self.twitter_email(auth_hash) + auth_hash['info']['email'] + end + def self.google_username(auth_hash) auth_hash['info']['email'].split('@').first end + def self.google_email(auth_hash) + auth_hash['info']['email'] + end + def room_url "/rooms/#{encrypted_id}" end diff --git a/app/views/notification_mailer/recording_ready_email.html.erb b/app/views/notification_mailer/recording_ready_email.html.erb new file mode 100644 index 00000000..76712bc8 --- /dev/null +++ b/app/views/notification_mailer/recording_ready_email.html.erb @@ -0,0 +1,11 @@ + + + + + + +

<%= t('.hi', name: @user.name) %>

+

<%= t('.phrase1') %>

+

<%= t('.phrase2', url: @room_url) %>

+ + diff --git a/config/application.rb b/config/application.rb index 4a9e08b6..ae422bc7 100644 --- a/config/application.rb +++ b/config/application.rb @@ -37,5 +37,36 @@ module Greenlight # BigBlueButton config.bigbluebutton_endpoint = ENV['BIGBLUEBUTTON_ENDPOINT'] config.bigbluebutton_secret = ENV['BIGBLUEBUTTON_SECRET'] + + # GreenLight + config.use_webhooks = ENV['GREENLIGHT_USE_WEBHOOKS'] + config.smtp_from = ENV['SMTP_FROM'] + config.smtp_server = ENV['SMTP_SERVER'] + config.smtp_domain = ENV['SMTP_DOMAIN'] + config.smtp_port = ENV['SMTP_PORT'] || 587 + config.smtp_username = ENV['SMTP_USERNAME'] + config.smtp_password = ENV['SMTP_PASSWORD'] + config.smtp_auth = ENV['SMTP_AUTH'] || "login" + config.smtp_starttls_auto = ENV['SMTP_STARTTLS_AUTO'].nil? ? true : ENV['SMTP_STARTTLS_AUTO'] + config.smtp_tls = ENV['SMTP_TLS'].nil? ? false : ENV['SMTP_TLS'] + + # SMTP + config.action_mailer.default_url_options = { host: ENV['GREENLIGHT_DOMAIN'] } + config.action_mailer.delivery_method = :smtp + config.action_mailer.perform_deliveries = true + config.action_mailer.raise_delivery_errors = true + config.action_mailer.smtp_settings = { + :address => config.smtp_server, + :domain => config.smtp_domain, + :port => config.smtp_port, + :user_name => config.smtp_username, + :password => config.smtp_password, + :authentication => config.smtp_auth, + :enable_starttls_auto => config.smtp_starttls_auto, + :tls => config.smtp_tls + } + config.action_mailer.default_options = { + from: config.smtp_from + } end end diff --git a/config/locales/en-us.yml b/config/locales/en-us.yml index 86365cbe..4a781a8c 100644 --- a/config/locales/en-us.yml +++ b/config/locales/en-us.yml @@ -82,6 +82,12 @@ en-US: subject: "%{user} invited you to a meeting" my_room: my room no: No + notification_mailer: + recording_ready_email: + hi: "Hi, %{name}" + phrase1: "One of your recordings has just been made available." + phrase2: "Access the following website to view it and publish to other users: %{url}" + subject: "Your recording is ready!" oauth_signup: Signup for customized sessions past_recordings: Past Recordings footer_html: "%{greenlight_link} build %{version}, Powered by %{bbb_link}" diff --git a/db/migrate/20161208185458_add_email_to_user.rb b/db/migrate/20161208185458_add_email_to_user.rb new file mode 100644 index 00000000..88e1da36 --- /dev/null +++ b/db/migrate/20161208185458_add_email_to_user.rb @@ -0,0 +1,22 @@ +# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/. +# +# Copyright (c) 2016 BigBlueButton Inc. and by respective authors (see below). +# +# This program is free software; you can redistribute it and/or modify it under the +# terms of the GNU Lesser General Public License as published by the Free Software +# Foundation; either version 3.0 of the License, or (at your option) any later +# version. +# +# BigBlueButton 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 Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with BigBlueButton; if not, see . + +class AddEmailToUser < ActiveRecord::Migration[5.0] + def change + add_column :users, :email, :string + add_index :users, :email, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index c82f317c..a3ce761f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20161108224701) do +ActiveRecord::Schema.define(version: 20161208185458) do create_table "users", force: :cascade do |t| t.string "provider", null: false @@ -20,6 +20,8 @@ ActiveRecord::Schema.define(version: 20161108224701) do t.datetime "updated_at", null: false t.string "username" t.string "encrypted_id", null: false + t.string "email" + t.index ["email"], name: "index_users_on_email", unique: true t.index ["encrypted_id"], name: "index_users_on_encrypted_id", unique: true t.index ["provider", "uid"], name: "index_users_on_provider_and_uid", unique: true t.index ["provider"], name: "index_users_on_provider" diff --git a/sample.env b/sample.env index 1485f639..54626cb4 100644 --- a/sample.env +++ b/sample.env @@ -17,6 +17,20 @@ BIGBLUEBUTTON_SECRET=8cd8ef52e8e101574e400365b55e11a6 # the application when recordings are done). GREENLIGHT_USE_WEBHOOKS=false +# The web site domain, needed for emails mostly +GREENLIGHT_DOMAIN=localhost + +# SMTP configurations +SMTP_FROM=email@gmail.com +SMTP_SERVER=smtp.gmail.com +SMTP_DOMAIN=gmail.com +SMTP_PORT=587 +SMTP_USERNAME=email@gmail.com +SMTP_PASSWORD=my-secret-password +# SMTP_TLS=false +# SMTP_AUTH=login +# SMTP_STARTTLS_AUTO=true + # OmniAuth TWITTER_ID= TWITTER_SECRET= diff --git a/test/mailers/previews/notification_mailer_preview.rb b/test/mailers/previews/notification_mailer_preview.rb new file mode 100644 index 00000000..135f99c0 --- /dev/null +++ b/test/mailers/previews/notification_mailer_preview.rb @@ -0,0 +1,6 @@ +# Preview all emails at http://localhost:3000/rails/mailers/example_mailer +class NotificationMailerPreview < ActionMailer::Preview + def recording_ready_email_preview + NotificationMailer.recording_ready_email(User.first) + end +end