Skip to content

Commit

Permalink
feat(dao): create a seamail thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Feb 11, 2019
1 parent f2c837a commit 924e802
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/CLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const CLI = () => {
.describe('n', 'list new threads and threads with new messages');
})
.command('read <id>', 'read a seamail thread')
.command('create <title> <users...>', 'create a new seamail thread');
.command('create <subject> <message> <users...>', 'create a new seamail thread');
})
.argv;

Expand Down Expand Up @@ -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]) {
Expand All @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions src/dao/SeamailDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
});
}
}
9 changes: 9 additions & 0 deletions test/dao/SeamailDAO.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
Expand Down Expand Up @@ -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();
});
});
});
});
32 changes: 32 additions & 0 deletions test/data/seamail-create-response.json
Original file line number Diff line number Diff line change
@@ -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
}
}
10 changes: 10 additions & 0 deletions test/rest/MockHTTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 924e802

Please sign in to comment.