Skip to content

Commit

Permalink
Merge branch 'accalia-dev' into integration
Browse files Browse the repository at this point in the history
  • Loading branch information
AccaliaDeElementia committed Dec 3, 2015
2 parents 5396e5e + f5b577a commit 33a458b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
15 changes: 11 additions & 4 deletions plugins/autoreader.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,31 @@ exports.stop = function () {
* reading any unread posts it finds that are older than the configured interval.
*/
exports.readify = function () {
const minAge = new Date().getTime() - internals.config.minAge;
const additional = Math.max(internals.config.minAge / 2, 25 * 60 * 60 * 1000);
const readCutoff = minAge - additional;
internals.browser.getTopics((topic, nextTopic) => {
if (!topic) {
return;
return nextTopic();
}
const lastPost = Date.parse(topic.last_posted_at);
if (lastPost < readCutoff) {
return nextTopic();
}

internals.events.emit('logMessage', 'Reading topic `' + topic.slug + '`');
const now = new Date().getTime() - internals.config.minAge;
const postNumbers = [];
internals.browser.getPosts(topic.id, (post, nextPost) => {
if (post && !post.read && Date.parse(post.created_at) < now) {
if (post && !post.read && Date.parse(post.created_at) < minAge) {
postNumbers.push(post.post_number);
}
nextPost();
}, () => {
if (postNumbers.length > 0) {
internals.browser.readPosts(topic.id, postNumbers, () => 0);
}
nextTopic();
});
nextTopic();
}, () => 0);
};

Expand Down
23 changes: 19 additions & 4 deletions test/plugins/autoreaderTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ describe('autoreader', () => {
});
describe('stop()', () => {
it('should stop timer', () => {
autoreader.internals.timer = {clear: () => 0};
autoreader.internals.timer = {
clear: () => 0
};
autoreader.stop();
expect(autoreader.internals.timer).to.be.undefined;
});
Expand All @@ -103,11 +105,13 @@ describe('autoreader', () => {
sandbox.restore();
});
it('should not read anything', () => {
const spy = sandbox.stub(browser, 'getTopics');
spy.callsArgWith(0, undefined);
const spy = sandbox.stub(browser, 'getTopics'),
spy2 = sinon.spy();
spy.callsArgWith(0, undefined, spy2);
autoreader.prepare(undefined, dummyCfg, events, browser);
autoreader.readify();
events.emit.calledWith('logMessage').should.be.false;
spy2.called.should.be.true;
});
it('should read the topic', () => {
const topicSpy = sandbox.stub(browser, 'getTopics');
Expand All @@ -118,7 +122,18 @@ describe('autoreader', () => {
sandbox.stub(browser, 'getPosts');
autoreader.prepare(undefined, dummyCfg, events, browser);
autoreader.readify();
events.emit.calledWith('logMessage', 'Reading topic `Test`').should.be.true;
});
it('should not read the topic when no new posts could have been made', () => {
const topicSpy = sandbox.stub(browser, 'getTopics');
topicSpy.callsArgWith(0, {
id: 1,
slug: 'Test',
'last_posted_at': new Date(Date.now() - 5 * 25 * 60 * 60 * 1000).toISOString()
}, () => 0);
sandbox.stub(browser, 'getPosts');
autoreader.prepare(undefined, dummyCfg, events, browser);
autoreader.readify();
events.emit.calledWith('logMessage', 'Reading topic `Test`').should.be.false;
});
/*eslint-disable camelcase */
it('should read the unread post', () => {
Expand Down

0 comments on commit 33a458b

Please sign in to comment.