Fix issue #20: Add notes to a map
This commit is contained in:
@@ -5,6 +5,7 @@ const upload = require('../services/storage');
|
||||
|
||||
const Campaign = require('../models/Campaign');
|
||||
const Map = require('../models/Map');
|
||||
const Note = require('../models/Note');
|
||||
|
||||
async function userOwnsCampaign(campaignId, userId) {
|
||||
const campaign = await Campaign.findOne({ _id: campaignId, createdBy: userId }).lean();
|
||||
@@ -92,4 +93,63 @@ router.post('/delete', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// A pin is a note placed at a spot of the map, in image pixels. Coordinates arrive as strings with
|
||||
// multipart requests and are never negative: the viewer clamps them to the image already.
|
||||
function toCoordinate(value) {
|
||||
const number = Number(value);
|
||||
if (!Number.isFinite(number)) return 0;
|
||||
return Math.max(0, Math.round(number));
|
||||
}
|
||||
|
||||
router.post('/pin', async (req, res) => {
|
||||
try {
|
||||
const { id, note, x, y } = req.body;
|
||||
if (!id || !note) return res.json({ status: 'error', msg: 'errors.missing-data' });
|
||||
|
||||
const map = await Map.findById(id);
|
||||
if (!map) return res.json({ status: 'error', msg: 'errors.notfound' });
|
||||
if (!(await userOwnsCampaign(map.campaign, req.user.id))) return res.json({ status: 'error', msg: 'unauthorized' });
|
||||
|
||||
// Only a note of the same campaign can be pinned onto one of its maps.
|
||||
const noteDoc = await Note.findOne({ _id: note, campaign: map.campaign }).select('_id').lean();
|
||||
if (!noteDoc) return res.json({ status: 'error', msg: 'errors.notfound' });
|
||||
|
||||
// A note is pinned once per map: pinning it again moves the marker instead of adding a twin.
|
||||
const pin = map.pins.find((current) => String(current.note) === String(note));
|
||||
if (pin) {
|
||||
pin.x = toCoordinate(x);
|
||||
pin.y = toCoordinate(y);
|
||||
} else {
|
||||
map.pins.push({ note, x: toCoordinate(x), y: toCoordinate(y) });
|
||||
}
|
||||
|
||||
await map.save();
|
||||
res.json({ status: 'ok', map });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.json({ status: 'error', msg: 'errors.internal' });
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/unpin', async (req, res) => {
|
||||
try {
|
||||
const { id, pin } = req.body;
|
||||
if (!id || !pin) return res.json({ status: 'error', msg: 'errors.missing-data' });
|
||||
|
||||
const map = await Map.findById(id);
|
||||
if (!map) return res.json({ status: 'error', msg: 'errors.notfound' });
|
||||
if (!(await userOwnsCampaign(map.campaign, req.user.id))) return res.json({ status: 'error', msg: 'unauthorized' });
|
||||
|
||||
// `_id` stays with the pins that remain: the viewer removes them by that id.
|
||||
map.pins = map.pins
|
||||
.filter((current) => String(current._id) !== String(pin))
|
||||
.map((current) => ({ _id: current._id, note: current.note, x: current.x, y: current.y }));
|
||||
await map.save();
|
||||
res.json({ status: 'ok', map });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.json({ status: 'error', msg: 'errors.internal' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user