Skip to content

Commit

Permalink
feat(dao): get seamail threads
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Feb 10, 2019
1 parent 566de10 commit 5b7d5eb
Show file tree
Hide file tree
Showing 8 changed files with 612 additions and 58 deletions.
58 changes: 11 additions & 47 deletions src/dao/SeamailDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>) {
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);
});
}
*/
}
6 changes: 3 additions & 3 deletions src/model/SeamailResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down
77 changes: 77 additions & 0 deletions test/dao/SeamailDAO.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
Expand All @@ -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);
Expand Down Expand Up @@ -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=<date>', 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();
});
});

});
50 changes: 50 additions & 0 deletions test/data/seamail_threads-after-epoch.json
Original file line number Diff line number Diff line change
@@ -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
}
149 changes: 149 additions & 0 deletions test/data/seamail_threads-exclude_read_messages-true.json
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 5b7d5eb

Please sign in to comment.