From 102306e4d1ec28350b613467c0f2c0676ea7c8a7 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Sun, 29 Dec 2013 20:49:44 -0500 Subject: [PATCH] VRFS-933 notification work --- ruby/lib/jam_ruby/app/mailers/user_mailer.rb | 44 ++++-- .../user_mailer/friend_request.html.erb | 3 + .../user_mailer/friend_request.text.erb | 1 + .../friend_request_acceptance.html.erb | 3 + .../friend_request_acceptance.text.erb | 1 + .../user_mailer/new_band_follower.html.erb | 3 + .../user_mailer/new_band_follower.text.erb | 1 + ruby/lib/jam_ruby/message_factory.rb | 2 +- ruby/lib/jam_ruby/models/notification.rb | 137 +++++++++++------- web/app/assets/javascripts/profile.js | 2 +- web/app/views/api_users/show.rabl | 4 +- 11 files changed, 129 insertions(+), 72 deletions(-) diff --git a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb index b59c74f86..14e7c72c2 100644 --- a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb +++ b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb @@ -92,24 +92,42 @@ def friend_request(source_user, target_user) - end + @user = source_user + sendgrid_category "Notification" + sendgrid_unique_args :type => "friend_request" - def friend_request_accepted(source_user, target_user) - end - - def new_user_follower(source_user, target_user) - @user = target_user - sendgrid_unique_args :type => "new_user_follower" - mail(:to => @user.email, :subject => "You have a new follower on JamKazam") do |format| + mail(:to => target_user.email, :subject => "You have a new JamKazam friend request") do |format| format.text format.html end end - def new_band_follower(source_user, target_users, band) - sendgrid_unique_args :type => "new_band_follower" + def friend_request_accepted(source_user, target_user) + @user = source_user + sendgrid_category "Notification" + sendgrid_unique_args :type => "friend_request_accepted" - mail(:to => extract_emails(target_users), :subject => "Your band has a new follower on JamKazam") do |format| + mail(:to => target_user.email, :subject => "#{@user.name} accepted your JamKazam friend request") do |format| + format.text + format.html + end + end + + def new_user_follower(source_user, target_user) + @user = source_user + sendgrid_category "Notification" + sendgrid_unique_args :type => "new_user_follower" + mail(:to => target_user.email, :subject => "You have a new follower on JamKazam") do |format| + format.text + format.html + end + end + + def new_band_follower(source_user, target_user, band) + @user, @band = source_user, band + sendgrid_category "Notification" + sendgrid_unique_args :type => "new_band_follower" + mail(:to => target_user, :subject => "Your band has a new follower on JamKazam") do |format| format.text format.html end @@ -123,9 +141,5 @@ @user = user end - def extract_emails(users) - users.map! { |u| u.email } - end - end end diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/friend_request.html.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/friend_request.html.erb index e69de29bb..3bfedccf4 100644 --- a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/friend_request.html.erb +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/friend_request.html.erb @@ -0,0 +1,3 @@ +<% provide(:title, 'New JamKazam Friend Request') %> + +

<% @user.name %> has sent you a friend request on JamKazam.

\ No newline at end of file diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/friend_request.text.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/friend_request.text.erb index e69de29bb..ac39cad90 100644 --- a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/friend_request.text.erb +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/friend_request.text.erb @@ -0,0 +1 @@ +<% @user.name %> has sent you a friend request on JamKazam. \ No newline at end of file diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/friend_request_acceptance.html.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/friend_request_acceptance.html.erb index e69de29bb..f7edf076b 100644 --- a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/friend_request_acceptance.html.erb +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/friend_request_acceptance.html.erb @@ -0,0 +1,3 @@ +<% provide(:title, 'Friend Request Accepted') %> + +

<% @user.name %> has accepted your friend request on JamKazam.

\ No newline at end of file diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/friend_request_acceptance.text.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/friend_request_acceptance.text.erb index e69de29bb..e2773a017 100644 --- a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/friend_request_acceptance.text.erb +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/friend_request_acceptance.text.erb @@ -0,0 +1 @@ +<% @user.name %> has accepted your friend request on JamKazam. \ No newline at end of file diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/new_band_follower.html.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/new_band_follower.html.erb index e69de29bb..74b17512b 100644 --- a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/new_band_follower.html.erb +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/new_band_follower.html.erb @@ -0,0 +1,3 @@ +<% provide(:title, 'New Band Follower on JamKazam') %> + +

<% @user.name %> is now following your band <%= @band.name %> on JamKazam.

