Vulcan/packages/nova-posts/lib/server/routes.js

31 lines
1 KiB
JavaScript
Raw Normal View History

import { runCallbacksAsync } from 'meteor/nova:core';
2016-07-22 10:25:17 +09:00
import escapeStringRegexp from 'escape-string-regexp';
import { Picker } from 'meteor/meteorhacks:picker';
import Posts from '../collection.js';
2015-09-18 16:27:59 +09:00
Picker.route('/out', ({ query}, req, res, next) => {
if(query.url){ // for some reason, query.url doesn't need to be decoded
/*
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
*/
const post = Posts.findOne({url: {$regex: escapeStringRegexp(query.url)}});
2015-09-18 16:27:59 +09:00
if (post) {
const ip = req.headers && req.headers['x-forwarded-for'] || req.connection.remoteAddress;
runCallbacksAsync('posts.click.async', post, ip);
2016-07-12 17:36:58 +09:00
res.writeHead(301, {'Location': query.url});
2015-09-18 16:27:59 +09:00
res.end();
} else {
// don't redirect if we can't find a post for that link
res.end('Invalid URL');
}
} else {
res.end("Please provide a URL");
}
});