From 73dcac9a6749ab4580da0522dd6179176d19f9fc Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Thu, 3 Oct 2013 03:16:27 -0400 Subject: [PATCH] VRFS-595 added session invitation notification --- pb/src/client_container.proto | 5 +- ruby/lib/jam_ruby/message_factory.rb | 4 +- ruby/lib/jam_ruby/models/notification.rb | 12 ++- web/app/assets/javascripts/jam_rest.js | 11 +++ web/app/assets/javascripts/layout.js | 2 +- web/app/assets/javascripts/session.js | 3 +- web/app/assets/javascripts/sidebar.js | 95 ++++++++++++++++--- .../controllers/api_invitations_controller.rb | 2 +- 8 files changed, 110 insertions(+), 24 deletions(-) diff --git a/pb/src/client_container.proto b/pb/src/client_container.proto index 31c0fcad4..aa9b7607c 100644 --- a/pb/src/client_container.proto +++ b/pb/src/client_container.proto @@ -278,7 +278,10 @@ message FriendUpdate { // route_to: user:[USER_ID] // let a user know they've been invited to a session message SessionInvitation { - optional string invitation = 1; + optional string sender_id = 1; + optional string session_id = 2; + optional string notification_id = 3; + optional string created_at = 4; } // route_to: client diff --git a/ruby/lib/jam_ruby/message_factory.rb b/ruby/lib/jam_ruby/message_factory.rb index 05c244c9b..f9d794446 100644 --- a/ruby/lib/jam_ruby/message_factory.rb +++ b/ruby/lib/jam_ruby/message_factory.rb @@ -137,8 +137,8 @@ return Jampb::ClientMessage.new(:type => ClientMessage::Type::TEST_SESSION_MESSAGE, :route_to => SESSION_TARGET_PREFIX + session_id, :test_session_message => test) end - def session_invitation(receiver_id, invitation_id) - session_invitation = Jampb::SessionInvitation.new(:invitation => invitation_id) + def session_invitation(receiver_id, sender_id, session_id, notification_id, created_at) + session_invitation = Jampb::SessionInvitation.new(:sender_id => sender_id, :session_id => session_id, :notification_id => notification_id, :created_at => created_at) return Jampb::ClientMessage.new(:type => ClientMessage::Type::SESSION_INVITATION, :route_to => USER_TARGET_PREFIX + receiver_id, :session_invitation => session_invitation) end diff --git a/ruby/lib/jam_ruby/models/notification.rb b/ruby/lib/jam_ruby/models/notification.rb index 0afc93ad5..9ecd6eca6 100644 --- a/ruby/lib/jam_ruby/models/notification.rb +++ b/ruby/lib/jam_ruby/models/notification.rb @@ -128,10 +128,12 @@ module JamRuby when NotificationTypes::MUSICIAN_SESSION_DEPART return "#{user.name} has left the session." + when NotificationTypes::SESSION_INVITATION + return "#{user.name} has invited you to a session." + # when "social_media_friend_joined" # when "join_request_approved" # when "join_request_rejected" - # when "session_invitation" # when "band_invitation" # when "band_invitation_accepted" # when "recording_available" @@ -199,17 +201,19 @@ module JamRuby end ################## SESSION INVITATION ################## - def send_session_invitation(receiver_id, invitation_id) + def send_session_invitation(receiver_id, sender_id, session_id) # (1) save to database notification = Notification.new notification.description = NotificationTypes::SESSION_INVITATION + notification.source_user_id = sender_id notification.target_user_id = receiver_id + notification.session_id = session_id notification.save # (2) create notification - msg = @@message_factory.session_invitation(receiver_id, invitation_id) - + msg = @@message_factory.session_invitation(receiver_id, sender_id, session_id, notification.id, notification.created_at.to_s) + # (3) send notification @@mq_router.publish_to_user(receiver_id, msg) end diff --git a/web/app/assets/javascripts/jam_rest.js b/web/app/assets/javascripts/jam_rest.js index 37f37ec4f..1a55aa9ad 100644 --- a/web/app/assets/javascripts/jam_rest.js +++ b/web/app/assets/javascripts/jam_rest.js @@ -22,6 +22,16 @@ }); } + function getSession(id) { + var url = "/api/sessions/" + id; + return $.ajax({ + type: "GET", + dataType: "json", + url: url, + async: false, + processData: false + }); + } function getUserDetail(options) { var id = getId(options); @@ -277,6 +287,7 @@ this.getFilepickerPolicy = getFilepickerPolicy; this.getFriends = getFriends; this.updateSession = updateSession; + this.getSession = getSession; this.getClientDownloads = getClientDownloads this.createInvitation = createInvitation; this.postFeedback = postFeedback; diff --git a/web/app/assets/javascripts/layout.js b/web/app/assets/javascripts/layout.js index e551d1d0f..a06338f7d 100644 --- a/web/app/assets/javascripts/layout.js +++ b/web/app/assets/javascripts/layout.js @@ -571,7 +571,7 @@ function setNotificationInfo(message, descriptor) { var $notify = $('[layout="notify"]'); $('h2', $notify).text(message.title); - $('p', $notify).text(message.text); + $('p', $notify).html(message.text); if (message.icon_url) { $('#avatar', $notify).attr('src', message.icon_url); diff --git a/web/app/assets/javascripts/session.js b/web/app/assets/javascripts/session.js index b197a6342..8e7d6078a 100644 --- a/web/app/assets/javascripts/session.js +++ b/web/app/assets/javascripts/session.js @@ -157,7 +157,8 @@ text: "The session you attempted to join is over." }, { no_cancel: true }); - }else { + } + else { app.ajaxError(xhr, textStatus, errorMessage); } }); diff --git a/web/app/assets/javascripts/sidebar.js b/web/app/assets/javascripts/sidebar.js index 8e2628771..758235423 100644 --- a/web/app/assets/javascripts/sidebar.js +++ b/web/app/assets/javascripts/sidebar.js @@ -140,23 +140,31 @@ $('#sidebar-notification-list').append(notificationHtml); + // val.description contains the notification record's description value from the DB (i.e., type) initializeActions(val, val.description); }); } - function initializeActions(notification, type) { + function initializeActions(payload, type) { // wire up "x" button to delete notification - $('li[notification-id=' + notification.notification_id + ']').find('#img-delete-notification').click(deleteNotificationHandler); + $('li[notification-id=' + payload.notification_id + ']').find('#img-delete-notification').click(deleteNotificationHandler); if (type === context.JK.MessageType.FRIEND_REQUEST) { - var $action_btn = $('li[notification-id=' + notification.notification_id + ']').find('#btn-notification-action'); + var $action_btn = $('li[notification-id=' + payload.notification_id + ']').find('#btn-notification-action'); $action_btn.text('ACCEPT'); $action_btn.click(function() { - acceptFriendRequest({ "friend_request_id": notification.friend_request_id, "notification_id": notification.notification_id }); + acceptFriendRequest({ "friend_request_id": payload.friend_request_id, "notification_id": payload.notification_id }); }); } else if (type === context.JK.MessageType.FRIEND_REQUEST_ACCEPTED) { - $('li[notification-id=' + notification.notification_id + ']').find('#div-actions').hide(); + $('li[notification-id=' + payload.notification_id + ']').find('#div-actions').hide(); + } + else if (type === context.JK.MessageType.SESSION_INVITATION) { + var $action_btn = $('li[notification-id=' + payload.notification_id + ']').find('#btn-notification-action'); + $action_btn.text('JOIN'); + $action_btn.click(function() { + joinSession({ "session_id": payload.session_d, "notification_id": payload.notification_id }); + }); } } @@ -300,7 +308,12 @@ function decrementNotificationCount() { var count = parseInt($('#sidebar-notification-count').html()); - $('#sidebar-notification-count').html(count - 1); + if (count === 0) { + $('#sidebar-notification-count').html(0); + } + else { + $('#sidebar-notification-count').html(count - 1); + } } function acceptFriendRequest(args) { @@ -314,15 +327,35 @@ }).error(app.ajaxError); } + function joinSession(args) { + logger.debug("Joining session " + args.session_id); + context.location = "#/session/" + args.session_id; + } + // default handler for incoming notification function handleNotification(payload, type) { - // update notifications panel in sidebar - notifications[payload.notification_id] = { - "id": payload.notification_id, - "photo_url": payload.photo_url, - "formatted_msg": payload.msg, - "created_at": context.JK.formatDate(payload.created_at) - }; + var sidebarText; + if (type === context.JK.MessageType.SESSION_INVITATION) { + sidebarText = "You have been invited to a session."; + // update notifications panel in sidebar + notifications[payload.notification_id] = { + "id": payload.notification_id, + "session_id": payload.session_id, + "formatted_msg": sidebarText, + "created_at": context.JK.formatDate(payload.created_at) + }; + } + else { + sidebarText = payload.msg; + + // update notifications panel in sidebar + notifications[payload.notification_id] = { + "id": payload.notification_id, + "photo_url": payload.photo_url, + "formatted_msg": sidebarText, + "created_at": context.JK.formatDate(payload.created_at) + }; + } incrementNotificationCount(); @@ -330,7 +363,7 @@ var notificationHtml = context.JK.fillTemplate(template, { notificationId: payload.notification_id, avatar_url: context.JK.resolveAvatarUrl(payload.photo_url), - text: payload.msg, + text: sidebarText, date: context.JK.formatDate(payload.created_at) }); @@ -488,6 +521,40 @@ "icon_url": context.JK.resolveAvatarUrl(payload.photo_url) }); }); + ////////////////////////////////////////////////////////////// + + // wire up SESSION_INVITATION handler + context.JK.JamServer.registerMessageCallback(context.JK.MessageType.SESSION_INVITATION, function(header, payload) { + logger.debug("Handling SESSION_INVITATION msg " + JSON.stringify(payload)); + + handleNotification(payload, header.type); + + var participants = []; + rest.getSession(payload.session_id).done(function(response) { + $.each(response.participants, function(index, val) { + logger.debug(val.user.photo_url + "," + val.user.name); + participants.push({"photo_url": val.user.photo_url, "name": val.user.name}); + }); + }).error(app.ajaxError); + + var participantHtml = "You have been invited to join a session with:

"; + + $.each(participants, function(index, val) { + participantHtml += " " + val.name + "
"; + }); + + // display notification + app.notify({ + "title": "Session Invitation", + "text": participantHtml + }, + { + "ok_text": "JOIN SESSION", + "ok_callback": joinSession, + "ok_callback_args": { "session_id": payload.session_id, "notification_id": payload.notification_id } + } + ); + }); ////////////////////////////////////////////////////////////// // watch for Invite More Users events diff --git a/web/app/controllers/api_invitations_controller.rb b/web/app/controllers/api_invitations_controller.rb index 1e5a8c858..b9f030a67 100644 --- a/web/app/controllers/api_invitations_controller.rb +++ b/web/app/controllers/api_invitations_controller.rb @@ -46,7 +46,7 @@ class ApiInvitationsController < ApiController User.save_session_settings(current_user, music_session) # send notification - # Notification.send_session_invitation(receiver.id, @invitation.id) + Notification.send_session_invitation(receiver.id, current_user.id, music_session.id) respond_with @invitation, :responder => ApiResponder, :location => api_invitation_detail_url(@invitation) else