From 1f253981b2dec1487a50162bf77a0ffa8adb794b Mon Sep 17 00:00:00 2001 From: Cadence Fish Date: Mon, 27 Jan 2020 04:15:53 +1300 Subject: [PATCH] RSS feed includes all gallery images --- src/lib/structures/Timeline.js | 8 ++++---- src/lib/structures/TimelineEntry.js | 18 ++++++++++++++---- src/site/api/feed.js | 5 +++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/lib/structures/Timeline.js b/src/lib/structures/Timeline.js index 0378a63..392ef2a 100644 --- a/src/lib/structures/Timeline.js +++ b/src/lib/structures/Timeline.js @@ -52,7 +52,7 @@ class Timeline { this.page_info = page.page_info } - getFeed() { + async fetchFeed() { const feed = new RSS({ title: `@${this.user.data.username}`, feed_url: `${config.website_origin}/u/${this.user.data.username}/rss.xml`, @@ -63,9 +63,9 @@ class Timeline { 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()) - } + await Promise.all(page.map(item => + item.fetchFeedData().then(feedData => feed.item(feedData)) + )) return feed } } diff --git a/src/lib/structures/TimelineEntry.js b/src/lib/structures/TimelineEntry.js index 51f0227..0693f1d 100644 --- a/src/lib/structures/TimelineEntry.js +++ b/src/lib/structures/TimelineEntry.js @@ -9,7 +9,8 @@ require("../testimports")(collectors, TimelineChild, TimelineBaseMethods) const rssDescriptionTemplate = compile(` p(style='white-space: pre-line')= caption -img(alt=alt src=src) +each child in children + img(alt=child.alt src=child.src width=child.width height=child.height) `) class TimelineEntry extends TimelineBaseMethods { @@ -198,13 +199,22 @@ class TimelineEntry extends TimelineBaseMethods { } } - getFeedData() { + async fetchFeedData() { + const children = await this.fetchChildren() return { title: this.getCaptionIntroduction() || `New post from @${this.getBasicOwner().username}`, - description: rssDescriptionTemplate({src: `${config.website_origin}${this.getDisplayUrlP()}`, alt: this.getAlt(), caption: this.getCaption()}), + description: rssDescriptionTemplate({ + caption: this.getCaption(), + children: children.map(child => ({ + src: `${config.website_origin}${child.getDisplayUrlP()}`, + alt: child.getAlt(), + width: child.data.dimensions.width, + height: child.data.dimensions.height + })) + }), author: this.data.owner.username, url: `${config.website_origin}/p/${this.data.shortcode}`, - guid: `${config.website_origin}/p/${this.data.shortcode}`, + guid: `${config.website_origin}/p/${this.data.shortcode}`, // Is it wise to keep the origin in here? The same post would have a different ID from different servers. date: new Date(this.data.taken_at_timestamp*1000) /* Readers should display the description as HTML rather than using the media enclosure. diff --git a/src/site/api/feed.js b/src/site/api/feed.js index ed7594f..6c6c6fd 100644 --- a/src/site/api/feed.js +++ b/src/site/api/feed.js @@ -5,11 +5,12 @@ const {render} = require("pinski/plugins") module.exports = [ {route: `/u/(${constants.external.username_regex})/rss.xml`, methods: ["GET"], code: async ({url, fill}) => { const user = await fetchUser(fill[0]) - const content = user.timeline.getFeed().xml() + const content = await user.timeline.fetchFeed() + const xml = content.xml() return { statusCode: 200, contentType: "application/rss+xml", // see https://stackoverflow.com/questions/595616/what-is-the-correct-mime-type-to-use-for-an-rss-feed - content + content: xml } }} ]