diff --git a/src/dao/SeamailDAO.ts b/src/dao/SeamailDAO.ts index 279ef7a3..b30c015c 100644 --- a/src/dao/SeamailDAO.ts +++ b/src/dao/SeamailDAO.ts @@ -18,55 +18,19 @@ export class SeamailDAO extends AbstractDAO { }); } - /* - public async login() { + public async getThreads(unread?: boolean, exclude_read_messages?: boolean, after?: Moment) { const options = new TwitarrHTTPOptions(); - options.data = { - password: this.http.getPassword(), - username: this.http.getUsername(), - }; - - return this.http.post('/api/v2/user/auth', options).then((result) => { - return this.handleErrors(result).then((data) => { - if (data && data.key) { - this.http.setKey(data.key); - return true; - } - throw new TwitarrError('No key returned from user auth', result.code, undefined, undefined, result); - }); - }); - } - - public async getProfile() { - return this.http.get('/api/v2/user/whoami').then((result) => { - return this.handleErrors(result).then((data) => { - return User.fromRest(data.user); - }); - }); - } - - public async createUser(registrationCode: string, username: string, password: string, displayName?: string) { - const options = new TwitarrHTTPOptions() - .withParameter('registration_code', registrationCode) - .withParameter('username', username) - .withParameter('password', password); - - if (displayName) { - options.parameters.display_name = displayName; + if (unread) { + options.parameters.unread = 'true'; } - - return this.http.post('/api/v2/user/new', options); - } - - private async handleErrors(result: TwitarrResult) { - if (result.isSuccess()) { - const status = result.data && result.data.status? result.data.status : 'ok'; - if (status === 'ok') { - // console.debug('result was ok:', result); - return Promise.resolve(result.data); - } + if (exclude_read_messages) { + options.parameters.exclude_read_messages = 'true'; } - throw new TwitarrError('Failed to parse result.', result.code, undefined, undefined, result.data); + if (after) { + options.parameters.after = '' + after.valueOf(); + } + return this.http.get('/api/v2/seamail_threads', options).then((result) => { + return SeamailResponse.fromRest(result.data); + }); } - */ } diff --git a/src/model/SeamailResponse.ts b/src/model/SeamailResponse.ts index ed6da08e..43cb987f 100644 --- a/src/model/SeamailResponse.ts +++ b/src/model/SeamailResponse.ts @@ -12,8 +12,8 @@ export class SeamailResponse { if (!Util.isEmpty(data.seamail_meta)) { ret.threads = data.seamail_meta.map((thread) => SeamailThread.fromRest(thread)); ret.is_meta = true; - } else if (!Util.isEmpty(data.seamail)) { - ret.threads = data.seamail.map((thread) => SeamailThread.fromRest(thread)); + } else if (!Util.isEmpty(data.seamail_threads)) { + ret.threads = data.seamail_threads.map((thread) => SeamailThread.fromRest(thread)); ret.is_meta = false; } @@ -38,7 +38,7 @@ export class SeamailResponse { if (this.is_meta) { ret.seamail_meta = this.threads; } else { - ret.seamail = this.threads; + ret.seamail_threads = this.threads; } return ret; } diff --git a/test/dao/SeamailDAO.spec.ts b/test/dao/SeamailDAO.spec.ts index 394e043d..488a4c1b 100644 --- a/test/dao/SeamailDAO.spec.ts +++ b/test/dao/SeamailDAO.spec.ts @@ -9,6 +9,7 @@ import { SeamailResponse } from '../../src/model/SeamailResponse'; import { Util } from '../../src/internal/Util'; import { MockHTTP } from '../rest/MockHTTP'; +import { sort } from 'shelljs'; const SERVER_NAME = 'Demo'; const SERVER_URL = 'http://demo.twitarr.com/'; @@ -17,6 +18,19 @@ const SERVER_PASSWORD = 'demo'; let dao: SeamailDAO, server, auth, mockHTTP; +/* tslint:disable object-literal-sort-keys max-line-length */ + +const assertThreadsMatch = (expected, actual) => { + expect(Object.keys(expected).length).toEqual(actual.threads.length); + Object.keys(expected).forEach((threadId, index) => { + const actualThread = actual.threads[index]; + expect(threadId).toEqual(actualThread.id); + const messages = actualThread.messages.map((message) => message.id); + expect(expected[threadId]).toEqual(expect.arrayContaining(messages)); + expect(messages).toEqual(expect.arrayContaining(expected[threadId])); + }); +}; + describe('dao/SeamailDAO', () => { beforeEach(() => { auth = new TwitarrAuthConfig(SERVER_USER, SERVER_PASSWORD); @@ -47,5 +61,68 @@ describe('dao/SeamailDAO', () => { done(); }); }); + describe('#getThreads', () => { + it('no arguments', async (done) => { + const ret = await dao.getThreads(); + expect(ret).toBeDefined(); + expect(ret).toBeInstanceOf(SeamailResponse); + expect(ret.threads.length).toEqual(3); + expect(ret.threads[0].users.length).toEqual(3); + expect(ret.threads[0].messages.length).toEqual(1); + expect(ret.threads[0].is_unread).toBeFalsy(); + expect(ret.threads[0].count_is_unread).toBeFalsy(); + assertThreadsMatch({ + '5c607d43ea204f5815755cda': ['5c607d43ea204f5815755cdb'], + '5c5cfd9f1fca877544f927da': ['5c5cfd9f1fca877544f927dc', '5c5cfd9f1fca877544f927db'], + '5c5cfd9f1fca877544f927d6': ['5c5cfd9f1fca877544f927d9', '5c5cfd9f1fca877544f927d8', '5c5cfd9f1fca877544f927d7'], + }, ret); + done(); + }); + it('exclude_read_messages=true', async (done) => { + const ret = await dao.getThreads(false, true); + expect(ret).toBeDefined(); + expect(ret).toBeInstanceOf(SeamailResponse); + expect(ret.threads.length).toEqual(3); + expect(ret.threads[0].users.length).toEqual(3); + expect(ret.threads[0].messages.length).toEqual(0); + expect(ret.threads[0].is_unread).toBeFalsy(); + expect(ret.threads[0].count_is_unread).toBeFalsy(); + assertThreadsMatch({ + '5c607d43ea204f5815755cda': [], + '5c5cfd9f1fca877544f927da': ['5c5cfd9f1fca877544f927dc', '5c5cfd9f1fca877544f927db'], + '5c5cfd9f1fca877544f927d6': ['5c5cfd9f1fca877544f927d9', '5c5cfd9f1fca877544f927d7'], + }, ret); + done(); + }); + it('unread=true', async (done) => { + const ret = await dao.getThreads(true); + expect(ret).toBeDefined(); + expect(ret).toBeInstanceOf(SeamailResponse); + expect(ret.threads.length).toEqual(2); + expect(ret.threads[0].users.length).toEqual(3); + expect(ret.threads[0].messages.length).toEqual(2); + expect(ret.threads[0].is_unread).toBeTruthy(); + expect(ret.threads[0].count_is_unread).toBeTruthy(); + assertThreadsMatch({ + '5c5cfd9f1fca877544f927da': ['5c5cfd9f1fca877544f927dc', '5c5cfd9f1fca877544f927db'], + '5c5cfd9f1fca877544f927d6': ['5c5cfd9f1fca877544f927d9', '5c5cfd9f1fca877544f927d7'], + }, ret); + done(); + }); + it('after=', async (done) => { + const ret = await dao.getThreads(undefined, undefined, Util.toMoment(1549827390000)); + expect(ret).toBeDefined(); + expect(ret).toBeInstanceOf(SeamailResponse); + expect(ret.threads.length).toEqual(1); + expect(ret.threads[0].users.length).toEqual(3); + expect(ret.threads[0].messages.length).toEqual(1); + expect(ret.threads[0].is_unread).toBeFalsy(); + expect(ret.threads[0].count_is_unread).toBeFalsy(); + assertThreadsMatch({ + '5c607d43ea204f5815755cda': ['5c607d43ea204f5815755cdb'], + }, ret); + done(); + }); + }); }); diff --git a/test/data/seamail_threads-after-epoch.json b/test/data/seamail_threads-after-epoch.json new file mode 100644 index 00000000..386fed1f --- /dev/null +++ b/test/data/seamail_threads-after-epoch.json @@ -0,0 +1,50 @@ +{ + "status": "ok", + "seamail_threads": [ + { + "id": "5c607d43ea204f5815755cda", + "users": [ + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + }, + { + "username": "rob", + "display_name": "Manager Rob", + "last_photo_updated": 1549826231532 + }, + { + "username": "kvort", + "display_name": "kvort", + "last_photo_updated": 1549827517608 + } + ], + "subject": "Meet up before the concert?", + "messages": [ + { + "id": "5c607d43ea204f5815755cdb", + "author": { + "username": "kvort", + "display_name": "kvort", + "last_photo_updated": 1549827517608 + }, + "text": "Maybe we can play a board game or something.", + "timestamp": 1549827395180, + "read_users": [ + { + "username": "kvort", + "display_name": "kvort", + "last_photo_updated": 1549827517608 + } + ] + } + ], + "message_count": 1, + "timestamp": 1549827395180, + "is_unread": false, + "count_is_unread": false + } + ], + "last_checked": 1549838805902 +} diff --git a/test/data/seamail_threads-exclude_read_messages-true.json b/test/data/seamail_threads-exclude_read_messages-true.json new file mode 100644 index 00000000..ee7e0dd7 --- /dev/null +++ b/test/data/seamail_threads-exclude_read_messages-true.json @@ -0,0 +1,149 @@ +{ + "status": "ok", + "seamail_threads": [ + { + "id": "5c607d43ea204f5815755cda", + "users": [ + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + }, + { + "username": "rob", + "display_name": "Manager Rob", + "last_photo_updated": 1549826231532 + }, + { + "username": "kvort", + "display_name": "kvort", + "last_photo_updated": 1549827517608 + } + ], + "subject": "Meet up before the concert?", + "messages": [], + "message_count": 1, + "timestamp": 1549827395180, + "is_unread": false, + "count_is_unread": false + }, + { + "id": "5c5cfd9f1fca877544f927da", + "users": [ + { + "username": "kvort", + "display_name": "kvort", + "last_photo_updated": 1549827517608 + }, + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + }, + { + "username": "steve", + "display_name": "steve", + "last_photo_updated": 1549598100329 + } + ], + "subject": "artemis?", + "messages": [ + { + "id": "5c5cfd9f1fca877544f927dc", + "author": { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + }, + "text": "Awesome!", + "timestamp": 1549531800000, + "read_users": [ + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + } + ] + }, + { + "id": "5c5cfd9f1fca877544f927db", + "author": { + "username": "steve", + "display_name": "steve", + "last_photo_updated": 1549598100329 + }, + "text": "We should go to the game room and play artemis at 15:00!", + "timestamp": 1549531380000, + "read_users": [ + { + "username": "steve", + "display_name": "steve", + "last_photo_updated": 1549598100329 + } + ] + } + ], + "message_count": 2, + "timestamp": 1549531800000, + "is_unread": true, + "count_is_unread": false + }, + { + "id": "5c5cfd9f1fca877544f927d6", + "users": [ + { + "username": "kvort", + "display_name": "kvort", + "last_photo_updated": 1549827517608 + }, + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + } + ], + "subject": "Hey lets meet up", + "messages": [ + { + "id": "5c5cfd9f1fca877544f927d9", + "author": { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + }, + "text": "Sounds great to me!", + "timestamp": 1549528080000, + "read_users": [ + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + } + ] + }, + { + "id": "5c5cfd9f1fca877544f927d7", + "author": { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + }, + "text": "How about at 10:30?", + "timestamp": 1549527780000, + "read_users": [ + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + } + ] + } + ], + "message_count": 3, + "timestamp": 1549528080000, + "is_unread": true, + "count_is_unread": false + } + ], + "last_checked": 1549838851518 +} diff --git a/test/data/seamail_threads-unread-true.json b/test/data/seamail_threads-unread-true.json new file mode 100644 index 00000000..faf45c2a --- /dev/null +++ b/test/data/seamail_threads-unread-true.json @@ -0,0 +1,123 @@ +{ + "status": "ok", + "seamail_threads": [ + { + "id": "5c5cfd9f1fca877544f927da", + "users": [ + { + "username": "kvort", + "display_name": "kvort", + "last_photo_updated": 1549827517608 + }, + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + }, + { + "username": "steve", + "display_name": "steve", + "last_photo_updated": 1549598100329 + } + ], + "subject": "artemis?", + "messages": [ + { + "id": "5c5cfd9f1fca877544f927dc", + "author": { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + }, + "text": "Awesome!", + "timestamp": 1549531800000, + "read_users": [ + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + } + ] + }, + { + "id": "5c5cfd9f1fca877544f927db", + "author": { + "username": "steve", + "display_name": "steve", + "last_photo_updated": 1549598100329 + }, + "text": "We should go to the game room and play artemis at 15:00!", + "timestamp": 1549531380000, + "read_users": [ + { + "username": "steve", + "display_name": "steve", + "last_photo_updated": 1549598100329 + } + ] + } + ], + "message_count": 2, + "timestamp": 1549531800000, + "is_unread": true, + "count_is_unread": true + }, + { + "id": "5c5cfd9f1fca877544f927d6", + "users": [ + { + "username": "kvort", + "display_name": "kvort", + "last_photo_updated": 1549827517608 + }, + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + } + ], + "subject": "Hey lets meet up", + "messages": [ + { + "id": "5c5cfd9f1fca877544f927d9", + "author": { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + }, + "text": "Sounds great to me!", + "timestamp": 1549528080000, + "read_users": [ + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + } + ] + }, + { + "id": "5c5cfd9f1fca877544f927d7", + "author": { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + }, + "text": "How about at 10:30?", + "timestamp": 1549527780000, + "read_users": [ + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + } + ] + } + ], + "message_count": 2, + "timestamp": 1549528080000, + "is_unread": true, + "count_is_unread": true + } + ], + "last_checked": 1549838833018 +} diff --git a/test/data/seamail_threads.json b/test/data/seamail_threads.json new file mode 100644 index 00000000..8f519113 --- /dev/null +++ b/test/data/seamail_threads.json @@ -0,0 +1,184 @@ +{ + "status": "ok", + "seamail_threads": [ + { + "id": "5c607d43ea204f5815755cda", + "users": [ + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + }, + { + "username": "rob", + "display_name": "Manager Rob", + "last_photo_updated": 1549826231532 + }, + { + "username": "kvort", + "display_name": "kvort", + "last_photo_updated": 1549827517608 + } + ], + "subject": "Meet up before the concert?", + "messages": [ + { + "id": "5c607d43ea204f5815755cdb", + "author": { + "username": "kvort", + "display_name": "kvort", + "last_photo_updated": 1549827517608 + }, + "text": "Maybe we can play a board game or something.", + "timestamp": 1549827395180, + "read_users": [ + { + "username": "kvort", + "display_name": "kvort", + "last_photo_updated": 1549827517608 + } + ] + } + ], + "message_count": 1, + "timestamp": 1549827395180, + "is_unread": false, + "count_is_unread": false + }, + { + "id": "5c5cfd9f1fca877544f927da", + "users": [ + { + "username": "kvort", + "display_name": "kvort", + "last_photo_updated": 1549827517608 + }, + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + }, + { + "username": "steve", + "display_name": "steve", + "last_photo_updated": 1549598100329 + } + ], + "subject": "artemis?", + "messages": [ + { + "id": "5c5cfd9f1fca877544f927dc", + "author": { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + }, + "text": "Awesome!", + "timestamp": 1549531800000, + "read_users": [ + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + } + ] + }, + { + "id": "5c5cfd9f1fca877544f927db", + "author": { + "username": "steve", + "display_name": "steve", + "last_photo_updated": 1549598100329 + }, + "text": "We should go to the game room and play artemis at 15:00!", + "timestamp": 1549531380000, + "read_users": [ + { + "username": "steve", + "display_name": "steve", + "last_photo_updated": 1549598100329 + } + ] + } + ], + "message_count": 2, + "timestamp": 1549531800000, + "is_unread": true, + "count_is_unread": false + }, + { + "id": "5c5cfd9f1fca877544f927d6", + "users": [ + { + "username": "kvort", + "display_name": "kvort", + "last_photo_updated": 1549827517608 + }, + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + } + ], + "subject": "Hey lets meet up", + "messages": [ + { + "id": "5c5cfd9f1fca877544f927d9", + "author": { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + }, + "text": "Sounds great to me!", + "timestamp": 1549528080000, + "read_users": [ + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + } + ] + }, + { + "id": "5c5cfd9f1fca877544f927d8", + "author": { + "username": "kvort", + "display_name": "kvort", + "last_photo_updated": 1549827517608 + }, + "text": "Alright, 10-forward?", + "timestamp": 1549527960000, + "read_users": [ + { + "username": "kvort", + "display_name": "kvort", + "last_photo_updated": 1549827517608 + } + ] + }, + { + "id": "5c5cfd9f1fca877544f927d7", + "author": { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + }, + "text": "How about at 10:30?", + "timestamp": 1549527780000, + "read_users": [ + { + "username": "james", + "display_name": "james", + "last_photo_updated": 1549598100329 + } + ] + } + ], + "message_count": 3, + "timestamp": 1549528080000, + "is_unread": true, + "count_is_unread": false + } + ], + "last_checked": 1549838873463 +} diff --git a/test/rest/MockHTTP.ts b/test/rest/MockHTTP.ts index b147ffc1..ad22cba0 100644 --- a/test/rest/MockHTTP.ts +++ b/test/rest/MockHTTP.ts @@ -47,19 +47,26 @@ export class MockHTTP extends AbstractHTTP { result.type = 'application/json'; return Promise.resolve(result); } - /* - case 'http://demo.opennms.org/opennms/rest/info': { - return Promise.resolve(TwitarrResult.ok({})); + case '/api/v2/seamail_threads': { + const result = TwitarrResult.ok(require('../data/seamail_threads.json')); + result.type = 'application/json'; + return Promise.resolve(result); } - case 'http://demo.opennms.org/opennms/rest/alarms/count': { - return Promise.resolve(TwitarrResult.ok(1)); + case '/api/v2/seamail_threads?exclude_read_messages=true': { + const result = TwitarrResult.ok(require('../data/seamail_threads-exclude_read_messages-true.json')); + result.type = 'application/json'; + return Promise.resolve(result); } - case 'rest/alarms/404725': { - const result = TwitarrResult.ok(require('./19.1.0/get/rest/alarms/404725.json')); + case '/api/v2/seamail_threads?unread=true': { + const result = TwitarrResult.ok(require('../data/seamail_threads-unread-true.json')); + result.type = 'application/json'; + return Promise.resolve(result); + } + case '/api/v2/seamail_threads?after=1549827390000': { + const result = TwitarrResult.ok(require('../data/seamail_threads-after-epoch.json')); result.type = 'application/json'; return Promise.resolve(result); } - */ } return Promise.reject(getError('GET', urlObj, options));