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

798 lines
24 KiB
JavaScript

(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) {
return $.ajax('/api/sessions/' + id, {
type: "PUT",
data : newSession,
dataType : 'json',
success: onSuccess
});
}
function addSessionComment(sessionId, userId, comment) {
return $.ajax({
url: '/api/sessions/' + sessionId + "/comments",
type: "POST",
data : JSON.stringify({"comment": comment, "user_id": userId}),
dataType : 'json',
contentType: 'application/json'
});
}
function addSessionLike(sessionId, userId) {
return $.ajax({
url: '/api/sessions/' + sessionId + "/likes",
type: "POST",
data : JSON.stringify({"user_id": userId}),
dataType : 'json',
contentType: 'application/json'
});
}
function addRecordingComment(recordingId, userId, comment) {
return $.ajax({
url: '/api/recordings/' + recordingId + "/comments",
type: "POST",
data : JSON.stringify({"comment": comment, "user_id": userId}),
dataType : 'json',
contentType: 'application/json'
});
}
function addRecordingLike(recordingId, userId) {
return $.ajax({
url: '/api/recordings/' + recordingId + "/likes",
type: "POST",
data : JSON.stringify({"user_id": userId}),
dataType : 'json',
contentType: 'application/json'
});
}
function addRecordingPlay(recordingId, userId) {
return $.ajax({
url: '/api/recordings/' + recordingId + "/plays",
type: "POST",
data : JSON.stringify({"user_id": userId}),
dataType : 'json',
contentType: 'application/json'
});
}
function getBand(bandId) {
return $.ajax({
type: "GET",
dataType: "json",
url: '/api/bands/' + bandId,
contentType: 'application/json',
processData: false
});
}
function createBand(band) {
return $.ajax({
type: "POST",
dataType: "json",
url: '/api/bands',
contentType: 'application/json',
processData: false,
data: JSON.stringify(band)
});
}
function updateBand(band, bandId) {
logger.debug("bandId=" + bandId);
return $.ajax({
type: "POST",
dataType: "json",
url: '/api/bands/' + bandId,
contentType: 'application/json',
processData: false,
data: JSON.stringify(band)
});
}
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) {
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 getResolvedLocation() {
return $.ajax('/api/resolved_location', {
dataType: 'json'
});
}
function getInstruments(options) {
return $.ajax('/api/instruments', {
data : { },
dataType : 'json'
});
}
function getGenres(options) {
return $.ajax('/api/genres', {
data: { },
dataType: 'json'
});
}
function updateAvatar(options) {
var id = getId(options);
var original_fpfile = options['original_fpfile'];
var cropped_fpfile = options['cropped_fpfile'];
var crop_selection = options['crop_selection'];
logger.debug(JSON.stringify({
original_fpfile : original_fpfile,
cropped_fpfile : cropped_fpfile,
crop_selection : 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
})
});
}
function deleteAvatar(options) {
var id = getId(options);
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 },
dataType : 'json'
});
}
function updateBandPhoto(options) {
var id = getId(options);
var original_fpfile = options['original_fpfile'];
var cropped_fpfile = options['cropped_fpfile'];
var crop_selection = options['crop_selection'];
logger.debug(JSON.stringify({
original_fpfile : original_fpfile,
cropped_fpfile : cropped_fpfile,
crop_selection : crop_selection
}));
var url = "/api/bands/" + id + "/photo";
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
})
});
}
function deleteBandPhoto(options) {
var id = getId(options);
var url = "/api/bands/" + id + "/photo";
return $.ajax({
type: "DELETE",
dataType: "json",
url: url,
contentType: 'application/json',
processData:false
});
}
function getBandPhotoFilepickerPolicy(options) {
var id = getId(options);
var handle = options && options["handle"];
var convert = options && options["convert"]
var url = "/api/bands/" + id + "/filepicker_policy";
return $.ajax(url, {
data : { handle : handle, convert: convert },
dataType : 'json'
});
}
function getFriends(options) {
var id = getId(options);
return $.ajax({
type: "GET",
url: '/api/users/' + id + '/friends',
dataType: 'json'
});
}
function removeFriend(options) {
var id = getId(options);
var friendId = options["friend_id"];
return $.ajax({
type: "DELETE",
dataType: "json",
url: "/api/users/" + id + "/friends/" + friendId,
processData: false
});
}
function addFollowing(options) {
var id = getId(options);
return $.ajax({
type: "POST",
dataType: "json",
contentType: 'application/json',
url: "/api/users/" + id + "/followings",
data: JSON.stringify(options),
processData: false
});
}
function removeFollowing(options) {
var id = getId(options);
return $.ajax({
type: "DELETE",
dataType: "json",
contentType: 'application/json',
url: "/api/users/" + id + "/followings",
data: JSON.stringify(options),
processData: false
});
}
function getFollowings(options) {
var userId = getId(options);
// FOLLOWINGS (USERS)
return $.ajax({
type: "GET",
dataType: "json",
url: "/api/users/" + userId + "/followings",
processData:false
});
}
function getFollowers(options) {
var userId = getId(options);
return $.ajax({
type: "GET",
dataType: "json",
url: "/api/users/" + userId + "/followers",
processData:false
});
}
function getBandFollowings(options) {
var userId = getId(options);
// FOLLOWINGS (BANDS)
return $.ajax({
type: "GET",
dataType: "json",
url: "/api/users/" + userId + "/band_followings",
processData:false
});
}
function getBandFollowing(options) {
var id = getId(options);
var bandId = options["band_id"];
return $.ajax({
type: "GET",
dataType: "json",
url: "/api/users/" + id + "/band_followings/" + bandId,
processData: false
});
}
function getBands(options) {
var userId = getId(options);
return $.ajax({
type: "GET",
dataType: "json",
url: "/api/users/" + userId + "/bands",
processData:false
});
}
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"
});
}
function getId(options) {
var id = options && options["id"]
if(!id) {
id = context.JK.currentUserId;
}
else {
delete options["id"];
}
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'
});
}
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
});
}
function getClaimedRecordings(options) {
return $.ajax({
type: "GET",
dataType: "json",
contentType: 'application/json',
url: "/api/claimed_recordings",
data: options
});
}
function claimRecording(options) {
var recordingId = options["id"];
return $.ajax({
type: "POST",
dataType: "json",
contentType: 'application/json',
url: "/api/recordings/" + recordingId + "/claim",
data: JSON.stringify(options)
})
}
function startPlayClaimedRecording(options) {
var musicSessionId = options["id"];
var claimedRecordingId = options["claimed_recording_id"];
delete options["id"];
delete options["claimed_recording_id"];
return $.ajax({
type: "POST",
dataType: "json",
contentType: 'application/json',
url: "/api/sessions/" + musicSessionId + "/claimed_recording/" + claimedRecordingId + "/start",
data: JSON.stringify(options)
})
}
function stopPlayClaimedRecording(options) {
var musicSessionId = options["id"];
var claimedRecordingId = options["claimed_recording_id"];
delete options["id"];
delete options["claimed_recording_id"];
return $.ajax({
type: "POST",
dataType: "json",
contentType: 'application/json',
url: "/api/sessions/" + musicSessionId + "/claimed_recording/" + claimedRecordingId + "/stop",
data: JSON.stringify(options)
})
}
function discardRecording(options) {
var recordingId = options["id"];
return $.ajax({
type: "POST",
dataType: "json",
contentType: 'application/json',
url: "/api/recordings/" + recordingId + "/discard",
data: JSON.stringify(options)
})
}
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;
this.getResolvedLocation = getResolvedLocation;
this.getInstruments = getInstruments;
this.getGenres = getGenres;
this.updateAvatar = updateAvatar;
this.deleteAvatar = deleteAvatar;
this.getFilepickerPolicy = getFilepickerPolicy;
this.getFriends = getFriends;
this.removeFriend = removeFriend;
this.addFollowing = addFollowing;
this.removeFollowing = removeFollowing;
this.getFollowings = getFollowings;
this.getFollowers = getFollowers;
this.getBandFollowings = getBandFollowings;
this.getBandFollowing = getBandFollowing;
this.getBands = getBands;
this.updateSession = updateSession;
this.addSessionComment = addSessionComment;
this.addSessionLike = addSessionLike;
this.addRecordingComment = addRecordingComment;
this.addRecordingLike = addRecordingLike;
this.addRecordingPlay = addRecordingPlay;
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;
this.updateUser = updateUser;
this.startRecording = startRecording;
this.stopRecording = stopRecording;
this.getRecording = getRecording;
this.getClaimedRecordings = getClaimedRecordings;
this.claimRecording = claimRecording;
this.startPlayClaimedRecording = startPlayClaimedRecording;
this.stopPlayClaimedRecording = stopPlayClaimedRecording;
this.discardRecording = discardRecording;
this.putTrackSyncChange = putTrackSyncChange;
this.createBand = createBand;
this.updateBand = updateBand;
this.updateBandPhoto = updateBandPhoto;
this.deleteBandPhoto = deleteBandPhoto;
this.getBandPhotoFilepickerPolicy = getBandPhotoFilepickerPolicy;
this.getBand = getBand;
this.createBandInvitation = createBandInvitation;
this.updateBandInvitation = updateBandInvitation;
this.removeBandMember = removeBandMember;
return this;
};
})(window,jQuery);