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
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
) => {
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
});
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
});
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
);
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
);
49 module
.exports
= router
;