chore: fix related books + deduplication

- fixed total plays formatting
- formatted queries
- improved proxy server
This commit is contained in:
Cory Dransfeldt 2024-10-21 12:04:17 -07:00
parent ab50866bb8
commit d2c4c78ff7
No known key found for this signature in database
40 changed files with 985 additions and 1357 deletions

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "coryd.dev",
"version": "1.6.0",
"version": "1.6.2",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "coryd.dev",
"version": "1.6.0",
"version": "1.6.2",
"license": "MIT",
"dependencies": {
"@cdransf/api-text": "^1.5.0",

View file

@ -1,6 +1,6 @@
{
"name": "coryd.dev",
"version": "1.6.1",
"version": "1.6.2",
"description": "The source for my personal site. Built using 11ty (and other tools).",
"type": "module",
"engines": {

View file

@ -1,21 +1,18 @@
CREATE OR REPLACE FUNCTION public.search_optimized_index(
search_query text,
page_size integer,
page_offset integer,
types text[]
) RETURNS TABLE(
result_id integer,
url text,
title text,
description text,
tags text,
genre_name text,
genre_url text,
type text,
total_plays integer,
rank real,
total_count bigint
) AS $$
CREATE OR REPLACE FUNCTION public.search_optimized_index(search_query text, page_size integer, page_offset integer, types text[])
RETURNS TABLE(
result_id integer,
url text,
title text,
description text,
tags text,
genre_name text,
genre_url text,
type text,
total_plays text, -- Changed to text
rank real,
total_count bigint
)
AS $$
BEGIN
RETURN QUERY
SELECT
@ -28,20 +25,19 @@ BEGIN
s.genre_url,
s.type,
s.total_plays,
ts_rank_cd(
to_tsvector('english', s.title || ' ' || s.description || array_to_string(s.tags, ' ')),
plainto_tsquery('english', search_query)
) AS rank,
COUNT(*) OVER () AS total_count
FROM optimized_search_index s
WHERE
(types IS NULL OR s.type = ANY(types))
AND plainto_tsquery('english', search_query) @@
to_tsvector('english', s.title || ' ' || s.description || array_to_string(s.tags, ' '))
ts_rank_cd(to_tsvector('english', s.title || ' ' || s.description || array_to_string(s.tags, ' ')), plainto_tsquery('english', search_query)) AS rank,
COUNT(*) OVER() AS total_count
FROM
optimized_search_index s
WHERE(types IS NULL
OR s.type = ANY(types))
AND plainto_tsquery('english', search_query) @@ to_tsvector('english', s.title || ' ' || s.description || array_to_string(s.tags, ' '))
ORDER BY
s.type = 'post' DESC,
s.content_date DESC NULLS LAST,
rank DESC
LIMIT page_size OFFSET page_offset;
END;
$$ LANGUAGE plpgsql;
$$
LANGUAGE plpgsql;

View file

@ -7,30 +7,20 @@ SELECT
l.link,
a.mastodon,
a.name,
json_build_object(
'name', a.name,
'url', a.url,
'mastodon', a.mastodon
) AS author,
json_build_object('name', a.name, 'url', a.url, 'mastodon', a.mastodon) AS author,
'link' AS type,
(
SELECT array_agg(t.name)
FROM links_tags lt
(
SELECT
array_agg(t.name)
FROM
links_tags lt
LEFT JOIN tags t ON lt.tags_id = t.id
WHERE lt.links_id = l.id
) AS tags,
json_build_object(
'title', CONCAT(l.title, ' via ', a.name),
'url', l.link,
'description', l.description,
'date', l.date
) AS feed
WHERE
lt.links_id = l.id) AS tags,
json_build_object('title', CONCAT(l.title, ' via ', a.name), 'url', l.link, 'description', l.description, 'date', l.date) AS feed
FROM
links l
JOIN
authors a ON l.author = a.id
JOIN authors a ON l.author = a.id
ORDER BY
l.date DESC;
l.date DESC;

View file

@ -8,145 +8,119 @@ SELECT
p.featured,
p.slug AS url,
p.mastodon_url,
CASE
WHEN df.filename_disk IS NOT NULL AND df.filename_disk != '' AND df.filename_disk != '/'
THEN CONCAT('/', df.filename_disk)
ELSE NULL
CASE WHEN df.filename_disk IS NOT NULL
AND df.filename_disk != ''
AND df.filename_disk != '/' THEN
CONCAT('/', df.filename_disk)
ELSE
NULL
END AS image,
p.image_alt,
CASE
WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, p.date)) > 3 THEN TRUE
ELSE FALSE
CASE WHEN EXTRACT(YEAR FROM AGE(CURRENT_DATE, p.date)) > 3 THEN
TRUE
ELSE
FALSE
END AS old_post,
(
SELECT json_agg(
CASE
WHEN pb.collection = 'youtube_player' THEN json_build_object(
'type', pb.collection,
'url', yp.url
)
WHEN pb.collection = 'github_banner' THEN json_build_object(
'type', pb.collection,
'url', gb.url
)
WHEN pb.collection = 'npm_banner' THEN json_build_object(
'type', pb.collection,
'url', nb.url,
'command', nb.command
)
WHEN pb.collection = 'rss_banner' THEN json_build_object(
'type', pb.collection,
'url', rb.url,
'text', rb.text
)
WHEN pb.collection = 'hero' THEN json_build_object(
'type', pb.collection,
'image', CONCAT('/', df_hero.filename_disk),
'alt_text', h.alt_text
)
WHEN pb.collection = 'markdown' THEN json_build_object(
'type', pb.collection,
'text', md.text
)
WHEN pb.collection = 'divider' THEN json_build_object(
'type', pb.collection,
'markup', d.markup
)
ELSE json_build_object('type', pb.collection)
END
)
FROM posts_blocks pb
LEFT JOIN youtube_player yp ON pb.collection = 'youtube_player' AND yp.id = pb.item::INTEGER
LEFT JOIN github_banner gb ON pb.collection = 'github_banner' AND gb.id = pb.item::INTEGER
LEFT JOIN npm_banner nb ON pb.collection = 'npm_banner' AND nb.id = pb.item::INTEGER
LEFT JOIN rss_banner rb ON pb.collection = 'rss_banner' AND rb.id = pb.item::INTEGER
LEFT JOIN hero h ON pb.collection = 'hero' AND h.id = pb.item::INTEGER
LEFT JOIN directus_files df_hero ON h.image = df_hero.id
LEFT JOIN markdown md ON pb.collection = 'markdown' AND md.id = pb.item::INTEGER
LEFT JOIN divider d ON pb.collection = 'divider' AND d.id = pb.item::INTEGER
WHERE pb.posts_id = p.id
) AS blocks,
(
SELECT array_agg(t.name)
FROM posts_tags pt
LEFT JOIN tags t ON pt.tags_id = t.id
WHERE pt.posts_id = p.id
) AS tags,
(
SELECT json_agg(json_build_object(
'name', g.name,
'url', g.slug
))
FROM posts_genres gp
LEFT JOIN genres g ON gp.genres_id = g.id
WHERE gp.posts_id = p.id
) AS genres,
(
SELECT json_agg(json_build_object(
'name', a.name_string,
'url', a.slug,
'country', a.country,
'total_plays', a.total_plays
))
FROM posts_artists pa
LEFT JOIN artists a ON pa.artists_id = a.id
WHERE pa.posts_id = p.id
) AS artists,
(
SELECT json_agg(json_build_object(
'title', b.title,
'author', b.author,
'url', b.slug
) ORDER BY b.title)
FROM posts_books pbk
LEFT JOIN books b ON pbk.books_id = b.id
WHERE pbk.posts_id = p.id
) AS books,
(
SELECT json_agg(json_build_object(
'title', m.title,
'year', m.year,
'url', m.slug
) ORDER BY m.year DESC)
FROM posts_movies pm
LEFT JOIN movies m ON pm.movies_id = m.id
WHERE pm.posts_id = p.id
) AS movies,
(
SELECT json_agg(json_build_object(
'title', s.title,
'year', s.year,
'url', s.slug
))
FROM posts_shows ps
LEFT JOIN shows s ON ps.shows_id = s.id
WHERE ps.posts_id = p.id
) AS shows,
json_build_object(
'title', p.title,
'url', CONCAT('https://coryd.dev', p.slug),
'description', p.content,
'date', p.date,
'image', CASE
WHEN df.filename_disk IS NOT NULL AND df.filename_disk != '' AND df.filename_disk != '/'
THEN CONCAT('/', df.filename_disk)
ELSE NULL
END
) AS feed
(
SELECT
json_agg(
CASE WHEN pb.collection = 'youtube_player' THEN
json_build_object('type', pb.collection, 'url', yp.url)
WHEN pb.collection = 'github_banner' THEN
json_build_object('type', pb.collection, 'url', gb.url)
WHEN pb.collection = 'npm_banner' THEN
json_build_object('type', pb.collection, 'url', nb.url, 'command', nb.command)
WHEN pb.collection = 'rss_banner' THEN
json_build_object('type', pb.collection, 'url', rb.url, 'text', rb.text)
WHEN pb.collection = 'hero' THEN
json_build_object('type', pb.collection, 'image', CONCAT('/', df_hero.filename_disk), 'alt_text', h.alt_text)
WHEN pb.collection = 'markdown' THEN
json_build_object('type', pb.collection, 'text', md.text)
WHEN pb.collection = 'divider' THEN
json_build_object('type', pb.collection, 'markup', d.markup)
ELSE
json_build_object('type', pb.collection)
END)
FROM
posts_blocks pb
LEFT JOIN youtube_player yp ON pb.collection = 'youtube_player'
AND yp.id = pb.item::integer
LEFT JOIN github_banner gb ON pb.collection = 'github_banner'
AND gb.id = pb.item::integer
LEFT JOIN npm_banner nb ON pb.collection = 'npm_banner'
AND nb.id = pb.item::integer
LEFT JOIN rss_banner rb ON pb.collection = 'rss_banner'
AND rb.id = pb.item::integer
LEFT JOIN hero h ON pb.collection = 'hero'
AND h.id = pb.item::integer
LEFT JOIN directus_files df_hero ON h.image = df_hero.id
LEFT JOIN markdown md ON pb.collection = 'markdown'
AND md.id = pb.item::integer
LEFT JOIN divider d ON pb.collection = 'divider'
AND d.id = pb.item::integer
WHERE
pb.posts_id = p.id) AS blocks,
(
SELECT
array_agg(t.name)
FROM
posts_tags pt
LEFT JOIN tags t ON pt.tags_id = t.id
WHERE
pt.posts_id = p.id) AS tags,
(
SELECT
json_agg(json_build_object('name', g.name, 'url', g.slug))
FROM
posts_genres gp
LEFT JOIN genres g ON gp.genres_id = g.id
WHERE
gp.posts_id = p.id) AS genres,
(
SELECT
json_agg(json_build_object('name', a.name_string, 'url', a.slug, 'country', a.country, 'total_plays', a.total_plays))
FROM
posts_artists pa
LEFT JOIN artists a ON pa.artists_id = a.id
WHERE
pa.posts_id = p.id) AS artists,
(
SELECT
json_agg(json_build_object('title', b.title, 'author', b.author, 'url', b.slug)
ORDER BY b.title)
FROM
posts_books pbk
LEFT JOIN books b ON pbk.books_id = b.id
WHERE
pbk.posts_id = p.id) AS books,
(
SELECT
json_agg(json_build_object('title', m.title, 'year', m.year, 'url', m.slug)
ORDER BY m.year DESC)
FROM
posts_movies pm
LEFT JOIN movies m ON pm.movies_id = m.id
WHERE
pm.posts_id = p.id) AS movies,
(
SELECT
json_agg(json_build_object('title', s.title, 'year', s.year, 'url', s.slug))
FROM
posts_shows ps
LEFT JOIN shows s ON ps.shows_id = s.id
WHERE
ps.posts_id = p.id) AS shows,
json_build_object('title', p.title, 'url', CONCAT('https://coryd.dev', p.slug), 'description', p.content, 'date', p.date, 'image', CASE WHEN df.filename_disk IS NOT NULL
AND df.filename_disk != ''
AND df.filename_disk != '/' THEN
CONCAT('/', df.filename_disk)
ELSE
NULL
END) AS feed
FROM
posts p
LEFT JOIN
directus_files df ON p.image = df.id
LEFT JOIN directus_files df ON p.image = df.id
GROUP BY
p.id, df.filename_disk;
p.id,
df.filename_disk;

View file

@ -1,91 +1,71 @@
CREATE OR REPLACE VIEW optimized_all_activity AS
WITH feed_data AS (
SELECT
p.date AS content_date,
p.title,
p.description,
CONCAT('https://coryd.dev', p.url) AS url,
NULL AS image,
NULL AS rating,
p.tags,
json_build_object(
'title', p.title,
'url', CONCAT('https://coryd.dev', p.url),
'description', p.description,
'date', p.date
) AS feed
FROM optimized_posts p
UNION ALL
SELECT
l.date AS content_date,
l.title,
l.description,
l.link AS url,
NULL AS image,
NULL AS rating,
l.tags,
json_build_object(
'title', l.title,
'url', l.link,
'description', l.description,
'date', l.date
) AS feed
FROM optimized_links l
UNION ALL
SELECT
b.date_finished AS content_date,
b.title,
b.description,
CONCAT('https://coryd.dev', b.url) AS url,
b.image,
b.rating,
b.tags,
CASE
WHEN LOWER(b.status) = 'finished' THEN json_build_object(
'title', b.title,
'url', CONCAT('https://coryd.dev', b.url),
'description', b.description,
'image', b.image,
'rating', b.rating,
'date', b.date_finished
)
ELSE NULL
END AS feed
FROM optimized_books b
UNION ALL
SELECT
m.last_watched AS content_date,
m.title,
m.description,
CONCAT('https://coryd.dev', m.url) AS url,
m.image,
m.rating,
m.tags,
CASE
WHEN m.last_watched IS NOT NULL THEN json_build_object(
'title', m.title,
'url', CONCAT('https://coryd.dev', m.url),
'description', m.description,
'image', m.image,
'rating', m.rating,
'date', m.last_watched
)
ELSE NULL
END AS feed
FROM optimized_movies m
SELECT
p.date AS content_date,
p.title,
p.description,
CONCAT('https://coryd.dev', p.url) AS url,
NULL AS image,
NULL AS rating,
p.tags,
json_build_object('title', p.title, 'url', CONCAT('https://coryd.dev', p.url), 'description', p.description, 'date', p.date) AS feed
FROM
optimized_posts p
UNION ALL
SELECT
l.date AS content_date,
l.title,
l.description,
l.link AS url,
NULL AS image,
NULL AS rating,
l.tags,
json_build_object('title', l.title, 'url', l.link, 'description', l.description, 'date', l.date) AS feed
FROM
optimized_links l
UNION ALL
SELECT
b.date_finished AS content_date,
b.title,
b.description,
CONCAT('https://coryd.dev', b.url) AS url,
b.image,
b.rating,
b.tags,
CASE WHEN LOWER(b.status) = 'finished' THEN
json_build_object('title', b.title, 'url', CONCAT('https://coryd.dev', b.url), 'description', b.description, 'image', b.image, 'rating', b.rating, 'date', b.date_finished)
ELSE
NULL
END AS feed
FROM
optimized_books b
UNION ALL
SELECT
m.last_watched AS content_date,
m.title,
m.description,
CONCAT('https://coryd.dev', m.url) AS url,
m.image,
m.rating,
m.tags,
CASE WHEN m.last_watched IS NOT NULL THEN
json_build_object('title', m.title, 'url', CONCAT('https://coryd.dev', m.url), 'description', m.description, 'image', m.image, 'rating', m.rating, 'date', m.last_watched)
ELSE
NULL
END AS feed
FROM
optimized_movies m
)
SELECT json_agg(feed_data.* ORDER BY feed_data.content_date DESC) AS feed
SELECT
json_agg(feed_data.* ORDER BY feed_data.content_date DESC) AS feed
FROM (
SELECT *
FROM feed_data
WHERE feed IS NOT NULL
ORDER BY content_date DESC
LIMIT 20
) AS feed_data;
SELECT
*
FROM
feed_data
WHERE
feed IS NOT NULL
ORDER BY
content_date DESC
LIMIT 20) AS feed_data;

