This repository has been archived by the owner on Sep 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
208 lines (179 loc) · 6.86 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
require('dotenv').config();
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const mongoUtil = require('./mongoUtil');
const SubmissionStore = require('./submissionStore');
const Submission = require('./submission');
const config = require('./config');
const index = require('./routes/index');
let graph = require('fbgraph');
let submissionPollster = require('./submissionPollster');
let submissionStore;
let app = express();
mongoUtil.connectToServer(function(err) {
if (err) { console.error(err); }
console.log("connected to mongodb. Open app url in browser to authenticate with facebook " +
"(if you're unsure what to do, try http://localhost:3000 )");
submissionStore = new SubmissionStore();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(require('node-sass-middleware')({
src: path.join(__dirname, 'public'),
dest: path.join(__dirname, 'public'),
indentedSyntax: true,
sourceMap: true
}));
app.use(express.static(path.join(__dirname, 'public')));
app.on('listening', index, function() {
console.log("Listening on port 3000, boss!")
});
// Routes
app.get('/', function(req, res){
res.redirect('/auth');
});
app.get('/auth', function (req, res) {
// we have no code yet
if (!req.query.code) {
console.log("Performing oauth");
let authUrl = graph.getOauthUrl({
"client_id": process.env.FB_APP_ID,
"redirect_uri": process.env.FB_REDIRECT_URI,
"scope": config.fb.scope
});
if (!req.query.error) {
res.redirect(authUrl);
} else {
res.send('access denied: ' + res.query.error);
}
}
// user is being redirected with code
else {
// send code and get access token
graph.authorize({
"client_id": process.env.FB_APP_ID,
"redirect_uri": process.env.FB_REDIRECT_URI,
"client_secret": process.env.FB_APP_SECRET,
"code": req.query.code
}, function (err) {
if (err) { console.dir(err); }
graph.setAccessToken(process.env.FB_PAGE_LONG_TOKEN);
res.redirect('/UserHasLoggedIn');
});
}
});
// Get new submissions from reddit and save them to mongo
app.get('/UserHasLoggedIn', updateFromReddit );
// Render route to browser and then start getting items from queue
app.get('/UserHasLoggedIn', start );
// catch 404 and forward to error handler
app.use(function(req, res, next) {
let err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
});
let start = function (req, res, next) {
let time = process.env.MINUTES_BETWEEN_POSTS * 60 * 1000;
console.log(
`Starting up: sharing hot submissions from /r/${process.env.REDDIT_SUB_NAME}
with > ${process.env.REDDIT_MIN_SCORE} upvotes every ${process.env.MINUTES_BETWEEN_POSTS} minutes.`);
updateFromReddit();
setInterval(function () { return updateFromReddit(); }, time);
shareQueueItem();
setInterval(shareQueueItem, time);
res.render("index", { conf: process.env});
};
let shareQueueItem = async function () {
let item = await submissionStore.queuePop();
if (item) {
await submissionStore.queueAck(item.ack);
console.log(`Sharing submission, title: ${item.payload.title}, domain: ${item.payload.domain}`);
postToFb(item.payload);
submissionStore.queueClean();
} else {
console.log("The submission queue is empty.")
}
};
let postToFb = function (item) {
let fbPost = {
message: item.title,
link: item.url
};
graph.post(`/${process.env.FB_PAGE_ID}/feed`, fbPost, function (err, res) {
if (err) { console.dir(err); }
});
};
let updateFromReddit = function (req, res, next) {
console.log(`Getting new posts from /r/${process.env.REDDIT_SUB_NAME}`);
submissionPollster.getHotPosts().then(
function (result) {
// save the result to use in routes
app.set('hotPosts', result);
result.forEach(function(item) {
let submission = new Submission (item);
stripPersonalTitle(submission);
if (isSubmissionOK(submission))
// store new submissions in cold storage and post queue.
submissionStore.ifSubmissionNew(submission, function() {
submissionStore.addToColdStore(submission);
submissionStore.queuePush(submission);
});
});
if (next) {
next();
}
}
);
};
// If the reddit submission's title is in the 1st person, remove the title
// for people to not assume authorship incorrectly.
// example "Hi guys, I've recorded an album last month, have a listen!".
function stripPersonalTitle(s) {
if (s.title.includes(" I ") || s.title.includes("I've")) { s.title = ""; }
}
function isSubmissionOK(submission) {
// Don't store NSFW or selftext posts
if (submission.is_nsfw || submission.selftext) { return false; }
let title = submission.title;
// for music pages: post from pre-approved hosting (a little hacky)
if (process.env.MUSICONLY === "true") {
let domain = submission.domain;
return domain.includes("youtu") || domain.includes("soundcloud") ||
domain.includes("bandcamp") || domain.includes("facebook");
}
// don't post:
// x-posts (those link to reddit posts)
// submissions referencing reddit comments in the title
if (title.includes("comments") || title.includes("r/") ||
title.includes("x-post") || title.includes("xpost")) { return false; }
// otherwise post it
return true;
}
require('./cleanup').Cleanup(cleanupOnClose);
function cleanupOnClose() {
console.log("Closing db connection");
mongoUtil.closeConnection();
}
module.exports = app;