diff --git a/index.js b/index.js index 114f33c..e8c8fe3 100644 --- a/index.js +++ b/index.js @@ -55,7 +55,7 @@ app.get('/api/feed',function(req,res){ opts.boosts = true; } } - + opts.replies = true; if (req.query.replies){ if (req.query.replies.toLowerCase() == 'no' || req.query.replies.toLowerCase() == 'false'){ @@ -64,6 +64,8 @@ app.get('/api/feed',function(req,res){ opts.replies = true; } } + opts.feedUrl = feedUrl; + opts.mastofeedUrl = req.url; var req = request.get(feedUrl); convert(req,opts,function(er,data){ diff --git a/lib/convert.js b/lib/convert.js index fe3063d..c320956 100644 --- a/lib/convert.js +++ b/lib/convert.js @@ -180,7 +180,37 @@ function buildUp(jsonObj,opts){ item.cw = item.summary; } } - + + // get a pagination ID for an entry + item.paginationId = false; + if (item['atom:link']){ + var links = item['atom:link']; + if (!isArray(links) && typeof links == 'object'){ + links = [links]; + }else if (!isArray(links)){ + links = []; + } + links.forEach((link)=>{ + if (!link['@']){return} + if (link['@']['rel']=='self'){ + // parse out the pagination id + // href looks like this in mastodon: https://octodon.social/users/fenwick67/updates/732275.atom + // href looks like this in pleroma (and we should ignore): https://social.fenwick.pizza/objects/1e2fa906-378c-43f8-98fa-271aae455758 + var href = link['@']['href']; + if (!href){return} + var match = href.match(/\/\d+.atom/); + if(!match){return} + var id = match[0].replace(/\D/g,''); + if (id){ + item.paginationId = id; + }else{ + return; + } + } + }) + + } + return true; }); @@ -197,9 +227,51 @@ function buildUp(jsonObj,opts){ }); } + + // get next page + jsonObj.feedUrl = opts.feedUrl; + jsonObj.isIndex = (opts.feedUrl.indexOf('?') == -1); + + // prefer link(rel=next) + var nextPageFeedUrl = ''; + var links = jsonObj.meta['atom:link']; + if (links){ + if (!isArray(links) && typeof links == 'object'){ + links = [links]; + }else if (!isArray(links)){ + links = []; + } + links.forEach(function(link){ + if (link['@'] && link['@']['rel'] == 'next' && link['@']['href']){ + nextPageFeedUrl = link['@']['href']; + } + }) + } + + if (!nextPageFeedUrl){ + // alternative: try to get the oldest entry id on this page + var lowestId = Infinity; + jsonObj.items.forEach(function(item){ + var id = Number(item.paginationId); + if ( id < lowestId ){ + lowestId = id; + } + }); + + if (lowestId < Infinity && opts.feedUrl){ + nextPageFeedUrl = opts.feedUrl.replace(/\?.+/g,'') + '?max_id='+lowestId; + } + } + + if(nextPageFeedUrl){ + jsonObj.nextPageLink = opts.mastofeedUrl.replace(encodeURIComponent(opts.feedUrl),encodeURIComponent(nextPageFeedUrl)); + console.log(jsonObj.nextPageLink); + } return template(jsonObj); } +// utilities below + // get obj[key]['#'] or '' function getH(obj,key){ diff --git a/lib/template.ejs b/lib/template.ejs index 3d5cdf6..4618a11 100644 --- a/lib/template.ejs +++ b/lib/template.ejs @@ -37,12 +37,10 @@ <% } %> +