1
0
mirror of https://git.sr.ht/~cadence/bibliogram synced 2024-09-28 14:57:30 +00:00
bibliogram/src/lib/structures/Timeline.js

68 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-01-14 14:38:33 +00:00
const RSS = require("rss")
2020-01-12 12:50:21 +00:00
const constants = require("../constants")
2020-01-14 14:38:33 +00:00
const config = require("../../../config")
2020-01-12 12:50:21 +00:00
const TimelineImage = require("./TimelineImage")
2020-01-18 15:38:14 +00:00
const InstaCache = require("../cache")
2020-01-12 12:50:21 +00:00
const collectors = require("../collectors")
2020-01-18 15:38:14 +00:00
require("../testimports")(constants, collectors, TimelineImage, InstaCache)
2020-01-12 12:50:21 +00:00
2020-01-14 14:38:33 +00:00
/** @param {any[]} edges */
2020-01-12 12:50:21 +00:00
function transformEdges(edges) {
2020-01-18 15:38:14 +00:00
return edges.map(e => collectors.createShortcodeFromData(e.node, false))
2020-01-12 12:50:21 +00:00
}
class Timeline {
/**
* @param {import("./User")} user
*/
constructor(user) {
this.user = user
2020-01-14 14:38:33 +00:00
/** @type {import("./TimelineImage")[][]} */
2020-01-12 12:50:21 +00:00
this.pages = []
this.addPage(this.user.data.edge_owner_to_timeline_media)
this.page_info = this.user.data.edge_owner_to_timeline_media.page_info
}
hasNextPage() {
return this.page_info.has_next_page
}
fetchNextPage() {
if (!this.hasNextPage()) return constants.symbols.NO_MORE_PAGES
return collectors.fetchTimelinePage(this.user.data.id, this.page_info.end_cursor).then(page => {
this.addPage(page)
return this.pages.slice(-1)[0]
})
}
async fetchUpToPage(index) {
while (this.pages[index] === undefined && this.hasNextPage()) {
await this.fetchNextPage()
}
}
addPage(page) {
this.pages.push(transformEdges(page.edges))
this.page_info = page.page_info
}
2020-01-14 14:38:33 +00:00
getFeed() {
const feed = new RSS({
title: `@${this.user.data.username}`,
feed_url: `${config.website_origin}/u/${this.user.data.username}/rss.xml`,
site_url: config.website_origin,
description: this.user.data.biography,
2020-01-16 10:10:15 +00:00
image_url: config.website_origin+this.user.proxyProfilePicture,
2020-01-14 14:38:33 +00:00
pubDate: new Date(this.user.cachedAt),
ttl: this.user.getTtl(1000*60) // scale to minute
})
const page = this.pages[0] // only get posts from first page
for (const item of page) {
feed.item(item.getFeedData())
}
return feed
}
2020-01-12 12:50:21 +00:00
}
module.exports = Timeline