'use strict'; /* Player messages and alliance broadcasts (stage 5). Fixed paths first, /messages/:id last. */ const { reply } = require('../http'); const { createSocialQueries } = require('../socialQueries'); const D = require('../../public/js/data.js'); const int1 = { type: 'integer', minimum: 1 }; const page = { after: { type: 'integer', minimum: 0 }, limit: { type: 'integer', minimum: 1, maximum: 100 } }; const listQuery = (extra) => ({ type: 'object', properties: Object.assign({}, page, { worldId: int1 }, extra || {}) }); function register(router, services) { const { commands } = services; const social = createSocialQueries(services); const run = (name, args) => commands.run(name, args); router.get('/messages/inbox', { summary: 'Received messages, newest first (cursor: after = last id of the previous page); `unread` counts all unread', tag: 'messages', query: listQuery({ unread: { type: 'boolean' } }), response: { $ref: 'MessageList' }, errors: ['unauthorized', 'not_found', 'validation_failed'], }, ({ user, query }) => social.inbox(user.id, query)); router.get('/messages/outbox', { summary: 'Sent messages, newest first', tag: 'messages', query: listQuery(), response: { $ref: 'MessageList' }, errors: ['unauthorized', 'not_found', 'validation_failed'], }, ({ user, query }) => social.outbox(user.id, query)); router.get('/messages/alliance', { summary: 'Broadcasts of your alliance, newest first', tag: 'messages', query: listQuery(), response: { $ref: 'MessageList' }, errors: ['unauthorized', 'not_found', 'not_in_alliance', 'validation_failed'], }, ({ user, query }) => social.allianceFeed(user.id, query)); router.get('/messages/thread/:operatorId', { summary: 'The conversation with one operator, both directions, newest first', tag: 'messages', query: listQuery(), response: { $ref: 'MessageList' }, errors: ['unauthorized', 'not_found', 'validation_failed'], }, ({ user, params, query }) => social.thread(user.id, params.operatorId, query)); router.get('/messages/blocks', { summary: 'Operators you blocked', tag: 'messages', query: { type: 'object', properties: { worldId: int1 } }, response: { $ref: 'BlockList' }, errors: ['unauthorized', 'not_found', 'validation_failed'], }, ({ user, query }) => social.blocks(user.id, query)); router.post('/messages/blocks', { summary: 'Block an operator: their messages are no longer delivered to you (they are not told)', tag: 'messages', body: { type: 'object', required: ['operatorId'], properties: { operatorId: int1 } }, response: { $ref: 'Changed' }, errors: ['unauthorized', 'not_found', 'validation_failed', 'conflict'], }, ({ user, body }) => run('messageBlock', { userId: user.id, operatorId: body.operatorId })); router.del('/messages/blocks/:operatorId', { summary: 'Unblock an operator', tag: 'messages', response: { $ref: 'Changed' }, errors: ['unauthorized', 'not_found', 'validation_failed'], }, ({ user, params }) => run('messageUnblock', { userId: user.id, operatorId: params.operatorId })); router.post('/messages/read', { summary: 'Mark received messages read: a list of ids (max 100) or all', tag: 'messages', body: { type: 'object', properties: { ids: { type: 'array', maxItems: 100, items: int1 }, all: { type: 'boolean' }, worldId: int1 } }, response: { $ref: 'Changed' }, errors: ['unauthorized', 'validation_failed'], }, ({ user, body }) => { const r = commands.run('messageRead', { userId: user.id, ids: body.ids, all: body.all, worldId: body.worldId }); return { changed: r.changed > 0, count: r.changed }; }); router.post('/messages', { summary: 'Send a message to an operator (toOperatorId or toName) or to your alliance (toAlliance). Plain text, max 2000 characters, rate-limited', tag: 'messages', status: 201, body: { type: 'object', required: ['text'], properties: { text: { type: 'string', minLength: 1, maxLength: D.SOCIAL.messageMax }, toOperatorId: int1, toName: { type: 'string', minLength: 1, maxLength: 40 }, toAlliance: { type: 'boolean' }, worldId: int1 } }, response: { $ref: 'MessageSent' }, errors: ['unauthorized', 'not_found', 'validation_failed', 'forbidden', 'not_in_alliance', 'rate_limited'], }, ({ user, body }) => reply(201, run('messageSend', { userId: user.id, worldId: body.worldId, toOperatorId: body.toOperatorId, toName: body.toName, toAlliance: body.toAlliance, text: body.text }))); router.get('/messages/:id', { summary: 'One message you sent or received', tag: 'messages', response: { $ref: 'MessageResponse' }, errors: ['unauthorized', 'not_found'], }, ({ user, params }) => social.message(user.id, params.id)); router.del('/messages/:id', { summary: 'Delete your own copy of a message (the other side keeps theirs)', tag: 'messages', response: { $ref: 'Changed' }, errors: ['unauthorized', 'not_found'], }, ({ user, params }) => run('messageDelete', { userId: user.id, messageId: params.id })); } module.exports = { register };