View file

@ -1,97 +1,98 @@
CREATE OR REPLACE VIEW optimized_search_index AS
WITH search_data AS (
SELECT
'post' AS type,
CONCAT('📝 ', p.title) AS title,
CONCAT('https://coryd.dev', p.url) AS url,
p.description AS description,
p.tags,
NULL AS genre_name,
NULL AS genre_url,
NULL::integer AS total_plays,
p.date AS content_date
FROM optimized_posts p
UNION ALL
SELECT
'link' AS type,
CONCAT('🔗 ', l.title, ' via ', l.name) AS title,
l.link AS url,
l.description AS description,
l.tags,
NULL AS genre_name,
NULL AS genre_url,
NULL::integer AS total_plays,
l.date AS content_date
FROM optimized_links l
UNION ALL
SELECT
'book' AS type,
CASE
WHEN b.rating IS NOT NULL THEN CONCAT('📖 ', b.title, ' (', b.rating, ')')
ELSE CONCAT('📖 ', b.title)
END AS title,
CONCAT('https://coryd.dev', b.url) AS url,
b.description AS description,
b.tags,
NULL AS genre_name,
NULL AS genre_url,
NULL::integer AS total_plays,
b.date_finished AS content_date
FROM optimized_books b
WHERE LOWER(b.status) = 'finished'
UNION ALL
SELECT
'artist' AS type,
CONCAT(COALESCE(ar.emoji, ar.genre_emoji, '🎧'), ' ', ar.name) AS title,
CONCAT('https://coryd.dev', ar.url) AS url,
ar.description AS description,
ARRAY[ar.genre_name] AS tags,
ar.genre_name,
CONCAT('https://coryd.dev', ar.genre_slug) AS genre_url,
ar.total_plays,
NULL AS content_date
FROM optimized_artists ar
UNION ALL
SELECT
'genre' AS type,
CONCAT(COALESCE(g.emoji, '🎵'), ' ', g.name) AS title,
CONCAT('https://coryd.dev', g.url) AS url,
g.description AS description,
NULL AS tags,
g.name AS genre_name,
CONCAT('https://coryd.dev', g.url) AS genre_url,
NULL::integer AS total_plays,
NULL AS content_date
FROM optimized_genres g
UNION ALL
SELECT
'show' AS type,
CASE
WHEN s.review IS NOT NULL THEN CONCAT('📺 ', s.title, ' (', s.year, ') - ', s.review)
ELSE CONCAT('📺 ', s.title, ' (', s.year, ')')
END AS title,
CONCAT('https://coryd.dev', s.url) AS url,
s.description AS description,
s.tags,
NULL AS genre_name,
NULL AS genre_url,
NULL::integer AS total_plays,
s.last_watched_at AS content_date
FROM optimized_shows s
WHERE s.last_watched_at IS NOT NULL
SELECT
'post' AS type,
CONCAT('📝 ', p.title) AS title,
CONCAT('https://coryd.dev', p.url) AS url,
p.description AS description,
p.tags,
NULL AS genre_name,
NULL AS genre_url,
NULL::text AS total_plays,
p.date AS content_date
FROM
optimized_posts p
UNION ALL
SELECT
'link' AS type,
CONCAT('🔗 ', l.title, ' via ', l.name) AS title,
l.link AS url,
l.description AS description,
l.tags,
NULL AS genre_name,
NULL AS genre_url,
NULL::text AS total_plays,
l.date AS content_date
FROM
optimized_links l
UNION ALL
SELECT
'book' AS type,
CASE WHEN b.rating IS NOT NULL THEN
CONCAT('📖 ', b.title, ' (', b.rating, ')')
ELSE
CONCAT('📖 ', b.title)
END AS title,
CONCAT('https://coryd.dev', b.url) AS url,
b.description AS description,
b.tags,
NULL AS genre_name,
NULL AS genre_url,
NULL::text AS total_plays,
b.date_finished AS content_date
FROM
optimized_books b
WHERE
LOWER(b.status) = 'finished'
UNION ALL
SELECT
'artist' AS type,
CONCAT(COALESCE(ar.emoji, ar.genre_emoji, '🎧'), ' ', ar.name) AS title,
CONCAT('https://coryd.dev', ar.url) AS url,
ar.description AS description,
ARRAY[ar.genre_name] AS tags,
ar.genre_name,
CONCAT('https://coryd.dev', ar.genre_slug) AS genre_url,
to_char(ar.total_plays::numeric, 'FM999,999,999,999') AS total_plays,
NULL AS content_date
FROM
optimized_artists ar
UNION ALL
SELECT
'genre' AS type,
CONCAT(COALESCE(g.emoji, '🎵'), ' ', g.name) AS title,
CONCAT('https://coryd.dev', g.url) AS url,
g.description AS description,
NULL AS tags,
g.name AS genre_name,
CONCAT('https://coryd.dev', g.url) AS genre_url,
NULL::text AS total_plays,
NULL AS content_date
FROM
optimized_genres g
UNION ALL
SELECT
'show' AS type,
CASE WHEN s.review IS NOT NULL THEN
CONCAT('📺 ', s.title, ' (', s.year, ') - ', s.review)
ELSE
CONCAT('📺 ', s.title, ' (', s.year, ')')
END AS title,
CONCAT('https://coryd.dev', s.url) AS url,
s.description AS description,
s.tags,
NULL AS genre_name,
NULL AS genre_url,
NULL::text AS total_plays,
s.last_watched_at AS content_date
FROM
optimized_shows s
WHERE
s.last_watched_at IS NOT NULL
)
SELECT
ROW_NUMBER() OVER (ORDER BY url) AS id,
*
FROM search_data;
ROW_NUMBER() OVER (ORDER BY url) AS id,
*
FROM
search_data;

