feat: show publicTrackData if track is visible but you are not author, change /api/tracks/:slug/TrackData to .../data

This commit is contained in:
Paul Bienkowski 2020-12-02 18:29:39 +01:00
parent a198cb620b
commit 028d224726
2 changed files with 14 additions and 2 deletions

View file

@ -53,6 +53,10 @@ class Track extends mongoose.Model {
return false;
}
isVisibleToPrivate(user) {
return user._id.equals(this.author._id);
}
/**
* Fills the trackData and publicTrackData with references to correct
* TrackData objects. For now, this is either the same, or publicTrackData

View file

@ -414,14 +414,22 @@ router.delete(
// return an track's trackData
router.get(
'/:track/TrackData',
'/:track/data',
auth.optional,
wrapRoute(async (req, res) => {
if (!req.track.isVisibleTo(req.user)) {
return res.sendStatus(403);
}
const trackData = await TrackData.findById(req.track.trackData);
let trackData;
if (req.track.isVisibleToPrivate(req.user)) {
trackData = await TrackData.findById(req.track.trackData);
} else if (!req.track.publicTrackData) {
return res.sendStatus(403);
} else {
trackData = await TrackData.findById(req.track.publicTrackData);
}
return res.json({ trackData });
}),