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]+$/))
21 NewsModel
.getNext(cursor
, (err
,newsList
) => {
22 res
.json(err
|| {newsList:newsList
});
27 router
.put("/news", access
.logged
, access
.ajax
, (req
,res
) => {
28 let news
= req
.body
.news
;
29 if (devs
.includes(req
.userId
) && news
.id
.toString().match(/^[0-9]+$/))
31 news
.content
= sanitizeHtml(news
.content
);
32 NewsModel
.update(news
);
37 router
.delete("/news", access
.logged
, access
.ajax
, (req
,res
) => {
38 const nid
= req
.query
.id
;
39 if (devs
.includes(req
.userId
) && nid
.toString().match(/^[0-9]+$/))
41 NewsModel
.remove(nid
);
46 module
.exports
= router
;