From c51ecb42ff54520f1b75fd16151d48ede3a35ec6 Mon Sep 17 00:00:00 2001 From: fatchan Date: Sat, 12 Oct 2019 22:54:22 +1100 Subject: [PATCH] start board sorting and searching --- db/boards.js | 22 +++++++++------ db/index.js | 1 + db/webring.js | 35 +++++++++++++++++++++++ gulp/res/css/style.css | 2 +- gulpfile.js | 4 ++- models/pages/boardlist.js | 59 +++++++++++++++++++++++++++++---------- schedules/webring.js | 6 ++-- views/pages/boardlist.pug | 27 ++++++++++++++++-- 8 files changed, 124 insertions(+), 32 deletions(-) create mode 100644 db/webring.js diff --git a/db/boards.js b/db/boards.js index f7ce88d7..33c40f6e 100644 --- a/db/boards.js +++ b/db/boards.js @@ -100,10 +100,14 @@ module.exports = { ); }, - boardSort: (skip=0, limit=50) => { - return db.find({ - 'settings.unlisted': false, - }, { + boardSort: (skip=0, limit=50, sort={ ips:-1, pph:-1, sequence_value:-1 }, filter={}) => { + const addedFilter = { + 'settings.unlisted': false + } + if (filter.name) { + addedFilter._id = filter.name; + } + return db.find(addedFilter, { 'projection': { '_id': 1, 'sequence_value': 1, @@ -113,11 +117,11 @@ module.exports = { 'settings.description': 1, 'settings.name': 1, } - }).sort({ - 'ips': -1, - 'pph': -1, - 'sequence_value': -1, - }).skip(skip).limit(limit).toArray(); + }) + .sort(sort) + .skip(skip) + .limit(limit) + .toArray(); }, count: (showUnlisted=false) => { diff --git a/db/index.js b/db/index.js index ca935b53..e42ddb3f 100644 --- a/db/index.js +++ b/db/index.js @@ -4,6 +4,7 @@ module.exports = { Posts: require(__dirname+'/posts.js'), Boards: require(__dirname+'/boards.js'), + Webring: require(__dirname+'/webring.js'), Stats: require(__dirname+'/stats.js'), Accounts: require(__dirname+'/accounts.js'), Bans: require(__dirname+'/bans.js'), diff --git a/db/webring.js b/db/webring.js new file mode 100644 index 00000000..8ce2b830 --- /dev/null +++ b/db/webring.js @@ -0,0 +1,35 @@ +'use strict'; + +const Mongo = require(__dirname+'/db.js') + , db = Mongo.client.db('jschan').collection('webring'); + +module.exports = { + + db, + + boardSort: (skip=0, limit=50, sort={ ips:-1, pph:-1, sequence_value:-1 }, filter={}) => { + const addedFilter = {}; + if (filter.name) { + addedfilter.uri = filter.name; + } + return db.find(addedFilter) + .sort({ + 'uniqueUsers': sort.ips, + 'postsPerHour': sort.pph, + 'totalPosts': sort.sequence_value, + }) + .skip(skip) + .limit(limit) + .toArray(); + }, + + count: () => { + //no need to countDocuments beacuse we dont filter anything. just use metadata + return db.estimateDocumentCount(); + }, + + deleteAll: (board) => { + return db.deleteMany({}); + }, + +} diff --git a/gulp/res/css/style.css b/gulp/res/css/style.css index fb6b9dfc..2db41235 100644 --- a/gulp/res/css/style.css +++ b/gulp/res/css/style.css @@ -814,7 +814,7 @@ table.boardtable th:nth-child(3),table.boardtable th:nth-child(4),table.boardtab } .form-post { - width: calc(100% - 10px)!important; + width: 100%; } .form-login { diff --git a/gulpfile.js b/gulpfile.js index f34d00a5..17a0295e 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -33,7 +33,7 @@ const gulp = require('gulp') async function wipe() { const Mongo = require(__dirname+'/db/db.js'); await Mongo.connect(); - const { Boards, Posts, Captchas, Ratelimits, + const { Webring, Boards, Posts, Captchas, Ratelimits, Accounts, Files, Stats, Modlogs, Bans } = require(__dirname+'/db/'); //wipe db shit @@ -43,6 +43,7 @@ async function wipe() { Accounts.deleteAll(), Posts.deleteAll(), Boards.deleteAll(), + Webring.deleteAll(), Bans.deleteAll(), Files.deleteAll(), Stats.deleteAll(), @@ -69,6 +70,7 @@ async function wipe() { }) //add indexes - should profiled and changed at some point if necessary , Stats.db.createIndex({board:1, hour:1}) + , Webring.db.createIndex({uniqueUsers:1, postsPerHour:1, totalPosts:1}) , Boards.db.createIndex({ips: 1, pph:1, sequence_value:1}) , Bans.db.dropIndexes() , Captchas.db.dropIndexes() diff --git a/models/pages/boardlist.js b/models/pages/boardlist.js index 0100b040..a7b8f636 100644 --- a/models/pages/boardlist.js +++ b/models/pages/boardlist.js @@ -1,9 +1,8 @@ 'use strict'; -const cache = require(__dirname+'/../../redis.js') - , { enableWebring } = require(__dirname+'/../../configs/main.json') - , { Boards } = require(__dirname+'/../../db/') - , limit = 20; +const { enableWebring } = require(__dirname+'/../../configs/main.json') + , { Boards, Webring } = require(__dirname+'/../../db/') + , limit = 1; module.exports = async (req, res, next) => { @@ -17,27 +16,57 @@ module.exports = async (req, res, next) => { page = 1; } const offset = (page-1) * limit; - let boards, webringBoards, localPages, webringPages; + + let sort = {}; + const { ips_sort, pph_sort, posts_sort, search } = req.query; + if (!(ips_sort || pph_sort || posts_sort)) { + sort = { + 'ips': -1, + 'pph': -1, + 'sequence_value': -1, + } + } else { + if (ips_sort) { + sort.ips = ips_sort == '1' ? 1 : -1; + } + if (pph_sort) { + sort.pph = pph_sort == '1' ? 1 : -1; + } + if (posts_sort) { + sort.sequence_value = posts_sort == '1' ? 1 : -1; + } + } + + let filter = {}; + if (search && !Array.isArray(search)) { + filter = { + //TODO: add tags searching once (if) the webring adds it + 'name': search, + } + } + + let localBoards, webringBoards, localPages, webringPages; try { - [ boards, webringBoards, localPages ] = await Promise.all([ - Boards.boardSort(offset, limit), - enableWebring ? cache.get('webring:boards') : null, + [ localBoards, localPages, webringBoards, webringPages ] = await Promise.all([ + Boards.boardSort(offset, limit, sort, filter), Boards.count(), + enableWebring ? Webring.boardSort(offset, limit, sort, filter) : null, + enableWebring ? Webring.count() : 0, ]); + localPages = Math.ceil(localPages / limit); + webringPages = Math.ceil(webringPages / limit); } catch (err) { return next(err); } - localPages = Math.ceil(localPages / limit); - if (enableWebring && webringBoards) { //sort webring boards - webringPages = Math.ceil(webringBoards.length / limit); - webringBoards = webringBoards.sort((a, b) => { return b.uniqueUsers - a.uniqueUsers }).splice(offset, limit); - } const maxPage = Math.max(localPages, webringPages); + return res.render('boardlist', { - boards, + localBoards, webringBoards, page, - maxPage + maxPage, + sort, + search: !Array.isArray(search) ? search : null, }); } diff --git a/schedules/webring.js b/schedules/webring.js index 2d0b0b1a..ac7fc0e6 100644 --- a/schedules/webring.js +++ b/schedules/webring.js @@ -3,7 +3,7 @@ const fetch = require('node-fetch') , { meta } = require(__dirname+'/../configs/main.json') , { logos, following, blacklist } = require(__dirname+'/../configs/webring.json') - , { Boards } = require(__dirname+'/../db/') + , { Boards, Webring } = require(__dirname+'/../db/') , { outputFile } = require('fs-extra') , cache = require(__dirname+'/../redis.js') , uploadDirectory = require(__dirname+'/../helpers/files/uploadDirectory.js') @@ -38,9 +38,9 @@ module.exports = async () => { } const known = [...new Set(found.concat(fetchWebring))] .filter(site => !blacklist.some(x => site.includes(x)) && !site.includes(meta.url)); - //add the known sites and boards to cache in redis (so can be used later in other places e.g. board list) cache.set('webring:sites', known); - cache.set('webring:boards', webringBoards); + await Webring.deleteAll(); + await Webring.db.insertMany(webringBoards); //now update the webring json with board list and known sites const boards = await Boards.boardSort(0, 0); //does not include unlisted boards const json = { diff --git a/views/pages/boardlist.pug b/views/pages/boardlist.pug index e239c587..4b33c71d 100644 --- a/views/pages/boardlist.pug +++ b/views/pages/boardlist.pug @@ -6,10 +6,31 @@ block head block content h1.board-title Board List - if boards && boards.length > 0 + section.form-wrapper.flexleft.mv-10 + form.form-post(action=`/boards.html` method='GET') + .row + .label Name + input(type='text' name='search' value=search) + .row + .label Users + select(name='ips_sort') + option(value='-1' selected=sort.ips === -1) Highest to Lowest + option(value='1' selected=sort.ips === 1) Lowest to Highest + .row + .label PPH + select(name='pph_sort') + option(value='-1' selected=sort.pph === -1) Highest to Lowest + option(value='1' selected=sort.pph === 1) Lowest to Highest + .row + .label Posts + select(name='posts_sort') + option(value='-1' selected=sort.sequence_value === -1) Highest to Lowest + option(value='1' selected=sort.sequence_value === 1) Lowest to Highest + input(type='submit', value='Filter') + if localBoards && localBoards.length > 0 h4.board-description Local Boards include ../includes/boardtable.pug - each board in boards + each board in localBoards tr td if board.settings.sfw @@ -34,5 +55,5 @@ block content td #{board.postsPerHour || '-'} td #{board.uniqueUsers || '-'} td #{board.totalPosts || '-'} - nav.pages.text-center + nav.pages.text-center.mt-5 include ../includes/boardlistpages.pug