const mongoose = require('mongoose'); const Schema = mongoose.Schema; // One row per service installation that wants to use a campaign: a Foundry // world, a Discord server, an Obsidian vault. Asking for a link resets the row // to a fresh pending request, so a service always has one row and that row is // also the record of the link that is active right now. // // The service starts the request and shows the sign-up link it gets back; a // Dragonroll user then signs in through that link and picks the campaign. The // service picks its credential up with the same code it was given. const ServiceLinkRequestSchema = new Schema({ service: { type: String, enum: ['foundry', 'discord', 'obsidian'], required: true }, // Who is asking, in its own terms: Foundry world id, Discord guild id, vault id. targetId: { type: String }, targetName: { type: String }, // The person who asked, as far as the service knows: GM name, Discord user... targetUser: { type: String }, // Whatever else the service describes itself with (Foundry system and URL). targetMeta: { type: Object }, codeHash: { type: String, required: true, unique: true }, campaign: { type: Schema.Types.ObjectId, ref: 'Campaign' }, dragonrollUser: { type: Schema.Types.ObjectId, ref: 'User' }, // 'pending' until a Dragonroll user picks a campaign through the sign-up // link, 'linked' while that choice stands, 'revoked' once the campaign // unlinked the service again. state: { type: String, enum: ['pending', 'linked', 'revoked'], default: 'pending' }, // What the service gets when it picks the link up: the Foundry sync token, // the Obsidian session. Dropped as soon as it has been delivered. credentials: { type: Object, select: false }, createdAt: { type: Date, default: Date.now }, // Only a request nobody answers expires: an empty value keeps the link. expiresAt: { type: Date }, linkedAt: { type: Date }, pickedUpAt: { type: Date }, // The Discord bot has confirmed this authorization in Discord. notifiedAt: { type: Date } }); ServiceLinkRequestSchema.index({ expiresAt: 1 }, { expireAfterSeconds: 0 }); ServiceLinkRequestSchema.index({ service: 1, targetId: 1 }); ServiceLinkRequestSchema.index({ campaign: 1, service: 1, state: 1 }); module.exports = mongoose.model('ServiceLinkRequest', ServiceLinkRequestSchema);