update visibility

This commit is contained in:
hpcwoess 2020-08-28 21:50:53 +02:00
parent dbfe62df6a
commit 1e72349026
2 changed files with 11 additions and 6 deletions

View file

@ -9,6 +9,7 @@ var TrackSchema = new mongoose.Schema({
title: String, title: String,
description: String, description: String,
body: String, body: String,
visible: Boolean,
numEvents: {type: Number, default: 0}, numEvents: {type: Number, default: 0},
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }], comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }],
author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
@ -37,6 +38,7 @@ TrackSchema.methods.toJSONFor = function(user){
createdAt: this.createdAt, createdAt: this.createdAt,
updatedAt: this.updatedAt, updatedAt: this.updatedAt,
visibleForAll: this.author ? this.author.areTracksVisibleForAll : false, visibleForAll: this.author ? this.author.areTracksVisibleForAll : false,
visible: this.visible,
author: this.author.toProfileJSONFor(user) author: this.author.toProfileJSONFor(user)
}; };
}; };

View file

@ -180,7 +180,7 @@ router.get('/', auth.optional, function(req, res, next) {
.skip(Number(offset)) .skip(Number(offset))
.sort({createdAt: 'desc'}) .sort({createdAt: 'desc'})
.populate('author') .populate('author')
.where('visibleForAll || author == user') .where('visible').equals(true)
.exec(), .exec(),
Track.countDocuments(query).exec(), Track.countDocuments(query).exec(),
req.payload ? User.findById(req.payload.id) : null, req.payload ? User.findById(req.payload.id) : null,
@ -191,10 +191,10 @@ router.get('/', auth.optional, function(req, res, next) {
//console.log(tracks); //console.log(tracks);
var retTracks = []; var retTracks = [];
for (t of tracks) { for (t of tracks) {
console.log(t); //console.log(t);
if (t.author.areTracksVisibleForAll || t.author == user) { //if (t.author.areTracksVisibleForAll || t.author == user) {
retTracks.push(t); retTracks.push(t);
} //}
} }
return res.json({ return res.json({
tracks: retTracks.map(function(track){ tracks: retTracks.map(function(track){
@ -277,6 +277,7 @@ router.post('/', auth.required, function(req, res, next) {
track.trackData = trackData._id; track.trackData = trackData._id;
track.author = user; track.author = user;
track.visible = track.author.areTracksVisibleForAll;
trackData.save(function (err){ trackData.save(function (err){
if(err){ if(err){
console.log("failed to save trackData"); console.log("failed to save trackData");
@ -422,10 +423,12 @@ router.put('/:track', auth.required, function(req, res, next) {
} }
if(typeof req.body.track.tagList !== 'undefined'){ if(typeof req.body.track.tagList !== 'undefined'){
req.track.tagList = req.body.track.tagList req.track.tagList = req.body.track.tagList;
} }
req.track.visible = req.body.track.visible;
console.log("saving track");
req.body.track.save().then(function(track){ req.track.save().then(function(track){
return res.json({track: track.toJSONFor(user)}); return res.json({track: track.toJSONFor(user)});
}).catch(next); }).catch(next);
} else { } else {