can get some of the statusses
This commit is contained in:
parent
1fc5890ec6
commit
167126da9a
|
@ -55,7 +55,7 @@ const hour = 3600000;
|
||||||
|
|
||||||
// note: rejects on HTTP 4xx or 5xx
|
// note: rejects on HTTP 4xx or 5xx
|
||||||
async function apGet(url,ttl) {
|
async function apGet(url,ttl) {
|
||||||
|
console.log("apGet: ", url);
|
||||||
return new Promise(function(resolve,reject){
|
return new Promise(function(resolve,reject){
|
||||||
|
|
||||||
// fail early
|
// fail early
|
||||||
|
@ -98,47 +98,54 @@ async function promiseSome(proms){
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = async function (opts) {
|
module.exports = async function (opts) {
|
||||||
console.log("opts:", opts);
|
|
||||||
var opts = opts;
|
var opts = opts;
|
||||||
|
|
||||||
var feedUrl = opts.feedUrl;
|
var feedUrl = opts.feedUrl;
|
||||||
var userUrl = opts.userUrl;
|
var userUrl = opts.userUrl;
|
||||||
var isIndex = false;
|
var isIndex = false;
|
||||||
|
var isUser = new URL(userUrl).pathname.startsWith("/users/");
|
||||||
|
opts.isUser = isUser;
|
||||||
|
|
||||||
|
console.log("opts:", opts);
|
||||||
|
|
||||||
if (!userUrl) {
|
if (!userUrl) {
|
||||||
throw new Error('need user URL');
|
throw new Error('need user URL');
|
||||||
}
|
}
|
||||||
|
|
||||||
var user, feed;
|
var user, feed;
|
||||||
|
if (isUser) {
|
||||||
|
// 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
|
||||||
|
|
||||||
// get user and feed in parallel if I have both URLs.
|
user = await apGet(userUrl,24 * hour);
|
||||||
// can cache feed aggressively since it is a specific start and end.
|
isIndex = true;
|
||||||
if (userUrl && feedUrl){
|
var outbox = await apGet(user.outbox, 1 * hour);
|
||||||
[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
|
|
||||||
if (typeof outbox.first == 'object'){
|
|
||||||
feed = outbox.first;
|
|
||||||
} else {
|
|
||||||
feed = await apGet(outbox.first, hour/6);// 10 mins. Because the base feed URL can get new toots quickly.
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// outbox.first can be a string for a URL, or an object with stuffs in it
|
||||||
|
if (typeof outbox.first == 'object'){
|
||||||
|
feed = outbox.first;
|
||||||
|
} else {
|
||||||
|
feed = await apGet(outbox.first, hour/6);// 10 mins. Because the base feed URL can get new toots quickly.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
feed = await apGet(userUrl, hour/6);
|
||||||
}
|
}
|
||||||
|
|
||||||
var templateData = {
|
var templateData = {
|
||||||
opts: opts,// from the request
|
opts: opts,// from the request
|
||||||
meta: metaForUser(user),
|
meta: isUser ? metaForUser(user) : null,
|
||||||
items: await itemsForFeed(opts,user,feed),
|
items: await itemsForFeed(opts,user,feed),
|
||||||
nextPageLink: getNextPage(opts,user,feed),
|
nextPageLink: getNextPage(opts,user,feed),
|
||||||
isIndex: isIndex
|
isIndex: isIndex
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log("templateData", templateData);
|
||||||
|
|
||||||
return template(templateData);
|
return template(templateData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,6 +160,35 @@ function metaForUser(user) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function itemsForFeed(opts,user,feed) {
|
async function itemsForFeed(opts,user,feed) {
|
||||||
|
if (feed.totalItems > 0 && typeof feed.orderedItems[0] != 'object') {
|
||||||
|
// we only have urls
|
||||||
|
feed.orderedItems = await promiseSome(
|
||||||
|
feed.orderedItems.map(function(url){
|
||||||
|
// we are getting objects of type note
|
||||||
|
return apGet(url, hour).then(
|
||||||
|
note => apGet(note.attributedTo).then(
|
||||||
|
userdata => ({
|
||||||
|
// build an announce object so we are compatible
|
||||||
|
// this is likely to be a case for refatoring
|
||||||
|
_userdata: userdata,
|
||||||
|
type: "Announce",
|
||||||
|
object: note,
|
||||||
|
actor: note.attributedTo,
|
||||||
|
to: note.to,
|
||||||
|
cc: note.cc,
|
||||||
|
published: note.published,
|
||||||
|
id: note.id
|
||||||
|
})
|
||||||
|
));
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("feed", feed);
|
||||||
|
console.log("feed object", feed.orderedItems[0].object);
|
||||||
|
console.log("feed context", feed.orderedItems[0]["@context"]);
|
||||||
|
console.log("feed _userdata", feed.orderedItems[0]._userdata);
|
||||||
|
|
||||||
var items = feed.orderedItems;
|
var items = feed.orderedItems;
|
||||||
|
|
||||||
if (opts.boosts){
|
if (opts.boosts){
|
||||||
|
@ -196,7 +232,7 @@ async function itemsForFeed(opts,user,feed) {
|
||||||
console.warn("warning: couldn't match boost to item: ",boostToot)
|
console.warn("warning: couldn't match boost to item: ",boostToot)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
boostToot._isBoost = true;
|
||||||
boostToot.object = boostToot;// this lets the later stage parser access object without errors :)
|
boostToot.object = boostToot;// this lets the later stage parser access object without errors :)
|
||||||
items[i] = boostToot;
|
items[i] = boostToot;
|
||||||
})
|
})
|
||||||
|
@ -220,8 +256,8 @@ async function itemsForFeed(opts,user,feed) {
|
||||||
var op = item._userdata?item._userdata:user;
|
var op = item._userdata?item._userdata:user;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
isBoost:!!item._userdata,
|
isBoost:item._isBoost,
|
||||||
title:item._userdata?user.preferredUsername+' shared a status by '+op.preferredUsername:'',
|
title:item._userdata && user?user.preferredUsername+' shared a status by '+op.preferredUsername:'',
|
||||||
isReply:!!(item.object && item.object.inReplyTo),
|
isReply:!!(item.object && item.object.inReplyTo),
|
||||||
hasCw:item.object.sensitive||false,
|
hasCw:item.object.sensitive||false,
|
||||||
cw:item.object.summary,
|
cw:item.object.summary,
|
||||||
|
|
Loading…
Reference in New Issue