View file

@ -1,68 +1,65 @@
CREATE OR REPLACE VIEW optimized_sitemap AS
WITH sitemap_data AS (
SELECT
p.date AS content_date,
p.title,
CONCAT('https://coryd.dev', p.url) AS url,
'monthly' AS changefreq,
0.7 AS priority
FROM optimized_posts p
UNION ALL
SELECT
b.date_finished AS content_date,
b.title,
CONCAT('https://coryd.dev', b.url) AS url,
'monthly' AS changefreq,
0.5 AS priority
FROM optimized_books b
UNION ALL
SELECT
m.last_watched AS content_date,
m.title,
CONCAT('https://coryd.dev', m.url) AS url,
'weekly' AS changefreq,
0.6 AS priority
FROM optimized_movies m
UNION ALL
SELECT
NULL AS content_date,
ar.name AS title,
CONCAT('https://coryd.dev', ar.url) AS url,
'monthly' AS changefreq,
0.5 AS priority
FROM optimized_artists ar
UNION ALL
SELECT
NULL AS content_date,
g.name AS title,
CONCAT('https://coryd.dev', g.url) AS url,
'yearly' AS changefreq,
0.3 AS priority
FROM optimized_genres g
UNION ALL
SELECT
s.last_watched_at AS content_date,
s.title,
CONCAT('https://coryd.dev', s.url) AS url,
'weekly' AS changefreq,
0.8 AS priority
FROM optimized_shows s
SELECT
p.date AS content_date,
p.title,
CONCAT('https://coryd.dev', p.url) AS url,
'monthly' AS changefreq,
0.7 AS priority
FROM
optimized_posts p
UNION ALL
SELECT
b.date_finished AS content_date,
b.title,
CONCAT('https://coryd.dev', b.url) AS url,
'monthly' AS changefreq,
0.5 AS priority
FROM
optimized_books b
UNION ALL
SELECT
m.last_watched AS content_date,
m.title,
CONCAT('https://coryd.dev', m.url) AS url,
'weekly' AS changefreq,
0.6 AS priority
FROM
optimized_movies m
UNION ALL
SELECT
NULL AS content_date,
ar.name AS title,
CONCAT('https://coryd.dev', ar.url) AS url,
'monthly' AS changefreq,
0.5 AS priority
FROM
optimized_artists ar
UNION ALL
SELECT
NULL AS content_date,
g.name AS title,
CONCAT('https://coryd.dev', g.url) AS url,
'yearly' AS changefreq,
0.3 AS priority
FROM
optimized_genres g
UNION ALL
SELECT
s.last_watched_at AS content_date,
s.title,
CONCAT('https://coryd.dev', s.url) AS url,
'weekly' AS changefreq,
0.8 AS priority
FROM
optimized_shows s
)
SELECT
url,
title,
content_date AS lastmod,
changefreq,
priority
FROM sitemap_data;
FROM
sitemap_data;

View file

@ -1,105 +1,79 @@
CREATE OR REPLACE VIEW optimized_syndication AS
WITH syndication_data AS (
SELECT
p.date AS content_date,
p.title,
p.description,
CONCAT('https://coryd.dev', p.url) AS url,
p.tags,
json_build_object(
'title', CONCAT('📝 ', p.title, ' ', (
SELECT array_to_string(
array_agg('#' || initcap(replace(t.name, ' ', ''))), ' '
)
FROM unnest(p.tags) AS t(name)
)),
'description', p.description,
'url', CONCAT('https://coryd.dev', p.url),
'date', p.date
) AS syndication
FROM optimized_posts p
UNION ALL
SELECT
l.date AS content_date,
l.title,
l.description,
l.link AS url,
l.tags,
json_build_object(
'title', CONCAT('🔗 ', l.title,
CASE
WHEN l.mastodon IS NOT NULL THEN
' via @' || split_part(l.mastodon, '@', 2) || '@' || split_part(split_part(l.mastodon, 'https://', 2), '/', 1)
ELSE CONCAT(' via ', l.name)
END, ' ', (
SELECT array_to_string(
array_agg('#' || initcap(replace(t.name, ' ', ''))), ' '
)
FROM unnest(l.tags) AS t(name)
)
),
'description', l.description,
'url', l.link,
'date', l.date
) AS syndication
FROM optimized_links l
UNION ALL
SELECT
b.date_finished AS content_date,
b.title,
b.description,
CONCAT('https://coryd.dev', b.url) AS url,
b.tags,
CASE
WHEN LOWER(b.status) = 'finished' THEN json_build_object(
'title', CONCAT('📖 ', b.title, ' ', (
SELECT array_to_string(
array_agg('#' || initcap(replace(t.name, ' ', ''))), ' '
)
FROM unnest(b.tags) AS t(name)
)),
'description', b.description,
'url', CONCAT('https://coryd.dev', b.url),
'date', b.date_finished
)
ELSE NULL
END AS syndication
FROM optimized_books b
UNION ALL
SELECT
m.last_watched AS content_date,
m.title,
m.description,
CONCAT('https://coryd.dev', m.url) AS url,
m.tags,
CASE
WHEN m.last_watched IS NOT NULL THEN json_build_object(
'title', CONCAT('🎥 ', m.title, ' ', (
SELECT array_to_string(
array_agg('#' || initcap(replace(t.name, ' ', ''))), ' '
)
FROM unnest(m.tags) AS t(name)
)),
'description', m.description,
'url', CONCAT('https://coryd.dev', m.url),
'date', m.last_watched
)
ELSE NULL
END AS syndication
FROM optimized_movies m
SELECT
p.date AS content_date,
p.title,
p.description,
CONCAT('https://coryd.dev', p.url) AS url,
p.tags,
json_build_object('title', CONCAT('📝 ', p.title, ' ',(
SELECT
array_to_string(array_agg('#' || initcap(replace(t.name, ' ', ''))), ' ')
FROM unnest(p.tags) AS t(name))), 'description', p.description, 'url', CONCAT('https://coryd.dev', p.url), 'date', p.date) AS syndication
FROM
optimized_posts p
UNION ALL
SELECT
l.date AS content_date,
l.title,
l.description,
l.link AS url,
l.tags,
json_build_object('title', CONCAT('🔗 ', l.title, CASE WHEN l.mastodon IS NOT NULL THEN
' via @' || split_part(l.mastodon, '@', 2) || '@' || split_part(split_part(l.mastodon, 'https://', 2), '/', 1)
ELSE
CONCAT(' via ', l.name)
END, ' ',(
SELECT
array_to_string(array_agg('#' || initcap(replace(t.name, ' ', ''))), ' ')
FROM unnest(l.tags) AS t(name))), 'description', l.description, 'url', l.link, 'date', l.date) AS syndication
FROM
optimized_links l
UNION ALL
SELECT
b.date_finished AS content_date,
b.title,
b.description,
CONCAT('https://coryd.dev', b.url) AS url,
b.tags,
CASE WHEN LOWER(b.status) = 'finished' THEN
json_build_object('title', CONCAT('📖 ', b.title, ' ',(
SELECT
array_to_string(array_agg('#' || initcap(replace(t.name, ' ', ''))), ' ')
FROM unnest(b.tags) AS t(name))), 'description', b.description, 'url', CONCAT('https://coryd.dev', b.url), 'date', b.date_finished)
ELSE
NULL
END AS syndication
FROM
optimized_books b
UNION ALL
SELECT
m.last_watched AS content_date,
m.title,
m.description,
CONCAT('https://coryd.dev', m.url) AS url,
m.tags,
CASE WHEN m.last_watched IS NOT NULL THEN
json_build_object('title', CONCAT('🎥 ', m.title, ' ',(
SELECT
array_to_string(array_agg('#' || initcap(replace(t.name, ' ', ''))), ' ')
FROM unnest(m.tags) AS t(name))), 'description', m.description, 'url', CONCAT('https://coryd.dev', m.url), 'date', m.last_watched)
ELSE
NULL
END AS syndication
FROM
optimized_movies m
)
SELECT json_agg(limited_data.*) AS syndication
SELECT
json_agg(limited_data.*) AS syndication
FROM (
SELECT *
FROM syndication_data
WHERE syndication IS NOT NULL
ORDER BY content_date DESC
LIMIT 20
) AS limited_data;
SELECT
*
FROM
syndication_data
WHERE
syndication IS NOT NULL
ORDER BY
content_date DESC
LIMIT 20) AS limited_data;

View file

@ -19,4 +19,4 @@ SELECT
FROM
globals g
LEFT JOIN directus_files df ON g.avatar = df.id
LEFT JOIN directus_files df2 ON g.avatar_transparent = df2.id
LEFT JOIN directus_files df2 ON g.avatar_transparent = df2.id

View file

@ -10,5 +10,5 @@ SELECT
p.permalink AS page_permalink
FROM
navigation n
LEFT JOIN
pages p ON n.pages = p.id;
LEFT JOIN pages p ON n.pages = p.id;

View file

