Refactor models (merge Players in Games), add cursor to correspondance games. Finishe...
[vchess.git] / server / routes / news.js
CommitLineData
604b951e
BA
1let router = require("express").Router();
2const access = require("../utils/access");
3const NewsModel = require("../models/News");
4const sanitizeHtml = require('sanitize-html');
866842c3 5const devs = [1]; //hard-coded list of developers IDs, allowed to post news
604b951e 6
866842c3 7router.post("/news", access.logged, access.ajax, (req,res) => {
0234201f 8 if (devs.includes(req.userId)) {
866842c3 9 const content = sanitizeHtml(req.body.news.content);
0234201f
BA
10 NewsModel.create(content, req.userId, (err, ret) => {
11 res.json(err || ret);
866842c3
BA
12 });
13 }
604b951e
BA
14});
15
866842c3
BA
16router.get("/news", access.ajax, (req,res) => {
17 const cursor = req.query["cursor"];
0234201f
BA
18 if (!!cursor.match(/^[0-9]+$/)) {
19 NewsModel.getNext(cursor, (err, newsList) => {
d9a7a1e4 20 res.json(err || { newsList: newsList });
866842c3
BA
21 });
22 }
604b951e
BA
23});
24
d9a7a1e4
BA
25router.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
604b951e 32router.put("/news", access.logged, access.ajax, (req,res) => {
604b951e 33 let news = req.body.news;
d9a7a1e4 34 if (devs.includes(req.userId) && news.id.toString().match(/^[0-9]+$/)) {
866842c3
BA
35 news.content = sanitizeHtml(news.content);
36 NewsModel.update(news);
37 res.json({});
38 }
604b951e
BA
39});
40
41router.delete("/news", access.logged, access.ajax, (req,res) => {
604b951e 42 const nid = req.query.id;
d9a7a1e4 43 if (devs.includes(req.userId) && nid.toString().match(/^[0-9]+$/)) {
866842c3
BA
44 NewsModel.remove(nid);
45 res.json({});
46 }
604b951e
BA
47});
48
49module.exports = router;