Fix issue #20: Add notes to a map

This commit is contained in:
Gitea OpenCode Agent
2026-10-14 14:46:58 +00:00
parent 9d9738b2b4
commit ba84e10525
16 changed files with 922 additions and 14 deletions

View File

@@ -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;

View File

@@ -4,6 +4,7 @@ const router = express.Router();
const Campaign = require('../models/Campaign');
const Note = require('../models/Note');
const Folder = require('../models/Folder');
const Map = require('../models/Map');
async function userOwnsCampaign(campaignId, userId) {
const campaign = await Campaign.findOne({ _id: campaignId, createdBy: userId }).lean();
@@ -64,6 +65,49 @@ router.get('/subfolder/list', async (req, res) => {
}
});
// Every note of the campaign, folders included, without their content: enough to offer a note
// picker and to label the pins other screens put on a map.
router.get('/all', async (req, res) => {
try {
const { campaign } = req.query;
if (!campaign) return res.json({ status: 'error', msg: 'errors.missing-data' });
const hasAccess = await userOwnsCampaign(campaign, req.user.id);
if (!hasAccess) return res.json({ status: 'error', msg: 'unauthorized' });
const notes = await Note.find({ campaign })
.select('_id title folder date')
.sort({ title: 1 })
.lean();
const folders = await Folder.find({ campaign })
.select('_id name parentFolder')
.lean();
res.json({ status: 'ok', notes, folders });
} catch (err) {
console.error(err);
res.json({ status: 'error', msg: 'errors.internal' });
}
});
// One note with its content, for the screens that show a note on their own (a map pin, for
// instance) instead of from the sidebar's note list.
router.get('/retrieve/:id', async (req, res) => {
try {
const note = await Note.findById(req.params.id).lean();
if (!note) return res.json({ status: 'error', msg: 'errors.notfound' });
const hasAccess = await userOwnsCampaign(note.campaign, req.user.id);
if (!hasAccess) return res.json({ status: 'error', msg: 'unauthorized' });
res.json({ status: 'ok', note });
} catch (err) {
console.error(err);
res.json({ status: 'error', msg: 'errors.internal' });
}
});
router.post('/create', async (req, res) => {
try {
const { title, content, campaign, folder } = req.body;
@@ -119,6 +163,9 @@ router.post('/delete', async (req, res) => {
const result = await Note.deleteOne({ _id: id });
if (result.deletedCount === 0) return res.json({ status: 'error', msg: 'errors.notfound' });
// A pin pointing at a note that is gone would open nothing: drop those along with the note.
await Map.updateMany({ 'pins.note': id }, { $pull: { pins: { note: id } } });
res.json({ status: 'ok' });
} catch (err) {
console.error(err);