Fix
[vchess.git] / server / routes / news.js
CommitLineData
604b951e
BA
1let router = require("express").Router();
2const access = require("../utils/access");
a9e79351 3const params = require("../config/parameters");
604b951e
BA
4const NewsModel = require("../models/News");
5const sanitizeHtml = require('sanitize-html');
604b951e 6
866842c3 7router.post("/news", access.logged, access.ajax, (req,res) => {
a9e79351 8 if (params.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"];
68e19a44 18 if (!!cursor && !!cursor.match(/^[0-9]+$/)) {
0234201f 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
7c8d5dc7
BA
27 NewsModel.getTimestamp((err, ts) => {
28 res.json(err || { timestamp: !!ts ? ts.added : 0 });
d9a7a1e4
BA
29 });
30});
31
604b951e 32router.put("/news", access.logged, access.ajax, (req,res) => {
604b951e 33 let news = req.body.news;
a9e79351
BA
34 if (
35 params.devs.includes(req.userId) &&
36 news.id.toString().match(/^[0-9]+$/)
37 ) {
866842c3
BA
38 news.content = sanitizeHtml(news.content);
39 NewsModel.update(news);
40 res.json({});
41 }
604b951e
BA
42});
43
44router.delete("/news", access.logged, access.ajax, (req,res) => {
604b951e 45 const nid = req.query.id;
a9e79351
BA
46 if (
47 params.devs.includes(req.userId) &&
48 nid.toString().match(/^[0-9]+$/)
49 ) {
866842c3
BA
50 NewsModel.remove(nid);
51 res.json({});
52 }
604b951e
BA
53});
54
55module.exports = router;