\ No newline at end of file diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/new_band_follower.text.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/new_band_follower.text.erb index e69de29bb..c5a2aae2b 100644 --- a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/new_band_follower.text.erb +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/new_band_follower.text.erb @@ -0,0 +1 @@ +<% @user.name %> is now following your band <%= @band.name %> on JamKazam. \ No newline at end of file diff --git a/ruby/lib/jam_ruby/message_factory.rb b/ruby/lib/jam_ruby/message_factory.rb index 425f182cf..c1f41a916 100644 --- a/ruby/lib/jam_ruby/message_factory.rb +++ b/ruby/lib/jam_ruby/message_factory.rb @@ -296,7 +296,7 @@ return Jampb::ClientMessage.new( :type => ClientMessage::Type::NEW_BAND_FOLLOWER, :route_to => USER_TARGET_PREFIX + receiver_id, - :new_user_follower => new_band_follower + :new_band_follower => new_band_follower ) end diff --git a/ruby/lib/jam_ruby/models/notification.rb b/ruby/lib/jam_ruby/models/notification.rb index 1a25eb7a8..2d4fd02c1 100644 --- a/ruby/lib/jam_ruby/models/notification.rb +++ b/ruby/lib/jam_ruby/models/notification.rb @@ -229,6 +229,7 @@ module JamRuby ################### FRIEND REQUEST ################### def send_friend_request(friend_request_id, user_id, friend_id) user = User.find(user_id) + friend = User.find(friend_id) # (1) save to database notification = Notification.new @@ -239,25 +240,31 @@ module JamRuby notification.save # (2) create notification - notification_msg = format_msg(notification.description, user) - msg = @@message_factory.friend_request( - friend_request_id, - user_id, - user.name, - user.photo_url, - friend_id, - notification_msg, - notification.id, - notification.created_at.to_s - ) + if user.online + notification_msg = format_msg(notification.description, user) + msg = @@message_factory.friend_request( + friend_request_id, + user_id, + user.name, + user.photo_url, + friend_id, + notification_msg, + notification.id, + notification.created_at.to_s + ) - # (3) send notification - @@mq_router.publish_to_user(friend_id, msg) + # (2a) send notification if user is online + @@mq_router.publish_to_user(friend_id, msg) + else + # (2b) send email if user is offline + UserMailer.friend_request(user, friend) + end end ############### FRIEND REQUEST ACCEPTED ############### def send_friend_request_accepted(user_id, friend_id) friend = User.find(friend_id) + user = User.find(user_id) # (1) save to database notification = Notification.new @@ -267,18 +274,23 @@ module JamRuby notification.save # (2) create notification - notification_msg = format_msg(notification.description, friend) - msg = @@message_factory.friend_request_accepted( - friend_id, - friend.name, - friend.photo_url, - user_id, notification_msg, - notification.id, - notification.created_at.to_s - ) + if friend.online + notification_msg = format_msg(notification.description, friend) + msg = @@message_factory.friend_request_accepted( + friend_id, + friend.name, + friend.photo_url, + user_id, notification_msg, + notification.id, + notification.created_at.to_s + ) - # (3) send notification - @@mq_router.publish_to_user(user_id, msg) + # (3) send notification + @@mq_router.publish_to_user(user_id, msg) + else + # (2b) send email if user is offline + UserMailer.friend_request(friend, user) + end end ################## FRIEND SESSION JOIN ################## @@ -321,41 +333,60 @@ module JamRuby notification.save # (2) create notification - notification_msg = format_msg(notification.description, follower) - msg = @@message_factory.new_user_follower( - user.id, - follower.name, - follower.photo_url, - notification_msg, - notification.id, - notification.created_at.to_s - ) + if user.online + notification_msg = format_msg(notification.description, follower) - # (3) send notification - @@mq_router.publish_to_user(user.id, msg) + msg = @@message_factory.new_user_follower( + user.id, + follower.name, + follower.photo_url, + notification_msg, + notification.id, + notification.created_at.to_s + ) + + # (2a) send notification if user is online + @@mq_router.publish_to_user(user.id, msg) + else + # (2b) send email if user is offline + UserMailer.new_user_follower(follower, user) + end end def send_new_band_follower(follower, band) - # (1) save to database - notification = Notification.new - notification.description = NotificationTypes::NEW_BAND_FOLLOWER - notification.source_user_id = follower.id - notification.target_user_id = user.id - notification.save - # (2) create notification + notifications = [] notification_msg = format_msg(notification.description, follower, band) - msg = @@message_factory.new_user_follower( - user.id, - follower.name, - follower.photo_url, - notification_msg, - notification.id, - notification.created_at.to_s - ) - # (3) send notification - @@mq_router.publish_to_user(user.id, msg) + band.band_musicians.each.each do |bm| + + # create notifications (saved below in bulk) + notification = Notification.new + notification.description = NotificationTypes::NEW_BAND_FOLLOWER + notification.source_user_id = follower.id + notification.target_user_id = bm.user.id + notifications << notification + + # send notification + if bm.user.online + msg = @@message_factory.new_user_follower( + bm.user_id, + follower.name, + follower.photo_url, + notification_msg, + notification.id, + notification.created_at.to_s + ) + + # (2a) send notification if user is online + @@mq_router.publish_to_user(user.id, msg) + else + # (2b) send email if user is offline + UserMailer.new_band_follower(follower, user, band) + end + end + + Notification.import notifications end ################## SESSION INVITATION ################## @@ -371,7 +402,7 @@ module JamRuby # (2) create notification msg = @@message_factory.session_invitation( - receiver_id, + receiver.id, sender.name, session_id, notification.id, diff --git a/web/app/assets/javascripts/profile.js b/web/app/assets/javascripts/profile.js index b6e1cb4d6..dda53a91c 100644 --- a/web/app/assets/javascripts/profile.js +++ b/web/app/assets/javascripts/profile.js @@ -240,7 +240,7 @@ } function isFollowing() { - return getUser() ? user.is_following : false; + return getUser() ? user.is_following : false; } function configureFollowingButton(following) { diff --git a/web/app/views/api_users/show.rabl b/web/app/views/api_users/show.rabl index 13360ac19..fb3e14d28 100644 --- a/web/app/views/api_users/show.rabl +++ b/web/app/views/api_users/show.rabl @@ -13,10 +13,10 @@ if @user == current_user attributes :email, :original_fpfile, :cropped_fpfile, :crop_selection, :session_settings, :show_whats_next, :subscribe_email elsif current_user node :is_friend do |uu| - @user.friends?(current_user) + current_user.friends?(@user) end node :is_following do |uu| - @user.following?(current_user) + current_user.following?(@user) end end