-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit-function.js
99 lines (96 loc) · 3.12 KB
/
reddit-function.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
const fli = require('fli-webtask');
const wt = require('webtask-tools');
const bodyParser = require('body-parser');
const snoowrap = require('snoowrap');
const btoa = require('btoa');
const express = fli.npm.express;
const request = fli.npm.request;
const as = fli.npm.async;
const _ = fli.npm.lodash;
const loader = fli.lib.loader;
const responseHandler = fli.lib.responseHandler;
const app = express();
const router = express.Router();
const validateMiddleware = (req, res, next) => {
if(req.webtaskContext.secrets.token !== req.query.token) {
const errMsgToken = 'No token.';
responseHandler(errMsgToken, res);
return next(errMsgToken);
}
if(!_.get(req, 'body._id')) {
const errMsgId = 'No _id provided.';
responseHandler(errMsgId, res);
return next(errMsgId);
}
var rUrl = _.get(req, 'body.payload.url');
if(!rUrl) {
const errMsgUrl = 'No url provided.';
responseHandler(errMsgUrl, res);
return next(errMsgUrl);
}
req.rUrl = rUrl;
return next();
};
const redditMiddleware = (req, res, next) => {
var db = _.get(req, 'body.db');
var index = _.get(req, 'body.index');
var userAgent = req.webtaskContext.secrets[`${db}-userAgent`];
var clientId = req.webtaskContext.secrets[`${db}-clientId`];
var clientSecret = req.webtaskContext.secrets[`${db}-clientSecret`];
var refreshToken = index !== undefined ?
req.webtaskContext.secrets[`${db}-${index}-refreshToken`] :
req.webtaskContext.secrets[`${db}-refreshToken`];
var subreddit = req.webtaskContext.secrets[`${db}-subreddit`];
if(!(userAgent && clientId && clientSecret && refreshToken && subreddit)) {
const errMsgReddit = 'No reddit publisher.';
responseHandler(errMsgReddit, res);
return next(errMsgReddit);
}
req.reddit = new snoowrap({
userAgent: userAgent,
clientId: clientId,
clientSecret: clientSecret,
refreshToken: refreshToken
});
req.subreddit = subreddit;
next();
};
router
.all('/publish', function (req, res) {
let url = req.rUrl;
let promoText = _.get(req, 'body.payload.promoText') || _.get(req, 'body.payload.info.title');
let promoDiscount = _.get(req, 'body.payload.promoDiscount');
let promoType = _.get(req, 'body.payload.promoType');
let promoImg = _.get(req, 'body.payload.promoImg');
let promoUrl = req.webtaskContext.secrets.prefix + btoa(url).replace(/\//mig, '+') + '?r=1';
if(promoType === 'PROMOCODE') {
promoText = `[${promoType}] ${promoText}`;
} else if(promoDiscount && promoText.indexOf("%") === -1) {
promoText = `[${promoDiscount}% off] ${promoText}`;
}
if(promoImg) {
promoUrl = promoUrl + '&img=' + btoa(promoImg).replace(/\//mig, '+');
}
console.log(`-- reddit published: ${promoText} ${url}`);
as.waterfall([
(next) => {
req.reddit
.getSubreddit(req.subreddit)
.submitLink({
title: promoText,
url: promoUrl
})
.then((data) => {
next(null, data);
})
.catch((err) => {
next(err);
});
}
],
(err, info) => responseHandler(null, res, info));
});
app
.use(bodyParser.json())
.use('/', validateMiddleware, redditMiddleware, router);
module.exports = wt.fromExpress(app);