a tool for shared writing and social publishing

sort rss feeds properly

+46 -36
+46 -36
app/lish/[did]/[publication]/generateFeed.ts
··· 54 54 }); 55 55 56 56 await Promise.all( 57 - publication.documents_in_publications.map(async (doc) => { 58 - if (!doc.documents) return; 59 - const record = normalizeDocumentRecord( 60 - doc.documents?.data, 61 - doc.documents?.uri, 62 - ); 63 - const uri = new AtUri(doc.documents?.uri); 64 - const rkey = uri.rkey; 65 - if (!record) return; 57 + publication.documents_in_publications 58 + .sort((a, b) => { 59 + const dateA = a.documents?.sort_date 60 + ? new Date(a.documents.sort_date).getTime() 61 + : 0; 62 + const dateB = b.documents?.sort_date 63 + ? new Date(b.documents.sort_date).getTime() 64 + : 0; 65 + return dateB - dateA; // Sort in descending order (newest first) 66 + }) 67 + .map(async (doc) => { 68 + if (!doc.documents) return; 69 + const record = normalizeDocumentRecord( 70 + doc.documents?.data, 71 + doc.documents?.uri, 72 + ); 73 + const uri = new AtUri(doc.documents?.uri); 74 + const rkey = uri.rkey; 75 + if (!record) return; 66 76 67 - let blocks: PubLeafletPagesLinearDocument.Block[] = []; 68 - if (hasLeafletContent(record) && record.content.pages[0]) { 69 - const firstPage = record.content.pages[0]; 70 - if (PubLeafletPagesLinearDocument.isMain(firstPage)) { 71 - blocks = firstPage.blocks || []; 77 + let blocks: PubLeafletPagesLinearDocument.Block[] = []; 78 + if (hasLeafletContent(record) && record.content.pages[0]) { 79 + const firstPage = record.content.pages[0]; 80 + if (PubLeafletPagesLinearDocument.isMain(firstPage)) { 81 + blocks = firstPage.blocks || []; 82 + } 72 83 } 73 - } 74 - const stream = await renderToReadableStream( 75 - createElement(StaticPostContent, { blocks, did: uri.host }), 76 - ); 77 - const reader = stream.getReader(); 78 - const chunks = []; 84 + const stream = await renderToReadableStream( 85 + createElement(StaticPostContent, { blocks, did: uri.host }), 86 + ); 87 + const reader = stream.getReader(); 88 + const chunks = []; 79 89 80 - let done, value; 81 - while (!done) { 82 - ({ done, value } = await reader.read()); 83 - if (value) { 84 - chunks.push(new TextDecoder().decode(value)); 90 + let done, value; 91 + while (!done) { 92 + ({ done, value } = await reader.read()); 93 + if (value) { 94 + chunks.push(new TextDecoder().decode(value)); 95 + } 85 96 } 86 - } 87 97 88 - const docUrl = getDocumentURL(record, doc.documents.uri, pubRecord); 89 - feed.addItem({ 90 - title: record.title, 91 - description: record.description, 92 - date: record.publishedAt ? new Date(record.publishedAt) : new Date(), 93 - id: docUrl, 94 - link: docUrl, 95 - content: chunks.join(""), 96 - }); 97 - }), 98 + const docUrl = getDocumentURL(record, doc.documents.uri, pubRecord); 99 + feed.addItem({ 100 + title: record.title, 101 + description: record.description, 102 + date: record.publishedAt ? new Date(record.publishedAt) : new Date(), 103 + id: docUrl, 104 + link: docUrl, 105 + content: chunks.join(""), 106 + }); 107 + }), 98 108 ); 99 109 100 110 return feed;