@ -6,58 +6,49 @@ SELECT
p.description,
CONCAT('/', df.filename_disk) AS open_graph_image,
p.updated,
(
SELECT json_agg(
CASE
WHEN pb.collection = 'youtube_player' THEN json_build_object(
'type', pb.collection,
'url', yp.url
)
WHEN pb.collection = 'github_banner' THEN json_build_object(
'type', pb.collection,
'url', gb.url
)
WHEN pb.collection = 'npm_banner' THEN json_build_object(
'type', pb.collection,
'url', nb.url,
'command', nb.command
)
WHEN pb.collection = 'rss_banner' THEN json_build_object(
'type', pb.collection,
'url', rb.url,
'text', rb.text
)
WHEN pb.collection = 'hero' THEN json_build_object(
'type', pb.collection,
'image', CONCAT('/', df_hero.filename_disk),
'alt', h.alt_text
)
WHEN pb.collection = 'markdown' THEN json_build_object(
'type', pb.collection,
'text', md.text
)
WHEN pb.collection = 'divider' THEN json_build_object(
'type', pb.collection,
'markup', d.markup
)
ELSE json_build_object('type', pb.collection)
END
ORDER BY pb.sort)
FROM pages_blocks pb
LEFT JOIN youtube_player yp ON pb.collection = 'youtube_player' AND yp.id = pb.item::INTEGER
LEFT JOIN github_banner gb ON pb.collection = 'github_banner' AND gb.id = pb.item::INTEGER
LEFT JOIN npm_banner nb ON pb.collection = 'npm_banner' AND nb.id = pb.item::INTEGER
LEFT JOIN rss_banner rb ON pb.collection = 'rss_banner' AND rb.id = pb.item::INTEGER
LEFT JOIN hero h ON pb.collection = 'hero' AND h.id = pb.item::INTEGER
LEFT JOIN directus_files df_hero ON h.image = df_hero.id
LEFT JOIN markdown md ON pb.collection = 'markdown' AND md.id = pb.item::INTEGER
LEFT JOIN divider d ON pb.collection = 'divider' AND d.id = pb.item::INTEGER
WHERE pb.pages_id = p.id
) AS blocks
(
SELECT
json_agg(
CASE WHEN pb.collection = 'youtube_player' THEN
json_build_object('type', pb.collection, 'url', yp.url)
WHEN pb.collection = 'github_banner' THEN
json_build_object('type', pb.collection, 'url', gb.url)
WHEN pb.collection = 'npm_banner' THEN
json_build_object('type', pb.collection, 'url', nb.url, 'command', nb.command)
WHEN pb.collection = 'rss_banner' THEN
json_build_object('type', pb.collection, 'url', rb.url, 'text', rb.text)
WHEN pb.collection = 'hero' THEN
json_build_object('type', pb.collection, 'image', CONCAT('/', df_hero.filename_disk), 'alt', h.alt_text)
WHEN pb.collection = 'markdown' THEN
json_build_object('type', pb.collection, 'text', md.text)
WHEN pb.collection = 'divider' THEN
json_build_object('type', pb.collection, 'markup', d.markup)
ELSE
json_build_object('type', pb.collection)
END ORDER BY pb.sort)
FROM
pages_blocks pb
LEFT JOIN youtube_player yp ON pb.collection = 'youtube_player'
AND yp.id = pb.item::integer
LEFT JOIN github_banner gb ON pb.collection = 'github_banner'
AND gb.id = pb.item::integer
LEFT JOIN npm_banner nb ON pb.collection = 'npm_banner'
AND nb.id = pb.item::integer
LEFT JOIN rss_banner rb ON pb.collection = 'rss_banner'
AND rb.id = pb.item::integer
LEFT JOIN hero h ON pb.collection = 'hero'
AND h.id = pb.item::integer
LEFT JOIN directus_files df_hero ON h.image = df_hero.id
LEFT JOIN markdown md ON pb.collection = 'markdown'
AND md.id = pb.item::integer
LEFT JOIN divider d ON pb.collection = 'divider'
AND d.id = pb.item::integer
WHERE
pb.pages_id = p.id) AS blocks
FROM
pages p
LEFT JOIN
directus_files df ON p.open_graph_image = df.id
LEFT JOIN directus_files df ON p.open_graph_image = df.id
GROUP BY
p.id, df.filename_disk;
p.id,
df.filename_disk;

View file

@ -1,120 +1,91 @@
CREATE OR REPLACE VIEW optimized_books AS
SELECT
b.date_finished,
EXTRACT(YEAR FROM b.date_finished) AS year,
b.author,
b.description,
b.title,
b.progress,
b.read_status AS status,
b.star_rating AS rating,
b.review,
b.slug AS url,
CONCAT('/', df.filename_disk) AS image,
b.favorite,
b.tattoo,
(
SELECT array_agg(t.name)
FROM books_tags bt
LEFT JOIN tags t ON bt.tags_id = t.id
WHERE bt.books_id = b.id
) AS tags,
(
SELECT json_agg(json_build_object(
'name', a.name_string,
'url', a.slug,
'country', a.country,
'total_plays', a.total_plays
))
FROM books_artists ba
LEFT JOIN artists a ON ba.artists_id = a.id
WHERE ba.books_id = b.id
) AS artists,
(
SELECT json_agg(json_build_object(
'title', m.title,
'year', m.year,
'url', m.slug
))
FROM movies_books mb
LEFT JOIN movies m ON mb.movies_id = m.id
WHERE mb.books_id = b.id
) AS movies,
(
SELECT json_agg(json_build_object(
'name', g.name,
'url', g.slug
))
FROM genres_books gb
LEFT JOIN genres g ON gb.genres_id = g.id
WHERE gb.books_id = b.id
) AS genres,
(
SELECT json_agg(json_build_object(
'title', s.title,
'year', s.year,
'url', s.slug
))
FROM shows_books sb
LEFT JOIN shows s ON sb.shows_id = s.id
WHERE sb.books_id = b.id
) AS shows,
(
SELECT json_agg(json_build_object(
'title', p.title,
'date', p.date,
'url', p.slug
) ORDER BY p.date DESC)
FROM posts_books pb
LEFT JOIN posts p ON pb.posts_id = p.id
WHERE pb.books_id = b.id
) AS posts,
(
SELECT json_agg(json_build_object(
'title', rb.title,
'author', rb.author,
'url', rb.slug
) ORDER BY rb.title)
FROM related_books rbk
LEFT JOIN books rb ON rbk.related_books_id = rb.id
WHERE rbk.books_id = b.id
) AS related_books,
json_build_object(
'title', CONCAT(b.title, ' by ', b.author),
'image', CONCAT('/', df.filename_disk),
'url', b.slug,
'alt', CONCAT(b.title, ' by ', b.author),
'subtext',
CASE
WHEN b.star_rating IS NOT NULL
THEN b.star_rating
ELSE NULL
END
) AS grid,
CASE
WHEN LOWER(b.read_status) = 'finished' AND b.star_rating IS NOT NULL THEN json_build_object(
'title', b.title,
'url', CONCAT('https://coryd.dev', b.slug),
'date', b.date_finished,
'description', b.description,
'image', CONCAT('/', df.filename_disk),
'rating', b.star_rating
)
ELSE NULL
END AS feed
b.date_finished,
EXTRACT(YEAR FROM b.date_finished) AS year,
b.author,
b.description,
b.title,
b.progress,
b.read_status AS status,
b.star_rating AS rating,
b.review,
b.slug AS url,
CONCAT('/', df.filename_disk) AS image,
b.favorite,
b.tattoo,
(
SELECT
array_agg(t.name)
FROM
books_tags bt
LEFT JOIN tags t ON bt.tags_id = t.id
WHERE
bt.books_id = b.id) AS tags,
(
SELECT
json_agg(json_build_object('name', a.name_string, 'url', a.slug, 'country', a.country, 'total_plays', a.total_plays))
FROM
books_artists ba
LEFT JOIN artists a ON ba.artists_id = a.id
WHERE
ba.books_id = b.id) AS artists,
(
SELECT
json_agg(json_build_object('title', m.title, 'year', m.year, 'url', m.slug))
FROM
movies_books mb
LEFT JOIN movies m ON mb.movies_id = m.id
WHERE
mb.books_id = b.id) AS movies,
(
SELECT
json_agg(json_build_object('name', g.name, 'url', g.slug))
FROM
genres_books gb
LEFT JOIN genres g ON gb.genres_id = g.id
WHERE
gb.books_id = b.id) AS genres,
(
SELECT
json_agg(json_build_object('title', s.title, 'year', s.year, 'url', s.slug))
FROM
shows_books sb
LEFT JOIN shows s ON sb.shows_id = s.id
WHERE
sb.books_id = b.id) AS shows,
(
SELECT
json_agg(json_build_object('title', p.title, 'date', p.date, 'url', p.slug)
ORDER BY p.date DESC)
FROM
posts_books pb
LEFT JOIN posts p ON pb.posts_id = p.id
WHERE
pb.books_id = b.id) AS posts,
(
SELECT
json_agg(json_build_object('title', rb.title, 'author', rb.author, 'url', rb.slug)
ORDER BY rb.title)
FROM
related_books rbk
LEFT JOIN books rb ON rbk.related_books_id = rb.id
WHERE
rbk.books_id = b.id) AS related_books,
json_build_object('title', CONCAT(b.title, ' by ', b.author), 'image', CONCAT('/', df.filename_disk), 'url', b.slug, 'alt', CONCAT(b.title, ' by ', b.author), 'subtext', CASE WHEN b.star_rating IS NOT NULL THEN
b.star_rating
ELSE
NULL
END) AS grid,
CASE WHEN LOWER(b.read_status) = 'finished'
AND b.star_rating IS NOT NULL THEN
json_build_object('title', b.title, 'url', CONCAT('https://coryd.dev', b.slug), 'date', b.date_finished, 'description', b.description, 'image', CONCAT('/', df.filename_disk), 'rating', b.star_rating)
ELSE
NULL
END AS feed
FROM
books b
LEFT JOIN
directus_files df ON b.art = df.id
books b
LEFT JOIN directus_files df ON b.art = df.id
GROUP BY
b.id, df.filename_disk;
b.id,
df.filename_disk;

View file

