-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpublic.js
More file actions
266 lines (246 loc) · 10.2 KB
/
public.js
File metadata and controls
266 lines (246 loc) · 10.2 KB
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
var url = require('url');
var _ = require('lodash');
var bandit = require('./bandit.js');
var Promise = require('bluebird');
var SOCIAL_AGENTS = ['facebookexternalhit', 'Facebot', 'Twitterbot'];
var socialAgentRegexp = new RegExp(SOCIAL_AGENTS.join('|'));
var init = function(app, schema, schemaActions, config) {
app._shareUrl = function(href, abver) {
//based on href and abver, generate a url that can be shared
href = href.replace(/^https?:\/\//, '');
return (config.baseUrl
+ '/r/'
+ (abver || '0') + '/'
+ href
);
};
// Empty page to confirm site is up
app.get('/ping', function (req, res) {
res.setHeader('Content-Type', 'text/plain');
res.end('OK');
});
app.get('/robots.txt', function (req, res) {
res.setHeader('Content-Type', 'text/plain');
res.end(
'User-agent: *\nDisallow: /\n\n'
+ SOCIAL_AGENTS.map(function(bot) {
return 'User-agent: ' + bot + '*\nAllow: /\n';
}).join('\n')
);
});
app.get('/r/:abver/:domain*',
function (req, res) {
//NOTE: any caching layer:
// in theory, you can whitelist domain matches, and if there is no abver,
// just redirect skip
// we can also, in theory cache it for facebook clients + abver
if (! (req.params.domain in config.domain_whitelist)) {
return res.status(404).send('Not found');
}
res.vary('User-Agent');
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
var domainInfo = config.domain_whitelist[req.params.domain];
var pathname = req.params[0];
var proto = domainInfo.proto;
var forwardedQuery = _.clone(req.query);
forwardedQuery.abver = req.params.abver;
var furl = url.format({
'protocol': proto,
'host': req.params.domain,
'pathname': decodeURIComponent(pathname),
'query': forwardedQuery
});
/// 1. Am I Facebook Crawler or TwitterBot?
//https://developers.facebook.com/docs/sharing/webmasters/crawler
if (socialAgentRegexp.test(req.get('User-Agent')) && parseInt(req.params.abver) >= 0 ) {
var murl = (req.params.domain + decodeURIComponent(pathname || '/').replace(/.fb\d+/,''));
schemaActions.trialLookup(murl, parseInt(req.params.abver)).then(function(trial) {
if (!trial) {
if (/testshare/.test(pathname)) {
res.render('shareheaders', {
'extraProperties': domainInfo.extraProperties || [],
'extraMeta': domainInfo. extraMeta || [],
'title': 'Fooooo',
'description': 'basdfasdf',
});
} else if (req.params.abver == '0') {
// if facebook is sent a sharebandit URL with no treatment id render best treatment metadata and refresh fb cache
schema.Metadata.findOne({
'where': { 'url':murl },
'order': [[ 'success_count', 'DESC' ]]
}).then(function(bestTrial) {
if (bestTrial) {
res.render('shareheaders', {
'extraProperties': domainInfo.extraProperties || [],
'title': bestTrial.headline,
'description': bestTrial.text,
'image': bestTrial.image_url,
'fullUrl': config.baseUrl + req.originalUrl
});
} else {
return res.status(404).send('Not found');
}
});
} else {
return res.status(404).send('Not found');
}
} else {
// console.log('FACEBOOK', req.originalUrl, furl);
var renderFacebook = function() {
res.render('shareheaders', {
'extraProperties': domainInfo.extraProperties || [],
'extraMeta': domainInfo.extraMeta || [],
'title': trial.headline,
'description': trial.text,
'image': trial.image_url,
'fullUrl': config.baseUrl + req.originalUrl
});
};
if (!req.params.abver) {
renderFacebook();
}
//UPSERT the sharer if abid is present:
if (req.query.abid) {
var newsharer = {'key': req.query.abid,
'trial': (parseInt(req.params.abver) || 0)
};
schemaActions.newShare(newsharer, parseInt(req.params.abver) || 0).then(function() {
if (req.params.abver) {
renderFacebook();
}
});
} else {
if (req.params.abver) {
renderFacebook();
}
}
}
});
/// 2. Am I a User?
} else {
if (!req.query.absync) {
res.redirect(furl);
}
//ASSUMING:
// on a share click, we INSERT a sharer with the key
// on creation of a trial (metadata row), we create a sharer with key=''
//We might want to consider auto-adding the row, AND/OR verifying that the url is correct
// e.g. with AND trial=(SELECT id FROM metadata WHERE url=$$trialurl) -- but that would slow it down
if (req.params.abver) {
var cautious_id = (parseInt(req.params.abver) || 0);
schemaActions.newEvent(cautious_id, req.query.abid || '', /*isAction=*/false)
.then(function(value) {
if (req.query.absync) {
res.redirect(furl);
}
}, function(reason) {
console.log('Failed save of transaction ' + req.originalUrl
+ ' -- ' + reason);
if (req.query.absync) {
res.redirect(furl); // redirect anyway, since the client shouldn't care
}
});
}
}
});
//smallest GIF
//http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever
//base64_decode('R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==')
var smallgif = [71,73,70,56,57,97,1,0,1,0,0,255,0,44,0,0,0,0,1,0,1,0,0,2,0,59].map(function(x){return String.fromCharCode(x);}).join('');
//transparent version
//smallgif = [71,73,70,56,57,97,1,0,1,0,0,0,0,33,249,4,1,10,0,1,0,44,0,0,0,0,1,0,1,0,0,2,2,76,1,0,59].map(function(x){return String.fromCharCode(x);}).join('');
//TODO: abstract out the similarities with this code and the /r/ code above (even though above, there are more paths)
app.get('/a/:abver/:domain*',
function (req, res) {
res.set('Content-Type', 'image/gif');
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
if (!req.query.absync) {
res.end(smallgif, 'binary');
}
if (req.params.abver) {
var cautious_id = (parseInt(req.params.abver) || 0);
schemaActions.newEvent(cautious_id, req.query.abid || '', /*isAction=*/true)
.then(function() {
if (req.query.absync) {
res.end(smallgif, 'binary');
}
});
} else {
if (req.query.absync) {
res.end(smallgif, 'binary');
}
}
});
var js_result = function(successMetric) {
return function (req, res) {
if (! (req.params.domain in config.domain_whitelist)) {
return res.status(404).send('Not found');
}
res.set('Content-Type', 'text/javascript');
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
var proto = config.domain_whitelist[req.params.domain].proto;
var murl = (req.params.domain + decodeURIComponent(req.params[0] || '/'));
bandit.choose(murl, successMetric, schemaActions).then(function(trialChoice) {
var burl = app._shareUrl('', trialChoice);
return res.render('jsshare', {baseUrl: burl, abver: trialChoice});
});
};
};
app.get('/js/:domain*', js_result('success'));
app.get('/jsaction/:domain*', js_result('action'));
var json_result = function (successMetric) {
return function (req, res) {
if (! (req.params.domain in config.domain_whitelist)) {
return res.status(404).send('Not found');
}
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
var xdomain = (req.get('Origin') || req.get('Referer') || '').match(/https?:\/\/([^\/]+)/);
if (xdomain && (xdomain[1] in config.domain_whitelist)) {
res.setHeader('Access-Control-Allow-Origin', xdomain[0])
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS, HEAD')
}
var murl = (req.params.domain + decodeURIComponent(req.params[0] || '/'));
bandit.choose(murl, successMetric, schemaActions).then(function(trialChoice) {
schemaActions.trialLookup(murl, trialChoice).then(function(trialMetadata) {
res.jsonp({
'id': trialMetadata.id,
'url': trialMetadata.url,
'headline': trialMetadata.headline,
'text': trialMetadata.text,
'image_url': trialMetadata.image_url,
'shareurl': app._shareUrl('', trialChoice) + murl
});
}, function(err) {
console.error('trial metadata not found', err);
res.status(404).send('Not found');
});
}, function(err) {
console.error('trialchoice not found', err);
res.status(404).send('Not found');
});
};
};
app.get('/json/:domain*', json_result('success'));
app.get('/jsonaction/:domain*', json_result('action'));
app.set('jsonp callback name', 'callback');
app.get('/jsonall/:domain*', function (req, res) {
var murl = (req.params.domain + decodeURIComponent(req.params[0] || '/'));
var params = {'url': murl, 'variants': []};
//res.setHeader('Content-Type', 'application/json');
// TODO: caching: gets all the url trials, but need success/action/trial counts
schema.Metadata.findAll({
where: {
url: murl
}
}).then(function(results) {
_.forEach(results, function(result) {
result.dataValues.shareUrl = app._shareUrl(result.dataValues.url, result.dataValues.id);
params.variants.push(result.dataValues);
});
return res.jsonp(params);
});
});
};
module.exports = init;