diff --git a/routes/news.js b/routes/news.js index 7c822f0d..27003687 100644 --- a/routes/news.js +++ b/routes/news.js @@ -8,35 +8,73 @@ var db = require("../core/db"); module.exports = function (app) { app.use(route.get("/news", router.accepts({ - "text/html": function *(){ + "text/html": function *() { this.redirect("/"); }, - "text/plain": function *(){ + "text/plain": function *() { this.redirect("/"); }, - "application/json": function *(){ + "application/json": function *() { var count = parseInt(this.query["count"]) || 10; var portion = parseInt(this.query["portion"]) || 0; var skip = portion * count; var query = {}; - if(this.query.type) query.type = this.query.type; + if (this.query.type) query.type = this.query.type; - this.body = yield db.c("posts").find(query).skip(skip).limit(count).toArray() - .then(function(items){ + var pinned = []; + if(portion == 0) { + var now = new Date(); + pinned = yield db.c("posts").find({until: {$gte: now}}).toArray() + .then(function (items) { + return items.sort(sortNewsByDate); + }); + } + var pinned_ids = pinned.map(function (p) { + return p["_id"]+""; + }); + + var posts = yield db.c("posts").find(query).skip(skip).limit(count).toArray() + .then(function (items) { + return items.sort(sortNewsByDate); + }) + .then(function (items) { + return items.filter(function (i) { + var id = i["_id"]+""; + return pinned_ids.indexOf(id) < 0; + }); + }); + + this.body = pinned.concat(posts); + //this.body = pinned_ids + } + }))); + + app.use(route.get("/news/pinned", router.accepts({ + "text/html": function *() { + this.redirect("/"); + }, + "text/plain": function *() { + this.redirect("/"); + }, + "application/json": function *() { + var now = new Date(); + var pinned = yield db.c("posts").find({until: {$gte: now}}).toArray() + .then(function (items) { return items.sort(sortNewsByDate); }); + this.body = pinned; } }))); app.use(route.get("/news/:uri", router.accepts({ "text/html": router.index(), "text/plain": router.index(), - "application/json": function *(uri){ + "application/json": function *(uri) { var item = yield db.c("posts").findOne({uri: uri}); - if(item){ + if (item) { this.body = item; - }else { + } else { this.status = 404; } }