Skip to content
Newer
Older
100644 308 lines (277 sloc) 8.54 KB
15360a2 @gerad wip
gerad authored
1 sys = require 'sys'
2 connect = require 'connect'
3 express = require 'express'
ac6a3a6 @gerad mongodb
gerad authored
4
15360a2 @gerad wip
gerad authored
5 models = require './models/models'
6 [Team, Person] = [models.Team, models.Person]
c6fce2a @gerad resend invitation, confirm delete
gerad authored
7
c67844f @gerad wip
gerad authored
8 pub = __dirname + '/public';
9 app = express.createServer(
10 connect.compiler({ src: pub, enable: ['sass'] }),
11 connect.staticProvider(pub)
12 )
b77ee23 @gerad edit person
gerad authored
13
d926e4d @gerad work on teams create
gerad authored
14 app.use connect.logger()
15 app.use connect.bodyDecoder()
0594309 @gerad working method override, order matters. fuck.
gerad authored
16 app.use connect.methodOverride()
d926e4d @gerad work on teams create
gerad authored
17 app.use connect.cookieDecoder()
b977208 @gerad use encrypted password
gerad authored
18
d926e4d @gerad work on teams create
gerad authored
19 app.enable 'show exceptions'
b977208 @gerad use encrypted password
gerad authored
20
d926e4d @gerad work on teams create
gerad authored
21 request = (type) ->
22 (path, fn) ->
23 app[type] path, (req, res, next) =>
58550ff @gerad edit person
gerad authored
24 Person.firstByAuthKey req.cookies.authkey, (error, person) =>
1efc0ea count teams rather than grabbing them all, on all pages
Visnu Pitiyanuvath authored
25 Team.count (error, teamCount) =>
26 ctx = {
27 sys: sys
28 req: req
29 res: res
30 next: next
31 redirect: __bind(res.redirect, res),
32 cookie: (key, value, options) ->
33 value ||= ''
34 options ||= {}
35 cookie = "#{key}=#{value}"
36 for k, v of options
37 cookie += "; #{k}=#{v}"
38 res.header('Set-Cookie', cookie)
39 render: (file, opts) ->
40 opts ||= {}
41 opts.locals ||= {}
42 opts.locals.view = file.replace(/\..*$/,'').replace(/\//,'-')
43 opts.locals.ctx = ctx
44 res.render file, opts
45 currentPerson: person
46 teamsLeft: 222 - teamCount
47 setCurrentPerson: (person, options) ->
48 @cookie 'authKey', person?.authKey(), options
49 redirectToTeam: (person, alternatePath) ->
50 Team.first { 'members._id': person._id }, (error, team) =>
51 if team?
52 @redirect '/teams/' + team.id()
53 else
54 @redirect (alternatePath or '/')
55 redirectToLogin: ->
56 @redirect "/login?return_to=#{@req.url}"
57 logout: (fn) ->
58 @currentPerson.logout (error, resp) =>
59 @setCurrentPerson null
60 fn()
61 canEditTeam: (team) ->
62 req.cookies.teamauthkey is team.authKey() or
63 team.hasMember(@currentPerson)
64 ensurePermitted: (other, fn) ->
65 permitted = if other.hasMember?
66 @canEditTeam other
d926e4d @gerad work on teams create
gerad authored
67 else
1efc0ea count teams rather than grabbing them all, on all pages
Visnu Pitiyanuvath authored
68 @currentPerson? and (other.id() is @currentPerson.id())
69 if permitted then fn()
a54244d @gerad teams/edit
gerad authored
70 else
1efc0ea count teams rather than grabbing them all, on all pages
Visnu Pitiyanuvath authored
71 unless @currentPerson?
72 @redirectToLogin()
73 else
74 # TODO flash "Oops! You don't have permissions to see that. Try logging in as somebody else."
75 @logout =>
76 @redirectToLogin()}
77 __bind(fn, ctx)()
d926e4d @gerad work on teams create
gerad authored
78 get = request 'get'
79 post = request 'post'
ef2dd99 @gerad wip teams update
gerad authored
80 put = request 'put'
2b2a930 @gerad delete working
gerad authored
81 del = request 'del'
31e8199 @gerad pass through context, view name
gerad authored
82
83 get /.*/, ->
84 [host, path] = [@req.header('host'), @req.url]
15360a2 @gerad wip
gerad authored
85 if host == 'www.nodeknockout.com' or host == 'nodeknockout.heroku.com'
c991680 @gerad more bug fixes
gerad authored
86 @redirect "http://nodeknockout.com#{path}", 301
15360a2 @gerad wip
gerad authored
87 else
31e8199 @gerad pass through context, view name
gerad authored
88 @next()
dadf031 @gerad permissions
gerad authored
89
31e8199 @gerad pass through context, view name
gerad authored
90 get '/', ->
1efc0ea count teams rather than grabbing them all, on all pages
Visnu Pitiyanuvath authored
91 @render 'index.html.haml'
dadf031 @gerad permissions
gerad authored
92
501adfc @gerad /me redirects to edit for the current person (or login if necessary)
gerad authored
93 get '/me', ->
94 if @currentPerson?
95 @redirect "/people/#{@currentPerson.id()}/edit"
96 else
97 @redirectToLogin()
98
31e8199 @gerad pass through context, view name
gerad authored
99 get '/*.js', ->
69566c4 @gerad wip application.js.coffee
gerad authored
100 try
c798a63 @gerad teams/new
gerad authored
101 @render "#{@req.params[0]}.js.coffee", { layout: false }
69566c4 @gerad wip application.js.coffee
gerad authored
102 catch e
31e8199 @gerad pass through context, view name
gerad authored
103 @next()
c67844f @gerad wip
gerad authored
104
9666788 @gerad /register /teams
gerad authored
105 get '/register', ->
1efc0ea count teams rather than grabbing them all, on all pages
Visnu Pitiyanuvath authored
106 altPath = if @teamsLeft <= 0
107 "/login?return_to=#{@req.url}"
108 else
109 'teams/new'
110 if @currentPerson?
111 @redirectToTeam @currentPerson, '/teams/new'
112 else
113 @redirect altPath
c991680 @gerad more bug fixes
gerad authored
114
9666788 @gerad /register /teams
gerad authored
115 # list teams
116 get '/teams', ->
117 Team.all (error, teams) =>
e311be4 segregate the teams by verified or not
Visnu Pitiyanuvath authored
118 [@teams, @unverifiedTeams] = [[], []]
119 for team in teams
120 if team.members.length == team.invited.length
121 @unverifiedTeams.push team
122 else
123 @teams.push team
9666788 @gerad /register /teams
gerad authored
124 @yourTeams = if @currentPerson?
125 _.select teams, (team) =>
126 # TODO this is gross
127 _ids = _.pluck(team.members, '_id')
128 _.include _.pluck(_ids, 'id'), @currentPerson._id.id
129 else []
130 @render 'teams/index.html.haml'
c798a63 @gerad teams/new
gerad authored
131
6a1583b @gerad handle joyent being full
gerad authored
132 get '/teams/attending', ->
133 Team.all (error, teams) =>
134 @joyentTotal = Team.joyentTotal teams
135 @teams = _.select teams, (team) ->
136 parseInt(team.joyent_count) > 0
137 @render 'teams/index.html.haml'
138
c798a63 @gerad teams/new
gerad authored
139 # new team
140 get '/teams/new', ->
1efc0ea count teams rather than grabbing them all, on all pages
Visnu Pitiyanuvath authored
141 if @teamsLeft <= 0
142 @redirect '/'
143 else
144 Team.all (error, teams) =>
6a1583b @gerad handle joyent being full
gerad authored
145 @joyentTotal = Team.joyentTotal teams
c798a63 @gerad teams/new
gerad authored
146 @team = new Team {}, =>
147 @render 'teams/new.html.haml'
d926e4d @gerad work on teams create
gerad authored
148
149 # create team
150 post '/teams', ->
6a1583b @gerad handle joyent being full
gerad authored
151 @req.body.joyent_count = parseInt(@req.body.joyent_count) || 0
3639113 @gerad wip team/create
gerad authored
152 @team = new Team @req.body, =>
d926e4d @gerad work on teams create
gerad authored
153 @team.save (errors, res) =>
154 if errors?
155 @errors = errors
156 @render 'teams/new.html.haml'
157 else
7381de5 @gerad fix cookie issue; fix stay loggedin
gerad authored
158 @cookie 'teamAuthKey', @team.authKey()
d926e4d @gerad work on teams create
gerad authored
159 @redirect '/teams/' + @team.id()
74dd45e @gerad teams/show
gerad authored
160
161 # show team
162 get '/teams/:id', ->
1efc0ea count teams rather than grabbing them all, on all pages
Visnu Pitiyanuvath authored
163 Team.first @req.param('id'), (error, team) =>
164 if team?
165 Team.all (error, teams) =>
6a1583b @gerad handle joyent being full
gerad authored
166 @joyentTotal = Team.joyentTotal teams
167 @team = team
168 people = team.members or []
169 @members = _.select people, (person) -> person.name
170 @invites = _.without people, @members...
171 @editAllowed = @canEditTeam team
172 @render 'teams/show.html.haml'
1efc0ea count teams rather than grabbing them all, on all pages
Visnu Pitiyanuvath authored
173 else
174 # TODO make this a 404
175 @redirect '/'
a54244d @gerad teams/edit
gerad authored
176
177 # edit team
178 get '/teams/:id/edit', ->
1efc0ea count teams rather than grabbing them all, on all pages
Visnu Pitiyanuvath authored
179 Team.first @req.param('id'), (error, team) =>
180 Team.all (error, teams) =>
6a1583b @gerad handle joyent being full
gerad authored
181 @ensurePermitted team, =>
182 @joyentTotal = Team.joyentTotal teams
183 @team = team
184 @render 'teams/edit.html.haml'
a54244d @gerad teams/edit
gerad authored
185
ef2dd99 @gerad wip teams update
gerad authored
186 # update team
187 put '/teams/:id', ->
188 Team.first @req.param('id'), (error, team) =>
189 @ensurePermitted team, =>
190 team.joyent_count ||= 0
6a1583b @gerad handle joyent being full
gerad authored
191 @req.body.joyent_count = parseInt(@req.body.joyent_count) || 0
ef2dd99 @gerad wip teams update
gerad authored
192 team.update @req.body
193 save = =>
194 team.save (errors, result) =>
195 if errors?
196 @errors = errors
197 @team = team
40a00e2 @gerad handle errors in joyent count
gerad authored
198 if @req.xhr
199 @res.send 'ERROR', 500
200 else
201 @render 'teams/edit.html.haml'
ef2dd99 @gerad wip teams update
gerad authored
202 else
40a00e2 @gerad handle errors in joyent count
gerad authored
203 if @req.xhr
204 @res.send 'OK', 200
205 else
206 @redirect '/teams/' + team.id()
ef2dd99 @gerad wip teams update
gerad authored
207 # TODO shouldn't need this
208 if @req.body.emails
209 team.setMembers @req.body.emails, save
210 else save()
2b2a930 @gerad delete working
gerad authored
211
212 # delete team
213 del '/teams/:id', ->
214 Team.first @req.param('id'), (error, team) =>
215 @ensurePermitted team, =>
216 team.remove (error, result) =>
217 @redirect '/'
218
5444ade @gerad reinvite
gerad authored
219 # resend invitation
220 get '/teams/:teamId/invite/:personId', ->
221 Team.first @req.param('teamId'), (error, team) =>
222 @ensurePermitted team, =>
223 Person.first @req.param('personId'), (error, person) =>
224 person.inviteTo team, =>
225 if @req.xhr
226 @res.send 'OK', 200
227 else
228 # TODO flash "Sent a new invitation to $@person.email"
229 @redirect '/teams/' + team.id()
230
19470be @gerad reset password, login page, start on people/edit
gerad authored
231 # sign in
232 get '/login', ->
233 @person = new Person()
234 @render 'login.html.haml'
235
bdefc7a @gerad login
gerad authored
236 post '/login', ->
237 Person.login @req.body, (error, person) =>
238 if person?
239 if @req.param 'remember'
240 d = new Date()
241 d.setTime(d.getTime() + 1000 * 60 * 60 * 24 * 180)
242 options = { expires: d }
243 @setCurrentPerson person, options
244 if person.name
245 if returnTo = @req.param('return_to')
246 @redirect returnTo
247 else @redirectToTeam person
248 else
249 @redirect '/people/' + person.id() + '/edit'
250 else
251 @errors = error
252 @person = new Person(@req.body)
253 @render 'login.html.haml'
19470be @gerad reset password, login page, start on people/edit
gerad authored
254
58550ff @gerad edit person
gerad authored
255 get '/logout', ->
256 @redirect '/' unless @currentPerson?
257 @logout =>
258 @redirect '/'
259
260 # reset password
261 post '/reset_password', ->
262 Person.first { email: @req.param('email') }, (error, person) =>
263 # TODO assumes xhr
264 unless person?
265 @res.send 'Email not found', 404
266 else
267 person.resetPassword =>
268 @res.send 'OK', 200
269
270 # edit person
271 get '/people/:id/edit', ->
272 Person.first @req.param('id'), (error, person) =>
273 @ensurePermitted person, =>
274 @person = person
275 @render 'people/edit.html.haml'
276
277 # update person
278 put '/people/:id', ->
279 Person.first @req.param('id'), (error, person) =>
280 @ensurePermitted person, =>
281 attributes = @req.body
282
283 # TODO this shouldn't be necessary
284 person.setPassword attributes.password if attributes.password
285 delete attributes.password
286
287 attributes.link = '' unless /^https?:\/\/.+\./.test attributes.link
288 person.update attributes
289 person.save (error, resp) =>
290 @redirectToTeam person
291
75ea889 try to find a view at the end
Visnu Pitiyanuvath authored
292 get '/*', ->
d78ce6c @gerad get rid of debugging information
gerad authored
293 try
294 @render "#{@req.params[0]}.html.haml"
295 catch e
296 throw e if e.errno != 2
297 @next()
75ea889 try to find a view at the end
Visnu Pitiyanuvath authored
298
e311be4 segregate the teams by verified or not
Visnu Pitiyanuvath authored
299 app.helpers {
300 pluralize: (n, str) ->
301 if n == 1
302 n + ' ' + str
303 else
304 n + ' ' + str + 's'
305 }
306
15360a2 @gerad wip
gerad authored
307 server = app.listen parseInt(process.env.PORT || 8000), null
Something went wrong with that request. Please try again.