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, CASE WHEN b.rating IS NOT NULL THEN ' (' || b.rating || ')' ELSE '' END, ' ',( 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, CASE WHEN m.rating IS NOT NULL THEN ' (' || m.rating || ')' ELSE '' END, ' ',( 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 FROM ( SELECT * FROM syndication_data WHERE syndication IS NOT NULL ORDER BY content_date DESC LIMIT 20) AS limited_data;