jam-cloud/web/app/assets/javascripts/jam_rest.js

504 lines
15 KiB
JavaScript
Raw Normal View History

(function(context,$) {
/**
* Javascript wrappers for the REST API
*/
"use strict";
context.JK = context.JK || {};
context.JK.Rest = function() {
var self = this;
var logger = context.JK.logger;
function createJoinRequest(joinRequest) {
return $.ajax({
type: "POST",
dataType: "json",
url: '/api/join_requests',
contentType: 'application/json',
processData: false,
data: JSON.stringify(joinRequest)
});
}
function updateJoinRequest(joinRequestId, isApproved) {
return $.ajax({
type: "PUT",
dataType: "json",
url: '/api/join_requests/' + joinRequestId,
contentType: 'application/json',
processData: false,
data: JSON.stringify({"approved": isApproved})
});
}
function updateSession(id, newSession, onSuccess) {
logger.debug('Rest.updateSession');
return $.ajax('/api/sessions/' + id, {
type: "PUT",
data : newSession,
dataType : 'json',
success: onSuccess
});
}
2013-12-01 20:09:44 +00:00
function getBand(bandId) {
return $.ajax({
type: "GET",
dataType: "json",
url: '/api/bands/' + bandId,
contentType: 'application/json',
processData: false
});
}
2013-11-21 06:24:40 +00:00
function createBand(band) {
return $.ajax({
type: "POST",
dataType: "json",
url: '/api/bands',
contentType: 'application/json',
processData: false,
data: JSON.stringify(band)
});
}
2013-12-01 20:09:44 +00:00
function updateBand(band, bandId) {
2013-12-01 23:16:33 +00:00
logger.debug("bandId=" + bandId);
2013-12-01 20:09:44 +00:00
return $.ajax({
type: "POST",
dataType: "json",
url: '/api/bands/' + bandId,
contentType: 'application/json',
processData: false,
data: JSON.stringify(band)
});
}
2013-11-21 06:24:40 +00:00
function updateBand(band) {
return $.ajax({
type: "POST",
dataType: "json",
url: '/api/bands/' + band.id,
contentType: 'application/json',
processData: false,
data: JSON.stringify(band)
});
}
function createBandInvitation(bandId, userId) {
var bandInvitation = {
band_id: bandId,
user_id: userId
};
return $.ajax({
type: "POST",
dataType: "json",
url: '/api/bands/' + bandId + "/invitations",
contentType: 'application/json',
processData: false,
data: JSON.stringify(bandInvitation)
});
}
function updateBandInvitation(bandId, invitationId, isAccepted) {
return $.ajax({
type: "POST",
dataType: "json",
url: '/api/bands/' + bandId + "/invitations/" + invitationId,
contentType: 'application/json',
processData: false,
data: JSON.stringify({"accepted": isAccepted})
});
}
function removeBandMember(bandId, userId) {
var url = "/api/bands/" + bandId + "/musicians/" + userId;
return $.ajax({
type: "DELETE",
dataType: "json",
url: url,
async: false,
processData:false
});
}
function getSession(id) {
var url = "/api/sessions/" + id;
return $.ajax({
type: "GET",
dataType: "json",
url: url,
async: false,
processData: false
});
}
function getUserDetail(options) {
2013-05-31 02:07:33 +00:00
var id = getId(options);
var url = "/api/users/" + id;
return $.ajax({
type: "GET",
dataType: "json",
url: url,
async: true,
processData: false
});
}
function getCities(options) {
var country = options['country']
var region = options['region']
return $.ajax('/api/cities', {
data : { country: country, region: region },
dataType : 'json'
});
}
function getRegions(options) {
var country = options["country"]
return $.ajax('/api/regions', {
data : { country: country},
dataType : 'json'
});
}
function getCountries() {
return $.ajax('/api/countries', {
dataType : 'json'
});
}
function getIsps(options) {
var country = options["country"]
return $.ajax('/api/isps', {
data : { country: country},
dataType : 'json'
});
}
function getInstruments(options) {
return $.ajax('/api/instruments', {
data : { },
dataType : 'json'
});
}
function getGenres(options) {
return $.ajax('/api/genres', {
data: { },
dataType: 'json'
});
}
2013-05-31 02:07:33 +00:00
function updateAvatar(options) {
var id = getId(options);
2013-05-23 13:53:37 +00:00
2013-05-31 02:07:33 +00:00
var original_fpfile = options['original_fpfile'];
var cropped_fpfile = options['cropped_fpfile'];
var crop_selection = options['crop_selection'];
var url = "/api/users/" + id + "/avatar";
return $.ajax({
type: "POST",
dataType: "json",
url: url,
contentType: 'application/json',
processData:false,
data: JSON.stringify({
original_fpfile : original_fpfile,
cropped_fpfile : cropped_fpfile,
crop_selection : crop_selection
})
});
}
2013-05-23 13:53:37 +00:00
2013-05-31 02:07:33 +00:00
function deleteAvatar(options) {
var id = getId(options);
2013-05-23 13:53:37 +00:00
2013-05-31 02:07:33 +00:00
var url = "/api/users/" + id + "/avatar";
return $.ajax({
type: "DELETE",
dataType: "json",
url: url,
contentType: 'application/json',
processData:false
});
}
function getFilepickerPolicy(options) {
var id = getId(options);
var handle = options && options["handle"];
var convert = options && options["convert"]
var url = "/api/users/" + id + "/filepicker_policy";
return $.ajax(url, {
data : { handle : handle, convert: convert },
2013-05-23 13:53:37 +00:00
dataType : 'json'
});
}
function getFriends(options) {
var friends = [];
var id = getId(options);
$.ajax({
type: "GET",
async: false,
url: '/api/users/' + id + '/friends',
dataType: 'json'
}).done(function(response) {
friends = response;
});
return friends;
}
function getMusicianFollowers(userId) {
}
function getBandFollowers(bandId) {
}
function getClientDownloads(options) {
return $.ajax({
type: "GET",
url: '/api/artifacts/clients',
dataType: 'json'
});
}
/** check if the server is alive */
function serverHealthCheck(options) {
console.log("serverHealthCheck")
return $.ajax({
type: "GET",
url: "/api/versioncheck"
});
}
2013-05-31 02:07:33 +00:00
function getId(options) {
var id = options && options["id"]
if(!id) {
id = context.JK.currentUserId;
}
return id;
}
function createInvitation(emailAddress, message) {
return $.ajax({
type: "POST",
dataType: "json",
url: '/api/invited_users',
contentType: 'application/json',
processData:false,
data: JSON.stringify({
email : emailAddress,
note: message
})
});
}
function postFeedback(email, body) {
return $.ajax({
type: "POST",
dataType: "json",
url: '/api/feedback',
contentType: 'application/json',
processData:false,
data: JSON.stringify({
email : email,
body: body
})
});
}
function acceptFriendRequest(options) {
var id = getId(options);
var friend_request_id = options["friend_request_id"];
var status = options["status"];
var friend_request = { status: status };
var deferred = $.ajax({
type: "POST",
dataType: "json",
contentType: 'application/json',
url: "/api/users/" + id + "/friend_requests/" + friend_request_id,
data: JSON.stringify(friend_request),
processData: false
});
deferred.done(function() {
context.JK.GA.trackFriendConnect(context.JK.GA.FriendConnectTypes.accept);
});
return deferred;
}
function userDownloadedClient(options) {
return $.ajax({
type: "POST",
dataType: "json",
contentType: 'application/json',
url: "/api/users/progression/downloaded_client",
processData: false
});
}
function userCertifiedGear(options) {
return $.ajax({
type: "POST",
dataType: "json",
contentType: 'application/json',
url: "/api/users/progression/certified_gear",
processData: false,
data: JSON.stringify({
success: options.success,
reason: options.reason
})
});
}
function userSocialPromoted(options) {
var id = getId(options);
return $.ajax({
type: "POST",
dataType: "json",
contentType: 'application/json',
url: "/api/users/progression/social_promoted",
processData: false
});
}
function signout() {
return $.ajax({
type: "DELETE",
dataType: "json",
url: '/signout',
contentType: 'application/json'
});
}
2013-10-21 22:13:53 +00:00
function updateUser(options) {
var id = getId(options);
delete options['id'];
return $.ajax({
type: "POST",
dataType: "json",
contentType: 'application/json',
url: "/api/users/" + id,
data: JSON.stringify(options),
processData: false
});
}
function startRecording(options) {
return $.ajax({
type: "POST",
dataType: "json",
contentType: 'application/json',
url: "/api/recordings/start",
data: JSON.stringify(options)
})
}
function stopRecording(options) {
var recordingId = options["id"]
return $.ajax({
type: "POST",
dataType: "json",
contentType: 'application/json',
url: "/api/recordings/" + recordingId + "/stop",
data: JSON.stringify(options)
})
}
function getRecording(options) {
var recordingId = options["id"];
return $.ajax({
type: "GET",
dataType: "json",
contentType: 'application/json',
url: "/api/recordings/" + recordingId
})
}
2013-11-16 04:35:40 +00:00
function putTrackSyncChange(options) {
var musicSessionId = options["id"]
delete options["id"];
return $.ajax({
type: "PUT",
dataType: "json",
url: '/api/sessions/' + musicSessionId + '/tracks',
contentType: 'application/json',
processData: false,
data: JSON.stringify(options)
});
}
function initialize() {
return self;
}
// Expose publics
this.initialize = initialize;
this.getUserDetail = getUserDetail;
this.getCities = getCities;
this.getRegions = getRegions;
this.getCountries = getCountries;
this.getIsps = getIsps;
2013-05-31 02:07:33 +00:00
this.getInstruments = getInstruments;
this.getGenres = getGenres;
2013-05-31 02:07:33 +00:00
this.updateAvatar = updateAvatar;
this.deleteAvatar = deleteAvatar;
this.getFilepickerPolicy = getFilepickerPolicy;
this.getFriends = getFriends;
this.updateSession = updateSession;
this.getSession = getSession;
this.getClientDownloads = getClientDownloads
this.createInvitation = createInvitation;
this.postFeedback = postFeedback;
this.serverHealthCheck = serverHealthCheck;
this.acceptFriendRequest = acceptFriendRequest;
this.signout = signout;
this.userDownloadedClient = userDownloadedClient;
this.userCertifiedGear = userCertifiedGear;
this.userSocialPromoted = userSocialPromoted;
this.createJoinRequest = createJoinRequest;
this.updateJoinRequest = updateJoinRequest;
2013-10-21 22:13:53 +00:00
this.updateUser = updateUser;
this.startRecording = startRecording;
this.stopRecording = stopRecording;
this.getRecording = getRecording;
2013-11-16 04:35:40 +00:00
this.putTrackSyncChange = putTrackSyncChange;
this.createBand = createBand;
this.updateBand = updateBand;
2013-12-01 20:09:44 +00:00
this.getBand = getBand;
this.createBandInvitation = createBandInvitation;
this.updateBandInvitation = updateBandInvitation;
this.removeBandMember = removeBandMember;
return this;
};
})(window,jQuery);