@ -14,112 +14,89 @@ SELECT
m.slug AS url,
CONCAT('/', df.filename_disk) AS image,
CONCAT('/', df2.filename_disk) AS backdrop,
json_build_object(
'title', m.title,
'url', m.slug,
'image', CONCAT('/', df.filename_disk),
'backdrop', CONCAT('/', df2.filename_disk),
'alt', CONCAT(m.title, ' (', m.year, ')'),
'subtext', CASE
WHEN m.star_rating IS NOT NULL THEN CONCAT(m.star_rating, ' (', m.year, ')')
ELSE CONCAT('(', m.year, ')')
END
) AS grid,
(
SELECT array_agg(t.name)
FROM movies_tags mt
json_build_object('title', m.title, 'url', m.slug, 'image', CONCAT('/', df.filename_disk), 'backdrop', CONCAT('/', df2.filename_disk), 'alt', CONCAT(m.title, ' (', m.year, ')'), 'subtext', CASE WHEN m.star_rating IS NOT NULL THEN
CONCAT(m.star_rating, ' (', m.year, ')')
ELSE
CONCAT('(', m.year, ')')
END) AS grid,
(
SELECT
array_agg(t.name)
FROM
movies_tags mt
LEFT JOIN tags t ON mt.tags_id = t.id
WHERE mt.movies_id = m.id
) AS tags,
(
SELECT json_agg(json_build_object(
'name', g.name,
'url', g.slug
) ORDER BY g.name ASC)
FROM genres_movies gm
LEFT JOIN genres g ON gm.genres_id = g.id
WHERE gm.movies_id = m.id
) AS genres,
(
SELECT json_agg(json_build_object(
'name', a.name_string,
'url', a.slug,
'country', a.country,
'total_plays', a.total_plays
) ORDER BY a.name_string ASC)
FROM movies_artists ma
LEFT JOIN artists a ON ma.artists_id = a.id
WHERE ma.movies_id = m.id
) AS artists,
(
SELECT json_agg(json_build_object(
'title', b.title,
'author', b.author,
'url', b.slug
) ORDER BY b.title ASC)
FROM movies_books mb
LEFT JOIN books b ON mb.books_id = b.id
WHERE mb.movies_id = m.id
) AS books,
(
SELECT json_agg(json_build_object(
'title', s.title,
'year', s.year,
'url', s.slug
) ORDER BY s.year DESC)
FROM shows_movies sm
LEFT JOIN shows s ON sm.shows_id = s.id
WHERE sm.movies_id = m.id
) AS shows,
(
SELECT json_agg(json_build_object(
'title', p.title,
'date', p.date,
'url', p.slug
) ORDER BY p.date DESC)
FROM posts_movies pm
LEFT JOIN posts p ON pm.posts_id = p.id
WHERE pm.movies_id = m.id
) AS posts,
(
SELECT json_agg(json_build_object(
'title', rm.title,
'year', rm.year,
'url', rm.slug
) ORDER BY rm.year DESC)
FROM related_movies r
LEFT JOIN movies rm ON r.related_movies_id = rm.id
WHERE r.movies_id = m.id
) AS related_movies,
CASE
WHEN m.star_rating IS NOT NULL AND m.last_watched IS NOT NULL THEN
json_build_object(
'title', m.title,
'url', CONCAT('https://coryd.dev/movies/', m.slug),
'date', m.last_watched,
'description', m.description,
'image', CONCAT('/', df.filename_disk),
'rating', m.star_rating
)
ELSE NULL
END AS feed
WHERE
mt.movies_id = m.id) AS tags,
(
SELECT
json_agg(json_build_object('name', g.name, 'url', g.slug)
ORDER BY g.name ASC)
FROM
genres_movies gm
LEFT JOIN genres g ON gm.genres_id = g.id
WHERE
gm.movies_id = m.id) AS genres,
(
SELECT
json_agg(json_build_object('name', a.name_string, 'url', a.slug, 'country', a.country, 'total_plays', a.total_plays)
ORDER BY a.name_string ASC)
FROM
movies_artists ma
LEFT JOIN artists a ON ma.artists_id = a.id
WHERE
ma.movies_id = m.id) AS artists,
(
SELECT
json_agg(json_build_object('title', b.title, 'author', b.author, 'url', b.slug)
ORDER BY b.title ASC)
FROM
movies_books mb
LEFT JOIN books b ON mb.books_id = b.id
WHERE
mb.movies_id = m.id) AS books,
(
SELECT
json_agg(json_build_object('title', s.title, 'year', s.year, 'url', s.slug)
ORDER BY s.year DESC)
FROM
shows_movies sm
LEFT JOIN shows s ON sm.shows_id = s.id
WHERE
sm.movies_id = m.id) AS shows,
(
SELECT
json_agg(json_build_object('title', p.title, 'date', p.date, 'url', p.slug)
ORDER BY p.date DESC)
FROM
posts_movies pm
LEFT JOIN posts p ON pm.posts_id = p.id
WHERE
pm.movies_id = m.id) AS posts,
(
SELECT
json_agg(json_build_object('title', rm.title, 'year', rm.year, 'url', rm.slug)
ORDER BY rm.year DESC)
FROM
related_movies r
LEFT JOIN movies rm ON r.related_movies_id = rm.id
WHERE
r.movies_id = m.id) AS related_movies,
CASE WHEN m.star_rating IS NOT NULL
AND m.last_watched IS NOT NULL THEN
json_build_object('title', m.title, 'url', CONCAT('https://coryd.dev/movies/', m.slug), 'date', m.last_watched, 'description', m.description, 'image', CONCAT('/', df.filename_disk), 'rating', m.star_rating)
ELSE
NULL
END AS feed
FROM
movies m
LEFT JOIN
directus_files df ON m.art = df.id
LEFT JOIN
directus_files df2 ON m.backdrop = df2.id
LEFT JOIN directus_files df ON m.art = df.id
LEFT JOIN directus_files df2 ON m.backdrop = df2.id
GROUP BY
m.id, df.filename_disk, df2.filename_disk
m.id,
df.filename_disk,
df2.filename_disk
ORDER BY
m.last_watched DESC, m.favorite DESC, m.title ASC;
m.last_watched DESC,
m.favorite DESC,
m.title ASC;

View file

@ -6,28 +6,13 @@ SELECT
a.total_plays,
CONCAT('/', df.filename_disk) AS image,
'album-release' AS type,
json_build_object(
'name', ar.name_string,
'url', ar.slug,
'description', ar.description
) AS artist,
json_build_object('name', ar.name_string, 'url', ar.slug, 'description', ar.description) AS artist,
EXTRACT(EPOCH FROM a.release_date) AS release_timestamp,
json_build_object(
'title', a.name,
'image', CONCAT('/', df.filename_disk),
'url', a.release_link,
'alt', CONCAT(a.name, ' by ', ar.name_string),
'subtext', CONCAT(ar.name_string, ' / ', TO_CHAR(a.release_date, 'Mon FMDD, YYYY'))
) AS grid
json_build_object('title', a.name, 'image', CONCAT('/', df.filename_disk), 'url', a.release_link, 'alt', CONCAT(a.name, ' by ', ar.name_string), 'subtext', CONCAT(ar.name_string, ' / ', TO_CHAR(a.release_date, 'Mon FMDD, YYYY'))) AS grid
FROM
albums a
LEFT JOIN
directus_files df ON a.art = df.id
LEFT JOIN
artists ar ON a.artist = ar.id
LEFT JOIN directus_files df ON a.art = df.id
LEFT JOIN artists ar ON a.artist = ar.id
WHERE
a.release_date IS NOT NULL;
a.release_date IS NOT NULL;

View file

@ -3,116 +3,92 @@ SELECT
ar.name_string AS name,
ar.slug AS url,
ar.tentative,
ar.total_plays,
to_char(ar.total_plays, 'FM999,999,999,999') AS total_plays, -- Format total_plays with commas
ar.country,
ar.description,
ar.favorite,
g.name AS genre_name,
g.slug AS genre_slug,
g.emoji AS genre_emoji,
json_build_object(
'name', g.name,
'url', g.slug,
'emoji', g.emoji
) AS genre,
json_build_object('name', g.name, 'url', g.slug, 'emoji', g.emoji) AS genre,
ar.emoji,
ar.tattoo,
CONCAT('/', df.filename_disk) AS image,
json_build_object(
'alt', CONCAT(ar.total_plays, ' plays of ', ar.name_string),
'subtext', CONCAT(ar.total_plays, ' plays')
) AS grid,
(
SELECT json_agg(json_build_object(
'name', a.name,
'release_year', a.release_year,
'total_plays', a.total_plays,
'art', df_album.filename_disk
) ORDER BY a.release_year)
FROM albums a
json_build_object('alt', CONCAT(to_char(ar.total_plays, 'FM999,999,999,999'), ' plays of ', ar.name_string), -- Format total_plays in alt text
'subtext', CONCAT(to_char(ar.total_plays, 'FM999,999,999,999'), ' plays') -- Format total_plays in subtext
) AS grid,
(
SELECT
json_agg(json_build_object('name', a.name, 'release_year', a.release_year, 'total_plays', to_char(a.total_plays, 'FM999,999,999,999'), -- Format total_plays here as well
'art', df_album.filename_disk)
ORDER BY a.release_year)
FROM
albums a
LEFT JOIN directus_files df_album ON a.art = df_album.id
WHERE a.artist = ar.id
) AS albums,
(
SELECT json_agg(json_build_object(
'id', c.id,
'date', c.date,
'venue_name', v.name,
'venue_name_short', trim(split_part(v.name, ',', 1)),
'venue_latitude', v.latitude,
'venue_longitude', v.longitude,
'notes', c.notes
) ORDER BY c.date DESC)
FROM concerts c
WHERE
a.artist = ar.id) AS albums,
(
SELECT
json_agg(json_build_object('id', c.id, 'date', c.date, 'venue_name', v.name, 'venue_name_short', trim(split_part(v.name, ',', 1)), 'venue_latitude', v.latitude, 'venue_longitude', v.longitude, 'notes', c.notes)
ORDER BY c.date DESC)
FROM
concerts c
LEFT JOIN venues v ON c.venue = v.id
WHERE c.artist = ar.id
) AS concerts,
(
SELECT json_agg(json_build_object(
'title', b.title,
'author', b.author,
'url', b.slug
) ORDER BY b.date_finished DESC)
FROM books_artists ba
WHERE
c.artist = ar.id) AS concerts,
(
SELECT
json_agg(json_build_object('title', b.title, 'author', b.author, 'url', b.slug)
ORDER BY b.date_finished DESC)
FROM
books_artists ba
LEFT JOIN books b ON ba.books_id = b.id
WHERE ba.artists_id = ar.id
) AS books,
(
SELECT json_agg(json_build_object(
'title', m.title,
'year', m.year,
'url', m.slug
) ORDER BY m.year DESC)
FROM movies_artists ma
WHERE
ba.artists_id = ar.id) AS books,
(
SELECT
json_agg(json_build_object('title', m.title, 'year', m.year, 'url', m.slug)
ORDER BY m.year DESC)
FROM
movies_artists ma
LEFT JOIN movies m ON ma.movies_id = m.id
WHERE ma.artists_id = ar.id
) AS movies,
(
SELECT json_agg(json_build_object(
'title', s.title,
'year', s.year,
'url', s.slug
) ORDER BY s.year DESC)
FROM shows_artists sa
WHERE
ma.artists_id = ar.id) AS movies,
(
SELECT
json_agg(json_build_object('title', s.title, 'year', s.year, 'url', s.slug)
ORDER BY s.year DESC)
FROM
shows_artists sa
LEFT JOIN shows s ON sa.shows_id = s.id
WHERE sa.artists_id = ar.id
) AS shows,
(
SELECT json_agg(json_build_object(
'title', p.title,
'date', p.date,
'url', p.slug
) ORDER BY p.date DESC)
FROM posts_artists pa
WHERE
sa.artists_id = ar.id) AS shows,
(
SELECT
json_agg(json_build_object('title', p.title, 'date', p.date, 'url', p.slug)
ORDER BY p.date DESC)
FROM
posts_artists pa
LEFT JOIN posts p ON pa.posts_id = p.id
WHERE pa.artists_id = ar.id
) AS posts,
(
SELECT json_agg(json_build_object(
'name', related_ar.name_string,
'url', related_ar.slug,
'country', related_ar.country,
'total_plays', related_ar.total_plays
) ORDER BY related_ar.name_string)
FROM related_artists ra
WHERE
pa.artists_id = ar.id) AS posts,
(
SELECT
json_agg(json_build_object('name', related_ar.name_string, 'url', related_ar.slug, 'country', related_ar.country, 'total_plays', to_char(related_ar.total_plays, 'FM999,999,999,999'))
ORDER BY related_ar.name_string)
FROM
related_artists ra
LEFT JOIN artists related_ar ON ra.related_artists_id = related_ar.id
WHERE ra.artists_id = ar.id
) AS related_artists
WHERE
ra.artists_id = ar.id) AS related_artists
FROM
artists ar
LEFT JOIN
directus_files df ON ar.art = df.id
LEFT JOIN
genres g ON ar.genres = g.id
LEFT JOIN directus_files df ON ar.art = df.id
LEFT JOIN genres g ON ar.genres = g.id
GROUP BY
ar.id, df.filename_disk, g.name, g.slug, g.emoji;
ar.id,
df.filename_disk,
g.name,
g.slug,
g.emoji;

