jdkjskdj
Some checks failed
Build and Deploy Nuxt / build (push) Successful in 1m10s
Test / test (push) Failing after 2s

This commit is contained in:
2026-10-19 19:42:10 +02:00
parent 1c98af69b2
commit b83f64ff9e
14 changed files with 299 additions and 9 deletions

View File

@@ -8,12 +8,11 @@ const serviceLinks = require('../services/service-links');
// Every service links to a campaign through the same sign-up link
// (routes/service-link.js); a campaign only drops one of them again.
async function unlinkService(campaign, service) {
if (service === 'foundry') {
campaign.foundryWorldId = undefined;
campaign.foundryWorldTitle = undefined;
campaign.foundrySystem = undefined;
campaign.set('foundrySyncTokenHash', undefined);
} else if (service === 'discord') {
// A Foundry world can also let the campaign go from the module itself
// (routes/foundry-link.js), so both go through the same helper.
if (service === 'foundry') return serviceLinks.unlinkFoundryWorld(campaign);
if (service === 'discord') {
// The Discord account the bot answers to stays: the server is simply
// left without a campaign until somebody picks one again.
campaign.discordGuildId = undefined;

View File

@@ -14,6 +14,7 @@ const { foundryActorToCharacter } = require('../services/actor-foundry');
const { foundryItemToItemData } = require('../services/item-foundry');
const { foundrySceneToMapData } = require('../services/map-foundry');
const { joinFoundryAssetUrl } = require('../services/foundry-icons');
const serviceLinks = require('../services/service-links');
// Upper bound for the Foundry names remembered per synced item (see Item model).
const MAX_FOUNDRY_NAMES = 20;
@@ -698,6 +699,25 @@ router.post('/sync/map-note', async (req, res) => {
}
});
// A world letting its campaign go from the module settings: the campaign forgets
// the world and the token this request used stops working, so the world is free
// to pair again — and the campaign's settings stop listing it as connected.
router.post('/sync/unlink', async (req, res) => {
try {
const campaign = await syncCampaign(req, res);
if (!campaign) return;
if (!requestMatchesWorld(campaign, req.body.worldId)) {
return res.json({ status: 'error', msg: 'errors.missing-data' });
}
await serviceLinks.unlinkFoundryWorld(campaign);
res.json({ status: 'ok' });
} catch (err) {
console.error(err);
res.json({ status: 'error', msg: 'errors.internal' });
}
});
module.exports = router;
// The writers every Foundry document goes through: the sync pushes above, and the
// force import a campaign asks for from its sidebar (routes/foundry.js). One set of

View File

@@ -165,7 +165,9 @@ async function startServiceLink(service, target) {
async function findServiceLinkByCode(code) {
if (!code) return null;
return ServiceLinkRequest.findOne({ codeHash: codeHash(code) }).select('+credentials').lean();
// Not `.lean()`: the link answered by /complete is saved back with its new
// state, and a plain object has no document behind it to save.
return ServiceLinkRequest.findOne({ codeHash: codeHash(code) }).select('+credentials');
}
// The credential that belongs to a link is handed over once and then dropped:
@@ -211,6 +213,18 @@ async function revokeServiceLink(service, campaignId) {
return result.matchedCount;
}
// A campaign letting a Foundry world go: it forgets the world, which frees the
// world to pair with a campaign again, and the token the world still holds stops
// working the next time it asks.
async function unlinkFoundryWorld(campaign) {
campaign.foundryWorldId = undefined;
campaign.foundryWorldTitle = undefined;
campaign.foundrySystem = undefined;
campaign.set('foundrySyncTokenHash', undefined);
await campaign.save();
await revokeServiceLink('foundry', campaign._id);
}
// What a campaign has connected to it, newest link first and one row per
// service, so the campaign settings can list its services together.
async function listLinkedServices(campaignId) {
@@ -254,5 +268,6 @@ module.exports = {
serviceName,
signUpLinkUrl,
startServiceLink,
takeServiceLinkCredentials
takeServiceLinkCredentials,
unlinkFoundryWorld
};