2020-11-18 19:53:35 +00:00
|
|
|
const router = require('express').Router();
|
|
|
|
const mongoose = require('mongoose');
|
|
|
|
const TrackData = mongoose.model('TrackData');
|
|
|
|
const Track = mongoose.model('Track');
|
|
|
|
const Comment = mongoose.model('Comment');
|
|
|
|
const User = mongoose.model('User');
|
|
|
|
const auth = require('../auth');
|
|
|
|
const currentTracks = new Map();
|
2020-11-21 22:07:44 +00:00
|
|
|
const { parseTrackPoints } = require('../../logic/tracks');
|
2020-11-20 10:30:12 +00:00
|
|
|
const wrapRoute = require('../../_helpers/wrapRoute');
|
2020-11-18 22:00:08 +00:00
|
|
|
|
2020-04-13 00:02:40 +00:00
|
|
|
// Preload track objects on routes with ':track'
|
2020-11-18 22:00:08 +00:00
|
|
|
router.param('track', async (req, res, next, slug) => {
|
|
|
|
try {
|
|
|
|
const track = await Track.findOne({ slug }).populate('author');
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
if (!track) {
|
|
|
|
return res.sendStatus(404);
|
|
|
|
}
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
req.track = track;
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
return next();
|
|
|
|
} catch (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2020-04-13 00:02:40 +00:00
|
|
|
});
|
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
router.param('comment', async (req, res, next, id) => {
|
|
|
|
try {
|
|
|
|
const comment = await Comment.findById(id);
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
if (!comment) {
|
|
|
|
return res.sendStatus(404);
|
|
|
|
}
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
req.comment = comment;
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
return next();
|
|
|
|
} catch (err) {
|
|
|
|
return next(err);
|
2020-04-13 00:02:40 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
router.get(
|
|
|
|
'/',
|
|
|
|
auth.optional,
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
const query = {};
|
|
|
|
let limit = 20;
|
|
|
|
let offset = 0;
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
if (typeof req.query.limit !== 'undefined') {
|
|
|
|
limit = req.query.limit;
|
|
|
|
}
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
if (typeof req.query.offset !== 'undefined') {
|
|
|
|
offset = req.query.offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof req.query.tag !== 'undefined') {
|
|
|
|
query.tagList = { $in: [req.query.tag] };
|
|
|
|
}
|
|
|
|
|
|
|
|
const [author, favoriter] = await Promise.all([
|
|
|
|
req.query.author ? User.findOne({ username: req.query.author }) : null,
|
|
|
|
req.query.favorited ? User.findOne({ username: req.query.favorited }) : null,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (author) {
|
|
|
|
query.author = author._id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (favoriter) {
|
|
|
|
query._id = { $in: favoriter.favorites };
|
|
|
|
} else if (req.query.favorited) {
|
|
|
|
query._id = { $in: [] };
|
|
|
|
}
|
|
|
|
|
|
|
|
const results = await Promise.all([
|
|
|
|
Track.find(query)
|
|
|
|
.limit(Number(limit))
|
|
|
|
.skip(Number(offset))
|
|
|
|
.sort({ createdAt: 'desc' })
|
|
|
|
.populate('author')
|
|
|
|
.where('visible')
|
|
|
|
.equals(true)
|
|
|
|
.exec(),
|
|
|
|
Track.countDocuments(query).exec(),
|
|
|
|
req.payload ? User.findById(req.payload.id) : null,
|
|
|
|
]);
|
|
|
|
|
|
|
|
const [tracks, tracksCount, user] = results;
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
tracks: tracks.map((track) => track.toJSONFor(user)),
|
|
|
|
tracksCount,
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
router.get(
|
|
|
|
'/feed',
|
|
|
|
auth.required,
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
let limit = 20;
|
|
|
|
let offset = 0;
|
|
|
|
|
|
|
|
if (typeof req.query.limit !== 'undefined') {
|
|
|
|
limit = req.query.limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof req.query.offset !== 'undefined') {
|
|
|
|
offset = req.query.offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await User.findById(req.payload.id);
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-18 19:53:35 +00:00
|
|
|
if (!user) {
|
|
|
|
return res.sendStatus(401);
|
2020-04-13 00:02:40 +00:00
|
|
|
}
|
2020-11-18 22:00:08 +00:00
|
|
|
|
|
|
|
const showByUserIds = [req.payload.id, ...(user.following || [])];
|
|
|
|
|
|
|
|
const [tracks, tracksCount] = await Promise.all([
|
|
|
|
Track.find({ author: { $in: showByUserIds } })
|
|
|
|
.limit(Number(limit))
|
|
|
|
.skip(Number(offset))
|
|
|
|
.populate('author')
|
|
|
|
.exec(),
|
|
|
|
Track.countDocuments({ author: { $in: showByUserIds } }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
tracks: tracks.map(function (track) {
|
|
|
|
return track.toJSONFor(user);
|
|
|
|
}),
|
|
|
|
tracksCount: tracksCount,
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
router.post(
|
|
|
|
'/',
|
|
|
|
auth.required,
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
const user = await User.findById(req.payload.id);
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
return res.sendStatus(401);
|
2020-04-13 00:02:40 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
const track = new Track(req.body.track);
|
|
|
|
const trackData = new TrackData();
|
|
|
|
track.trackData = trackData._id;
|
2020-07-03 11:50:07 +00:00
|
|
|
|
2020-11-21 16:49:38 +00:00
|
|
|
if (req.body.track.body && req.body.track.body.trim()) {
|
2020-11-21 22:07:44 +00:00
|
|
|
trackData.points = Array.from(parseTrackPoints(track.body));
|
2020-11-21 12:47:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
track.author = user;
|
|
|
|
track.visible = track.author.areTracksVisibleForAll;
|
|
|
|
await trackData.save();
|
2020-07-03 11:50:07 +00:00
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
await track.save();
|
2020-04-14 01:05:07 +00:00
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
// console.log(track.author);
|
|
|
|
return res.json({ track: track.toJSONFor(user) });
|
|
|
|
}),
|
|
|
|
);
|
2020-11-17 16:59:05 +00:00
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
router.post(
|
2020-11-22 09:44:21 +00:00
|
|
|
'/begin',
|
2020-11-18 22:00:08 +00:00
|
|
|
auth.optional,
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
const user = await User.findById(req.body.id);
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
return res.sendStatus(401);
|
|
|
|
}
|
2020-04-14 01:05:07 +00:00
|
|
|
|
2020-11-22 09:44:21 +00:00
|
|
|
const track = new Track(req.body.track);
|
|
|
|
const trackData = new TrackData();
|
|
|
|
track.trackData = trackData._id;
|
|
|
|
track.author = user;
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-22 09:44:21 +00:00
|
|
|
// remember which is the actively building track for this user
|
|
|
|
currentTracks.set(user.id, track._id);
|
2020-11-21 22:01:22 +00:00
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
return res.sendStatus(200);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
router.post(
|
2020-11-22 09:44:21 +00:00
|
|
|
'/add',
|
2020-11-18 22:00:08 +00:00
|
|
|
auth.optional,
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
const user = await User.findById(req.body.id);
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
return res.sendStatus(401);
|
|
|
|
}
|
|
|
|
|
2020-11-22 09:44:21 +00:00
|
|
|
if (!currentTracks.has(user.id)) {
|
|
|
|
throw new Error('current user has no active track, start one with POST to /tracks/begin');
|
|
|
|
}
|
2020-11-18 22:00:08 +00:00
|
|
|
|
2020-11-22 09:44:21 +00:00
|
|
|
const trackId = currentTracks.get(user.id);
|
|
|
|
|
|
|
|
const track = await Track.findById(trackId);
|
|
|
|
if (!track) {
|
|
|
|
throw new Error('current user active track is gone, retry upload');
|
|
|
|
}
|
|
|
|
|
|
|
|
track.body += req.body.track.body;
|
|
|
|
await track.save();
|
2020-11-18 22:00:08 +00:00
|
|
|
|
|
|
|
return res.sendStatus(200);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
router.post(
|
|
|
|
'/end',
|
|
|
|
auth.optional,
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
const user = await User.findById(req.body.id);
|
2020-11-21 22:01:22 +00:00
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
if (!user) {
|
|
|
|
return res.sendStatus(401);
|
|
|
|
}
|
|
|
|
|
2020-11-21 22:01:22 +00:00
|
|
|
if (!currentTracks.has(user.id)) {
|
|
|
|
throw new Error('current user has no active track, start one with POST to /tracks/begin');
|
2020-11-18 22:00:08 +00:00
|
|
|
}
|
2020-07-03 11:50:07 +00:00
|
|
|
|
2020-11-21 22:01:22 +00:00
|
|
|
const trackId = currentTracks.get(user.id);
|
2020-11-18 22:00:08 +00:00
|
|
|
|
2020-11-21 22:01:22 +00:00
|
|
|
const track = await Track.findById(trackId);
|
|
|
|
if (!track) {
|
|
|
|
throw new Error('current user active track is gone, retry upload');
|
|
|
|
}
|
|
|
|
|
|
|
|
const trackData = await TrackData.findById(track.trackData);
|
2020-11-21 22:07:44 +00:00
|
|
|
trackData.points = Array.from(parseTrackPoints(track.body));
|
2020-11-21 22:01:22 +00:00
|
|
|
await trackData.save();
|
2020-11-18 22:00:08 +00:00
|
|
|
|
2020-11-21 22:01:22 +00:00
|
|
|
// We are done with this track, it is complete.
|
|
|
|
currentTracks.delete(user.id);
|
2020-11-18 22:00:08 +00:00
|
|
|
|
|
|
|
return res.sendStatus(200);
|
|
|
|
}),
|
|
|
|
);
|
2020-04-13 00:02:40 +00:00
|
|
|
|
|
|
|
// return a track
|
2020-11-18 22:00:08 +00:00
|
|
|
router.get(
|
|
|
|
'/:track',
|
|
|
|
auth.optional,
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
const [user] = await Promise.all([
|
|
|
|
req.payload ? User.findById(req.payload.id) : null,
|
|
|
|
req.track.populate('author').execPopulate(),
|
|
|
|
]);
|
|
|
|
return res.json({ track: req.track.toJSONFor(user, { body: true }) });
|
|
|
|
}),
|
|
|
|
);
|
2020-04-13 00:02:40 +00:00
|
|
|
|
|
|
|
// update track
|
2020-11-20 10:30:12 +00:00
|
|
|
router.put(
|
|
|
|
'/:track',
|
|
|
|
auth.required,
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
const user = await User.findById(req.payload.id);
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-20 10:30:12 +00:00
|
|
|
if (req.track.author._id.toString() !== req.payload.id.toString()) {
|
|
|
|
return res.sendStatus(403);
|
|
|
|
}
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-20 10:30:12 +00:00
|
|
|
if (typeof req.body.track.title !== 'undefined') {
|
|
|
|
req.track.title = req.body.track.title;
|
|
|
|
}
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-20 10:30:12 +00:00
|
|
|
if (typeof req.body.track.description !== 'undefined') {
|
|
|
|
req.track.description = req.body.track.description;
|
|
|
|
}
|
2020-11-18 19:53:35 +00:00
|
|
|
|
2020-11-20 10:30:12 +00:00
|
|
|
if (req.body.track.body && req.body.track.body.trim()) {
|
|
|
|
req.track.body = req.body.track.body.trim();
|
2020-11-18 21:17:00 +00:00
|
|
|
|
2020-11-20 10:30:12 +00:00
|
|
|
let trackData = await TrackData.findById(req.track.trackData);
|
|
|
|
if (!trackData) {
|
|
|
|
trackData = new TrackData();
|
|
|
|
req.track.trackData = trackData._id;
|
|
|
|
}
|
2020-11-21 22:07:44 +00:00
|
|
|
trackData.points = Array.from(parseTrackPoints(req.track.body));
|
2020-11-20 10:30:12 +00:00
|
|
|
await trackData.save();
|
2020-04-13 00:02:40 +00:00
|
|
|
}
|
2020-11-18 21:17:00 +00:00
|
|
|
|
2020-11-20 10:30:12 +00:00
|
|
|
if (typeof req.body.track.tagList !== 'undefined') {
|
|
|
|
req.track.tagList = req.body.track.tagList;
|
|
|
|
}
|
|
|
|
req.track.visible = req.body.track.visible;
|
2020-11-18 21:17:00 +00:00
|
|
|
|
2020-11-20 10:30:12 +00:00
|
|
|
const track = await req.track.save();
|
|
|
|
return res.json({ track: track.toJSONFor(user) });
|
|
|
|
}),
|
|
|
|
);
|
2020-04-13 00:02:40 +00:00
|
|
|
|
|
|
|
// delete track
|
2020-11-18 22:00:08 +00:00
|
|
|
router.delete(
|
|
|
|
'/:track',
|
|
|
|
auth.required,
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
const user = await User.findById(req.payload.id);
|
|
|
|
if (!user) {
|
|
|
|
return res.sendStatus(401);
|
|
|
|
}
|
|
|
|
if (req.track.author._id.toString() === req.payload.id.toString()) {
|
|
|
|
await TrackData.findByIdAndDelete(req.track.trackData);
|
|
|
|
await req.track.remove();
|
|
|
|
return res.sendStatus(204);
|
|
|
|
} else {
|
|
|
|
return res.sendStatus(403);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
2020-04-13 00:02:40 +00:00
|
|
|
|
|
|
|
// Favorite an track
|
2020-11-18 22:00:08 +00:00
|
|
|
router.post(
|
|
|
|
'/:track/favorite',
|
|
|
|
auth.required,
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
const trackId = req.track._id;
|
|
|
|
|
|
|
|
const user = await User.findById(req.payload.id);
|
|
|
|
if (!user) {
|
|
|
|
return res.sendStatus(401);
|
|
|
|
}
|
|
|
|
|
|
|
|
await user.favorite(trackId);
|
|
|
|
const track = await req.track.updateFavoriteCount();
|
|
|
|
return res.json({ track: track.toJSONFor(user) });
|
|
|
|
}),
|
|
|
|
);
|
2020-04-13 00:02:40 +00:00
|
|
|
|
|
|
|
// Unfavorite an track
|
2020-11-18 22:00:08 +00:00
|
|
|
router.delete(
|
|
|
|
'/:track/favorite',
|
|
|
|
auth.required,
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
const trackId = req.track._id;
|
|
|
|
|
|
|
|
const user = await User.findById(req.payload.id);
|
|
|
|
if (!user) {
|
|
|
|
return res.sendStatus(401);
|
|
|
|
}
|
|
|
|
|
|
|
|
await user.unfavorite(trackId);
|
|
|
|
const track = await req.track.updateFavoriteCount();
|
|
|
|
return res.json({ track: track.toJSONFor(user) });
|
|
|
|
}),
|
|
|
|
);
|
2020-04-13 00:02:40 +00:00
|
|
|
|
|
|
|
// return an track's comments
|
2020-11-18 22:00:08 +00:00
|
|
|
router.get(
|
|
|
|
'/:track/comments',
|
|
|
|
auth.optional,
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
const user = await Promise.resolve(req.payload ? User.findById(req.payload.id) : null);
|
|
|
|
|
|
|
|
await req.track
|
|
|
|
.populate({
|
|
|
|
path: 'comments',
|
|
|
|
populate: {
|
|
|
|
path: 'author',
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
sort: {
|
|
|
|
createdAt: 'desc',
|
2020-11-18 19:53:35 +00:00
|
|
|
},
|
2020-11-18 22:00:08 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
.execPopulate();
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
comments: req.track.comments.map(function (comment) {
|
|
|
|
return comment.toJSONFor(user);
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
);
|
2020-04-13 00:02:40 +00:00
|
|
|
|
|
|
|
// create a new comment
|
2020-11-18 22:00:08 +00:00
|
|
|
router.post(
|
|
|
|
'/:track/comments',
|
|
|
|
auth.required,
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
const user = await User.findById(req.payload.id);
|
|
|
|
if (!user) {
|
|
|
|
return res.sendStatus(401);
|
|
|
|
}
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-18 22:00:08 +00:00
|
|
|
const comment = new Comment(req.body.comment);
|
|
|
|
comment.track = req.track;
|
|
|
|
comment.author = user;
|
|
|
|
|
|
|
|
await comment.save();
|
|
|
|
|
|
|
|
req.track.comments.push(comment);
|
|
|
|
|
|
|
|
await req.track.save();
|
|
|
|
return res.json({ comment: comment.toJSONFor(user) });
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
router.delete(
|
|
|
|
'/:track/comments/:comment',
|
|
|
|
auth.required,
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
if (req.comment.author.toString() === req.payload.id.toString()) {
|
|
|
|
req.track.comments.remove(req.comment._id);
|
|
|
|
await req.track.save();
|
|
|
|
await Comment.find({ _id: req.comment._id }).remove();
|
|
|
|
res.sendStatus(204);
|
|
|
|
} else {
|
|
|
|
res.sendStatus(403);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-08-14 20:15:12 +00:00
|
|
|
// return an track's trackData
|
2020-11-18 22:00:08 +00:00
|
|
|
router.get(
|
|
|
|
'/:track/TrackData',
|
|
|
|
auth.optional,
|
|
|
|
wrapRoute(async (req, res) => {
|
|
|
|
// console.log("requestTrackData"+req.track);
|
|
|
|
const trackData = await TrackData.findById(req.track.trackData);
|
|
|
|
// console.log({trackData: trackData});
|
|
|
|
return res.json({ trackData: trackData });
|
|
|
|
}),
|
|
|
|
);
|
2020-08-14 20:15:12 +00:00
|
|
|
|
2020-04-13 00:02:40 +00:00
|
|
|
module.exports = router;
|