From 70cba2d98d1eab1de9ee132fba6516131d098d5e Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Sat, 5 Oct 2013 09:04:07 -0400 Subject: [PATCH] VRFS-595 session invitations / fix bug with session join notifications arriving twice --- pb/src/client_container.proto | 4 +- .../jam_ruby/constants/notification_types.rb | 1 + ruby/lib/jam_ruby/message_factory.rb | 4 +- ruby/lib/jam_ruby/models/notification.rb | 7 ++-- web/app/assets/javascripts/session.js | 1 - web/app/assets/javascripts/sidebar.js | 42 ++++++------------- .../controllers/api_invitations_controller.rb | 2 +- web/lib/music_session_manager.rb | 9 +--- 8 files changed, 26 insertions(+), 44 deletions(-) diff --git a/pb/src/client_container.proto b/pb/src/client_container.proto index aa9b7607c..f3ca4a907 100644 --- a/pb/src/client_container.proto +++ b/pb/src/client_container.proto @@ -28,6 +28,7 @@ message ClientMessage { MUSICIAN_SESSION_FRESH = 115; MUSICIAN_SESSION_STALE = 116; HEARTBEAT_ACK = 117; + SESSION_ENDED = 118; TEST_SESSION_MESSAGE = 200; @@ -72,6 +73,7 @@ message ClientMessage { optional MusicianSessionFresh musician_session_fresh = 115; optional MusicianSessionStale musician_session_stale = 116; optional HeartbeatAck heartbeat_ack = 117; + optional SessionEnded session_ended = 118; // Client-Session messages (to/from) optional TestSessionMessage test_session_message = 200; @@ -278,7 +280,7 @@ message FriendUpdate { // route_to: user:[USER_ID] // let a user know they've been invited to a session message SessionInvitation { - optional string sender_id = 1; + optional string sender_name = 1; optional string session_id = 2; optional string notification_id = 3; optional string created_at = 4; diff --git a/ruby/lib/jam_ruby/constants/notification_types.rb b/ruby/lib/jam_ruby/constants/notification_types.rb index f9faaef96..6d0f98eeb 100644 --- a/ruby/lib/jam_ruby/constants/notification_types.rb +++ b/ruby/lib/jam_ruby/constants/notification_types.rb @@ -8,6 +8,7 @@ module NotificationTypes # session notifications SESSION_INVITATION = "SESSION_INVITATION" + SESSION_ENDED = "SESSION_ENDED" # used to remove session-related notification from sidebar # musician notifications MUSICIAN_SESSION_JOIN = "MUSICIAN_SESSION_JOIN" diff --git a/ruby/lib/jam_ruby/message_factory.rb b/ruby/lib/jam_ruby/message_factory.rb index f9d794446..c9bb4e55c 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, 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) + def session_invitation(receiver_id, sender_name, session_id, notification_id, created_at) + session_invitation = Jampb::SessionInvitation.new(:sender_name => sender_name, :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 9ecd6eca6..c0f54bf97 100644 --- a/ruby/lib/jam_ruby/models/notification.rb +++ b/ruby/lib/jam_ruby/models/notification.rb @@ -201,18 +201,18 @@ module JamRuby end ################## SESSION INVITATION ################## - def send_session_invitation(receiver_id, sender_id, session_id) + def send_session_invitation(receiver_id, sender, session_id) # (1) save to database notification = Notification.new notification.description = NotificationTypes::SESSION_INVITATION - notification.source_user_id = sender_id + 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, sender_id, session_id, notification.id, notification.created_at.to_s) + msg = @@message_factory.session_invitation(receiver_id, sender.name, session_id, notification.id, notification.created_at.to_s) # (3) send notification @@mq_router.publish_to_user(receiver_id, msg) @@ -224,6 +224,7 @@ module JamRuby msg = @@message_factory.musician_session_join(music_session.id, user.id, user.name, user.photo_url) # (2) send notification + puts "CONNECTION.CLIENT_ID=#{connection.client_id}" @@mq_router.server_publish_to_session(music_session, msg, sender = {:client_id => connection.client_id}) end diff --git a/web/app/assets/javascripts/session.js b/web/app/assets/javascripts/session.js index 8e7d6078a..79c505d12 100644 --- a/web/app/assets/javascripts/session.js +++ b/web/app/assets/javascripts/session.js @@ -146,7 +146,6 @@ ); sessionModel.subscribe('sessionScreen', sessionChanged); - logger.debug("sessionId=" + sessionId); sessionModel.joinSession(sessionId) .fail(function(xhr, textStatus, errorMessage) { if(xhr.status == 404) { diff --git a/web/app/assets/javascripts/sidebar.js b/web/app/assets/javascripts/sidebar.js index 589b43dae..fff299f1a 100644 --- a/web/app/assets/javascripts/sidebar.js +++ b/web/app/assets/javascripts/sidebar.js @@ -6,7 +6,6 @@ context.JK.Sidebar = function(app) { var logger = context.JK.logger; var friends = []; - var notifications = []; var invitationDialog = new context.JK.InvitationDialog(app); var rest = context.JK.Rest(); @@ -111,8 +110,7 @@ url: url, processData: false, success: function(response) { - - notifications = response; + updateNotificationList(response); // set notification count @@ -127,8 +125,6 @@ $.each(response, function(index, val) { - notifications[val.notification_id] = val; - // fill in template for Connect pre-click var template = $('#template-notification-panel').html(); var notificationHtml = context.JK.fillTemplate(template, { @@ -146,24 +142,26 @@ } function initializeActions(payload, type) { - // wire up "x" button to delete notification - $('li[notification-id=' + payload.notification_id + ']').find('#img-delete-notification').click(deleteNotificationHandler); + var $notification = $('li[notification-id=' + payload.notification_id + ']'); + + // wire up "x" button to delete notification + $notification.find('#img-delete-notification').click(deleteNotificationHandler); if (type === context.JK.MessageType.FRIEND_REQUEST) { - var $action_btn = $('li[notification-id=' + payload.notification_id + ']').find('#btn-notification-action'); + var $action_btn = .find('#btn-notification-action'); $action_btn.text('ACCEPT'); $action_btn.click(function() { 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=' + payload.notification_id + ']').find('#div-actions').hide(); + $notification.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'); + var $action_btn = $notification.find('#btn-notification-action'); $action_btn.text('JOIN'); $action_btn.click(function() { - joinSession({ "session_id": payload.session_d, "notification_id": payload.notification_id }); + joinSession({ "session_id": payload.session_id, "notification_id": payload.notification_id }); }); } } @@ -183,7 +181,6 @@ url: url, processData: false, success: function(response) { - delete notifications[notificationId]; $('li[notification-id=' + notificationId + ']').hide(); decrementNotificationCount(); }, @@ -328,33 +325,18 @@ } function joinSession(args) { - logger.debug("Joining session " + args.session_id); context.location = "#/session/" + args.session_id; + deleteNotification(args.notification_id); } // default handler for incoming notification function handleNotification(payload, type) { 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) - }; + sidebarText = payload.sender_name + " has invited you to a session."; } 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(); @@ -541,7 +523,9 @@ participantHtml += ""; $.each(participants, function(index, val) { + if (index < 4) { participantHtml += ""; + } }); participantHtml += "
" + val.name + "
"; diff --git a/web/app/controllers/api_invitations_controller.rb b/web/app/controllers/api_invitations_controller.rb index b9f030a67..48faa4fb5 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, current_user.id, music_session.id) + Notification.send_session_invitation(receiver.id, current_user, music_session.id) respond_with @invitation, :responder => ApiResponder, :location => api_invitation_detail_url(@invitation) else diff --git a/web/lib/music_session_manager.rb b/web/lib/music_session_manager.rb index ad6386e4f..0433bf3f1 100644 --- a/web/lib/music_session_manager.rb +++ b/web/lib/music_session_manager.rb @@ -45,12 +45,7 @@ MusicSessionManager < BaseManager # auto-join this user into the newly created session as_musician = true - connection = ConnectionManager.new.join_music_session(user, client_id, music_session, as_musician, tracks) do |db_conn, connection| - if as_musician && music_session.musician_access - Notification.send_musician_session_join(music_session, connection, user) - Notification.send_friend_session_join(db_conn, connection, user) - end - end + connection = ConnectionManager.new.join_music_session(user, client_id, music_session, as_musician, tracks) unless connection.errors.any? return_value = music_session @@ -106,8 +101,8 @@ MusicSessionManager < BaseManager connection = ConnectionManager.new.join_music_session(user, client_id, music_session, as_musician, tracks) do |db_conn, connection| if as_musician && music_session.musician_access - Notification.send_musician_session_join(music_session, connection, user) Notification.send_friend_session_join(db_conn, connection, user) + Notification.send_musician_session_join(music_session, connection, user) end end