From 1ab316c7745eccd750e0fae584d2526da2659bd4 Mon Sep 17 00:00:00 2001 From: hpcwoess Date: Sun, 16 Aug 2020 23:23:18 +0200 Subject: [PATCH] tracks visible for all --- models/Track.js | 3 +-- models/User.js | 7 ++++--- routes/api/tracks.js | 14 ++++++++++---- routes/api/users.js | 3 +++ 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/models/Track.js b/models/Track.js index 48ec539..b94928e 100644 --- a/models/Track.js +++ b/models/Track.js @@ -36,8 +36,7 @@ TrackSchema.methods.toJSONFor = function(user){ description: this.description, createdAt: this.createdAt, updatedAt: this.updatedAt, - points: this.points, - visibleForAll: user ? user.areTracksVisibleForAll : false, + visibleForAll: this.author ? this.author.areTracksVisibleForAll : false, author: this.author.toProfileJSONFor(user) }; }; diff --git a/models/User.js b/models/User.js index a6f9d78..1b94331 100644 --- a/models/User.js +++ b/models/User.js @@ -47,6 +47,7 @@ UserSchema.methods.toAuthJSON = function(){ token: this.generateJWT(), bio: this.bio, image: this.image, + areTracksVisibleForAll: this.areTracksVisibleForAll, apiKey: this._id }; }; @@ -79,9 +80,9 @@ UserSchema.methods.isFavorite = function(id){ }); }; -//UserSchema.methods.areTracksVisibleForAll = function(id){ -// return this.areTracksVisibleForAll(); -//}; +UserSchema.methods.isTrackVisible = function(id){ + return this.areTracksVisibleForAll(); +}; UserSchema.methods.follow = function(id){ diff --git a/routes/api/tracks.js b/routes/api/tracks.js index 9f029c5..f2f22dd 100644 --- a/routes/api/tracks.js +++ b/routes/api/tracks.js @@ -188,12 +188,18 @@ router.get('/', auth.optional, function(req, res, next) { var tracksCount = results[1]; var user = results[2]; //console.log(tracks); - + var retTracks = []; + for (t of tracks) { + console.log(t); + if (t.author.areTracksVisibleForAll || t.author == user) { + retTracks.push(t); + } + } return res.json({ - tracks: tracks.map(function(track){ - return track.toJSONFor(user); + tracks: retTracks.map(function(track){ + return track.toJSONFor(user); }), - tracksCount: tracksCount + tracksCount: retTracks.length }); }); }).catch(next); diff --git a/routes/api/users.js b/routes/api/users.js index cc2e444..4a28dda 100644 --- a/routes/api/users.js +++ b/routes/api/users.js @@ -29,6 +29,9 @@ router.put('/user', auth.required, function(req, res, next){ if(typeof req.body.user.image !== 'undefined'){ user.image = req.body.user.image; } + if(typeof req.body.user.areTracksVisibleForAll !== 'undefined'){ + user.areTracksVisibleForAll = req.body.user.areTracksVisibleForAll; + } if(typeof req.body.user.password !== 'undefined'){ user.setPassword(req.body.user.password); }