-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.coffee
214 lines (185 loc) · 7.35 KB
/
app.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
argv = require("optimist").default("port", 3000).argv
errorhandler = require("errorhandler")
express = require("express")
stylus = require("stylus")
require("./lib/date")
db = require("./db")
renderError = (err, req, res) ->
res.render "error", {
error: err.message
site: req.query.site
}
preprocessQuery = (req, res, next) ->
for own key, value of req.query
req.query[key] = value[0] if Object.prototype.toString.call(value) == "[object Array]"
next()
baseArgs = (req) ->
{
site: req.query.site
category: req.query.category || ""
startDate: req.query.start_date || ""
endDate: req.query.end_date || ""
page: req.query.page || ""
}
app = module.exports = express()
app.set('views', "#{__dirname}/views")
app.set("view engine", "jade")
app.use(stylus.middleware({src: "#{__dirname}/views", dest: "#{__dirname}/public"}))
app.use(express.static("#{__dirname}/public"))
for own key, val of require("./helpers")
app.locals[key] = val
app.get "/", (req, res) ->
db.dataDate (date) ->
res.render "index", {
dataDate: date.format("dd MMM yyyy")
site: "mefi"
}
app.get "/comments/bested", preprocessQuery, (req, res) ->
db.userByID req.query.commenter_id, (commenter) ->
return renderError(new Error("Unknown user."), req, res) if !commenter
args = baseArgs req
args.commenter = commenter
db.commentsBested args, (comments) ->
args.comments = comments
res.render "comments_bested", args
app.get "/comments/favorited", preprocessQuery, (req, res) ->
db.userByID req.query.commenter_id, (commenter) ->
return renderError(new Error("Unknown user."), req, res) if !commenter
db.userByID req.query.faver_id, (faver) ->
return renderError(new Error("Unknown user."), req, res) if !faver
args = baseArgs req
args.commenter = commenter
args.faver = faver
db.commentsFavorited args, (comments) ->
args.comments = comments
res.render "comments_favorited", args
app.get "/comments/favorites", preprocessQuery, (req, res) ->
db.userByName req.query.commenter_name, (commenter) ->
return renderError(new Error("Unknown username."), req, res) if req.query.commenter_name && !commenter
args = baseArgs req
args.commenter = commenter
db.commentsFavorites args, (comments) ->
args.comments = comments
res.render "comments_favorites", args
app.get "/comments/longest", preprocessQuery, (req, res) ->
db.userByName req.query.commenter_name, (commenter) ->
return renderError(new Error("Unknown username."), req, res) if req.query.commenter_name && !commenter
args = baseArgs req
args.commenter = commenter
db.commentsLongest args, (comments) ->
args.comments = comments
res.render "comments_longest", args
app.get "/favees/comments", preprocessQuery, (req, res) ->
db.userByName req.query.faver_name, (faver) ->
return renderError(new Error("Unknown username."), req, res) if req.query.faver_name && !faver
args = baseArgs req
args.faver = faver
db.faveesComments args, (users) ->
args.users = users
res.render "favees_comments", args
app.get "/favees/posts", preprocessQuery, (req, res) ->
db.userByName req.query.faver_name, (faver) ->
return renderError(new Error("Unknown username."), req, res) if req.query.faver_name && !faver
args = baseArgs req
args.faver = faver
db.faveesPosts args, (users) ->
args.users = users
res.render "favees_posts", args
app.get "/favers/comments", preprocessQuery, (req, res) ->
db.userByName req.query.commenter_name, (commenter) ->
return renderError(new Error("Unknown username."), req, res) if !commenter
args = baseArgs req
args.commenter = commenter
db.faversComments args, (favers) ->
args.favers = favers
res.render "favers_comments", args
app.get "/favers/posts", preprocessQuery, (req, res) ->
db.userByName req.query.poster_name, (poster) ->
return renderError(new Error("Unknown username."), req, res) if !poster
args = baseArgs req
args.poster = poster
db.faversPosts args, (favers) ->
args.favers = favers
res.render "favers_posts", args
app.get "/posts/comments", preprocessQuery, (req, res) ->
db.userByName req.query.poster_name, (poster) ->
return renderError(new Error("Unknown username."), req, res) if req.query.poster_name && !poster
args = baseArgs req
args.tag = req.query.tag
args.poster = poster
db.postsComments args, (posts) ->
args.posts = posts
res.render "posts_comments", args
app.get "/posts/favorited", preprocessQuery, (req, res) ->
db.userByID req.query.poster_id, (poster) ->
return renderError(new Error("Unknown user."), req, res) if !poster
db.userByID req.query.faver_id, (faver) ->
return renderError(new Error("Unknown user."), req, res) if !faver
args = baseArgs req
args.poster = poster
args.faver = faver
db.postsFavorited args, (posts) ->
args.posts = posts
res.render "posts_favorited", args
app.get "/posts/favorites", preprocessQuery, (req, res) ->
db.userByName req.query.poster_name, (poster) ->
return renderError(new Error("Unknown username."), req, res) if req.query.poster_name && !poster
args = baseArgs req
args.tag = req.query.tag
args.poster = poster
db.postsFavorites args, (posts) ->
args.posts = posts
res.render "posts_favorites", args
app.get "/posts/favorites_comments_ratio", preprocessQuery, (req, res) ->
db.userByName req.query.poster_name, (poster) ->
return renderError(new Error("Unknown username."), req, res) if req.query.poster_name && !poster
args = baseArgs req
args.tag = req.query.tag
args.poster = poster
db.postsFavoritesCommentsRatio args, (posts) ->
args.posts = posts
res.render "posts_favorites_comments_ratio", args
app.get "/posts/find", preprocessQuery, (req, res) ->
args = baseArgs req
args.username = req.query.username
args.title = req.query.title
args.tag = req.query.tag
args.reason = req.query.reason
args.commentsCount = req.query.comments_count
args.favoritesCount = req.query.favorites_count
db.postsFind args, (posts) ->
args.posts = posts
res.render "posts_find", args
app.get "/posts/longest", preprocessQuery, (req, res) ->
db.userByName req.query.poster_name, (poster) ->
return renderError(new Error("Unknown username."), req, res) if req.query.poster_name && !poster
args = baseArgs req
args.tag = req.query.tag
args.poster = poster
db.postsLongest args, (posts) ->
args.posts = posts
res.render "posts_longest", args
app.get "/users/best", preprocessQuery, (req, res) ->
args = baseArgs req
db.usersBest args, (users) ->
args.users = users
res.render "users_best", args
app.get "/users/comments", preprocessQuery, (req, res) ->
args = baseArgs req
db.usersComments args, (users) ->
args.users = users
res.render "users_comments", args
app.get "/users/find", preprocessQuery, (req, res) ->
args = baseArgs req
args.username = req.query.username
db.usersFind args, (users) ->
args.users = users
res.render "users_find", args
app.get "/users/posts", preprocessQuery, (req, res) ->
args = baseArgs req
db.usersPosts args, (users) ->
args.users = users
res.render "users_posts", args
app.use(errorhandler())
app.listen argv.port, () ->
console.log("running on port #{argv.port}...")