'use strict'; /* * The alliance's shared report folder. Two commands in one file (registered separately in commands/index.js): * reportShare { userId, reportId } put one of YOUR reports into your alliance's folder (needs `shareReport`) * reportUnshare { userId, reportId } take it out again: whoever shared it, or `unshareReport` (leader, officer) * The alliance is the caller's alliance in the report's world. Sharing an already shared report changes nothing * ({ changed: false }). A shared report lives at least reportRetentionMs from the moment it was shared (the expiry is pushed * out; it is one row for everybody, so the participants keep it that long too) and is deleted with the report. */ const R = require('../../public/js/rules.js'); const D = require('../../public/js/data.js'); const { GameError } = require('../errors'); const A = require('../alliances'); const { ensureCleanup } = require('../military/reports'); function context(db, userId, reportId) { const rep = db.get(`SELECT r.id, r.world_id, r.expires_at FROM reports r JOIN report_recipients rr ON rr.report_id = r.id AND rr.user_id = ? WHERE r.id = ?`, userId, reportId); if (!rep) throw new GameError('not_found', { what: 'report' }); // not yours: never reveal it exists const op = db.get('SELECT * FROM operators WHERE user_id = ? AND world_id = ?', userId, rep.world_id); if (!op) throw new GameError('not_found', { what: 'report' }); const m = A.membership(db, op.id); if (!m) throw new GameError('not_in_alliance'); return { rep, op, member: m }; } function reportShare(ctx, { userId, reportId }) { const { db, now } = ctx; const { rep, op, member } = context(db, userId, reportId); if (!R.allianceCan(member.role, 'shareReport')) throw new GameError('forbidden', { need: 'shareReport', role: member.role }); const r = db.run('INSERT OR IGNORE INTO report_shares (report_id, alliance_id, shared_by, created_at) VALUES (?, ?, ?, ?)', rep.id, member.alliance_id, op.id, now); if (!r.changes) return { changed: false, reportId: rep.id, allianceId: member.alliance_id }; const until = now + D.MILITARY.reportRetentionMs; if (rep.expires_at < until) db.run('UPDATE reports SET expires_at = ? WHERE id = ?', until, rep.id); ensureCleanup(ctx, rep.world_id, now); return { changed: true, reportId: rep.id, allianceId: member.alliance_id }; } function reportUnshare(ctx, { userId, reportId }) { const { db } = ctx; // the report may no longer list the caller as a recipient (it is only theirs through the alliance): look the share up directly const share = db.get(`SELECT s.*, r.world_id FROM report_shares s JOIN reports r ON r.id = s.report_id WHERE s.report_id = ? AND s.alliance_id IN (SELECT m.alliance_id FROM alliance_members m JOIN operators o ON o.id = m.operator_id WHERE o.user_id = ?)`, reportId, userId); if (!share) throw new GameError('not_found', { what: 'shared report' }); const op = db.get('SELECT * FROM operators WHERE user_id = ? AND world_id = ?', userId, share.world_id); const member = A.membership(db, op.id); if (op.id !== share.shared_by && !R.allianceCan(member.role, 'unshareReport')) throw new GameError('forbidden', { need: 'unshareReport', role: member.role }); db.run('DELETE FROM report_shares WHERE report_id = ? AND alliance_id = ?', share.report_id, share.alliance_id); return { changed: true, reportId: share.report_id, allianceId: share.alliance_id }; } module.exports = { reportShare, reportUnshare };