2022-02-06 20:46:27 +00:00
|
|
|
import { compile } from "ejs";
|
|
|
|
import { readFileSync } from "fs";
|
|
|
|
var template = compile(readFileSync("./lib/template.ejs", "utf8"));
|
|
|
|
import { format } from "timeago.js";
|
2022-02-06 19:14:18 +00:00
|
|
|
|
2022-02-06 20:46:27 +00:00
|
|
|
import got from "got";
|
2022-02-06 19:14:18 +00:00
|
|
|
const map = new Map();
|
|
|
|
|
|
|
|
const hour = 3600000;
|
|
|
|
|
|
|
|
// get JSON for an AP URL, by either fetching it or grabbing it from a cache.
|
|
|
|
|
|
|
|
// note: rejects on HTTP 4xx or 5xx
|
2022-02-06 20:46:27 +00:00
|
|
|
async function apGet(url) {
|
2022-02-06 19:14:18 +00:00
|
|
|
|
|
|
|
return new Promise(function(resolve,reject){
|
|
|
|
|
|
|
|
// fail early
|
|
|
|
if (!url){
|
2022-02-06 20:46:27 +00:00
|
|
|
reject(new Error("URL is invalid"));
|
2022-02-06 19:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
got( {
|
|
|
|
url:url,
|
|
|
|
cache:map,
|
|
|
|
headers: {
|
|
|
|
"accept": "application/activity+json"
|
|
|
|
}
|
|
|
|
})
|
2022-02-06 20:46:27 +00:00
|
|
|
.then(response=>JSON.parse(response.body))
|
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
2022-02-06 19:14:18 +00:00
|
|
|
|
2022-02-06 20:46:27 +00:00
|
|
|
});
|
2022-02-06 19:14:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// like Promise.all except returns null on error instead of failing all the promises
|
|
|
|
async function promiseSome(proms){
|
|
|
|
|
|
|
|
function noRejectWrap(prom){
|
2022-02-06 20:46:27 +00:00
|
|
|
return new Promise(function(resolve){
|
2022-02-06 19:14:18 +00:00
|
|
|
|
|
|
|
prom // it's already been called
|
2022-02-06 20:46:27 +00:00
|
|
|
.then(resolve)
|
|
|
|
.catch( ()=>{
|
2022-02-06 19:14:18 +00:00
|
|
|
// console.warn(e);// for debugging
|
2022-02-06 20:46:27 +00:00
|
|
|
resolve(null);
|
|
|
|
});
|
2022-02-06 19:14:18 +00:00
|
|
|
|
2022-02-06 20:46:27 +00:00
|
|
|
});
|
2022-02-06 19:14:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-06 20:46:27 +00:00
|
|
|
return await Promise.all(proms.map(noRejectWrap));
|
2022-02-06 19:14:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default async function (opts) {
|
2022-02-06 20:46:27 +00:00
|
|
|
// var opts = opts;
|
2022-02-06 19:14:18 +00:00
|
|
|
|
|
|
|
var feedUrl = opts.feedUrl;
|
|
|
|
var userUrl = opts.userUrl;
|
|
|
|
var isIndex = false;
|
|
|
|
|
|
|
|
if (!userUrl) {
|
2022-02-06 20:46:27 +00:00
|
|
|
throw new Error("need user URL");
|
2022-02-06 19:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var user, feed;
|
|
|
|
|
|
|
|
// get user and feed in parallel if I have both URLs.
|
|
|
|
// can cache feed aggressively since it is a specific start and end.
|
|
|
|
if (userUrl && feedUrl){
|
|
|
|
[user, feed] = await Promise.all([ apGet(userUrl), apGet(feedUrl) ]);
|
|
|
|
}else{
|
|
|
|
// get user, then outbox, then feed
|
|
|
|
|
|
|
|
user = await apGet(userUrl,24 * hour);
|
|
|
|
isIndex = true;
|
|
|
|
var outbox = await apGet(user.outbox, 1 * hour);
|
|
|
|
|
|
|
|
// outbox.first can be a string for a URL, or an object with stuffs in it
|
2022-02-06 20:46:27 +00:00
|
|
|
if (typeof outbox.first == "object"){
|
2022-02-06 19:14:18 +00:00
|
|
|
feed = outbox.first;
|
|
|
|
} else {
|
|
|
|
feed = await apGet(outbox.first, hour/6);// 10 mins. Because the base feed URL can get new toots quickly.
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var templateData = {
|
|
|
|
opts: opts,// from the request
|
|
|
|
meta: metaForUser(user),
|
|
|
|
items: await itemsForFeed(opts,user,feed),
|
|
|
|
nextPageLink: getNextPage(opts,user,feed),
|
|
|
|
isIndex: isIndex
|
|
|
|
};
|
|
|
|
|
|
|
|
return template(templateData);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function metaForUser(user) {
|
|
|
|
return {
|
|
|
|
avatar: user.icon && user.icon.url?user.icon.url:null,
|
|
|
|
headerImage:user.image && user.image.url?user.image.url:null,
|
|
|
|
title: user.name||user.preferredUsername||null,
|
|
|
|
description: user.summary||null,
|
|
|
|
link:user.url||"#"
|
2022-02-06 20:46:27 +00:00
|
|
|
};
|
2022-02-06 19:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function itemsForFeed(opts,user,feed) {
|
|
|
|
|
|
|
|
var items = feed.orderedItems;
|
|
|
|
|
|
|
|
if (opts.boosts){
|
|
|
|
// yes, I have to fetch all the fucking boosts for this whole feed apparently >:/
|
|
|
|
var boostData = [];
|
|
|
|
var boostUrls = feed.orderedItems.filter(i=>i.type=="Announce").map(i=>i.object);
|
|
|
|
// console.log(boostUrls);
|
|
|
|
boostData = await promiseSome(boostUrls.map(apGet));
|
|
|
|
|
|
|
|
// now get user data for each of those
|
2022-02-06 20:46:27 +00:00
|
|
|
let userData = await promiseSome(boostData.map(d=>d?d.attributedTo||"":null).map(apGet));
|
2022-02-06 19:14:18 +00:00
|
|
|
|
|
|
|
// put a ._userdata key on the item object if this is a boost
|
|
|
|
for (var i = 0; i < boostData.length; i ++){
|
|
|
|
if (userData[i] && boostData[i]){
|
|
|
|
boostData[i]._userdata = userData[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// some URLs may have failed but IDGAF
|
|
|
|
|
|
|
|
// console.log(boostData[0]);
|
|
|
|
|
|
|
|
boostData.forEach((boostToot)=>{
|
|
|
|
|
|
|
|
if (!boostToot){// failed request
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// inject in-place into items
|
|
|
|
|
|
|
|
var index = -1;
|
|
|
|
for (var i = 0; i < items.length; i ++){
|
|
|
|
if (items[i].object == boostToot.id){
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == -1){
|
2022-02-06 20:46:27 +00:00
|
|
|
console.warn("warning: couldn't match boost to item: ",boostToot);
|
2022-02-06 19:14:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
boostToot.object = boostToot;// this lets the later stage parser access object without errors :)
|
|
|
|
items[i] = boostToot;
|
2022-02-06 20:46:27 +00:00
|
|
|
});
|
2022-02-06 19:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return items.filter((item)=>{
|
2022-02-06 20:46:27 +00:00
|
|
|
return typeof item.object == "object";// handle weird cases
|
2022-02-06 19:14:18 +00:00
|
|
|
}).map((item)=>{
|
|
|
|
|
|
|
|
var enclosures = (item.object.attachment||[]).filter((a)=>{
|
|
|
|
return a.type == "Document";
|
|
|
|
}).map((a)=>{
|
|
|
|
return {
|
|
|
|
name:a.name,
|
|
|
|
type:a.mediaType,
|
|
|
|
url:a.url
|
2022-02-06 20:46:27 +00:00
|
|
|
};
|
2022-02-06 19:14:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var op = item._userdata?item._userdata:user;
|
|
|
|
|
|
|
|
return {
|
|
|
|
isBoost:!!item._userdata,
|
2022-02-06 20:46:27 +00:00
|
|
|
title:item._userdata?user.preferredUsername+" shared a status by "+op.preferredUsername:"",
|
2022-02-06 19:14:18 +00:00
|
|
|
isReply:!!(item.object && item.object.inReplyTo),
|
|
|
|
hasCw:item.object.sensitive||false,
|
|
|
|
cw:item.object.summary,
|
2022-02-06 20:46:27 +00:00
|
|
|
content: item.object&&item.object.content?item.object.content:"",//TODO sanitize then render without entity escapes
|
|
|
|
atomHref:item.published?item.published.replace(/\W+/g,""):Math.random().toString().replace(".",""),// used for IDs
|
2022-02-06 19:14:18 +00:00
|
|
|
enclosures:enclosures,
|
2022-02-06 20:46:27 +00:00
|
|
|
stringDate:item.published?getTimeDisplay(Date.parse(item.published)):"",
|
|
|
|
permalink:item.object.id?item.object.id:"#",
|
2022-02-06 19:14:18 +00:00
|
|
|
author:{
|
|
|
|
uri:op.url,// link to author page
|
2022-02-06 20:46:27 +00:00
|
|
|
avatar:op.icon&&op.icon.url?op.icon.url:"",
|
2022-02-06 19:14:18 +00:00
|
|
|
displayName:op.name || op.preferredUsername,
|
2022-02-06 20:46:27 +00:00
|
|
|
fullName:op.preferredUsername+"@"+(new URL(op.url).hostname),
|
2022-02-06 19:14:18 +00:00
|
|
|
}
|
2022-02-06 20:46:27 +00:00
|
|
|
};
|
|
|
|
});
|
2022-02-06 19:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getNextPage(opts,user,feed){
|
|
|
|
//based on feed.next
|
2022-02-06 20:46:27 +00:00
|
|
|
if (!feed.next){return null;}
|
2022-02-06 19:14:18 +00:00
|
|
|
// take feed.next, uriencode it, then take user url, then take options.mastofeedUrl
|
2022-02-06 20:46:27 +00:00
|
|
|
//var base = opts.mastofeedUrl.slice(0,opts.mastofeedUrl.indexOf("?"));
|
2022-02-06 19:14:18 +00:00
|
|
|
|
2022-02-06 20:46:27 +00:00
|
|
|
var ret = "/api/v1/feed?userurl=" + encodeURIComponent(opts.userUrl) + "&feedurl=" +encodeURIComponent(feed.next) + "&instance_type=" + opts.instance_type;
|
2022-02-06 19:14:18 +00:00
|
|
|
|
|
|
|
// add other params to the end
|
2022-02-06 20:46:27 +00:00
|
|
|
(["theme","header","size","boosts","replies"]).forEach((k)=>{
|
|
|
|
if (typeof opts[k] != "undefined"){
|
2022-02-06 19:14:18 +00:00
|
|
|
ret+=`&${k}=${ opts[k].toString() }`;
|
|
|
|
}
|
2022-02-06 20:46:27 +00:00
|
|
|
});
|
2022-02-06 19:14:18 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// utilities below
|
|
|
|
|
|
|
|
function getTimeDisplay(d) {
|
2022-02-06 20:46:27 +00:00
|
|
|
// var d = d;
|
|
|
|
if (typeof d !== "object") {
|
2022-02-06 19:14:18 +00:00
|
|
|
d = new Date(d);
|
|
|
|
}
|
|
|
|
// convert to number
|
|
|
|
let dt = d.getTime();
|
|
|
|
var now = Date.now();
|
|
|
|
|
|
|
|
var delta = now - dt;
|
|
|
|
|
|
|
|
// over 6 days ago
|
|
|
|
if (delta > 1000 * 60 * 60 * 24 * 6) {
|
|
|
|
return isoDateToEnglish(d.toISOString());
|
|
|
|
} else {
|
|
|
|
return format(dt);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function isoDateToEnglish(d) {
|
|
|
|
|
2022-02-06 20:46:27 +00:00
|
|
|
var dt = d.split(/[t-]/ig);
|
2022-02-06 19:14:18 +00:00
|
|
|
var months = ["January", "February", "March", "April", "May", "June",
|
|
|
|
"July", "August", "September", "October", "November", "December"];
|
|
|
|
|
2022-02-06 20:46:27 +00:00
|
|
|
return months[Number(dt[1]) - 1] + " " + dt[2] + ", " + dt[0];
|
2022-02-06 19:14:18 +00:00
|
|
|
}
|