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
))
10 const content
= sanitizeHtml(req
.body
.news
.content
);
11 NewsModel
.create(content
, req
.userId
, (err
,ret
) => {
12 res
.json(err
|| { id: ret
.nid
});
17 router
.get("/news", access
.ajax
, (req
,res
) => {
18 const cursor
= req
.query
["cursor"];
19 if (cursor
.match(/^[0-9]+$/)) {
20 NewsModel
.getNext(cursor
, (err
,newsList
) => {
21 res
.json(err
|| { newsList: newsList
});
26 router
.get("/newsts", access
.ajax
, (req
,res
) => {
27 // Special query for footer: just return timestamp of last news
28 NewsModel
.getTimestamp((err
,ts
) => {
29 res
.json(err
|| { timestamp: ts
.added
});
33 router
.put("/news", access
.logged
, access
.ajax
, (req
,res
) => {
34 let news
= req
.body
.news
;
35 if (devs
.includes(req
.userId
) && news
.id
.toString().match(/^[0-9]+$/)) {
36 news
.content
= sanitizeHtml(news
.content
);
37 NewsModel
.update(news
);
42 router
.delete("/news", access
.logged
, access
.ajax
, (req
,res
) => {
43 const nid
= req
.query
.id
;
44 if (devs
.includes(req
.userId
) && nid
.toString().match(/^[0-9]+$/)) {
45 NewsModel
.remove(nid
);
50 module
.exports
= router
;