diff --git a/web/app/assets/javascripts/findSession.js b/web/app/assets/javascripts/findSession.js index a6dbb9c1f..7a5831f37 100644 --- a/web/app/assets/javascripts/findSession.js +++ b/web/app/assets/javascripts/findSession.js @@ -239,7 +239,6 @@ renderScheduledSessions(json); }); } - /******************************************************/ function beforeShow(data) { context.JK.GenreSelectorHelper.render('#find-session-genre', 'Any Genre'); diff --git a/web/app/assets/javascripts/notificationPanel.js b/web/app/assets/javascripts/notificationPanel.js index 1772c91cd..3f3258767 100644 --- a/web/app/assets/javascripts/notificationPanel.js +++ b/web/app/assets/javascripts/notificationPanel.js @@ -21,6 +21,7 @@ var textMessageDialog = null; var queuedNotification = null; var queuedNotificationCreatedAt = null; + var sessionUtils = context.JK.SessionUtils; function isNotificationsPanelVisible() { return $contents.is(':visible') @@ -446,22 +447,11 @@ context.JK.popExternalLink('/sessions/' + args.session_id); } - /*********** TODO: THE NEXT 3 FUNCTIONS ARE COPIED FROM sessionList.js. REFACTOR TO COMMON PLACE. *************/ function joinSession(args) { - // NOTE: invited musicians get their own notification, so no need to check if user has invitation here - // like other places because an invited user would never get this notification - if (args.musician_access) { - if (args.approval_required) { - openAlert(args.session_id); - } - else { - openTerms(args); - } - } + sessionUtils.joinSession(args.session_id); deleteNotification(args.notification_id); } - function registerJoinRequestApproved() { context.JK.JamServer.registerMessageCallback(context.JK.MessageType.JOIN_REQUEST_APPROVED, function(header, payload) { logger.debug("Handling JOIN_REQUEST_APPROVED msg " + JSON.stringify(payload)); @@ -1173,19 +1163,6 @@ return true; } - - function onCreateJoinRequest(sessionId) { - var joinRequest = {}; - joinRequest.music_session = sessionId; - joinRequest.user = context.JK.currentUserId; - rest.createJoinRequest(joinRequest) - .done(function(response) { - - }).error(context.JK.app.ajaxError); - - context.JK.app.layout.closeDialog('alert'); - } - function approveJoinRequest(args) { rest.updateJoinRequest(args.join_request_id, true) .done(function(response) { @@ -1193,13 +1170,6 @@ }).error(app.ajaxError); } - function rejectJoinRequest(args) { - rest.updateJoinRequest(args.join_request_id, false) - .done(function(response) { - deleteNotification(args.notification_id); - }).error(app.ajaxError); - } - function openTerms(args) { var termsDialog = new context.JK.TermsDialog(app, args, onTermsAccepted); termsDialog.initialize(); @@ -1208,17 +1178,14 @@ function onTermsAccepted(args) { deleteNotification(args.notification_id); - context.location = '/client#/session/' + args.session_id; + sessionUtils.onJoinSessionTermsAccepted(args.session_id); } - - function openAlert(sessionId) { - var alertDialog = new context.JK.AlertDialog(context.JK.app, "YES", - "You must be approved to join this session. Would you like to send a request to join?", - sessionId, onCreateJoinRequest); - - alertDialog.initialize(); - context.JK.app.layout.showDialog('alert'); + function rejectJoinRequest(args) { + rest.updateJoinRequest(args.join_request_id, false) + .done(function(response) { + deleteNotification(args.notification_id); + }).error(app.ajaxError); } function listenToRecording(args) { diff --git a/web/app/assets/javascripts/sessionList.js b/web/app/assets/javascripts/sessionList.js index c2103a66d..a8fc38a9d 100644 --- a/web/app/assets/javascripts/sessionList.js +++ b/web/app/assets/javascripts/sessionList.js @@ -121,7 +121,7 @@ }) }) .done(function(){ - joinClick(session.id); + sessionUtils.joinSession(session.id); }) return false; @@ -376,108 +376,6 @@ return context.JK.fillTemplate($notationFileTemplate.html(), notationVals); } - function joinClick(sessionId) { - var hasInvitation = false; - var session = null; - // we need to do a real-time check of the session in case the settings have - // changed while the user was sitting on the Find Session screen - rest.getSession(sessionId) - .done(function(response) { - session = response; - if ("invitations" in session) { - var invitation; - // user has invitations for this session - for (var i=0; i < session.invitations.length; i++) { - invitation = session.invitations[i]; - // session contains an invitation for this user - if (invitation.receiver_id === JK.currentUserId) { - hasInvitation = true; - break; - } - } - } - - if (session) { - // if user has an invitation, always open terms and allow joining regardless of settings - if (hasInvitation) { - logger.debug("Found invitation for user " + JK.currentUserId + ", session " + sessionId); - openTerms(sessionId); - } - else { - if(session.user_id == JK.currentUserId) { - openTerms(sessionId); - } - else if (session.musician_access) { - if (session.approval_required) { - openAlert(sessionId); - } - else { - openTerms(sessionId); - } - } - } - } - }) - .fail(function(xhr, textStatus, errorMessage) { - logger.debug("xhr.status = " + xhr.status); - if (xhr.status === 404) { - sessionNotJoinableAlert(); - } - else { - JK.app.notify( - { title: "Unable to Join Session", - text: "There was an unexpected error while attempting to join the session." - }, - null, - true); - } - }); - } - - function openAlert(sessionId) { - var alertDialog = new context.JK.AlertDialog(app, "YES", - "You must be approved to join this session. Would you like to send a request to join?", - sessionId, onCreateJoinRequest); - - alertDialog.initialize(); - app.layout.showDialog('alert'); - } - - function sessionNotJoinableAlert() { - var alertDialog = new context.JK.AlertDialog(app, "OK", - "This session is over or is no longer public and cannot be joined. Please click Refresh to update the session list.", - null, - function(evt) { - app.layout.closeDialog('alert'); - } - ); - - alertDialog.initialize(); - app.layout.showDialog('alert'); - } - - function onCreateJoinRequest(sessionId) { - var joinRequest = {}; - joinRequest.music_session = sessionId; - joinRequest.user = context.JK.currentUserId; - rest.createJoinRequest(joinRequest) - .done(function(response) { - - }).error(app.ajaxError); - - app.layout.closeDialog('alert'); - } - - function openTerms(sessionId) { - var termsDialog = new context.JK.TermsDialog(app, sessionId, onTermsAccepted); - termsDialog.initialize(); - app.layout.showDialog('terms'); - } - - function onTermsAccepted(sessionId) { - context.location = '/client#/session/' + sessionId; - } - this.renderActiveSession = renderActiveSession; this.renderInactiveSession = renderInactiveSession; diff --git a/web/app/assets/javascripts/session_utils.js b/web/app/assets/javascripts/session_utils.js index 3743bdbd4..ebb2b69ff 100644 --- a/web/app/assets/javascripts/session_utils.js +++ b/web/app/assets/javascripts/session_utils.js @@ -11,8 +11,6 @@ context.JK.SessionUtils = sessionUtils; var logger = context.JK.logger; - - var LATENCY = sessionUtils.LATENCY = { ME : {description: "ME", style: "latency-me", min: -1, max: -1}, GOOD : {description: "GOOD", style: "latency-good", min: 0.0, max: 40.0}, @@ -124,6 +122,108 @@ } } + sessionUtils.joinSession = function(sessionId) { + var hasInvitation = false; + var session = null; + // we need to do a real-time check of the session in case the settings have + // changed while the user was sitting on the Find Session screen + rest.getSession(sessionId) + .done(function(response) { + session = response; + if ("invitations" in session) { + var invitation; + // user has invitations for this session + for (var i=0; i < session.invitations.length; i++) { + invitation = session.invitations[i]; + // session contains an invitation for this user + if (invitation.receiver_id === JK.currentUserId) { + hasInvitation = true; + break; + } + } + } + + if (session) { + // if user has an invitation, always open terms and allow joining regardless of settings + if (hasInvitation) { + logger.debug("Found invitation for user " + JK.currentUserId + ", session " + sessionId); + openJoinSessionTerms(sessionId); + } + else { + if(session.user_id == JK.currentUserId) { + openJoinSessionTerms(sessionId); + } + else if (session.musician_access) { + if (session.approval_required) { + openJoinRequestAlert(sessionId); + } + else { + openJoinSessionTerms(sessionId); + } + } + } + } + }) + .fail(function(xhr, textStatus, errorMessage) { + logger.debug("xhr.status = " + xhr.status); + if (xhr.status === 404) { + sessionNotJoinableAlert(); + } + else { + context.JK.app.notify( + { title: "Unable to Join Session", + text: "There was an unexpected error while attempting to join the session." + }, + null, + true); + } + }); + } + + function openJoinRequestAlert(sessionId) { + var alertDialog = new context.JK.AlertDialog(JK.app, "YES", + "You must be approved to join this session. Would you like to send a request to join?", + sessionId, onCreateJoinRequest); + + alertDialog.initialize(); + context.JK.app.layout.showDialog('alert'); + } + + function sessionNotJoinableAlert() { + var alertDialog = new context.JK.AlertDialog(JK.app, "OK", + "This session is over or is no longer public and cannot be joined. Please click Refresh to update the session list.", + null, + function(evt) { + context.JK.app.layout.closeDialog('alert'); + } + ); + + alertDialog.initialize(); + context.JK.app.layout.showDialog('alert'); + } + + sessionUtils.openJoinSessionTerms = function(sessionId) { + var termsDialog = new context.JK.TermsDialog(context.JK.app, sessionId, onJoinSessionTermsAccepted); + termsDialog.initialize(); + context.JK.app.layout.showDialog('terms'); + } + + sessionUtils.onJoinSessionTermsAccepted = function(sessionId) { + context.location = '/client#/session/' + sessionId; + } + + function onCreateJoinRequest(sessionId) { + var joinRequest = {}; + joinRequest.music_session = sessionId; + joinRequest.user = context.JK.currentUserId; + rest.createJoinRequest(joinRequest) + .done(function(response) { + + }).error(context.JK.app.ajaxError); + + context.JK.app.layout.closeDialog('alert'); + } + sessionUtils.FTUEPageEnter = function() { logger.debug("sessionUtils: FTUEPageEnter"); clearAudioTimeout(); diff --git a/web/app/views/clients/_hoverMusician.html.erb b/web/app/views/clients/_hoverMusician.html.erb index 0a1d882ca..0a185a3ed 100644 --- a/web/app/views/clients/_hoverMusician.html.erb +++ b/web/app/views/clients/_hoverMusician.html.erb @@ -5,6 +5,7 @@