From d2c4c78ff7571dc1bd567fde9e018f644ebde2c4 Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt Date: Mon, 21 Oct 2024 12:04:17 -0700 Subject: [PATCH] chore: fix related books + deduplication - fixed total plays formatting - formatted queries - improved proxy server --- package-lock.json | 4 +- package.json | 2 +- queries/functions/search.psql | 54 ++-- queries/views/content/links.psql | 34 +-- queries/views/content/posts.psql | 246 ++++++++---------- queries/views/feeds/all-content.psql | 154 +++++------ queries/views/feeds/search.psql | 187 ++++++------- queries/views/feeds/sitemap.psql | 115 ++++---- queries/views/feeds/syndication.psql | 176 ++++++------- queries/views/globals/index.psql | 2 +- queries/views/globals/nav.psql | 4 +- queries/views/globals/pages.psql | 95 +++---- queries/views/media/books.psql | 203 +++++++-------- queries/views/media/movies.psql | 185 ++++++------- queries/views/media/music/album-releases.psql | 27 +- queries/views/media/music/artists.psql | 164 +++++------- queries/views/media/music/concerts.psql | 31 +-- queries/views/media/music/genres.psql | 86 +++--- queries/views/media/music/listens.psql | 20 +- queries/views/media/music/month/albums.psql | 16 +- queries/views/media/music/month/artists.psql | 15 +- queries/views/media/music/month/genres.psql | 11 +- queries/views/media/music/month/tracks.psql | 24 +- queries/views/media/music/now-playing.psql | 7 +- queries/views/media/music/recent-tracks.psql | 12 +- .../views/media/music/three-month/albums.psql | 22 -- .../media/music/three-month/artists.psql | 22 -- .../views/media/music/three-month/genres.psql | 17 -- .../views/media/music/three-month/tracks.psql | 37 --- queries/views/media/music/week/albums.psql | 16 +- queries/views/media/music/week/artists.psql | 15 +- queries/views/media/music/week/genres.psql | 11 +- queries/views/media/music/week/tracks.psql | 24 +- queries/views/media/shows.psql | 177 +++++-------- server.js | 47 ++-- src/includes/base.liquid | 50 ++-- src/pages/dynamic/dynamic.html | 4 - workers/dynamic-pages/index.js | 2 +- workers/dynamic-pages/utils/generators.js | 18 +- workers/dynamic-pages/wrangler.template.toml | 6 - 40 files changed, 985 insertions(+), 1357 deletions(-) delete mode 100644 queries/views/media/music/three-month/albums.psql delete mode 100644 queries/views/media/music/three-month/artists.psql delete mode 100644 queries/views/media/music/three-month/genres.psql delete mode 100644 queries/views/media/music/three-month/tracks.psql delete mode 100644 src/pages/dynamic/dynamic.html diff --git a/package-lock.json b/package-lock.json index a3aa69a9..f21116c5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 9cc912ee..c3db2300 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/queries/functions/search.psql b/queries/functions/search.psql index 1aa3275c..e08e4b62 100644 --- a/queries/functions/search.psql +++ b/queries/functions/search.psql @@ -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; \ No newline at end of file +$$ +LANGUAGE plpgsql; + diff --git a/queries/views/content/links.psql b/queries/views/content/links.psql index c50d22e6..28af34e0 100644 --- a/queries/views/content/links.psql +++ b/queries/views/content/links.psql @@ -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; \ No newline at end of file + l.date DESC; + diff --git a/queries/views/content/posts.psql b/queries/views/content/posts.psql index e53b49d2..f63964e0 100644 --- a/queries/views/content/posts.psql +++ b/queries/views/content/posts.psql @@ -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; \ No newline at end of file + p.id, + df.filename_disk; + diff --git a/queries/views/feeds/all-content.psql b/queries/views/feeds/all-content.psql index 31859211..a6f85ff9 100644 --- a/queries/views/feeds/all-content.psql +++ b/queries/views/feeds/all-content.psql @@ -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; \ No newline at end of file + SELECT + * + FROM + feed_data + WHERE + feed IS NOT NULL + ORDER BY + content_date DESC + LIMIT 20) AS feed_data; + diff --git a/queries/views/feeds/search.psql b/queries/views/feeds/search.psql index 18e42164..12cf8afa 100644 --- a/queries/views/feeds/search.psql +++ b/queries/views/feeds/search.psql @@ -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; \ No newline at end of file + ROW_NUMBER() OVER (ORDER BY url) AS id, + * +FROM + search_data; + diff --git a/queries/views/feeds/sitemap.psql b/queries/views/feeds/sitemap.psql index 84628298..5654db07 100644 --- a/queries/views/feeds/sitemap.psql +++ b/queries/views/feeds/sitemap.psql @@ -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; \ No newline at end of file +FROM + sitemap_data; + diff --git a/queries/views/feeds/syndication.psql b/queries/views/feeds/syndication.psql index 33321bbc..45baeed1 100644 --- a/queries/views/feeds/syndication.psql +++ b/queries/views/feeds/syndication.psql @@ -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; + diff --git a/queries/views/globals/index.psql b/queries/views/globals/index.psql index 4410b9e1..a51018a3 100644 --- a/queries/views/globals/index.psql +++ b/queries/views/globals/index.psql @@ -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 \ No newline at end of file + LEFT JOIN directus_files df2 ON g.avatar_transparent = df2.id diff --git a/queries/views/globals/nav.psql b/queries/views/globals/nav.psql index 89b48dc8..946344df 100644 --- a/queries/views/globals/nav.psql +++ b/queries/views/globals/nav.psql @@ -10,5 +10,5 @@ SELECT p.permalink AS page_permalink FROM navigation n -LEFT JOIN - pages p ON n.pages = p.id; \ No newline at end of file + LEFT JOIN pages p ON n.pages = p.id; + diff --git a/queries/views/globals/pages.psql b/queries/views/globals/pages.psql index c3c3a55c..1b9ccca6 100644 --- a/queries/views/globals/pages.psql +++ b/queries/views/globals/pages.psql @@ -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; \ No newline at end of file + p.id, + df.filename_disk; + diff --git a/queries/views/media/books.psql b/queries/views/media/books.psql index 1c825dad..456361fc 100644 --- a/queries/views/media/books.psql +++ b/queries/views/media/books.psql @@ -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; \ No newline at end of file + b.id, + df.filename_disk; + diff --git a/queries/views/media/movies.psql b/queries/views/media/movies.psql index a7d850e3..b7ce4df5 100644 --- a/queries/views/media/movies.psql +++ b/queries/views/media/movies.psql @@ -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; \ No newline at end of file + m.last_watched DESC, + m.favorite DESC, + m.title ASC; + diff --git a/queries/views/media/music/album-releases.psql b/queries/views/media/music/album-releases.psql index fe40b162..d8e92991 100644 --- a/queries/views/media/music/album-releases.psql +++ b/queries/views/media/music/album-releases.psql @@ -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; \ No newline at end of file + a.release_date IS NOT NULL; + diff --git a/queries/views/media/music/artists.psql b/queries/views/media/music/artists.psql index 1bd6d7fb..8c8c2418 100644 --- a/queries/views/media/music/artists.psql +++ b/queries/views/media/music/artists.psql @@ -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; \ No newline at end of file + ar.id, + df.filename_disk, + g.name, + g.slug, + g.emoji; + diff --git a/queries/views/media/music/concerts.psql b/queries/views/media/music/concerts.psql index d4483e6d..d1e73016 100644 --- a/queries/views/media/music/concerts.psql +++ b/queries/views/media/music/concerts.psql @@ -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; \ No newline at end of file + c.date DESC; + diff --git a/queries/views/media/music/genres.psql b/queries/views/media/music/genres.psql index b1b6719b..fff13125 100644 --- a/queries/views/media/music/genres.psql +++ b/queries/views/media/music/genres.psql @@ -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; \ No newline at end of file diff --git a/queries/views/media/music/listens.psql b/queries/views/media/music/listens.psql index 753944b6..fe981a6a 100644 --- a/queries/views/media/music/listens.psql +++ b/queries/views/media/music/listens.psql @@ -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; \ No newline at end of file + l.id, + l.listened_at, + l.track_name, + l.artist_name, + l.album_name; + diff --git a/queries/views/media/music/month/albums.psql b/queries/views/media/music/month/albums.psql index 4106ea89..c818b9e2 100644 --- a/queries/views/media/music/month/albums.psql +++ b/queries/views/media/music/month/albums.psql @@ -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; \ No newline at end of file + plays DESC; + diff --git a/queries/views/media/music/month/artists.psql b/queries/views/media/music/month/artists.psql index 95804957..27bd349d 100644 --- a/queries/views/media/music/month/artists.psql +++ b/queries/views/media/music/month/artists.psql @@ -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; \ No newline at end of file + plays DESC; + diff --git a/queries/views/media/music/month/genres.psql b/queries/views/media/music/month/genres.psql index 7cee2489..ee303ad7 100644 --- a/queries/views/media/music/month/genres.psql +++ b/queries/views/media/music/month/genres.psql @@ -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; \ No newline at end of file + plays DESC; + diff --git a/queries/views/media/music/month/tracks.psql b/queries/views/media/music/month/tracks.psql index caed29aa..d40455dc 100644 --- a/queries/views/media/music/month/tracks.psql +++ b/queries/views/media/music/month/tracks.psql @@ -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; \ No newline at end of file + 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; + diff --git a/queries/views/media/music/now-playing.psql b/queries/views/media/music/now-playing.psql index eede35b9..ad662f9d 100644 --- a/queries/views/media/music/now-playing.psql +++ b/queries/views/media/music/now-playing.psql @@ -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; \ No newline at end of file + row_num = 1; + diff --git a/queries/views/media/music/recent-tracks.psql b/queries/views/media/music/recent-tracks.psql index b0f5db74..4121b952 100644 --- a/queries/views/media/music/recent-tracks.psql +++ b/queries/views/media/music/recent-tracks.psql @@ -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; \ No newline at end of file + TO_TIMESTAMP(ol.listened_at) DESC; + diff --git a/queries/views/media/music/three-month/albums.psql b/queries/views/media/music/three-month/albums.psql deleted file mode 100644 index fe813889..00000000 --- a/queries/views/media/music/three-month/albums.psql +++ /dev/null @@ -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; \ No newline at end of file diff --git a/queries/views/media/music/three-month/artists.psql b/queries/views/media/music/three-month/artists.psql deleted file mode 100644 index 485fd4c5..00000000 --- a/queries/views/media/music/three-month/artists.psql +++ /dev/null @@ -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; \ No newline at end of file diff --git a/queries/views/media/music/three-month/genres.psql b/queries/views/media/music/three-month/genres.psql deleted file mode 100644 index 25d7f894..00000000 --- a/queries/views/media/music/three-month/genres.psql +++ /dev/null @@ -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; \ No newline at end of file diff --git a/queries/views/media/music/three-month/tracks.psql b/queries/views/media/music/three-month/tracks.psql deleted file mode 100644 index 0edc8a87..00000000 --- a/queries/views/media/music/three-month/tracks.psql +++ /dev/null @@ -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; \ No newline at end of file diff --git a/queries/views/media/music/week/albums.psql b/queries/views/media/music/week/albums.psql index ba1c50e5..2ecb8440 100644 --- a/queries/views/media/music/week/albums.psql +++ b/queries/views/media/music/week/albums.psql @@ -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; \ No newline at end of file + plays DESC; + diff --git a/queries/views/media/music/week/artists.psql b/queries/views/media/music/week/artists.psql index add8a30e..df8f65f3 100644 --- a/queries/views/media/music/week/artists.psql +++ b/queries/views/media/music/week/artists.psql @@ -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; \ No newline at end of file + plays DESC; + diff --git a/queries/views/media/music/week/genres.psql b/queries/views/media/music/week/genres.psql index fffe157e..e0b1502b 100644 --- a/queries/views/media/music/week/genres.psql +++ b/queries/views/media/music/week/genres.psql @@ -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; \ No newline at end of file + plays DESC; + diff --git a/queries/views/media/music/week/tracks.psql b/queries/views/media/music/week/tracks.psql index f108af6f..9d643d8c 100644 --- a/queries/views/media/music/week/tracks.psql +++ b/queries/views/media/music/week/tracks.psql @@ -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; \ No newline at end of file + 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; + diff --git a/queries/views/media/shows.psql b/queries/views/media/shows.psql index 8bc08cc5..401763e1 100644 --- a/queries/views/media/shows.psql +++ b/queries/views/media/shows.psql @@ -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; \ No newline at end of file + MAX(e.last_watched_at) DESC; + diff --git a/server.js b/server.js index 158de43f..b64dfaad 100644 --- a/server.js +++ b/server.js @@ -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(); }); diff --git a/src/includes/base.liquid b/src/includes/base.liquid index 0ad225be..5c3160a8 100644 --- a/src/includes/base.liquid +++ b/src/includes/base.liquid @@ -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 -%} diff --git a/src/pages/dynamic/dynamic.html b/src/pages/dynamic/dynamic.html deleted file mode 100644 index 690353c6..00000000 --- a/src/pages/dynamic/dynamic.html +++ /dev/null @@ -1,4 +0,0 @@ ---- -layout: default -permalink: "/dynamic.html" ---- \ No newline at end of file diff --git a/workers/dynamic-pages/index.js b/workers/dynamic-pages/index.js index 5a50d387..e8dbcede 100644 --- a/workers/dynamic-pages/index.js +++ b/workers/dynamic-pages/index.js @@ -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); diff --git a/workers/dynamic-pages/utils/generators.js b/workers/dynamic-pages/utils/generators.js index 045e3c92..f394c33a 100644 --- a/workers/dynamic-pages/utils/generators.js +++ b/workers/dynamic-pages/utils/generators.js @@ -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 = ``; + 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 - ? ` ${ - item.total_plays - } ${item.total_plays === 1 ? "play" : "plays"}` + ? ` ${item.total_plays} ${ + item.total_plays === 1 ? "play" : "plays" + }` : ""; + 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 ""; diff --git a/workers/dynamic-pages/wrangler.template.toml b/workers/dynamic-pages/wrangler.template.toml index 5eb10d01..602e1f05 100644 --- a/workers/dynamic-pages/wrangler.template.toml +++ b/workers/dynamic-pages/wrangler.template.toml @@ -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}" }, ] \ No newline at end of file