2017-03-23 16:27:59 +09:00
|
|
|
import { runCallbacksAsync } from 'meteor/vulcan:core';
|
2016-07-22 10:25:17 +09:00
|
|
|
import escapeStringRegexp from 'escape-string-regexp';
|
2017-01-05 07:52:34 +01:00
|
|
|
import { Picker } from 'meteor/meteorhacks:picker';
|
2016-11-26 02:46:55 +08:00
|
|
|
import Posts from '../collection.js';
|
2015-09-18 16:27:59 +09:00
|
|
|
|
2017-01-05 07:52:34 +01:00
|
|
|
Picker.route('/out', ({ query}, req, res, next) => {
|
|
|
|
if(query.url){ // for some reason, query.url doesn't need to be decoded
|
2016-11-26 02:46:55 +08:00
|
|
|
/*
|
2016-07-22 10:25:17 +09:00
|
|
|
If the URL passed to ?url= is in plain text, any hash fragment
|
|
|
|
will get stripped out.
|
|
|
|
So we search for any post whose URL contains the current URL to get a match
|
|
|
|
even without the hash
|
|
|
|
*/
|
|
|
|
|
2017-04-03 16:06:57 +09:00
|
|
|
// decoce url just in case
|
|
|
|
const decodedUrl = decodeURIComponent(query.url);
|
2017-02-02 09:52:47 +01:00
|
|
|
|
2017-04-03 16:06:57 +09:00
|
|
|
try {
|
|
|
|
const post = Posts.findOne({url: {$regex: escapeStringRegexp(decodedUrl)}}, {sort: {postedAt: -1, createdAt: -1}});
|
|
|
|
|
|
|
|
if (post) {
|
|
|
|
const ip = req.headers && req.headers['x-forwarded-for'] || req.connection.remoteAddress;
|
|
|
|
|
|
|
|
runCallbacksAsync('posts.click.async', post, ip);
|
|
|
|
|
|
|
|
res.writeHead(301, {'Location': query.url});
|
|
|
|
res.end();
|
|
|
|
} else {
|
|
|
|
// don't redirect if we can't find a post for that link
|
|
|
|
res.end(`Invalid URL: ${query.url}`);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log('// /out error')
|
|
|
|
console.log(error)
|
|
|
|
console.log(query)
|
2015-09-18 16:27:59 +09:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res.end("Please provide a URL");
|
|
|
|
}
|
2016-11-26 02:46:55 +08:00
|
|
|
});
|