View file

@ -3,34 +3,17 @@ SELECT
c.id,
c.date,
c.notes,
CASE
WHEN c.artist IS NOT NULL THEN
json_build_object(
'name', a.name_string,
'url', a.slug
)
ELSE
json_build_object(
'name', c.artist_name_string,
'url', NULL
)
CASE WHEN c.artist IS NOT NULL THEN
json_build_object('name', a.name_string, 'url', a.slug)
ELSE
json_build_object('name', c.artist_name_string, 'url', NULL)
END AS artist,
json_build_object(
'name', v.name,
'name_short', trim(split_part(v.name, ',', 1)),
'latitude', v.latitude,
'longitude', v.longitude,
'notes', v.notes
) AS venue,
json_build_object('name', v.name, 'name_short', trim(split_part(v.name, ',', 1)), 'latitude', v.latitude, 'longitude', v.longitude, 'notes', v.notes) AS venue,
c.notes AS concert_notes
FROM
concerts c
LEFT JOIN artists a ON c.artist = a.id
LEFT JOIN venues v ON c.venue = v.id
ORDER BY
c.date DESC;
c.date DESC;

View file

@ -4,53 +4,45 @@ SELECT
g.name,
g.description,
g.emoji,
g.total_plays,
to_char(g.total_plays, 'FM999,999,999,999') AS total_plays,
g.wiki_link,
g.slug AS url,
(
SELECT
json_agg(json_build_object('name', a.name_string, 'url', a.slug, 'total_plays', to_char(a.total_plays, 'FM999,999,999,999'))
ORDER BY a.total_plays DESC)
FROM
artists a
WHERE
a.genres = g.id) AS artists,
(
SELECT
json_agg(json_build_object('title', b.title, 'author', b.author, 'url', b.slug))
FROM
books b
JOIN genres_books gb ON gb.books_id = b.id
WHERE
gb.genres_id = g.id) AS books,
(
SELECT
json_agg(json_build_object('title', m.title, 'year', m.year, 'url', m.slug)
ORDER BY m.year DESC)
FROM
movies m
JOIN genres_movies gm ON gm.movies_id = m.id
WHERE
gm.genres_id = g.id) AS movies,
(
SELECT
json_agg(json_build_object('title', p.title, 'date', p.date, 'url', p.slug)
ORDER BY p.date DESC)
FROM
posts_genres pg
LEFT JOIN posts p ON pg.posts_id = p.id
WHERE
pg.genres_id = g.id) AS posts
FROM
genres g
ORDER BY
g.id ASC;
(
SELECT json_agg(json_build_object(
'name', a.name_string,
'url', a.slug,
'total_plays', a.total_plays
) ORDER BY a.total_plays DESC)
FROM artists a
WHERE a.genres = g.id
) AS artists,
(
SELECT json_agg(json_build_object(
'title', b.title,
'author', b.author,
'url', b.slug
))
FROM books b
JOIN genres_books gb ON gb.books_id = b.id
WHERE gb.genres_id = g.id
) AS books,
(
SELECT json_agg(json_build_object(
'title', m.title,
'year', m.year,
'url', m.slug
) ORDER BY m.year DESC)
FROM movies m
JOIN genres_movies gm ON gm.movies_id = m.id
WHERE gm.genres_id = g.id
) AS movies,
(
SELECT json_agg(json_build_object(
'title', p.title,
'date', p.date,
'url', p.slug
) ORDER BY p.date DESC)
FROM posts_genres pg
LEFT JOIN posts p ON pg.posts_id = p.id
WHERE pg.genres_id = g.id
) AS posts
FROM
genres g
ORDER BY
g.id ASC;

View file

