-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
118 lines (95 loc) · 3.22 KB
/
app.js
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
var express = require('express');
var mongoose = require('mongoose');
var Q = require('q');
//configure dependencies
var MONGO_URL = process.env.MONGODB_URI ||
'mongodb://localhost/test';
var PORT = process.env.PORT || 3000;
mongoose.connect(MONGO_URL);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback() {
console.log("Connected to database.");
});
var ephemeralDB = require('./models');
var models = ephemeralDB.init(mongoose);
require('./scheduler').init(ephemeralDB, models);
// Configure express.js
var app = express();
app.use(express.json());
app.use(express.urlencoded());
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
// adds user count to the request object
app.use(function(req, res, next) {
var numSent = Q.defer();
var numUnsent = Q.defer();
var totalNumMsg = Q.defer();
ephemeralDB.getNumSentEphemerals(models, numSent.resolve);
ephemeralDB.getNumUnsentEphemerals(models, numUnsent.resolve);
ephemeralDB.getTotalMessageCount(models, totalNumMsg.resolve);
Q.all([numSent.promise, numUnsent.promise, totalNumMsg.promise]).then(function(counts) {
app.locals.counts = {
sent: counts[0],
unsent: counts[1],
totalMsg: counts[2]
};
next();
});
});
app.use('/public', express.static(__dirname + '/public'));
// Main page with the form
app.get('/', function(req, res) {
res.render('index', {});
});
// Generic onSuccess and onFailure functions
var onSuccess = function(result) {
console.log("Success! " + result);
};
var onFailure = function(result) {
console.log("Error! " + result);
};
app.get('/error', function(req, res) {
res.render('error');
});
app.get('/success', function(req, res) {
res.render('success');
});
app.get('/ephemeral_stats', function(req, res) {
res.render('ephemeralStats');
});
// Add an Ephemeral
app.post('/message', function(req, res) {
// Need to create new date Object based on user's current time zone
var d = new Date(req.body.send_date);
d.setUTCMinutes(req.body.timezone_offset);
var message = {
"content": req.body.content,
"recipient": req.body.recipient,
"send_date": d.toJSON(),
"from_user": req.body.from_user
};
ephemeralDB.addEphemeral(models, message, onSuccess, onFailure);
res.redirect('/success');
});
// Tracking pixel to delete ephemeral when the page has loaded!
app.get('/tracking.px', function(req, res) {
var _id = new mongoose.Types.ObjectId(req.query.message_id);
ephemeralDB.findAndRemoveEphemeral(models, _id, onSuccess("Ephemeral Deleted"));
res.redirect('/public/images/track.png');
});
// Get and display message from server/database
app.get('/:message_id', function(req, res) {
var id = req.params.message_id;
// Redirect user to error page if the ephemeral doesn't exist
function onFoundSuccess(ephemeral) {
if (!ephemeral) {
res.status(404);
res.redirect('/error');
}
res.render('ephemeral', ephemeral);
}
ephemeralDB.getEphemeral(models, id, onFoundSuccess, onFailure);
});
app.listen(PORT);
console.log('Listening on port ' + PORT);