Skip to content

Commit

Permalink
feat(): integration work for CruiseMonkey
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Mar 18, 2019
1 parent cab286e commit ce1964d
Show file tree
Hide file tree
Showing 39 changed files with 392 additions and 258 deletions.
31 changes: 23 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@babel/preset-env": "^7.0.0",
"@types/jest": "^24.0.0",
"@types/lodash": "^4.14.120",
"@types/luxon": "^1.11.1",
"@types/node": "^10.12.24",
"@types/urijs": "^1.15.38",
"@typescript-eslint/eslint-plugin": "^1.4.0",
Expand Down Expand Up @@ -71,15 +72,17 @@
"dependencies": {
"@babel/runtime": "^7.3.1",
"axios": "^0.18.0",
"browser-or-node": "^1.1.0",
"buffer": "^5.2.1",
"cli-table2": "^0.2.0",
"es6-promise": "^4.2.6",
"form-data": "^2.3.3",
"lodash.clonedeep": "^4.5.0",
"lodash.startcase": "^4.4.0",
"moment": "^2.24.0",
"luxon": "^1.11.3",
"node-fetch": "^2.3.0",
"qs": "^6.6.0",
"string-hash": "^1.1.3",
"urijs": "^1.19.1",
"word-wrap": "^1.2.3",
"xmldom": "^0.1.27"
Expand Down
6 changes: 4 additions & 2 deletions src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { UserDAO } from './dao/UserDAO';

import { AlertResponse } from './model/AlertResponse';
import { Announcement } from './model/Announcement';
import { Event } from './model/Event';
import { CalendarEvent } from './model/CalendarEvent';
import { ForumPost } from './model/ForumPost';
import { ForumResponse } from './model/ForumResponse';
import { ForumThread } from './model/ForumThread';
Expand All @@ -34,6 +34,7 @@ import { StreamResponse } from './model/StreamResponse';
import { User } from './model/User';
import { UserProfileInfo } from './model/UserProfileInfo';

import { AutomaticHTTP } from './rest/AutomaticHTTP';
import { BrowserHTTP } from './rest/BrowserHTTP';
import { CordovaHTTP } from './rest/CordovaHTTP';
import { NodeHTTP } from './rest/NodeHTTP';
Expand Down Expand Up @@ -68,7 +69,7 @@ const DAO = Object.freeze({
const Model = Object.freeze({
AlertResponse,
Announcement,
Event,
CalendarEvent,
ForumPost,
ForumResponse,
ForumThread,
Expand All @@ -87,6 +88,7 @@ const Model = Object.freeze({

/** @hidden */
const Rest = Object.freeze({
AutomaticHTTP,
BrowserHTTP,
CordovaHTTP,
NodeHTTP,
Expand Down
34 changes: 17 additions & 17 deletions src/CLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ const CLI = () => {
continue;
}
if (Util.isDateObject(value)) {
value = Util.toMoment(value).fromNow();
value = Util.toDateTime(value).toRelative();
}
t.push([name + ':', value]);
}
Expand Down Expand Up @@ -347,7 +347,7 @@ const CLI = () => {
const t = new Table(format);

for (const thread of seamail.threads) {
const row = [thread.id, thread.subject, thread.timestamp.fromNow()];
const row = [thread.id, thread.subject, thread.timestamp.toRelative()];
if (!n) {
row.unshift(thread.is_unread ? '*' : '');
}
Expand All @@ -366,13 +366,13 @@ const CLI = () => {

const thread = seamail.threads[0];
t.push(['Subject:', thread.subject]);
t.push(['Last Updated:', thread.timestamp.fromNow()]);
t.push(['Last Updated:', thread.timestamp.toRelative()]);
t.push(['Participants:', thread.users.map(user => user.toString()).join('\n')]);
console.log(t.toString());
console.log('');
t = new Table(tableFormat);
thread.messages.reverse().forEach(message => {
t.push([message.author.getDisplayName(), message.text, message.timestamp.fromNow()]);
t.push([message.author.getDisplayName(), message.text, message.timestamp.toRelative()]);
});
console.log(t.toString());
console.log('');
Expand All @@ -398,7 +398,7 @@ const CLI = () => {
for (const post of response.posts.reverse()) {
console.log(post.author.toString());
console.log(wrap(post.text.trim(), { indent: ' ', width: 60 }));
console.log(' - ' + post.timestamp.fromNow() + ' (id: ' + post.id + ')');
console.log(' - ' + post.timestamp.toRelative() + ' (id: ' + post.id + ')');
console.log('');
}
};
Expand Down Expand Up @@ -471,18 +471,18 @@ const CLI = () => {

const printEvent = (event: any) => {
const prefix = ' ';
const startTime = event.start_time.format('hh:mm a');
const startTime = event.start_time.toFormat('hh:mm a');
console.log(startTime + ' ' + (event.following ? '* ' : ' ') + event.title + (event.official ? '' : ' (shadow)'));
if (event.location) console.log(prefix + event.location);
if (event.end_time) console.log(prefix + 'ends: ' + event.end_time.format('hh:mm a'));
if (event.end_time) console.log(prefix + 'ends: ' + event.end_time.toFormat('hh:mm a'));
if (event.description) console.log(wrap(event.description.trim(), { indent: prefix, width: 50 }));
console.log(prefix + 'id: ' + event.id);
console.log('');
};

const doListEvents = async (mine?: boolean, today?: boolean) => {
const client = getClient();
const now = Util.toMoment(new Date().getTime());
const now = Util.toDateTime(new Date().getTime());
let events;
if (mine || today) {
events = await client.events().getDay(now, mine);
Expand All @@ -491,7 +491,7 @@ const CLI = () => {
}
let lastDay;
for (const event of events) {
let day = event.start_time.format('ddd, MMM Do');
let day = event.start_time.toFormat('ccc, MMM dd');
if (day !== lastDay) {
console.log('[ ' + day + ' ]');
lastDay = day;
Expand All @@ -509,12 +509,12 @@ const CLI = () => {
};

const doUpdateEvent = async (id: string, title?: string, description?: string, location?: string, startTime?: string, endTime?: string) => {
const start = startTime ? Util.toMoment(startTime) : undefined;
const end = endTime ? Util.toMoment(endTime) : undefined;
const start = startTime ? Util.toDateTime(startTime) : undefined;
const end = endTime ? Util.toDateTime(endTime) : undefined;
const event = await getClient()
.events()
.update(id, title, description, location, start, end);
let day = event.start_time.format('ddd, MMM Do');
let day = event.start_time.toFormat('ccc, MMM dd');
console.log('[ ' + day + ' ]');
printEvent(event);
};
Expand Down Expand Up @@ -574,14 +574,14 @@ const CLI = () => {
.text()
.announcements();
for (const announcement of announcements) {
console.log(announcement.timestamp.fromNow() + ' - ' + announcement.author.toString());
console.log(announcement.timestamp.toRelative() + ' - ' + announcement.author.toString());
console.log(wrap(announcement.text, { indent: ' ', size: 50 }));
console.log('');
}
};

const printForumPost = post => {
const time = post.timestamp.fromNow();
const time = post.timestamp.toRelative();
console.log('-----');
console.log(post.author.toString() + ' posted ' + time);
const statusLine =
Expand All @@ -597,7 +597,7 @@ const CLI = () => {
};

const printThreadSummary = (thread, page?: number) => {
const lastTime = thread.timestamp ? thread.timestamp.format('yyyy-MM-DD hh:mm a') : thread.latest_read.format('yyyy-MM-DD hh:mm a');
const lastTime = thread.timestamp ? thread.timestamp.toFormat('yyyy-MM-dd hh:mm a') : thread.latest_read.toFormat('yyyy-MM-dd hh:mm a');
console.log('### ' + thread.subject + ' ###');
console.log(lastTime + (thread.sticky ? ' [sticky]' : '') + (thread.locked ? ' [locked]' : ''));
const statusLine =
Expand Down Expand Up @@ -792,11 +792,11 @@ const CLI = () => {
};
if (args.newerThan) {
options.newer_posts = true;
options.start = Util.toMoment(args.newerThan);
options.start = Util.toDateTime(args.newerThan);
}
if (args.olderThan) {
options.newer_posts = false;
options.start = Util.toMoment(args.olderThan);
options.start = Util.toDateTime(args.olderThan);
}
await doStreamRead(options);
break;
Expand Down
4 changes: 2 additions & 2 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { StreamDAO } from './dao/StreamDAO';
import { TextDAO } from './dao/TextDAO';
import { UserDAO } from './dao/UserDAO';

import { BrowserHTTP } from './rest/BrowserHTTP';
import { AutomaticHTTP } from './rest/AutomaticHTTP';

/**
* The Twitarr client. This is the primary interface to Twitarr servers.
Expand Down Expand Up @@ -65,7 +65,7 @@ export class Client implements IHasHTTP {
if (httpImpl) {
Client.defaultHttp = httpImpl;
} else {
Client.defaultHttp = new BrowserHTTP();
Client.defaultHttp = new AutomaticHTTP();
}
this.http = Client.defaultHttp;
}
Expand Down
4 changes: 3 additions & 1 deletion src/api/TwitarrServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ export class TwitarrServer {
return this.url;
}
let uri = URI(this.url);
if (forFragment.indexOf('/') === 0 || forFragment.indexOf('http') === 0) {
if (forFragment.indexOf('http') === 0) {
uri = URI(forFragment);
} else if (forFragment.indexOf('/') === 0) {
uri.pathname(forFragment);
} else {
uri = uri.segment(forFragment);
}
Expand Down
25 changes: 20 additions & 5 deletions src/dao/AlertDAO.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Moment } from 'moment';
import { DateTime } from 'luxon';

import { TwitarrHTTPOptions } from '../api/TwitarrHTTPOptions';

import { AlertResponse } from '../model/AlertResponse';

import { Util } from '../internal/Util';

import { AbstractDAO } from './AbstractDAO';

interface IAlertCounts {
Expand All @@ -18,10 +20,10 @@ export class AlertDAO extends AbstractDAO {
/**
* Retrieve an alert response containing all posts/etc. that are un-viewed.
*/
public async get(last_checked_time?: Moment, no_reset?: boolean) {
public async get(last_checked_time?: DateTime | number, no_reset?: boolean) {
const options = new TwitarrHTTPOptions().withParameter('app', 'plain');
if (last_checked_time) {
options.parameters.last_checked_time = '' + last_checked_time.valueOf();
options.parameters.last_checked_time = '' + Util.toDateTime(last_checked_time).toMillis();
}
if (no_reset) {
options.parameters.no_reset = '' + no_reset;
Expand All @@ -34,10 +36,23 @@ export class AlertDAO extends AbstractDAO {
});
}

public async count(last_checked_time?: Moment) {
public async lastChecked(last_checked_time: DateTime | number) {
const options = new TwitarrHTTPOptions().withData({
last_checked_time: Util.toDateTime(last_checked_time).toMillis(),
});

return this.http
.post('/api/v2/alerts/last_checked', options)
.then(result => this.handleErrors(result))
.then(data => {
return Util.toDateTime(data.last_checked_time);
});
}

public async count(last_checked_time?: DateTime | number) {
const options = new TwitarrHTTPOptions().withParameter('app', 'plain');
if (last_checked_time) {
options.parameters.last_checked_time = '' + last_checked_time.valueOf();
options.parameters.last_checked_time = '' + Util.toDateTime(last_checked_time).toMillis();
}
return this.http
.get('/api/v2/alerts/check', options)
Expand Down
Loading

0 comments on commit ce1964d

Please sign in to comment.