make v1 API redirect to the v2 api after parsing parameters

This commit is contained in:
fenwick67 2019-08-02 11:33:15 -04:00
parent 9d85c029f4
commit 892e100e43
2 changed files with 17 additions and 46 deletions

View file

@ -33,6 +33,8 @@ function doCache(res,durationSecs){
})
}
// this just redirects to the
app.options('/api/feed',cors());
app.get('/api/feed',cors(),function(req,res){
@ -41,53 +43,21 @@ app.get('/api/feed',cors(),function(req,res){
if (!feedUrl){
res.status(400);
res.send('You need to specify a feed URL');
return;
}
var opts = {};
if (req.query.size){
opts.size = req.query.size;
}
if (req.query.theme){
opts.theme = req.query.theme;
}
if (req.query.header){
if (req.query.header.toLowerCase() == 'no' || req.query.header.toLowerCase() == 'false'){
opts.header = false;
}else{
opts.header = true;
}
}
var userUrl = feedUrl.replace(/\.atom$/i,'');
opts.boosts = true;
if (req.query.boosts){
if (req.query.boosts.toLowerCase() == 'no' || req.query.boosts.toLowerCase() == 'false'){
opts.boosts = false;
}else{
opts.boosts = true;
}
}
var redirectUrl = '/apiv2/feed?';
var qs = ['userurl='+userUrl];
opts.replies = true;
if (req.query.replies){
if (req.query.replies.toLowerCase() == 'no' || req.query.replies.toLowerCase() == 'false'){
opts.replies = false;
}else{
opts.replies = true;
(['size','theme','boosts','replies']).forEach(key=>{
if (typeof req.query[key] != 'undefined'){
qs.push(key+'='+encodeURIComponent(req.query[key]));
}
}
opts.feedUrl = feedUrl;
opts.mastofeedUrl = req.url;
})
var req = request.get(feedUrl);
convert(req,opts,function(er,data){
if (er){
res.status(500);
return res.send('Error fetching or parsing your feed.');
}
res.status(200);
doCache(res,60*60)
res.send(data);
});
res.redirect(redirectUrl + qs.join('&'));
});
@ -100,6 +70,7 @@ app.get('/apiv2/feed',cors(),function(req,res){
if (!userUrl){
res.status(400);
res.send('You need to specify a user URL');
return;
}
var feedUrl = req.query.feedurl;

View file

@ -35,7 +35,7 @@
<label>Use this markup in your HTML: <br><textarea id="result" placeholder="result will go here"></textarea></label>
<br>
<h3>Live Preview:</h3>
<iframe id="frame" allowfullscreen sandbox="allow-top-navigation allow-scripts" width="400" height="800" src="/api/feed?url=https%3A%2F%2Foctodon.social%2Fusers%2Ffenwick67.atom&theme=dark&size=100&header=true"></iframe>
<iframe id="frame" allowfullscreen sandbox="allow-top-navigation allow-scripts" width="400" height="800" src="/apiv2/feed?userurl=https%3A%2F%2Foctodon.social%2Fusers%2Ffenwick67&replies=false&boosts=true"></iframe>
<br>
</div>
<script>
@ -44,13 +44,13 @@ window.genUrl = function genUrl(){
return document.getElementById(id).value;
}
var inUrl = 'https://' + val('urlin') + '/users/'+val('usernamein')+'.atom';
var inUrl = 'https://' + val('urlin') + '/users/'+val('usernamein');
var showBoosts = (!document.getElementById('hideboosts').checked).toString();
var showReplies = (!document.getElementById('hidereplies').checked).toString();
var showBoosts = (!document.getElementById('hideboosts').checked).toString();
var showReplies = (!document.getElementById('hidereplies').checked).toString();
var iframeUrl = window.location.protocol + '//'+ window.location.hostname +((window.location.port && window.location.port!=80)?(':'+window.location.port):'')
+"/api/feed?url="+encodeURIComponent(inUrl)+"&theme="+val('theme')+'&size='+val('size')
+"/apiv2/feed?userurl="+encodeURIComponent(inUrl)+"&theme="+val('theme')+'&size='+val('size')
+ "&header="+(document.getElementById('header').checked.toString())+'&replies='+showReplies+'&boosts='+showBoosts;
document.getElementById('result').value = '<iframe allowfullscreen sandbox="allow-top-navigation allow-scripts" width="'+val('width')+'" height="'+val('height')+'" src="'+iframeUrl+'"></iframe>';