Skip to content
This repository has been archived by the owner on Nov 11, 2018. It is now read-only.

Commit

Permalink
Merge pull request #108 from christianbundy/master
Browse files Browse the repository at this point in the history
4/24
  • Loading branch information
christianbundy committed Apr 24, 2014
2 parents 6ce8bc5 + 51a1b81 commit c40678d
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 173 deletions.
44 changes: 25 additions & 19 deletions client/global/header/globalHeader.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-9">
<ul class="nav navbar-nav navbar-right">
{{#unless loggingIn}}
{{#if currentUser}}
{{#if isFeed}}
<!--
<li class="highlight"><a href="/new-post">New Post</a></li>
Expand All @@ -27,37 +26,43 @@
<a href="/" class="dropdown-toggle" data-toggle="dropdown">{{sortType}}<b class="caret"></b></a>
<ul class="dropdown-menu" role="menu">
{{#unless sortHot}}
<li><a href="/hot/weekly">Hot</a></li>
<li><a href="/hot">Hot</a></li>
{{/unless}}
{{#unless sortBest}}
<li><a href="/best/{{toLowerCase timeType}}">Best</a></li>
{{#unless sortTop}}
<li><a href="/top/week">Top</a></li>
{{/unless}}
{{#unless sortRecent}}
<li><a href="/recent/daily">Recent</a></li>
<li><a href="/recent">Recent</a></li>
{{/unless}}
{{#unless sortRandom}}
<li><a href="/random/daily">Random</a></li>
<li><a href="/random">Random</a></li>
{{/unless}}
</ul>
</li>
{{#if sortBest}}
{{#if sortTop}}
<li class="dropdown">
<a href="/" class="dropdown-toggle" data-toggle="dropdown">{{timeType}}<b class="caret"></b></a>
<ul class="dropdown-menu" role="menu">
{{#unless timeDaily}}
<li><a href="/{{toLowerCase sortType}}/daily">Daily</a></li>
{{#unless timeHour}}
<li><a href="/{{toLowerCase sortType}}/hour">Hour</a></li>
{{/unless}}
{{#unless timeWeekly}}
<li><a href="/{{toLowerCase sortType}}/weekly">Weekly</a></li>
{{#unless timeDay}}
<li><a href="/{{toLowerCase sortType}}/day">Day</a></li>
{{/unless}}
{{#unless timeFortnightly}}
<li><a href="/{{toLowerCase sortType}}/fortnightly">Fortnightly</a></li>
{{#unless timeWeek}}
<li><a href="/{{toLowerCase sortType}}/week">Week</a></li>
{{/unless}}
{{#unless timeMonthly}}
<li><a href="/{{toLowerCase sortType}}/monthly">Monthly</a></li>
{{#unless timeFortnight}}
<li><a href="/{{toLowerCase sortType}}/fortnight">Fortnight</a></li>
{{/unless}}
{{#unless timeYearly}}
<li><a href="/{{toLowerCase sortType}}/yearly">Yearly</a></li>
{{#unless timeMonth}}
<li><a href="/{{toLowerCase sortType}}/month">Month</a></li>
{{/unless}}
{{#unless timeQuarter}}
<li><a href="/{{toLowerCase sortType}}/quarter">Quarter</a></li>
{{/unless}}
{{#unless timeYear}}
<li><a href="/{{toLowerCase sortType}}/year">Year</a></li>
{{/unless}}
{{#unless timeEver}}
<li><a href="/{{toLowerCase sortType}}/ever">Ever</a></li>
Expand All @@ -66,17 +71,18 @@
</li>
{{/if}}
{{/if}}
{{#if currentUser}}
<li class="dropdown">
<a href="/" class="dropdown-toggle" data-toggle="dropdown">{{currentUser.username}}<b class="caret"></b></a>
<ul class="dropdown-menu" role="menu">
<li><a href="/user/{{currentUser.username}}">Profile</a></li>
<!--<li><a href="/user/{{currentUser.username}}">Profile</a></li>-->
<li><a href="/sign-out">Sign Out</a></li>
</ul>
</li>
{{else}}
<li><a href="/sign-in">Sign In</a></li>
<li class="highlight"><a href="/sign-up">Sign Up</a></li>
{{/if}}
{{/if}}
{{/unless}}
</ul>
</div>
Expand Down
35 changes: 21 additions & 14 deletions client/posts/view/viewPost.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
/*
// gets just the domain of a title
var getDomain = function (uri) {
"use strict";
return uri.replace('http://','').replace('https://','').split(/[/?#]/)[0];
};
*/

// http://stackoverflow.com/a/3177838
// adapted from http://stackoverflow.com/a/3177838
var timeSince = function (date) {
"use strict";
var seconds = Math.floor((new Date() - date) / 1000);

var timeString = function (int, str) {
var suffix = '';
if (Math.floor(int) !== 1) {
suffix = 's';
}
return int + ' ' + str + suffix;
};

var seconds = Math.floor((new Date() - date) / 1000);
var interval = Math.floor(seconds / 31536000);

if (interval > 1) {
return interval + " years";
if (interval > 0) {
return timeString(interval, 'year');
}
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return interval + " months";
if (interval > 0) {
return timeString(interval, 'month');
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
return interval + " days";
if (interval > 0) {
return timeString(interval, 'day');
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
return interval + " hours";
if (interval > 0) {
return timeString(interval, 'hour');
}
interval = Math.floor(seconds / 60);
if (interval > 1) {
return interval + " minutes";
if (interval > 0) {
return timeString(interval, 'minute');
}
return Math.floor(seconds) + " seconds";
return timeString(interval, 'second');
};

Template.post.helpers({
Expand Down
121 changes: 46 additions & 75 deletions server/hn/readHn.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,53 @@
//var queue = new PowerQueue();
var hn = Meteor.require('hacker-news-api');

var readHn = function () {
var readHn = function (before) {
"use strict";
console.log('Reading last hour, 12 hours, 24 hours, and week of Hacker News');
var now = Date.now() / 1000;
var querylist = [];

// past hour
querylist.push('search?tags=story&numericFilters=created_at_i>' +
(now - 60 * 60) + ',created_at_i<' +
now);

// past 12 hours
querylist.push('search?tags=story&numericFilters=created_at_i>' +
(now - 60 * 60 * 12) + ',created_at_i<' +
now);

// past 24 hours
querylist.push('search?tags=story&numericFilters=created_at_i>' +
(now - 60 * 60 * 24) + ',created_at_i<' +
now);

// past week
querylist.push('search?tags=story&numericFilters=created_at_i>' +
(now - 60 * 60 * 24 * 7) + ',created_at_i<' +
now);

// past month
querylist.push('search?tags=story&numericFilters=created_at_i>' +
(now - 60 * 60 * 24 * 30) + ',created_at_i<' +
now);

// past year
querylist.push('search?tags=story&numericFilters=created_at_i>' +
(now - 60 * 60 * 24 * 365) + ',created_at_i<' +
now);

// past ever
querylist.push('search?tags=story&numericFilters=created_at_i>' +
0 + ',created_at_i<' +
now);

_.forEach(querylist, function (query) {
hn.call(query, Meteor.bindEnvironment(
function (error, data) {
if (error) {
throw error;
}
_(data.hits).forEach(function (item) {
/*jshint camelcase: false */
var obj = {
oldId: parseInt(item.objectID, 10),
oldPoints: parseInt(item.points, 10),
createdAt: new Date(item.created_at),
site: 'hn',
author: item.author,
title: item.title,
url: item.url,
oldComments: parseInt(item.num_comments, 10),
};

Posts.upsert({
oldId: obj.oldId
},{
$set: obj
});

});
}, function (error) {
console.log('Reading the past ' + before + ' seconds of Hacker News');
var now = Math.floor(Date.now() / 1000);
var query = 'search?tags=story&numericFilters=created_at_i>';
query += (now - before) + ',created_at_i<' + now;

hn.call(query, Meteor.bindEnvironment(
function (error, data) {
if (error) {
throw error;
})
);
});
}
_(data.hits).forEach(function (item) {
/*jshint camelcase: false */
var obj = {
oldId: parseInt(item.objectID, 10),
oldPoints: parseInt(item.points, 10),
createdAt: new Date(item.created_at),
site: 'hn',
author: item.author,
title: item.title,
url: item.url,
oldComments: parseInt(item.num_comments, 10),
};

Posts.upsert({
oldId: obj.oldId
},{
$set: obj
});

});
}, function (error) {
throw error;
})
);
};

// read last 24 hours of hn
readHn();
Meteor.setInterval(function () {
"use strict";
readHn(60); // top for the minute
readHn(60 * 60); // top for the hour
readHn(24 * 60 * 60); // top for the day
readHn(7 * 24 * 60 * 60); // top for the week
}, 60 * 1000); // every minute

// reread hn every half hour
Meteor.setInterval(readHn, 1000 * 60 * 30);
Meteor.setInterval(function () {
"use strict";
readHn(Math.floor(Date.now() / 1000)); // top ever
readHn(7 * 24 * 60 * 60); // top for the week
}, 60 * 60 * 1000); // every hour
2 changes: 1 addition & 1 deletion server/posts/publishPosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Meteor.publish('recentPosts', function () {
});
});

Meteor.publish('bestPosts', function (start) {
Meteor.publish('topPosts', function (start) {
"use strict";
return Posts.find({
oldPoints: {
Expand Down
6 changes: 3 additions & 3 deletions server/sort/setHeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var setHeat = function () {

_(allArr).forEach(function (post) {
var secondsAgo = Math.abs((new Date()) - (new Date(post.createdAt))) / 1000;
var hoursAgo = secondsAgo / 60;
var hoursAgo = secondsAgo / 60 / 60;
var points = (post.oldPoints - 1);
var decay = Math.pow(hoursAgo + 2, 1.5);
Posts.update({
Expand All @@ -25,5 +25,5 @@ var setHeat = function () {

Meteor.startup(setHeat);

// run every 10 seconds
Meteor.setInterval(setHeat, 10 * 1000);
// run every 6 seconds
Meteor.setInterval(setHeat, 6 * 1000);
Loading

0 comments on commit c40678d

Please sign in to comment.