Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Show and make reposts - integrate SoundCloud API v2 [in progress] #430

Merged
merged 1 commit into from
Dec 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ <h4 id="playerUser" class="player_user" ng-click="goToUser($event)"></h4>

<!-- services/factories -->
<script src="public/js/common/queueService.js"></script>
<script src="public/js/common/rateLimitService.js"></script>
<script src="public/js/common/SCapiService.js"></script>
<script src="public/js/common/SC2apiService.js"></script>
<script src="public/js/common/playerService.js"></script>
<script src="public/js/common/notificationFactory.js"></script>

Expand Down Expand Up @@ -240,6 +242,7 @@ <h4 id="playerUser" class="player_user" ng-click="goToUser($event)"></h4>
<script src="public/js/common/songDirective.js"></script>
<script src="public/js/common/openExternalLinkDirective.js"></script>
<script src="public/js/common/favoriteSongDirective.js"></script>
<script src="public/js/common/repostedSongDirective.js"></script>
<script src="public/js/common/showMoreDirective.js"></script>
<script src="public/js/common/tracksDirective.js"></script>
<script src="public/js/common/playlistDirective.js"></script>
Expand Down
136 changes: 136 additions & 0 deletions app/public/js/common/SC2apiService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
'use strict';

// Service to work with SoundCloud API v2
// Note: v2 API is not officially supported by SoundCloud yet,
// be careful using these methods, because they might stop working at any moment

app.service('SC2apiService', function (
$rootScope,
$window,
$http,
$q,
rateLimit
) {

/**
* Soundcloud v2 API endpoint
* @type {String}
*/
var SOUNDCLOUD_API_V2 = 'https://api-v2.soundcloud.com/';

/**
* Store URL of the next page, when acessing resource supporting pagination
* @type {String}
*/
var nextPageUrl = '';


// Public API

/**
* Get current user stream
* @return {promise}
*/
this.getStream = function () {
var params = {
limit: 30
};
return sendRequest('stream', { params: params })
.then(onResponseSuccess)
.then(updateNextPageUrl)
.catch(onResponseError);
};

/**
* Get next page for last requested resource
* @return {promise}
*/
this.getNextPage = function () {
if (!nextPageUrl) {
return $q.reject('No next page URL');
}
return sendRequest(nextPageUrl)
.then(onResponseSuccess)
.then(updateNextPageUrl)
.catch(onResponseError);
};

/**
* Get extended information about particular tracks
* @param {array} ids - tracks ids
* @return {promise}
*/
this.getTracksByIds = function (ids) {
var params = {
urns: ids.map(function (trackId) {
return 'soundcloud:tracks:' + trackId.toString();
}).join(',')
};
return sendRequest('tracks', { params: params })
.then(onResponseSuccess)
.catch(onResponseError);
};

// Private methods

/**
* Utility method to send http request
* @param {resource} resource - url part with resource name
* @param {object} config - options for $http
* @param {object} options - custom options (show loading, etc)
* @return {promise}
*/
function sendRequest(resource, config, options) {
config = config || {};
// Check if passed absolute url
if (resource.indexOf('http') === 0) {
config.url = resource;
} else {
config.url = SOUNDCLOUD_API_V2 + resource;
}
config.params = config.params || {};
config.params.oauth_token = $window.scAccessToken;

options = options || {};
if (options.loading !== false) {
$rootScope.isLoading = true;
}

return $http(config);
}

/**
* Response success handler
* @param {object} response - $http response object
* @return {object} - response data
*/
function onResponseSuccess(response) {
if (response.status !== 200) {
return $q.reject(response.data);
}
return response.data;
}

/**
* Response error handler
* @param {object} response - $http response object
* @return {promise}
*/
function onResponseError(response) {
if (response.status === 429) {
rateLimit.showNotification();
}
return $q.reject(response.data);
}

/**
* Update value of the next page for paginatable resources
* @param {data} data - response data
* @return {object} - pass through data to use function in promise chain
*/
function updateNextPageUrl(data) {
nextPageUrl = data.next_href || '';
return data;
}

});
108 changes: 77 additions & 31 deletions app/public/js/common/SCapiService.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
'use strict';

