fix: parse tracks when they are fully uploaded in /end request, not before

This commit is contained in:
Paul Bienkowski 2020-11-21 23:01:22 +01:00
parent 13ed2ef936
commit b31d3e0e7d

View file

@ -6,7 +6,6 @@ const Comment = mongoose.model('Comment');
const User = mongoose.model('User'); const User = mongoose.model('User');
const auth = require('../auth'); const auth = require('../auth');
const currentTracks = new Map(); const currentTracks = new Map();
const TrackInfo = require('../../logic/TrackInfo');
const { addPointsToTrack } = require('../../logic/tracks'); const { addPointsToTrack } = require('../../logic/tracks');
const wrapRoute = require('../../_helpers/wrapRoute'); const wrapRoute = require('../../_helpers/wrapRoute');
@ -175,30 +174,26 @@ router.post(
'/add', '/add',
auth.optional, auth.optional,
wrapRoute(async (req, res) => { wrapRoute(async (req, res) => {
// console.log("Add");
// console.log(req.payload);
const user = await User.findById(req.body.id); const user = await User.findById(req.body.id);
if (!user) { if (!user) {
return res.sendStatus(401); return res.sendStatus(401);
} }
let ti = null; if (!currentTracks.has(user.id)) {
if (currentTracks.has(req.body.id)) ti = currentTracks.get(req.body.id); throw new Error('current user has no active track, start one with POST to /tracks/begin');
// console.log("TI" + ti);
// console.log("TILen" + ti.trackData.points.length);
// console.log("TITrack" + ti.track);
// console.log("Body" + req.body.track.body);
if (ti.track) {
addPointsToTrack(ti, req.body.track.body);
// console.log("TLen" + ti.trackData.points.length);
ti.track.author = user;
} }
// await track.save() const trackId = currentTracks.get(user.id);
// return res.json({ track: track.toJSONFor(user) });
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();
return res.sendStatus(200); return res.sendStatus(200);
}), }),
); );
@ -207,32 +202,20 @@ router.post(
'/begin', '/begin',
auth.optional, auth.optional,
wrapRoute(async (req, res) => { wrapRoute(async (req, res) => {
// console.log("Begin");
// console.log(req.payload);
const user = await User.findById(req.body.id); const user = await User.findById(req.body.id);
if (!user) { if (!user) {
return res.sendStatus(401); return res.sendStatus(401);
} }
if (currentTracks.has(req.body.id)) currentTracks.delete(req.body.id); // delete old parts if there are leftovers const track = new Track(req.body.track);
const ti = new TrackInfo(new Track(req.body.track), new TrackData()); const trackData = new TrackData();
ti.track.trackData = ti.trackData._id; track.trackData = trackData._id;
currentTracks.set(req.body.id, ti); track.author = user;
// console.log("addToTrack"+req.body); // remember which is the actively building track for this user
currentTracks.set(user.id, track._id);
addPointsToTrack(ti, ti.track.body);
// console.log("TLen" + ti.track);
// console.log("TLen" + ti.trackData);
// console.log("TLen" + ti.trackData.points.length);
// console.log(track.trackData.points[0].date);
ti.track.author = user;
// await track.save()
// console.log(track.author);
return res.sendStatus(200); return res.sendStatus(200);
}), }),
); );
@ -242,34 +225,28 @@ router.post(
auth.optional, auth.optional,
wrapRoute(async (req, res) => { wrapRoute(async (req, res) => {
const user = await User.findById(req.body.id); const user = await User.findById(req.body.id);
if (!user) { if (!user) {
return res.sendStatus(401); return res.sendStatus(401);
} }
let ti; if (!currentTracks.has(user.id)) {
if (currentTracks.has(req.body.id)) { throw new Error('current user has no active track, start one with POST to /tracks/begin');
ti = currentTracks.get(req.body.id);
addPointsToTrack(ti, req.body.track.body);
} else {
ti = new TrackInfo(new Track(req.body.track), new TrackData());
ti.track.trackData = ti.trackData._id;
addPointsToTrack(ti, ti.track.body);
}
if (ti.track) {
ti.track.author = user;
} }
currentTracks.delete(req.body.id); // we are done with this track, it is complete const trackId = currentTracks.get(user.id);
ti.track.author = user;
// console.log(track); const track = await Track.findById(trackId);
// console.log("user:"+user); if (!track) {
await ti.track.save(); throw new Error('current user active track is gone, retry upload');
}
// console.log("TLen" + ti.track); const trackData = await TrackData.findById(track.trackData);
// console.log("TLen" + ti.trackData); addPointsToTrack({ trackData }, track.body);
// console.log("TLen" + ti.trackData.points.length); await trackData.save();
await ti.trackData.save();
// We are done with this track, it is complete.
currentTracks.delete(user.id);
return res.sendStatus(200); return res.sendStatus(200);
}), }),