Use GQL for rss feed generation

This commit is contained in:
Jorge Vargas 2024-08-27 22:06:40 -06:00
parent f22cc01005
commit a5a5f4ee2a
6 changed files with 67 additions and 60 deletions

View file

@ -1,39 +1,46 @@
import rss, { type RSSFeedItem } from '@astrojs/rss';
import type { APIContext } from 'astro';
// @ts-ignore
import { Op } from 'sequelize'
import db from '@/sequelize';
import { getApolloClient } from '@/graphql/apolloClient.mjs';
import { gql } from '@/graphql/__generated__/client';
const addedQuery = gql(`
query LastAdded ($limit: Int) {
added: searchAlbum(limit: $limit, status: ["show"]) {
rows {
id
createdAt
title
subTitle
artists {
name
}
}
}
}
`)
export async function GET(context: APIContext) {
const date = new Date()
date.setDate(date.getDate() - 8)
const client = await getApolloClient()
const { data } = await client.query({ query: addedQuery, variables: { limit: 15 } })
const isoDate = date.toISOString()
const limitDate = `${isoDate.split('T')[0]}T00:00:00Z`
const commonArgs = { include: [db.models.artist], order: [['createdAt', 'DESC']] }
if (!data.added?.rows) throw new Error()
const { rows } = data.added
let albums = await db.models.album.findAll({ where: { createdAt: { [Op.gte]: limitDate } }, ...commonArgs })
if (albums.length < 15) albums = await db.models.album.findAll({ limit: 15, ...commonArgs })
const items: RSSFeedItem[] = albums.map((album: any) => {
const { dataValues } = album
const item: RSSFeedItem = {
title: dataValues.title,
pubDate: dataValues.createdAt,
description: dataValues.subTitle || dataValues.artists.map((a: { name: string }) => a.name).join(' - '),
link: `https://www.sittingonclouds.net/album/${dataValues.id}`,
customData: `<media:content
const items: RSSFeedItem[] = rows.map((album) => ({
guid: `album/${album?.id}`,
title: album?.title,
pubDate: new Date(album?.createdAt || ''),
description: album?.subTitle || album?.artists.map(a => a?.name).join(' - '),
link: `https://www.sittingonclouds.net/album/${album?.id}`,
customData: `<media:content
type="image/png"
width="100"
height="100"
medium="image"
url="https://cdn.sittingonclouds.net/album/${dataValues.id}.png" />
url="https://cdn.sittingonclouds.net/album/${album?.id}.png" />
`,
}
return item
})
}))
return rss({
xmlns: {