-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
194 lines (166 loc) · 6.16 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
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
'use strict';
// modules
const snoowrap = require('snoowrap');
const http = require('http');
const express = require('express');
const hbs = require('hbs');
const config = require('./config');
const cookieParser = require('cookie-parser');
const session = require('express-session');
// messing around with partials
hbs.registerPartials(__dirname + '/views/partials');
hbs.registerHelper('getCurrentYear', function () {
return new Date().getFullYear();
})
var app = express();
// Using MemoryStore for sessions since this is a small app
app.use(cookieParser());
app.use(session({
secret: "Shh, its a secret!",
resave: false,
saveUninitialized: false
}));
app.use("/assets", express.static(__dirname + "/assets"));
app.use("/views", express.static(__dirname + "/views"));
const port = 8080;
const url = "http://localhost:8080";
// views folder is default directory express uses
app.set('view engine', 'hbs');
// redirect to /saved when authorization is successful
app.get('/authorize_callback', (req, res) => {
req.session.AuthCodeProperties = {};
req.session.AuthCodeProperties = req.query;
res.redirect('/saved')
})
// call API for saved posts
app.get('/saved', (req, res) => {
if( !req.session.authObject) {
snoowrap.fromAuthCode({
code: req.session.AuthCodeProperties.code,
userAgent: config.userAgent,
clientId: config.clientId,
clientSecret: config.clientSecret,
redirectUri: url + "/authorize_callback"
}).then(r => {
req.session.authObject = r;
// don't reload data if we have it
if (!req.session.loadedSavedData) {
// results returned can be changed here
r.getMe().getSavedContent({limit: Infinity}).then(jsonResponse => {
req.session.data = jsonResponse;
req.session.sortedObj = {};
req.session.sortedComments = {};
req.session.subreddits = [];
seperateCategories(jsonResponse, req);
req.session.loadedSavedData = true;
renderMainPage(res, req);
})
} else {
renderMainPage(res, req);
}
})
} else {
const authObject = new snoowrap(
req.session.authObject
);
authObject.getMe().getSavedContent({limit: Infinity}).then(jsonResponse => {
req.session.data = jsonResponse;
req.session.sortedObj = {};
req.session.sortedComments = {};
req.session.subreddits = [];
seperateCategories(jsonResponse, req);
req.session.loadedSavedData = true;
renderMainPage(res, req);
})
}
})
// create authentication URL and render home page
app.get('/', function(req,res) {
var authenticationUrl = snoowrap.getAuthUrl({
clientId: config.clientId,
scope: [ 'save', 'history', 'identity', 'read'],
redirectUri: url + '/authorize_callback',
permanent: false,
state: 'fe311bebc52eb3da9bef8db6e63104d3' // a random string, this could be validated when the user is redirected back
});
// --> 'https://www.reddit.com/api/v1/authorize?client_id=foobarbaz&response_type=code&state= ...'
res.render('start.hbs', {
authenticationUrl: authenticationUrl,
});
});
// get raw json
app.get('/unformatted', (req, res) => {
var x = r.getMe().getSavedContent({limit: 3}).then(jsonResponse => {
res.send(jsonResponse);
})
})
// unsave post
app.get('/unsaveSubmission/:id', (req, res) => {
const authObject = new snoowrap(
req.session.authObject
);
for (var i = 0, len = req.session.data.length; i < len; i++) {
if (req.session.data[i].id === req.params.id) {
authObject.getSubmission(req.params.id).unsave().then(test => {
res.send('success');
})
}
}
})
// unsave comment
app.get('/unsaveComment/:id', (req, res) => {
const authObject = new snoowrap(
req.session.authObject
);
for (var i = 0, len = req.session.data.length; i < len; i++) {
if (req.session.data[i].id === req.params.id) {
authObject.getComment(req.params.id).unsave().then(test => {
res.send('success');
})
}
}
// res.send('fail')
})
app.listen(port, () => {
console.log('Our app is running on http://localhost:' + port);
})
var renderMainPage = function(res,req){
res.render('saved.hbs', {
pageTitle: 'Saved',
formattedData : req.session.sortedObj,
formmatedComments : req.session.sortedComments,
hasPosts : !isEmpty(req.session.sortedObj),
hasComments : !isEmpty(req.session.sortedComments),
subreddits : req.session.subreddits
});
}
var isEmpty = function (obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return JSON.stringify(obj) === JSON.stringify({});
}
var seperateCategories = function (unsortedObj, req) {
for (var i = 0; i < unsortedObj.length; i++) {
// if object is a post
// using body_html to differentiate posts form comments
if (unsortedObj[i].body_html === undefined) {
if (req.session.sortedObj[unsortedObj[i].subreddit_name_prefixed]){
req.session.sortedObj[unsortedObj[i].subreddit_name_prefixed].push(unsortedObj[i]);
} else {
req.session.sortedObj[unsortedObj[i].subreddit_name_prefixed] = [ unsortedObj[i] ];
//add to subreddits array since this is first TODO DRY
req.session.subreddits.push(unsortedObj[i].subreddit_name_prefixed);
}
}
// if object is a comment
else {
if (req.session.sortedComments[unsortedObj[i].subreddit_name_prefixed]){
req.session.sortedComments[unsortedObj[i].subreddit_name_prefixed].push(unsortedObj[i]);
} else {
req.session.sortedComments[unsortedObj[i].subreddit_name_prefixed] = [ unsortedObj[i] ];
}
}
}
}