app.service('SCapiService', function ($http, $window, $q, $log, $state, $stateParams, $rootScope, ngDialog) {

function rateLimitReached() {
ngDialog.open({
showClose: false,
template: 'views/common/modal.html',
controller: ['$scope', function ($scope) {
var urlGH = 'https://api.github.com/repos/Soundnode/soundnode-about/contents/rate-limit-reached.html';
var config = {
headers: {
'Accept': 'application/vnd.github.v3.raw+json'
}
};

$scope.content = '';

$scope.closeModal = function () {
ngDialog.closeAll();
};

$http.get(urlGH, config)
.success(function (data) {
$scope.content = data;
})
.error(function (error) {
console.log('Error', error)
});
}]
});
}
app.service('SCapiService', function (
$http,
$window,
$q,
$log,
$state,
$stateParams,
$rootScope,
rateLimit
) {

/**
* Responsible to store next url for pagination request
Expand All @@ -56,7 +36,7 @@ app.service('SCapiService', function ($http, $window, $q, $log, $state, $statePa
return $http.get(url)
.then(function (response, status) {
if (response.status === 429) {
rateLimitReached();
rateLimit.showNotification();
return [];
}

Expand Down Expand Up @@ -419,5 +399,71 @@ app.service('SCapiService', function ($http, $window, $q, $log, $state, $statePa
});
};

/**
* Get ids of all user reposts
* @param {string} next_href - next page of ids, coming from recursive call
* @return {promise}
*/
this.getRepostsIds = function(next_href) {
var url = next_href || 'https://api.soundcloud.com/e1/me/track_reposts/ids?linked_partitioning=1&limit=200&oauth_token=' + $window.scAccessToken;
var that = this;
return $http.get(url)
.then(function (response) {
if (!angular.isObject(response.data)) {
return $q.reject(response.data);
}
if (response.data.hasOwnProperty('next_href')) {
// call itself to get all repost ids
return that.getRepostsIds(response.data.next_href)
.then(function (next_href_response) {
// sums all ids
return response.data.collection.concat(next_href_response.collection);
});
}
return response.data.collection;

})
.catch(function (response) {
return $q.reject(response.data);
});
};

/**
* Create repost for a track
* @param {string} songId
* @return {promise}
*/
this.createRepost = function (songId) {
var url = 'https://api.soundcloud.com/e1/me/track_reposts/' + songId + '?&oauth_token=' + $window.scAccessToken;
return $http.put(url)
.then(function (response) {
if (!angular.isObject(response.data)) {
return $q.reject(response.data);
}
return response.data;
})
.catch(function (response) {
return $q.reject(response.data);
});
};

/**
* Delete repost for a track
* @param {string} songId
* @return {promise}
*/
this.deleteRepost = function (songId) {
var url = 'https://api.soundcloud.com/e1/me/track_reposts/' + songId + '?&oauth_token=' + $window.scAccessToken;
return $http.delete(url)
.then(function (response) {
if (!angular.isObject(response.data)) {
return $q.reject(response.data);
}
return response.data;
})
.catch(function (response) {
return $q.reject(response.data);
});
};

});
4 changes: 0 additions & 4 deletions app/public/js/common/favoriteSongDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

app.directive('favoriteSong', function(
$rootScope,
$log,
SCapiService,
$timeout,
$state,
$stateParams,
notificationFactory
) {
return {
Expand Down
21 changes: 21 additions & 0 deletions app/public/js/common/queueCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,27 @@ app.controller('QueueCtrl', function(
});
};

/**
* repost track from queue
* @param {object} $event - Angular event
* @return {promise}
*/
$scope.repost = function ($event) {
var trackData = $($event.target).closest('.queueListView_list_item').data();
var songId = trackData.songId;

return SCapiService.createRepost(songId)
.then(function (status) {
if (angular.isObject(status)) {
notificationFactory.success('Song added to reposts!');
utilsService.markTrackAsReposted(songId);
}
})
.catch(function (status) {
notificationFactory.error('Something went wrong!');
});
};

/**
* add track to playlist
* @param $event
Expand Down
37 changes: 37 additions & 0 deletions app/public/js/common/rateLimitService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

// Displays popup with a warning that rate limit is reached, with a link
// to SoundCloud docs attached. Call it when response returns 429 status
app.service('rateLimit', function (
$http,
ngDialog
) {
this.showNotification = function () {
return ngDialog.open({
showClose: false,
template: 'views/common/modal.html',
controller: ['$scope', function ($scope) {
var urlGH = 'https://api.github.com/repos/Soundnode/soundnode-about/contents/rate-limit-reached.html';
var config = {
headers: {
'Accept': 'application/vnd.github.v3.raw+json'
}
};

$scope.content = '';

$scope.closeModal = function () {
ngDialog.closeAll();
};

$http.get(urlGH, config)
.then(function (response) {
$scope.content = response.data;
})
.catch(function (response) {
console.log('Error', response.data);
});
}]
});
};
});
Loading