Skip to content

Commit

Permalink
only display delete option if track was added by current user
Browse files Browse the repository at this point in the history
  • Loading branch information
edoparearyee committed Aug 4, 2015
1 parent 6c18ad1 commit b89d9bd
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<script src="js/auth/spotify.js"></script>
<script src="js/auth/controllers/LoginGoogleCtrl.js"></script>
<script src="js/auth/controllers/LoginSpotifyCtrl.js"></script>
<script src="js/auth/services/GoogleAuthService.js"></script>
<script src="js/auth/services/SpotifyAuthService.js"></script>

<script src="js/alert/alert.js"></script>
Expand Down
4 changes: 4 additions & 0 deletions app/js/api/factories/UsersResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ angular.module("FM.api.UsersResource",[
id: "@id"
},
{
current: {
method: "GET",
url: env.FM_API_SERVER_ADDRESS + "users" + "/:id/authenticated"
},
stats: {
method: "GET",
url: env.FM_API_SERVER_ADDRESS + "users" + "/:id/stats"
Expand Down
16 changes: 11 additions & 5 deletions app/js/auth/controllers/LoginGoogleCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @author SOON_
*/
angular.module("FM.auth.LoginGoogleCtrl", [
"FM.auth.GoogleAuthService",
"FM.alert",
"satellizer",
"FM.api.ERRORS"
Expand All @@ -20,15 +21,17 @@ angular.module("FM.auth.LoginGoogleCtrl", [
"$auth",
"ERRORS",
"AlertService",
"GoogleAuthService",
/**
* @constructor
* @param {Object} $scope
* @param {Service} $route
* @param {Service} $auth satellizer $auth service
* @param {Object} ERRORS error message copy
* @param {Object} AlertService Service which displays errors in the player
* @param {Service} $auth satellizer $auth service
* @param {Object} ERRORS error message copy
* @param {Object} AlertService Service which displays errors in the player
* @param {Object} GoogleAuthService Stores google auth data
*/
function ($scope, $route, $auth, ERRORS, AlertService) {
function ($scope, $route, $auth, ERRORS, AlertService, GoogleAuthService) {

/**
* Authenticate with google oauth
Expand All @@ -37,7 +40,10 @@ angular.module("FM.auth.LoginGoogleCtrl", [
$scope.authenticate = function authenticate() {

$auth.authenticate("google")
.then($route.reload)
.then(function(){
GoogleAuthService.loadUser();
$route.reload();
})
.catch(function (error){
// Satellizer returns an error if there is no token, parse the error to get the original API response
var response = JSON.parse(error.message.match(/\{.*\}/));
Expand Down
8 changes: 7 additions & 1 deletion app/js/auth/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
angular.module("FM.auth.google", [
"FM.auth.LoginGoogleCtrl",
"FM.auth.GoogleAuthService",
"config",
"satellizer"
])
Expand Down Expand Up @@ -42,7 +43,8 @@ angular.module("FM.auth.google", [
.run([
"$rootScope",
"$auth",
function ($rootScope, $auth){
"GoogleAuthService",
function ($rootScope, $auth, GoogleAuthService){

/**
* Set isAuthenticatedGoogle if token exists
Expand All @@ -52,5 +54,9 @@ angular.module("FM.auth.google", [
return Boolean($auth.getToken());
};

if ($rootScope.isAuthenticatedGoogle()){
GoogleAuthService.loadUser();
}

}
]);
58 changes: 58 additions & 0 deletions app/js/auth/services/GoogleAuthService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"use strict";
/**
* Authentication service for google login
* @module FM.auth.GoogleAuthService
* @author SOON_
*/
angular.module("FM.auth.GoogleAuthService", [
"FM.api.UsersResource",
"satellizer"
])
/**
* @class GoogleAuthService
* @param {Service} $q
* @param {Factory} UsersResource
*/
.service("GoogleAuthService", [
"$rootScope",
"$q",
"$auth",
"UsersResource",
function ($rootScope, $q, $auth, UsersResource){

/**
* @private
* @property user
* @type {Object}
*/
var user = null;

/**
* @public
* @method loadUser
* @return {Promise} The current user object
*/
this.loadUser = function loadUser() {
var deferred = $q.defer();

UsersResource.current().$promise
.then(function (response) {
user = response;
deferred.resolve(response);
})
.catch(deferred.reject);

return deferred.promise;
};

/**
* @public
* @method getUser
* @return {Object} The current user object
*/
this.getUser = function getUser() {
return user;
};

}
]);
19 changes: 18 additions & 1 deletion app/js/player/directives/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
angular.module("FM.player.trackDirective", [
"FM.player.removeLeadingZeros",
"FM.api.PlayerQueueResource",
"FM.auth.GoogleAuthService",
"ui.bootstrap.popover",
"template/popover/popover-template.html",
"template/popover/popover.html",
Expand All @@ -21,7 +22,8 @@ angular.module("FM.player.trackDirective", [
*/
.directive("fmTrack",[
"PlayerQueueResource",
function (PlayerQueueResource){
"GoogleAuthService",
function (PlayerQueueResource, GoogleAuthService){
return {
restrict: "EA",
scope: {
Expand All @@ -35,6 +37,21 @@ angular.module("FM.player.trackDirective", [
templateUrl: "partials/track.html",
link: function($scope){

/**
* @property currentUser
* @type {Object}
*/
$scope.currentUser = GoogleAuthService.getUser();

/**
* Whether the track has been added by the current user
* @property addedByCurrent
* @type {Boolean}
*/
$scope.addedByCurrent = ($scope.currentUser && $scope.user && $scope.currentUser.id && $scope.user.id ) ?
($scope.currentUser.id === $scope.user.id) :
false;

/**
* Remove track from queue
* @method removeTrack
Expand Down
2 changes: 1 addition & 1 deletion app/partials/track.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</a>
<ul class="dropdown-menu" role="menu">
<li><a href ng-click="addToPlaylist(track)">Add to playlist</a></li>
<li><a href ng-click="removeTrack(id)" ng-if="id" class="option-delete">Delete from queue</a></li>
<li><a href ng-click="removeTrack(id)" ng-if="id && addedByCurrent" class="option-delete">Delete from queue</a></li>
</ul>
</div>

Expand Down
1 change: 1 addition & 0 deletions scripts.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"./app/js/auth/spotify.js",
"./app/js/auth/controllers/LoginGoogleCtrl.js",
"./app/js/auth/controllers/LoginSpotifyCtrl.js",
"./app/js/auth/services/GoogleAuthService.js",
"./app/js/auth/services/SpotifyAuthService.js",

"./app/js/alert/alert.js",
Expand Down

0 comments on commit b89d9bd

Please sign in to comment.