add comments feed
This commit is contained in:
		
							parent
							
								
									6ba968b1f9
								
							
						
					
					
						commit
						88d98edbce
					
				
					 3 changed files with 86 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -6,6 +6,7 @@ import helmet from 'helmet';
 | 
			
		|||
import RateLimit from 'express-rate-limit';
 | 
			
		||||
import morgan from 'morgan';
 | 
			
		||||
import cors from 'cors';
 | 
			
		||||
import { Feed } from 'feed';
 | 
			
		||||
 | 
			
		||||
// Constants
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -187,6 +188,59 @@ ORDER BY c.id
 | 
			
		|||
    res.json(comments);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
// Feed
 | 
			
		||||
 | 
			
		||||
const domain = process.env.DOMAIN || 'comments.alloca.space';
 | 
			
		||||
 | 
			
		||||
app.get('/atom/:site', (req, res) => {
 | 
			
		||||
    const site = req.params.site;
 | 
			
		||||
 | 
			
		||||
    /* lets create an rss feed */
 | 
			
		||||
    var feed = new Feed({
 | 
			
		||||
        title: 'UCS',
 | 
			
		||||
        description: 'תגובות עבור האתר ' + site,
 | 
			
		||||
        id: domain + '/feed/' + site,
 | 
			
		||||
        link: domain,
 | 
			
		||||
        language: 'he'
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    const stmt = db.prepare(`
 | 
			
		||||
SELECT
 | 
			
		||||
  c.id,
 | 
			
		||||
  s.url as site,
 | 
			
		||||
  c.path,
 | 
			
		||||
  c.user,
 | 
			
		||||
  c.user_website,
 | 
			
		||||
  c.message,
 | 
			
		||||
  c.published
 | 
			
		||||
FROM
 | 
			
		||||
  (SELECT id, url from site where url = @site) s
 | 
			
		||||
JOIN comment c
 | 
			
		||||
ON c.site = s.id
 | 
			
		||||
ORDER BY c.published DESC
 | 
			
		||||
;`);
 | 
			
		||||
    const comments = stmt.all({ site });
 | 
			
		||||
 | 
			
		||||
   for (const comment of comments) {
 | 
			
		||||
       feed.addItem({
 | 
			
		||||
           title:  `New message by '${comment.user}' on ${comment.path}`,
 | 
			
		||||
           description: comment.message,
 | 
			
		||||
           id: `${comment.site}/${comment.path}#comment-${comment.id}`,
 | 
			
		||||
           link: `${comment.site}/${comment.path}#comment-${comment.id}`,
 | 
			
		||||
           date: new Date(comment.published)
 | 
			
		||||
       });
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
    var xml = feed.atom1();
 | 
			
		||||
 | 
			
		||||
    res.set('Content-Type', 'text/xml');
 | 
			
		||||
    res.send(xml);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// Listen
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
app.listen(port, () => {
 | 
			
		||||
    console.log(`Listening on port ${port}`);
 | 
			
		||||
});
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										31
									
								
								backend/package-lock.json
									
										
									
										generated
									
									
									
								
							
							
						
						
									
										31
									
								
								backend/package-lock.json
									
										
									
										generated
									
									
									
								
							| 
						 | 
				
			
			@ -15,6 +15,7 @@
 | 
			
		|||
        "cors": "^2.8.5",
 | 
			
		||||
        "express": "^4.21.2",
 | 
			
		||||
        "express-rate-limit": "^7.5.0",
 | 
			
		||||
        "feed": "^4.2.2",
 | 
			
		||||
        "helmet": "^8.0.0",
 | 
			
		||||
        "morgan": "^1.10.0"
 | 
			
		||||
      },
 | 
			
		||||
| 
						 | 
				
			
			@ -646,6 +647,18 @@
 | 
			
		|||
      "integrity": "sha512-C55Cr/dQWQHdwuMTF+ySNMYqydVclmKdgFHoC/8gTu5Zoe1Nrx6jgArwfZ+7jKU78VjDfhrkGAJ38ucf0lozeQ==",
 | 
			
		||||
      "license": "MIT"
 | 
			
		||||
    },
 | 
			
		||||
    "node_modules/feed": {
 | 
			
		||||
      "version": "4.2.2",
 | 
			
		||||
      "resolved": "https://registry.npmjs.org/feed/-/feed-4.2.2.tgz",
 | 
			
		||||
      "integrity": "sha512-u5/sxGfiMfZNtJ3OvQpXcvotFpYkL0n9u9mM2vkui2nGo8b4wvDkJ8gAkYqbA8QpGyFCv3RK0Z+Iv+9veCS9bQ==",
 | 
			
		||||
      "license": "MIT",
 | 
			
		||||
      "dependencies": {
 | 
			
		||||
        "xml-js": "^1.6.11"
 | 
			
		||||
      },
 | 
			
		||||
      "engines": {
 | 
			
		||||
        "node": ">=0.4.0"
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
    "node_modules/file-uri-to-path": {
 | 
			
		||||
      "version": "1.0.0",
 | 
			
		||||
      "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz",
 | 
			
		||||
| 
						 | 
				
			
			@ -1452,6 +1465,12 @@
 | 
			
		|||
      "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
 | 
			
		||||
      "license": "MIT"
 | 
			
		||||
    },
 | 
			
		||||
    "node_modules/sax": {
 | 
			
		||||
      "version": "1.4.1",
 | 
			
		||||
      "resolved": "https://registry.npmjs.org/sax/-/sax-1.4.1.tgz",
 | 
			
		||||
      "integrity": "sha512-+aWOz7yVScEGoKNd4PA10LZ8sk0A/z5+nXQG5giUO5rprX9jgYsTdov9qCchZiPIZezbZH+jRut8nPodFAX4Jg==",
 | 
			
		||||
      "license": "ISC"
 | 
			
		||||
    },
 | 
			
		||||
    "node_modules/semver": {
 | 
			
		||||
      "version": "7.7.1",
 | 
			
		||||
      "resolved": "https://registry.npmjs.org/semver/-/semver-7.7.1.tgz",
 | 
			
		||||
| 
						 | 
				
			
			@ -1824,6 +1843,18 @@
 | 
			
		|||
      "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
 | 
			
		||||
      "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
 | 
			
		||||
      "license": "ISC"
 | 
			
		||||
    },
 | 
			
		||||
    "node_modules/xml-js": {
 | 
			
		||||
      "version": "1.6.11",
 | 
			
		||||
      "resolved": "https://registry.npmjs.org/xml-js/-/xml-js-1.6.11.tgz",
 | 
			
		||||
      "integrity": "sha512-7rVi2KMfwfWFl+GpPg6m80IVMWXLRjO+PxTq7V2CDhoGak0wzYzFgUY2m4XJ47OGdXd8eLE8EmwfAmdjw7lC1g==",
 | 
			
		||||
      "license": "MIT",
 | 
			
		||||
      "dependencies": {
 | 
			
		||||
        "sax": "^1.2.4"
 | 
			
		||||
      },
 | 
			
		||||
      "bin": {
 | 
			
		||||
        "xml-js": "bin/cli.js"
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -21,6 +21,7 @@
 | 
			
		|||
    "cors": "^2.8.5",
 | 
			
		||||
    "express": "^4.21.2",
 | 
			
		||||
    "express-rate-limit": "^7.5.0",
 | 
			
		||||
    "feed": "^4.2.2",
 | 
			
		||||
    "helmet": "^8.0.0",
 | 
			
		||||
    "morgan": "^1.10.0"
 | 
			
		||||
  },
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue