'use strict'; /* POST /messages/blocks { operatorId } and DELETE /messages/blocks/:operatorId. Blocked senders' messages are not * delivered (the sender is not told). Both idempotent. Registered as messageBlock and messageUnblock. */ const { GameError } = require('../errors'); const A = require('../alliances'); function resolve(db, userId, operatorId) { const target = db.get('SELECT * FROM operators WHERE id = ?', operatorId); if (!target) throw new GameError('not_found', { what: 'operator' }); const me = db.get('SELECT * FROM operators WHERE user_id = ? AND world_id = ?', userId, target.world_id); if (!me) throw new GameError('not_found', { what: 'operator' }); if (me.id === target.id) throw new GameError('validation_failed', { errors: [{ path: 'operatorId', message: 'you cannot block yourself' }] }); return { me, target }; } function messageBlock(ctx, { userId, operatorId }) { const { db, now } = ctx; const { me, target } = resolve(db, userId, operatorId); if (db.get('SELECT 1 AS x FROM message_blocks WHERE operator_id = ? AND blocked_id = ?', me.id, target.id)) return { changed: false }; if (db.get('SELECT COUNT(*) AS n FROM message_blocks WHERE operator_id = ?', me.id).n >= A.S.maxBlocks) throw new GameError('conflict', { what: 'block list full', max: A.S.maxBlocks }); db.run('INSERT INTO message_blocks (operator_id, blocked_id, created_at) VALUES (?, ?, ?)', me.id, target.id, now); return { changed: true }; } function messageUnblock(ctx, { userId, operatorId }) { const { db } = ctx; const { me, target } = resolve(db, userId, operatorId); return { changed: db.run('DELETE FROM message_blocks WHERE operator_id = ? AND blocked_id = ?', me.id, target.id).changes > 0 }; } module.exports = { messageBlock, messageUnblock };