@@ -3,16 +3,85 @@ var router = express.Router();
3
3
var path = require ( 'path' ) ;
4
4
var bodyParser = require ( 'body-parser' ) ;
5
5
var parseForm = bodyParser . urlencoded ( { extended : false } ) ;
6
+ var Articles = require ( path . join ( __dirname , '..' , 'services' , 'articles' ) ) ;
7
+ var Users = require ( path . join ( __dirname , '..' , 'services' , 'users' ) ) ;
8
+ var Comments = require ( path . join ( __dirname , '..' , 'services' , 'comments' ) ) ;
9
+ var moment = require ( 'moment' ) ;
10
+ var _ = require ( 'lodash' ) ;
6
11
var mcapi = require ( 'mailchimp-api' ) ;
7
12
8
13
mc = new mcapi . Mailchimp ( process . env . MAILCHIMP_API ) ;
9
14
10
15
/* GET home page. */
11
- router . get ( '/' , function ( req , res , next ) {
12
- res . render ( 'index' ) ;
16
+ router . get ( '/' , parseForm , function ( req , res ) {
17
+ var offset = req . query . page ;
18
+ var more ;
19
+ if ( offset ) {
20
+ Articles . paginated ( offset , function ( all ) {
21
+ Articles . totalPublished ( function ( total ) {
22
+ var lastDay ;
23
+ // TODO: Move date functionality into a serivce. It'll be used practically
24
+ // everywhere. Oh, and refactor this blasphemy.
25
+ //
26
+ // TODO: We shouldn't be needing to use moment in order to make the time
27
+ // UTC. There's an issue with pg and it's parsing the dates from the db
28
+ // incorrectly. This is a temporary fix until I can snipe the bug.
29
+ all . map ( function ( item ) {
30
+ item . date = moment . utc ( item . published_at ) . format ( 'LL' ) ;
31
+ if ( item . date == moment . utc ( Date . now ( ) ) . format ( 'LL' ) ) {
32
+ item . date = 'Today'
33
+ } else if ( item . date == moment . utc ( Date . now ( ) ) . subtract ( 1 , 'days' ) . format ( 'LL' ) ) {
34
+ item . date = 'Yesterday'
35
+ } else {
36
+ item . date = moment ( item . date ) . format ( 'dddd, LL' ) ;
37
+ }
38
+ } ) ;
39
+ // There are strings and integers here - not so good.
40
+ if ( all . length ) {
41
+ lastDay = all [ all . length - 1 ] . date ;
42
+ }
43
+ more = ( all . length == ( total [ 0 ] . count - offset ) ) ? false : true ;
44
+ all = _ . groupBy ( all , 'date' ) ;
45
+ res . json ( { flow : all , more : more , lastDay : lastDay } ) ;
46
+ } ) ;
47
+ } ) ;
48
+ } else {
49
+ Articles . recent ( function ( all ) {
50
+ Articles . totalPublished ( function ( total ) {
51
+ var flow = [ ] , news = [ ] , lastDay ;
52
+ // TODO: Move date functionality into a serivce. It'll be used practically
53
+ // everywhere. Oh, and refactor this blasphemy.
54
+ //
55
+ // TODO: We shouldn't be needing to use moment in order to make the time
56
+ // UTC. There's an issue with pg and it's parsing the dates from the db
57
+ // incorrectly. This is a temporary fix until I can snipe the bug.
58
+ all . map ( function ( item ) {
59
+ item . date = moment . utc ( item . published_at ) . format ( 'LL' ) ;
60
+ if ( item . date == moment . utc ( Date . now ( ) ) . format ( 'LL' ) ) {
61
+ item . date = 'Today'
62
+ } else if ( item . date == moment . utc ( Date . now ( ) ) . subtract ( 1 , 'days' ) . format ( 'LL' ) ) {
63
+ item . date = 'Yesterday'
64
+ } else {
65
+ item . date = moment ( item . date ) . format ( 'dddd, LL' ) ;
66
+ }
67
+ if ( item . news ) {
68
+ news . push ( item ) ;
69
+ } else {
70
+ flow . push ( item ) ;
71
+ }
72
+ } ) ;
73
+ if ( flow . length ) {
74
+ lastDay = flow [ flow . length - 1 ] . date ;
75
+ }
76
+ more = ( flow . length === parseInt ( total [ 0 ] . count ) ) ? false : true ;
77
+ flow = _ . groupBy ( flow , 'date' ) ;
78
+ res . render ( 'news/index' , { flow_collection : flow , news_collection : news , more : more , lastDay : lastDay } ) ;
79
+ } ) ;
80
+ } ) ;
81
+ }
13
82
} ) ;
14
83
15
- /* POST subscribe an email to JS5 list. */
84
+ /* POST subscribe an email to JS6 list. */
16
85
router . post ( '/subscribe' , parseForm , function ( req , res ) {
17
86
mc . lists . subscribe (
18
87
{
0 commit comments