From 60a004a2521a0c2c6a47d92472b4e8140d8ba0cd Mon Sep 17 00:00:00 2001 From: Bert Owen Date: Thu, 5 Jun 2014 03:19:45 +0800 Subject: [PATCH] integrating api, (without music_notations, start scheduled information) --- ruby/lib/jam_ruby/models/music_session.rb | 1 + web/app/assets/javascripts/jam_rest.js | 12 + .../assets/javascripts/scheduled_session.js | 227 ++++++++++++++---- .../api_music_notations_controller.rb | 2 +- .../api_music_sessions_controller.rb | 2 +- .../views/clients/_scheduledSession.html.erb | 20 +- 6 files changed, 200 insertions(+), 64 deletions(-) diff --git a/ruby/lib/jam_ruby/models/music_session.rb b/ruby/lib/jam_ruby/models/music_session.rb index 67489f9bc..12bb31356 100644 --- a/ruby/lib/jam_ruby/models/music_session.rb +++ b/ruby/lib/jam_ruby/models/music_session.rb @@ -140,6 +140,7 @@ module JamRuby current_time = Time.now query = MusicSession.where("music_sessions.user_id = '#{user.id}'") query = query.where("music_sessions.scheduled_start IS NOT NULL AND music_sessions.scheduled_start < '#{current_time + 12.hours}'") + query = query.where("music_session_id IS NULL") query = query.order("music_sessions.scheduled_start ASC") return query diff --git a/web/app/assets/javascripts/jam_rest.js b/web/app/assets/javascripts/jam_rest.js index 9eb526b69..d8caf2adc 100644 --- a/web/app/assets/javascripts/jam_rest.js +++ b/web/app/assets/javascripts/jam_rest.js @@ -45,6 +45,17 @@ }); } + function createScheduledSession(options) { + return $.ajax({ + type: "POST", + dataType: "json", + contentType: 'application/json', + url: "/api/sessions", + processData: false, + data: JSON.stringify(options) + }); + } + function legacyJoinSession(options) { var sessionId = options["session_id"]; delete options["session_id"]; @@ -1102,6 +1113,7 @@ // Expose publics this.initialize = initialize; this.legacyCreateSession = legacyCreateSession; + this.createScheduledSession = createScheduledSession; this.legacyJoinSession = legacyJoinSession; this.joinSession = joinSession; this.getUserDetail = getUserDetail; diff --git a/web/app/assets/javascripts/scheduled_session.js b/web/app/assets/javascripts/scheduled_session.js index 3b1ed51f8..bfa2e8c77 100644 --- a/web/app/assets/javascripts/scheduled_session.js +++ b/web/app/assets/javascripts/scheduled_session.js @@ -79,6 +79,8 @@ }); context._.each(sessionList, function (session) { + session.scheduled_start = (new Date(session.scheduled_start)).toDateString() + + getFormattedTime(new Date(session.scheduled_start), false); var options = { id: session.id, name: session.name, @@ -215,42 +217,55 @@ } function beforeMoveStep1() { - createSessionSettings.startDate = $('#session-start-date').val(); - createSessionSettings.startTime = $('#start-time-list').val(); - createSessionSettings.endTime = $('#end-time-list').val(); - createSessionSettings.selectedSessionId = $scheduledSessions.find('input[name="scheduled-session-info"][checked="checked"]').attr('id'); - var $timezoneList = $('#timezone-list'); - createSessionSettings.timezone.value = $timezoneList.val(); - createSessionSettings.timezone.label = $timezoneList.get(0).options[$timezoneList.get(0).selectedIndex].text; - var $recurringMode = $('#recurring-mode-list'); - createSessionSettings.recurring_mode.label = $recurringMode.get(0).options[$recurringMode.get(0).selectedIndex].text; - createSessionSettings.recurring_mode.value = $recurringMode.val(); - - var errors = false; - if (createSessionSettings.createType == 'start-scheduled') { - if (createSessionSettings.selectedSessionId) { - var session = scheduledSessions[createSessionSettings.selectedSessionId]; - var scheduledDate = new Date(session.scheduled_start); - var currentDate = new Date(); - var diff = scheduledDate - currentDate; - if (diff > 60 * 60) { - } - } + var session = scheduledSessions[createSessionSettings.selectedSessionId]; + createSessionSettings.startDate = new Date(session.scheduled_start).toDateString(); + createSessionSettings.startTime = getFormattedTime(new Date(session.scheduled_start), false); + createSessionSettings.genresValues = ['Pop']; + createSessionSettings.genres = ['pop']; + createSessionSettings.timezone.label = "(GMT-06:00) Central Time (US & Canada)"; + createSessionSettings.timezone.value = "Central Time (US & Canada),America/Chicago"; + createSessionSettings.name = "Private Test Session"; + createSessionSettings.description = "Private session set up just to test things out in the session interface by myself."; + createSessionSettings.notations = []; + createSessionSettings.language.label = 'English'; + createSessionSettings.language.value = 'en'; + createSessionSettings.session_policy = 'Standard'; + createSessionSettings.musician_access.label = "Only RSVP musicians may join"; + createSessionSettings.musician_access.value = "only-rsvp"; + createSessionSettings.fans_access.label = "Fans may not listen to session"; + createSessionSettings.fans_access.value = "no-listen-chat"; + } + else if (createSessionSettings.createType == 'quick-start') { + createSessionSettings.genresValues = ['Pop']; + createSessionSettings.genres = ['pop']; + createSessionSettings.timezone.label = "(GMT-06:00) Central Time (US & Canada)"; + createSessionSettings.timezone.value = "Central Time (US & Canada),America/Chicago"; + createSessionSettings.name = "Private Test Session"; + createSessionSettings.description = "Private session set up just to test things out in the session interface by myself."; + createSessionSettings.notations = []; + createSessionSettings.language.label = 'English'; + createSessionSettings.language.value = 'en'; + createSessionSettings.session_policy = 'Standard'; + createSessionSettings.musician_access.label = "Only RSVP musicians may join"; + createSessionSettings.musician_access.value = "only-rsvp"; + createSessionSettings.fans_access.label = "Fans may not listen to session"; + createSessionSettings.fans_access.value = "no-listen-chat"; + } + else { + createSessionSettings.startDate = $('#session-start-date').val(); + createSessionSettings.startTime = $('#start-time-list').val(); + createSessionSettings.endTime = $('#end-time-list').val(); + createSessionSettings.selectedSessionId = $scheduledSessions.find('input[name="scheduled-session-info"][checked="checked"]').attr('id'); + var $timezoneList = $('#timezone-list'); + createSessionSettings.timezone.value = $timezoneList.val(); + createSessionSettings.timezone.label = $timezoneList.get(0).options[$timezoneList.get(0).selectedIndex].text; + var $recurringMode = $('#recurring-mode-list'); + createSessionSettings.recurring_mode.label = $recurringMode.get(0).options[$recurringMode.get(0).selectedIndex].text; + createSessionSettings.recurring_mode.value = $recurringMode.val(); } return true; -// var confirmDialog = new context.JK.ConfirmDialog(app, "Start Session Now", -// "You are starting a session that is scheduled to begin more than one hour from now. Are you sure you want to do this?", -// "Future Session", function() { -// errors = true; -// app.layout.closeDialog('confirm'); -// $.holdReady(false); -// } -// ); -// -// confirmDialog.initialize(); -// app.layout.showDialog('confirm'); } function beforeMoveStep2() { @@ -334,6 +349,105 @@ function beforeMoveStep5() { } + function startSession() { + var data = {}; + data.name = createSessionSettings.name; + data.description = createSessionSettings.description; + data.genres = createSessionSettings.genres; + if (createSessionSettings.musician_access.value == 'only_rsvp') { + data.musician_access = false; + data.approval_required = false; + } + else if (createSessionSettings.musician_access.value == 'musicians-approval') { + data.musician_access = true; + data.approval_required = true; + } + else if (createSessionSettings.musician_access.value == 'musicians') { + data.musician_access = true; + data.approval_required = false; + } + + if (createSessionSettings.fans_access.value == 'no-listen-chat') { + data.fan_access = false; data.fan_chat = false; + } + else if (createSessionSettings.fans_access.value == 'listen-chat-each') { + data.fan_access = true; data.fan_chat = false; + } + else if (createSessionSettings.fans_access.value == 'listen-chat-band') { + data.fan_access = true; data.fan_chat = true; + } + data.legal_policy = createSessionSettings.session_policy; + data.legal_terms = true; + data.language = createSessionSettings.language.value; + if (createSessionSettings.createType == 'quick-start') { + data.start = new Date().toDateString() + ' ' + getFormattedTime(new Date(), false); + } + data.invitations = inviteMusiciansUtil.getInvitedFriends(); + data.recurring_mode = createSessionSettings.recurring_mode; + data.music_notations = createSessionSettings.music_notations; + + if (createSessionSettings.createType == 'start-scheduled') { + data = scheduledSessions[createSessionSettings.selectedSessionId]; + } + + var tracks = context.JK.TrackHelpers.getUserTracks(context.jamClient); + if(tracks.length == 0) { + logger.error("we should never have 0 tracks and have gotten this far. Launch FTUE is the best we can do right now") + // If user hasn't completed FTUE - do so now. + app.afterFtue = function() { startSession(); }; + app.layout.startNewFtue(); + return false; + } + + var joinSession = function(sessionId) { +// var options = {}; +// options.client_id = app.clientId; +// options.session_id = sessionId; +// options.as_musician = true; +// options.tracks = tracks; +// rest.joinSession(options) +// .done(function(response) { + var invitationCount = data.invitations.length; + + context.location = '/client#/session/' + sessionId; + + context.JK.GA.trackSessionCount(data.musician_access, data.fan_access, invitationCount); + context.JK.GA.trackSessionMusicians(context.JK.GA.SessionCreationTypes.create); +// }) +// .fail(function(jqXHR) { +// var handled = false; +// if(jqXHR.status = 422) { +// var response = JSON.parse(jqXHR.responseText); +// if(response["errors"] && response["errors"]["tracks"] && response["errors"]["tracks"][0] == "Please select at least one track") { +// app.notifyAlert("No Inputs Configured", $('You will need to reconfigure your audio device.')); +// handled = true; +// } +// } +// if(!handled) { +// app.notifyServerError(jqXHR, "Unable to Create Session"); +// } +// }) + }; + + if (createSessionSettings.createType == 'start-scheduled') { + joinSession(createSessionSettings.selectedSessionId); + } + else { + rest.createScheduledSession(data) + .done(function(response) { + var newSessionId = response.id; + $(".btn-next").off('click'); + + if (createSessionSettings.createType == 'quick-start') { + joinSession(newSessionId); + } + }) + .fail(function(jqXHR){ + app.notifyServerError(jqXHR, "Unable to Create Session"); + }); + } + } + var STEPS = { 0: { beforeShow: beforeShowStep1, @@ -357,8 +471,6 @@ } }; - - function moveToStep() { var $nextWizardStep = $wizardSteps.filter($('[layout-wizard-step=' + step + ']')); @@ -445,9 +557,11 @@ if (step == STEP_SELECT_CONFIRM) { $btnNext.html(createSessionSettings.startType); + $btnNext.on('click', startSession); } + else + $btnNext.on('click', next); - $btnNext.on('click', next); $btnBack.on('click', back); $sessionButtons.empty(); @@ -520,6 +634,30 @@ function afterShow() { } + function getFormattedTime(date, change) { + if (change) { + date.setMinutes(Math.ceil(date.getMinutes() / 30) * 30); + } + var h12h = date.getHours(); + var m12h = date.getMinutes(); + var ampm; + if (h12h >= 0 && h12h < 12) { + if (h12h === 0) { + h12h = 12; // 0 becomes 12 + } + ampm = "AM"; + } + else { + if (h12h > 12) { + h12h -= 12; // 13-23 becomes 1-11 + } + ampm = "PM"; + } + var timeString = ("00" + h12h).slice(-2) + ":" + ("00" + m12h).slice(-2) + " " + ampm; + + return timeString; + } + function toggleDate() { var selectedDate = new Date($('#session-start-date').val()); var currentDate = new Date(); @@ -528,23 +666,8 @@ if (currentDate.getYear() == selectedDate.getYear() && currentDate.getMonth() == selectedDate.getMonth() && currentDate.getDate() == selectedDate.getDate()) { - currentDate.setMinutes(Math.ceil(currentDate.getMinutes() / 30) * 30); - var h12h = currentDate.getHours(); - var m12h = currentDate.getMinutes(); - var ampm; - if (h12h >= 0 && h12h < 12) { - if (h12h === 0) { - h12h = 12; // 0 becomes 12 - } - ampm = "AM"; - } - else { - if (h12h > 12) { - h12h -= 12; // 13-23 becomes 1-11 - } - ampm = "PM"; - } - var timeString = ("00" + h12h).slice(-2) + ":" + ("00" + m12h).slice(-2) + " " + ampm; + + var timeString = getFormattedTime(currentDate, true); startIndex = defaultTimeArray.indexOf(timeString); } var $startTimeList = $('#start-time-list'); diff --git a/web/app/controllers/api_music_notations_controller.rb b/web/app/controllers/api_music_notations_controller.rb index 121f23da2..a3c150cba 100644 --- a/web/app/controllers/api_music_notations_controller.rb +++ b/web/app/controllers/api_music_notations_controller.rb @@ -9,7 +9,7 @@ class ApiMusicNotationsController < ApiController client_id = params[:client_id] if client_id.nil? - raise JamArgumentError, "client_id must be asdfasfdasdf specified" + raise JamArgumentError, "client_id must be specified" end @music_notation = MusicNotation.new diff --git a/web/app/controllers/api_music_sessions_controller.rb b/web/app/controllers/api_music_sessions_controller.rb index 4c4e3eb34..d6d1aa9a7 100644 --- a/web/app/controllers/api_music_sessions_controller.rb +++ b/web/app/controllers/api_music_sessions_controller.rb @@ -241,7 +241,7 @@ class ApiMusicSessionsController < ApiController client_id = params[:client_id] if client_id.nil? - raise JamArgumentError, "client_id must be asdfasfdasdf specified" + raise JamArgumentError, "client_id must be specified" end begin diff --git a/web/app/views/clients/_scheduledSession.html.erb b/web/app/views/clients/_scheduledSession.html.erb index 2e270a4cf..0e5e52579 100644 --- a/web/app/views/clients/_scheduledSession.html.erb +++ b/web/app/views/clients/_scheduledSession.html.erb @@ -270,7 +270,7 @@
  • - + @@ -299,7 +299,7 @@ the music. Use the following link for full legal details of this choice: www.jamkazam.com/session-legal-policies/standard. -