@ -1,5 +1,4 @@
CREATE OR REPLACE VIEW optimized_listens AS
SELECT DISTINCT ON (l.id, l.listened_at, l.track_name, l.artist_name, l.album_name)
CREATE OR REPLACE VIEW optimized_listens AS SELECT DISTINCT ON (l.id, l.listened_at, l.track_name, l.artist_name, l.album_name)
l.id,
l.listened_at,
l.track_name,
@ -15,10 +14,15 @@ SELECT DISTINCT ON (l.id, l.listened_at, l.track_name, l.artist_name, l.album_na
CONCAT('/', df_album.filename_disk) AS album_art
FROM
listens l
LEFT JOIN artists a ON (l.artist_name = a.name_string)
LEFT JOIN albums al ON (l.album_key = al.key)
LEFT JOIN directus_files df_art ON (a.art = df_art.id)
LEFT JOIN directus_files df_album ON (al.art = df_album.id)
LEFT JOIN genres g ON (a.genres = g.id)
LEFT JOIN artists a ON (l.artist_name = a.name_string)
LEFT JOIN albums al ON (l.album_key = al.key)
LEFT JOIN directus_files df_art ON (a.art = df_art.id)
LEFT JOIN directus_files df_album ON (al.art = df_album.id)
LEFT JOIN genres g ON (a.genres = g.id)
ORDER BY
l.id, l.listened_at, l.track_name, l.artist_name, l.album_name;
l.id,
l.listened_at,
l.track_name,
l.artist_name,
l.album_name;

View file

@ -5,18 +5,16 @@ SELECT
COUNT(*) AS plays,
ol.album_art,
ol.artist_url,
json_build_object(
'title', ol.album_name,
'image', ol.album_art,
'url', ol.artist_url,
'alt', CONCAT(ol.album_name, ' by ', ol.artist_name),
'subtext', ol.artist_name
) AS grid
json_build_object('title', ol.album_name, 'image', ol.album_art, 'url', ol.artist_url, 'alt', CONCAT(ol.album_name, ' by ', ol.artist_name), 'subtext', ol.artist_name) AS grid
FROM
optimized_listens ol
WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '30 days'
GROUP BY
ol.album_name, ol.artist_name, ol.album_art, ol.artist_url
ol.album_name,
ol.artist_name,
ol.album_art,
ol.artist_url
ORDER BY
plays DESC;
plays DESC;

View file

@ -5,18 +5,15 @@ SELECT
ol.artist_art,
ol.artist_url,
ARRAY_AGG(DISTINCT ol.genre_name) AS genres,
json_build_object(
'title', ol.artist_name,
'image', ol.artist_art,
'url', ol.artist_url,
'alt', CONCAT(COUNT(*), ' plays of ', ol.artist_name),
'subtext', CONCAT(COUNT(*), ' plays')
) AS grid
json_build_object('title', ol.artist_name, 'image', ol.artist_art, 'url', ol.artist_url, 'alt', CONCAT(COUNT(*), ' plays of ', ol.artist_name), 'subtext', CONCAT(COUNT(*), ' plays')) AS grid
FROM
optimized_listens ol
WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '30 days'
GROUP BY
ol.artist_name, ol.artist_art, ol.artist_url
ol.artist_name,
ol.artist_art,
ol.artist_url
ORDER BY
plays DESC;
plays DESC;

View file

@ -3,15 +3,14 @@ SELECT
ol.genre_name,
ol.genre_url,
COUNT(*) AS plays,
json_build_object(
'alt', ol.genre_name,
'subtext', CONCAT(COUNT(*), ' plays')
) AS grid
json_build_object('alt', ol.genre_name, 'subtext', CONCAT(COUNT(*), ' plays')) AS grid
FROM
optimized_listens ol
WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '30 days'
GROUP BY
ol.genre_name, ol.genre_url
ol.genre_name,
ol.genre_url
ORDER BY
plays DESC;
plays DESC;

View file

@ -14,7 +14,11 @@ WITH track_stats AS (
WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '30 days'
GROUP BY
ol.track_name, ol.artist_name, ol.album_name, ol.album_art, ol.artist_url
ol.track_name,
ol.artist_name,
ol.album_name,
ol.album_art,
ol.artist_url
)
SELECT
track_name,
@ -24,14 +28,10 @@ SELECT
last_listened,
album_art,
artist_url,
json_build_object(
'title', track_name,
'artist', artist_name,
'url', artist_url,
'plays', plays,
'alt', CONCAT(track_name, ' by ', artist_name),
'subtext', CONCAT(album_name, ' (', plays, ' plays)'),
'percentage', ROUND((plays::decimal / most_played) * 100, 2)
) AS chart
FROM track_stats
ORDER BY plays DESC, last_listened DESC;
json_build_object('title', track_name, 'artist', artist_name, 'url', artist_url, 'plays', plays, 'alt', CONCAT(track_name, ' by ', artist_name), 'subtext', CONCAT(album_name, ' (', plays, ' plays)'), 'percentage', ROUND((plays::decimal / most_played) * 100, 2)) AS chart
FROM
track_stats
ORDER BY
plays DESC,
last_listened DESC;

View file

@ -9,8 +9,8 @@ WITH latest_listen AS (
ROW_NUMBER() OVER (ORDER BY l.listened_at DESC) AS row_num
FROM
listens l
JOIN artists a ON l.artist_name = a.name_string
LEFT JOIN genres g ON a.genres = g.id
JOIN artists a ON l.artist_name = a.name_string
LEFT JOIN genres g ON a.genres = g.id
)
SELECT
track_name,
@ -21,4 +21,5 @@ SELECT
FROM
latest_listen
WHERE
row_num = 1;
row_num = 1;

View file

@ -13,17 +13,11 @@ SELECT
ol.album_art,
ol.artist_url,
ol.genre_url,
json_build_object(
'title', ol.track_name,
'subtext', ol.artist_name,
'alt', CONCAT(ol.track_name, ' by ', ol.artist_name),
'url', ol.artist_url,
'image', ol.album_art,
'played_at', ol.listened_at
) AS chart
json_build_object('title', ol.track_name, 'subtext', ol.artist_name, 'alt', CONCAT(ol.track_name, ' by ', ol.artist_name), 'url', ol.artist_url, 'image', ol.album_art, 'played_at', ol.listened_at) AS chart
FROM
optimized_listens ol
WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '7 days'
ORDER BY
TO_TIMESTAMP(ol.listened_at) DESC;
TO_TIMESTAMP(ol.listened_at) DESC;

View file

@ -1,22 +0,0 @@
CREATE OR REPLACE VIEW three_month_albums AS
SELECT
ol.album_name,
ol.artist_name,
COUNT(*) AS plays,
ol.album_art,
ol.artist_url,
json_build_object(
'title', ol.album_name,
'image', ol.album_art,
'url', ol.artist_url,
'alt', CONCAT(ol.album_name, ' by ', ol.artist_name),
'subtext', ol.artist_name
) AS grid
FROM
optimized_listens ol
WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '3 months'
GROUP BY
ol.album_name, ol.artist_name, ol.album_art, ol.artist_url
ORDER BY
plays DESC;

View file

@ -1,22 +0,0 @@
CREATE OR REPLACE VIEW three_month_artists AS
SELECT
ol.artist_name,
COUNT(*) AS plays,
ol.artist_art,
ol.artist_url,
ARRAY_AGG(DISTINCT ol.genre_name) AS genres,
json_build_object(
'title', ol.artist_name,
'image', ol.artist_art,
'url', ol.artist_url,
'alt', CONCAT(COUNT(*), ' plays of ', ol.artist_name),
'subtext', CONCAT(COUNT(*), ' plays')
) AS grid
FROM
optimized_listens ol
WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '3 months'
GROUP BY
ol.artist_name, ol.artist_art, ol.artist_url
ORDER BY
plays DESC;

View file

@ -1,17 +0,0 @@
CREATE OR REPLACE VIEW three_month_genres AS
SELECT
ol.genre_name,
ol.genre_url,
COUNT(*) AS plays,
json_build_object(
'alt', ol.genre_name,
'subtext', CONCAT(COUNT(*), ' plays')
) AS grid
FROM
optimized_listens ol
WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '3 months'
GROUP BY
ol.genre_name, ol.genre_url
ORDER BY
plays DESC;

View file

@ -1,37 +0,0 @@
CREATE OR REPLACE VIEW three_month_tracks AS
WITH track_stats AS (
SELECT
ol.track_name,
ol.artist_name,
ol.album_name,
COUNT(*) AS plays,
MAX(ol.listened_at) AS last_listened,
ol.album_art,
ol.artist_url,
MAX(COUNT(*)) OVER () AS most_played
FROM
optimized_listens ol
WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '3 months'
GROUP BY
ol.track_name, ol.artist_name, ol.album_name, ol.album_art, ol.artist_url
)
SELECT
track_name,
artist_name,
album_name,
plays,
last_listened,
album_art,
artist_url,
json_build_object(
'title', track_name,
'artist', artist_name,
'url', artist_url,
'plays', plays,
'alt', CONCAT(track_name, ' by ', artist_name),
'subtext', CONCAT(album_name, ' (', plays, ' plays)'),
'percentage', ROUND((plays::decimal / most_played) * 100, 2)
) AS chart
FROM track_stats
ORDER BY plays DESC, last_listened DESC;

View file

@ -5,18 +5,16 @@ SELECT
COUNT(*) AS plays,
ol.album_art,
ol.artist_url,
json_build_object(
'title', ol.album_name,
'image', ol.album_art,
'url', ol.artist_url,
'alt', CONCAT(ol.album_name, ' by ', ol.artist_name),
'subtext', ol.artist_name
) AS grid
json_build_object('title', ol.album_name, 'image', ol.album_art, 'url', ol.artist_url, 'alt', CONCAT(ol.album_name, ' by ', ol.artist_name), 'subtext', ol.artist_name) AS grid
FROM
optimized_listens ol
WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '7 days'
GROUP BY
ol.album_name, ol.artist_name, ol.album_art, ol.artist_url
ol.album_name,
ol.artist_name,
ol.album_art,
ol.artist_url
ORDER BY
plays DESC;
plays DESC;

View file

@ -5,18 +5,15 @@ SELECT
ol.artist_art,
ol.artist_url,
ARRAY_AGG(DISTINCT ol.genre_name) AS genres,
json_build_object(
'title', ol.artist_name,
'image', ol.artist_art,
'url', ol.artist_url,
'alt', CONCAT(COUNT(*), ' plays of ', ol.artist_name),
'subtext', CONCAT(COUNT(*), ' plays')
) AS grid
json_build_object('title', ol.artist_name, 'image', ol.artist_art, 'url', ol.artist_url, 'alt', CONCAT(COUNT(*), ' plays of ', ol.artist_name), 'subtext', CONCAT(COUNT(*), ' plays')) AS grid
FROM
optimized_listens ol
WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '7 days'
GROUP BY
ol.artist_name, ol.artist_art, ol.artist_url
ol.artist_name,
ol.artist_art,
ol.artist_url
ORDER BY
plays DESC;
plays DESC;

View file

@ -3,15 +3,14 @@ SELECT
ol.genre_name,
ol.genre_url,
COUNT(*) AS plays,
json_build_object(
'alt', ol.genre_name,
'subtext', CONCAT(COUNT(*), ' plays')
) AS grid
json_build_object('alt', ol.genre_name, 'subtext', CONCAT(COUNT(*), ' plays')) AS grid
FROM
optimized_listens ol
WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '7 days'
GROUP BY
ol.genre_name, ol.genre_url
ol.genre_name,
ol.genre_url
ORDER BY
plays DESC;
plays DESC;

View file

@ -14,7 +14,11 @@ WITH track_stats AS (
WHERE
TO_TIMESTAMP(ol.listened_at) >= NOW() - INTERVAL '7 days'
GROUP BY
ol.track_name, ol.artist_name, ol.album_name, ol.album_art, ol.artist_url
ol.track_name,
ol.artist_name,
ol.album_name,
ol.album_art,
ol.artist_url
)
SELECT
track_name,
@ -24,14 +28,10 @@ SELECT
last_listened,
album_art,
artist_url,
json_build_object(
'title', track_name,
'artist', artist_name,
'url', artist_url,
'plays', plays,
'alt', CONCAT(track_name, ' by ', artist_name),
'subtext', CONCAT(album_name, ' (', plays, ' plays)'),
'percentage', ROUND((plays::decimal / most_played) * 100, 2)
) AS chart
FROM track_stats
ORDER BY plays DESC, last_listened DESC;
json_build_object('title', track_name, 'artist', artist_name, 'url', artist_url, 'plays', plays, 'alt', CONCAT(track_name, ' by ', artist_name), 'subtext', CONCAT(album_name, ' (', plays, ' plays)'), 'percentage', ROUND((plays::decimal / most_played) * 100, 2)) AS chart
FROM
track_stats
ORDER BY
plays DESC,
last_listened DESC;

View file

@ -11,121 +11,84 @@ SELECT
s.slug AS url,
CONCAT('/', df_art.filename_disk) AS image,
CONCAT('/', df_backdrop.filename_disk) AS backdrop,
json_build_object(
'title', s.title,
'image', CONCAT('/', df_art.filename_disk),
'backdrop', CONCAT('/', df_backdrop.filename_disk),
'url', s.slug,
'alt', CONCAT(s.title, ' (', s.year, ')'),
'subtext', COALESCE(
(
SELECT
CASE
WHEN e1.last_watched_at >= NOW() - INTERVAL '90 days' THEN
CONCAT('S', e1.season_number, 'E', e1.episode_number)
ELSE
CONCAT('(', s.year::TEXT, ')')
END
FROM episodes e1
WHERE e1.show = s.id
ORDER BY e1.last_watched_at DESC, e1.season_number DESC, e1.episode_number DESC
LIMIT 1
),
CONCAT('(', s.year::TEXT, ')')
)
) AS grid,
json_build_object(
'title', s.title,
'year', s.year,
'url', s.slug,
'image', CONCAT('/', df_art.filename_disk),
'backdrop', CONCAT('/', df_backdrop.filename_disk),
'formatted_episode', COALESCE(
(SELECT CONCAT('S', e2.season_number, 'E', e2.episode_number)
FROM episodes e2
WHERE e2.show = s.id
ORDER BY e2.season_number DESC, e2.episode_number DESC
LIMIT 1)
),
'last_watched_at', MAX(e.last_watched_at)
) AS episode,
(
SELECT json_agg(json_build_object(
'title', m.title,
'year', m.year,
'url', m.slug
) ORDER BY m.year DESC)
FROM shows_movies sm
json_build_object('title', s.title, 'image', CONCAT('/', df_art.filename_disk), 'backdrop', CONCAT('/', df_backdrop.filename_disk), 'url', s.slug, 'alt', CONCAT(s.title, ' (', s.year, ')'), 'subtext', COALESCE((
SELECT
CASE WHEN e1.last_watched_at >= NOW() - INTERVAL '90 days' THEN
CONCAT('S', e1.season_number, 'E', e1.episode_number)
ELSE
CONCAT('(', s.year::text, ')')
END FROM episodes e1
WHERE
e1.show = s.id ORDER BY e1.last_watched_at DESC, e1.season_number DESC, e1.episode_number DESC LIMIT 1), CONCAT('(', s.year::text, ')'))) AS grid,
json_build_object('title', s.title, 'year', s.year, 'url', s.slug, 'image', CONCAT('/', df_art.filename_disk), 'backdrop', CONCAT('/', df_backdrop.filename_disk), 'formatted_episode', COALESCE((
SELECT
CONCAT('S', e2.season_number, 'E', e2.episode_number)
FROM episodes e2
WHERE
e2.show = s.id ORDER BY e2.season_number DESC, e2.episode_number DESC LIMIT 1)), 'last_watched_at', MAX(e.last_watched_at)) AS episode,
(
SELECT
json_agg(json_build_object('title', m.title, 'year', m.year, 'url', m.slug)
ORDER BY m.year DESC)
FROM
shows_movies sm
LEFT JOIN movies m ON sm.movies_id = m.id
WHERE sm.shows_id = s.id
) AS movies,
(
SELECT json_agg(json_build_object(
'title', b.title,
'author', b.author,
'url', b.slug
) ORDER BY b.title)
FROM shows_books sb
WHERE
sm.shows_id = s.id) AS movies,
(
SELECT
json_agg(json_build_object('title', b.title, 'author', b.author, 'url', b.slug)
ORDER BY b.title)
FROM
shows_books sb
LEFT JOIN books b ON sb.books_id = b.id
WHERE sb.shows_id = s.id
) AS books,
(
SELECT json_agg(json_build_object(
'title', p.title,
'date', p.date,
'url', p.slug
) ORDER BY p.date DESC)
FROM posts_shows ps
WHERE
sb.shows_id = s.id) AS books,
(
SELECT
json_agg(json_build_object('title', p.title, 'date', p.date, 'url', p.slug)
ORDER BY p.date DESC)
FROM
posts_shows ps
LEFT JOIN posts p ON ps.posts_id = p.id
WHERE ps.shows_id = s.id
) AS posts,
(
SELECT array_agg(t.name)
FROM shows_tags st
WHERE
ps.shows_id = s.id) AS posts,
(
SELECT
array_agg(t.name)
FROM
shows_tags st
LEFT JOIN tags t ON st.tags_id = t.id
WHERE st.shows_id = s.id
) AS tags,
(
SELECT json_agg(json_build_object(
'title', rs.title,
'year', rs.year,
'url', rs.slug
) ORDER BY rs.year DESC)
FROM related_shows sr
WHERE
st.shows_id = s.id) AS tags,
(
SELECT
json_agg(json_build_object('title', rs.title, 'year', rs.year, 'url', rs.slug)
ORDER BY rs.year DESC)
FROM
related_shows sr
LEFT JOIN shows rs ON sr.related_shows_id = rs.id
WHERE sr.shows_id = s.id
) AS related_shows,
(
SELECT json_agg(json_build_object(
'name', a.name_string,
'url', a.slug,
'country', a.country,
'total_plays', a.total_plays
) ORDER BY a.name_string)
FROM shows_artists sa
WHERE
sr.shows_id = s.id) AS related_shows,
(
SELECT
json_agg(json_build_object('name', a.name_string, 'url', a.slug, 'country', a.country, 'total_plays', a.total_plays)
ORDER BY a.name_string)
FROM
shows_artists sa
LEFT JOIN artists a ON sa.artists_id = a.id
WHERE sa.shows_id = s.id
) AS artists,
WHERE
sa.shows_id = s.id) AS artists,
MAX(e.last_watched_at) AS last_watched_at
FROM
shows s
LEFT JOIN
episodes e ON s.id = e.show
LEFT JOIN
directus_files df_art ON s.art = df_art.id
LEFT JOIN
directus_files df_backdrop ON s.backdrop = df_backdrop.id
LEFT JOIN episodes e ON s.id = e.show
LEFT JOIN directus_files df_art ON s.art = df_art.id
LEFT JOIN directus_files df_backdrop ON s.backdrop = df_backdrop.id
GROUP BY
s.id, df_art.filename_disk, df_backdrop.filename_disk
s.id,
df_art.filename_disk,
df_backdrop.filename_disk
ORDER BY
MAX(e.last_watched_at) DESC;
MAX(e.last_watched_at) DESC;

View file

@ -34,7 +34,6 @@ const startEleventy = () =>
["eleventy", "--serve", "--port", ELEVENTY_PORT],
"Eleventy"
);
const startWorker = () =>
startProcess(
"npx",
@ -44,30 +43,30 @@ const startWorker = () =>
const app = express();
app.use((req, res, next) => {
if (req.path === "/js/script.js") {
res.setHeader("Content-Type", "application/javascript");
return res.send("");
const setContentType = (req, res) => {
const contentTypeMap = {
".css": "text/css",
".js": "application/javascript",
".json": "application/json",
"/api/": "application/json",
"/feeds/all": "application/xml",
"/feeds/books": "application/xml",
"/feeds/links": "application/xml",
"/feeds/movies": "application/xml",
"/feeds/posts": "application/xml",
"/feeds/syndication": "application/xml",
};
for (const [key, value] of Object.entries(contentTypeMap)) {
if (req.path.endsWith(key) || req.path.startsWith(key)) {
res.setHeader("Content-Type", value);
break;
}
}
if (req.path.endsWith(".css")) res.setHeader("Content-Type", "text/css");
else if (req.path.endsWith(".js"))
res.setHeader("Content-Type", "application/javascript");
else if (req.path.endsWith(".json"))
res.setHeader("Content-Type", "application/json");
else if (req.path.startsWith("/api/"))
res.setHeader("Content-Type", "application/json");
else if (req.path.startsWith("/feeds/all"))
res.setHeader("Content-Type", "application/xml");
else if (req.path.startsWith("/feeds/books"))
res.setHeader("Content-Type", "application/xml");
else if (req.path.startsWith("/feeds/links"))
res.setHeader("Content-Type", "application/xml");
else if (req.path.startsWith("/feeds/movies"))
res.setHeader("Content-Type", "application/xml");
else if (req.path.startsWith("/feeds/posts"))
res.setHeader("Content-Type", "application/xml");
else if (req.path.startsWith("/feeds/syndication"))
res.setHeader("Content-Type", "application/xml");
};
app.use((req, res, next) => {
setContentType(req, res);
next();
});

View file

@ -1,53 +1,45 @@
{%- assign fullUrl = globals.url | append: page.url -%}
{%- assign pageTitle = globals.site_name -%}
{%- if schema == 'blog' -%}
{%- assign pageTitle = post.title -%}
{%- elsif title -%}
{%- assign pageTitle = title | append: ' / ' | append: globals.site_name -%}
{%- elsif schema == 'music-index' -%}
{%- assign pageTitle = 'Music / ' | append: globals.site_name -%}
{%- elsif schema == 'music-period' -%}
{%- assign pageTitle = 'Music / ' | append: page.title | append: globals.site_name -%}
{%- elsif year.value -%}
{%- assign pageTitle = ' / Books ' | prepend: year.value | append: ' / ' | append: globals.site_name -%}
{%- elsif page.description -%}
{%- assign pageTitle = page.title | append: ' / ' | append: globals.site_name -%}
{%- endif -%}
{%- assign pageDescription = globals.site_description -%}
{%- if schema == 'blog' -%}
{%- assign pageDescription = post.description | markdown | strip_html -%}
{%- elsif page.description -%}
{%- case schema -%}
{%- when 'blog' -%}
{%- assign pageTitle = post.title -%}
{%- assign pageDescription = post.description | markdown | strip_html | default: globals.site_description -%}
{%- when 'music-index' -%}
{%- assign pageTitle = 'Music / ' | append: globals.site_name -%}
{%- when 'music-period' -%}
{%- assign pageTitle = 'Music / ' | append: page.title | append: ' / ' | append: globals.site_name -%}
{%- when 'books-year' -%}
{%- assign pageTitle = year.value | append: ' / Books / ' | append: globals.site_name -%}
{%- else -%}
{%- assign pageTitle = title | append: ' / ' | append: globals.site_name -%}
{%- endcase -%}
{%- if page.description -%}
{%- assign pageDescription = page.description -%}
{%- elsif description -%}
{%- assign pageDescription = description -%}
{%- endif -%}
{%- assign ogImage = globals.cdn_url | append: globals.avatar -%}
{%- case schema -%}
{%- when 'music' -%}
{%- assign ogImage = globals.cdn_url | append: page.image -%}
{%- when 'music-index' -%}
{%- assign ogImage = globals.cdn_url | append: music.week.artists[0].grid.image -%}
{%- when 'watching' -%}
{%- when 'watching' or 'favorite-movies' -%}
{%- assign featuredMovie = movies.recentlyWatched | first -%}
{%- assign ogImage = globals.cdn_url | append: featuredMovie.grid.backdrop -%}
{%- when 'watching-shows' -%}
{%- when 'watching-shows' or 'favorite-shows' -%}
{%- assign featuredShow = tv.recentlyWatched | first -%}
{%- assign ogImage = globals.cdn_url | append: featuredShow.grid.backdrop -%}
{%- when 'favorite-movies' -%}
{%- assign featuredMovie = movies.favorites| shuffleArray | first -%}
{%- assign ogImage = globals.cdn_url | append: featuredMovie.grid.backdrop -%}
{%- when 'favorite-shows' -%}
{%- assign featuredShow = tv.favorites | shuffleArray | first -%}
{%- assign ogImage = globals.cdn_url | append: featuredShow.grid.backdrop -%}
{%- when 'books' -%}
{%- when 'books' or 'books-year' -%}
{%- assign featuredBook = books.all | filterBooksByStatus: 'started' | reverse | first -%}
{%- assign ogImage = globals.cdn_url | append: featuredBook.grid.image -%}
{%- when 'books-year' -%}
{%- assign featuredBook = books.currentYear | first -%}
{%- assign ogImage = globals.cdn_url | append: featuredBook.grid.image -%}
{%- endcase -%}
{%- assign escapedPageDescription = pageDescription | escape -%}
<!doctype html>

View file

@ -1,4 +0,0 @@
---
layout: default
permalink: "/dynamic.html"
---

View file

@ -64,7 +64,7 @@ export default {
break;
}
const templateResponse = await fetch(`${BASE_URL}/dynamic.html`);
const templateResponse = await fetch(`${BASE_URL}`);
const template = await templateResponse.text();
const metadata = generateMetadata(data, type, globals);
const html = updateDynamicContent(template, metadata, mediaHtml);

View file

@ -1,11 +1,11 @@
import truncateHtml from "truncate-html";
import { convert } from "html-to-text";
import { parseCountryField } from "./countries.js";
import { formatDate, md } from "./formatters.js";
import { ICON_MAP } from "./icons.js";
const warningBanner = `<div class="banner warning"><p>${ICON_MAP["alertTriangle"]}There are probably spoilers after this banner — this is a warning about them.</p></div>`;
const generateAssociatedMediaHTML = (data, isGenre = false) => {
const sections = [
{
@ -50,6 +50,12 @@ const generateAssociatedMediaHTML = (data, isGenre = false) => {
category: "article",
title: "Related Post(s)",
},
{
key: "books",
icon: "books",
category: "books",
title: "Related Book(s)",
},
{
key: "related_books",
icon: "books",
@ -75,10 +81,11 @@ const generateAssociatedMediaHTML = (data, isGenre = false) => {
const year = item.year ? ` (${item.year})` : "";
const author = item.author ? ` by ${item.author}` : "";
const totalPlays = item.total_plays
? ` <strong class="highlight-text">${
item.total_plays
}</strong> ${item.total_plays === 1 ? "play" : "plays"}`
? ` <strong class="highlight-text">${item.total_plays} ${
item.total_plays === 1 ? "play" : "plays"
}</strong>`
: "";
let listItemContent = name;
if (key === "artists" || key === "related_artists") {
@ -89,7 +96,7 @@ const generateAssociatedMediaHTML = (data, isGenre = false) => {
key === "related_shows"
) {
listItemContent = `${name}${year}`;
} else if (key === "related_books") {
} else if (key === "books" || key === "related_books") {
listItemContent = `${name}${author}`;
}
@ -101,6 +108,7 @@ const generateAssociatedMediaHTML = (data, isGenre = false) => {
})
.join("");
};
const generateMediaLinks = (data, type, count = 10) => {
if (!data || !type) return "";

View file

@ -16,10 +16,4 @@ routes = [
{ pattern = "https://coryd.dev/music/artists/*", zone_id = "${CF_ZONE_ID}" },
{ pattern = "https://coryd.dev/music/genres/*", zone_id = "${CF_ZONE_ID}" },
{ pattern = "https://coryd.dev/books/*", zone_id = "${CF_ZONE_ID}" },
]
[[env.production.excludes]]
routes = [
{ pattern = "https://coryd.dev/books", zone_id = "${CF_ZONE_ID}" },
{ pattern = "https://coryd.dev/books/", zone_id = "${CF_ZONE_ID}" },
]