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-26 14:56:59 +00:00
|
|
|
const TimelineEntry = require("./TimelineEntry")
|
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-26 14:56:59 +00:00
|
|
|
require("../testimports")(constants, collectors, TimelineEntry, 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-26 14:56:59 +00:00
|
|
|
return edges.map(e => {
|
|
|
|
/** @type {import("../types").TimelineEntryAll} */
|
|
|
|
const data = e.node
|
|
|
|
const entry = collectors.getOrCreateShortcode(data.shortcode)
|
|
|
|
entry.apply(data)
|
|
|
|
return entry
|
|
|
|
})
|
2020-01-12 12:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Timeline {
|
|
|
|
/**
|
|
|
|
* @param {import("./User")} user
|
|
|
|
*/
|
|
|
|
constructor(user) {
|
|
|
|
this.user = user
|
2020-01-26 14:56:59 +00:00
|
|
|
/** @type {import("./TimelineEntry")[][]} */
|
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
|