-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.coffee
270 lines (241 loc) · 7.43 KB
/
main.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# Configuration Files
config = require './config.coffee'
# Requires and Variables
exp = require 'express'
app = exp.createServer()
coffee = require 'coffee-script'
less = require 'less'
fs = require 'fs'
md = require('node-markdown').Markdown
auth = require './auth.coffee'
crypto = require 'crypto'
gzippo = require 'gzippo'
process.on 'uncaughtException', (err) ->
console.log err.stack
# Session perstistence implemented with CouchDB
sessionDB = require('connect-couchdb')(exp)
# App Configuration
app.configure () ->
app.set 'view engine', 'jade'
app.set 'views', __dirname + '/views'
app.use exp.errorHandler { dumpExceptions: true, showStack: true }
app.use exp.methodOverride()
app.use exp.bodyParser()
app.use exp.cookieParser()
app.use exp.session {secret: 'nawollenwirdochmalsehn', store: new sessionDB({
host: config.sessionDBHost,
name: config.sessionDBName,
username: config.sessionDBUser,
password: config.sessionDBPass,
reapInterval: 600000,
compactInterval: 300000
})}
app.use exp.compiler { src: __dirname + '/public', dest: __dirname + '/public', enable: ['less'] }
#app.use exp.static __dirname + '/public'
app.use gzippo.staticGzip __dirname+'/public'
app.use auth.middleware()
auth.helpExpress app
# Articler Class
Articler = require('./articler.coffee').Articler
scene = new Articler config.mainDBHost, config.mainDBPort, config.mainDB
app.get '/', (req, res) ->
console.log req.user
scene.findAll (err, docs) ->
threeDocs = []
for tempDoc in [1..3]
position= Math.floor(Math.random() * docs.length)
threeDocs.push docs[position]
docs.splice position, 1
res.render 'index', {
locals: {
title: 'Spark.'
docs: threeDocs
flashError: req.flash('error') || ''
flashInfo: req.flash('info') || ''
}
}
app.get '/scenes', (req, res) ->
scene.findAll (err, docs) ->
res.render 'scenes', {
locals: {
title: 'Spark.'
docs: docs
flashError: req.flash('error') || ''
flashInfo: req.flash('info') || ''
}
}
app.get '/contact', (req, res) ->
res.render 'contact',{
locals: {
title: 'Spark.'
}
}
app.get '/new', (req, res) ->
res.render 'new', {
locals: {
title: 'Spark.'
flashError: req.flash('error') || ''
flashInfo: req.flash('info') || ''
}
}
app.post '/new', (req, res) ->
if (req.session.auth.loggedIn)
console.log req.session.auth
uid = req.session.auth.userId
scene.save {
createUserId: uid
createUserName: req.session.auth.twitter.user.name
title: req.param 'title'
body: req.param 'body'
url: req.param 'url'
found: req.param "found"
creator: req.param "creator"
created_at: new Date()
stories: []
}, (err, returnedDoc, returnedData) ->
req.flash 'info', 'Scene was successfully created! Now add a Story!'
res.redirect('/'+returnedData.id)
else
req.flash 'error', 'Scene was not created. You are not logged in.'
res.redirect('/')
app.get '/concept', (req, res) ->
fs.readFile 'CONCEPT.md', 'ascii', (err, data) ->
throw err if err
res.end md data
app.get '/howto', (req, res) ->
res.render 'howto', {
locals: {
title: 'Spark.'
}
}
app.get '/random', (req, res) ->
scene.findAllIds (err, ids) ->
if err
throw err
res.redirect '/'
else
res.redirect '/'+ids[Math.floor(Math.random() * ids.length)].id
app.get '/:id', (req, res) ->
scene.findById req.params.id, (err, doc) ->
flashInfo = req.flash 'info'
flashError = req.flash 'error'
if doc
res.render 'single', {
locals:{
title:"Spark."
id: req.params.id
doc: doc.value
flashInfo: flashInfo || ''
flashError: flashError || ''
}
}
else
res.redirect('/')
app.get '/:id/edit', (req, res) ->
scene.findById req.params.id, (err, doc) ->
if doc
res.render 'edit', {
locals:{
title:"Spark."
id: req.params.id
doc: doc.value
flashError: req.flash('error') || ''
flashInfo: req.flash('info') || ''
}
}
else
res.redirect '/'
app.post '/:id/edit', (req, res) ->
if (req.session.auth.loggedIn)
scene.findById req.params.id, (err, doc) ->
newStoryUserId = doc.value.createUserId
if (newStoryUserId == req.session.auth.userId)
newDoc = doc.value
newDoc.title = req.param 'title'
newDoc.url = req.param 'url'
newDoc.body = req.param 'body'
newDoc.found = req.param 'found'
newDoc.creator = req.param 'creator'
scene.saveById req.params.id, newDoc, (saveErr, saveDoc, saveRes) ->
throw saveErr if saveErr
req.flash 'info', 'Scene successfully edited.'
res.redirect '/'+req.params.id
else
req.flash 'error', 'Scene not edited. You are not the owner of this Scene.'
res.redirect '/'+req.params.id
else
req.flash 'error', 'Scene not edited. You are not logged in.'
res.redirect '/'+req.params.id
app.post '/:id/delete', (req, res) ->
storyId=req.params.id
scene.findById storyId, (err, doc) ->
if (req.session.auth.loggedIn)
uid = req.session.auth.userId
if (doc.value.createUserId == uid)
scene.deleteById storyId, doc.value._rev, (deleteErr, deleteRes) ->
throw deleteErr if deleteErr
req.flash 'info', 'Scene was deleted. Aww.'
res.redirect '/'
app.post '/:id/add', (req, res) ->
scene.findById req.params.id, (findErr, originalDoc) ->
if originalDoc
tempDoc = originalDoc.value
uid = req.session.auth.userId
newStory = {
title: req.param "title"
story: req.param "story"
createUserName: req.session.auth.twitter.user.name
createUserId: uid
}
hash = crypto.createHmac('sha1', 'abcdeg').update(newStory.title+newStory.story+newStory.createUserId).digest('hex')
newStory._id=hash
tempDoc.stories.push newStory
scene.saveById req.params.id, tempDoc, (saveErr, doc, saveRes) ->
throw saveErr if saveErr
req.flash 'info', 'Story was added to this Scene. Thank you!'
res.redirect('/'+req.params.id)
else
req.flash 'error', 'That did not work.'
res.redirect('/'+req.params.id)
app.post '/:id/delete/:commentId', (req, res) ->
scene.findById req.params.id, (err, doc) ->
newStoryUserId = 'not authed'
newStories = []
stories = doc.value.stories
stories.forEach (story) ->
if (story._id != req.params.commentId)
newStories.push story
else
newStoryUserId = story.createUserId
doc.value.stories = newStories
if (newStoryUserId == req.session.auth.userId)
scene.saveById req.params.id, doc.value, (saveErr, saveDoc, saveRes) ->
throw saveErr if saveErr
req.flash 'info', 'The Story is now gone. Care to write another one?'
res.redirect '/'+req.params.id
else
req.flash 'error', 'You are not the owner of this Story. Someone will get mad!'
res.redirect '/'+req.params.id
app.post '/:id/save/:commentId', (req, res) ->
scene.findById req.params.id, (err, doc) ->
newStoryUserId = 'not authed'
newStories = []
stories = doc.value.stories
stories.forEach (story) ->
if (story._id == req.params.commentId)
story.title = req.param 'title'
story.story = req.param 'story'
newStoryUserId = story.createUserId
newStories.push story
doc.value.stories = newStories
if (newStoryUserId == req.session.auth.userId)
scene.saveById req.params.id, doc.value, (saveErr, saveDoc, saveRes) ->
throw saveErr if saveErr
req.flash 'info', 'Story successfully changed.'
res.redirect '/'+req.params.id
else
req.flash 'error', 'That was not your Story.'
res.redirect '/'+req.params.id
# Run App
app.listen config.appPort
console.log 'Spark running on port '+config.appPort