diff --git a/src/CLI.ts b/src/CLI.ts index ba8007c1..a59a1b2c 100644 --- a/src/CLI.ts +++ b/src/CLI.ts @@ -80,7 +80,7 @@ const CLI = () => { .describe('n', 'list new threads and threads with new messages'); }) .command('read ', 'read a seamail thread') - .command('create <users...>', 'create a new seamail thread'); + .command('create <subject> <message> <users...>', 'create a new seamail thread'); }) .argv; @@ -225,6 +225,13 @@ const CLI = () => { console.log(''); }; + const doSeamailCreate = async (subject: string, message: string, users: string[]) => { + const client = getClient(); + const seamail = await client.seamail().create(subject, message, ...users); + console.log(colors.green('Created thread ' + seamail.threads[0].id)); + console.log(''); + }; + const processArgs = async (args) => { try { switch (args._[0]) { @@ -247,6 +254,10 @@ const CLI = () => { await doSeamailRead(args.id); break; } + case 'create': { + await doSeamailCreate(args.subject, args.message, args.users); + break; + } default: throw new TwitarrError('Unhandled seamail command: ' + command); } break; diff --git a/src/dao/SeamailDAO.ts b/src/dao/SeamailDAO.ts index f165c646..1ce2b1db 100644 --- a/src/dao/SeamailDAO.ts +++ b/src/dao/SeamailDAO.ts @@ -4,6 +4,7 @@ import { AbstractDAO } from './AbstractDAO'; import { TwitarrHTTPOptions } from '../api/TwitarrHTTPOptions'; import { SeamailResponse } from '../model/SeamailResponse'; import { TwitarrError } from '../api/TwitarrError'; +import { User } from '../model/User'; export class SeamailDAO extends AbstractDAO { public async get(id: string, skip_mark_read?: boolean) { @@ -47,4 +48,17 @@ export class SeamailDAO extends AbstractDAO { return SeamailResponse.fromRest(result.data); }); } + + public async create(subject: string, message: string, ...users: string[]) { + const options = new TwitarrHTTPOptions(); + // tslint:disable object-literal-shorthand + options.data = { + subject: subject, + text: message, + users: users, + }; + return this.http.post('/api/v2/seamail', options).then((result) => { + return SeamailResponse.fromRest(result.data); + }); + } } diff --git a/test/dao/SeamailDAO.spec.ts b/test/dao/SeamailDAO.spec.ts index 0a2c3912..cffda35e 100644 --- a/test/dao/SeamailDAO.spec.ts +++ b/test/dao/SeamailDAO.spec.ts @@ -10,6 +10,7 @@ import { Util } from '../../src/internal/Util'; import { MockHTTP } from '../rest/MockHTTP'; import { sort } from 'shelljs'; +import { User } from '../../src/model/User'; const SERVER_NAME = 'Demo'; const SERVER_URL = 'http://demo.twitarr.com/'; @@ -151,5 +152,13 @@ describe('dao/SeamailDAO', () => { done(); }); }); + + it('create()', async (done) => { + dao.create('Test Subject', 'Test Message', 'twitarrteam', 'rangerrick').then((response) => { + expect(response).toBeDefined(); + expect(response).toBeInstanceOf(SeamailResponse); + done(); + }); + }); }); }); diff --git a/test/data/seamail-create-response.json b/test/data/seamail-create-response.json new file mode 100644 index 00000000..d68ac25c --- /dev/null +++ b/test/data/seamail-create-response.json @@ -0,0 +1,32 @@ +{ + "status": "ok", + "seamail": { + "id": "5c607d43ea204f5815755cda", + "users": [ + { + "username": "rangerrick", + "display_name": "rangerrick" + }, + { + "username": "twitarrteam", + "display_name": "twitarrteam" + } + ], + "subject": "Test Subject", + "messages": [ + { + "id": "5c607d43ea204f5815755cdb", + "author": { + "username": "rangerrick", + "display_name": "rangerrick" + }, + "text": "Test Message", + "timestamp": 1549827395180 + } + ], + "message_count": 1, + "timestamp": 1549827395180, + "is_unread": false, + "count_is_unread": false + } +} diff --git a/test/rest/MockHTTP.ts b/test/rest/MockHTTP.ts index 959db10d..3fb69fa3 100644 --- a/test/rest/MockHTTP.ts +++ b/test/rest/MockHTTP.ts @@ -129,6 +129,16 @@ export class MockHTTP extends AbstractHTTP { }); return Promise.reject(result); } + break; + } + case '/api/v2/seamail': { + // tslint:disable-next-line max-line-length + if (options.data.subject === 'Test Subject' && options.data.text === 'Test Message' && options.data.users.length === 2) { + const result = TwitarrResult.ok(require('../data/seamail-create-response.json')); + result.type = 'application/json'; + return Promise.resolve(result); + } + break; } }