Refactor Games structure on server: no longer use an extra 'Players' table
[vchess.git] / server / routes / news.js
1 let router = require("express").Router();
2 const access = require("../utils/access");
3 const NewsModel = require("../models/News");
4 const sanitizeHtml = require('sanitize-html');
5 const devs = [1]; //hard-coded list of developers IDs, allowed to post news
6
7 router.post("/news", access.logged, access.ajax, (req,res) => {
8 if (devs.includes(req.userId)) {
9 const content = sanitizeHtml(req.body.news.content);
10 NewsModel.create(content, req.userId, (err, ret) => {
11 res.json(err || ret);
12 });
13 }
14 });
15
16 router.get("/news", access.ajax, (req,res) => {
17 const cursor = req.query["cursor"];
18 if (!!cursor.match(/^[0-9]+$/)) {
19 NewsModel.getNext(cursor, (err, newsList) => {
20 res.json(err || { newsList: newsList });
21 });
22 }
23 });
24
25 router.get("/newsts", access.ajax, (req,res) => {
26 // Special query for footer: just return timestamp of last news
27 NewsModel.getTimestamp((err,ts) => {
28 res.json(err || { timestamp: ts.added });
29 });
30 });
31
32 router.put("/news", access.logged, access.ajax, (req,res) => {
33 let news = req.body.news;
34 if (devs.includes(req.userId) && news.id.toString().match(/^[0-9]+$/)) {
35 news.content = sanitizeHtml(news.content);
36 NewsModel.update(news);
37 res.json({});
38 }
39 });
40
41 router.delete("/news", access.logged, access.ajax, (req,res) => {
42 const nid = req.query.id;
43 if (devs.includes(req.userId) && nid.toString().match(/^[0-9]+$/)) {
44 NewsModel.remove(nid);
45 res.json({});
46 }
47 });
48
49 module.exports = router;