start board sorting and searching

merge-requests/208/head
fatchan 5 years ago
parent 5dd1c9e7af
commit c51ecb42ff
  1. 22
      db/boards.js
  2. 1
      db/index.js
  3. 35
      db/webring.js
  4. 2
      gulp/res/css/style.css
  5. 4
      gulpfile.js
  6. 59
      models/pages/boardlist.js
  7. 6
      schedules/webring.js
  8. 27
      views/pages/boardlist.pug

@ -100,10 +100,14 @@ module.exports = {
); );
}, },
boardSort: (skip=0, limit=50) => { boardSort: (skip=0, limit=50, sort={ ips:-1, pph:-1, sequence_value:-1 }, filter={}) => {
return db.find({ const addedFilter = {
'settings.unlisted': false, 'settings.unlisted': false
}, { }
if (filter.name) {
addedFilter._id = filter.name;
}
return db.find(addedFilter, {
'projection': { 'projection': {
'_id': 1, '_id': 1,
'sequence_value': 1, 'sequence_value': 1,
@ -113,11 +117,11 @@ module.exports = {
'settings.description': 1, 'settings.description': 1,
'settings.name': 1, 'settings.name': 1,
} }
}).sort({ })
'ips': -1, .sort(sort)
'pph': -1, .skip(skip)
'sequence_value': -1, .limit(limit)
}).skip(skip).limit(limit).toArray(); .toArray();
}, },
count: (showUnlisted=false) => { count: (showUnlisted=false) => {

@ -4,6 +4,7 @@ module.exports = {
Posts: require(__dirname+'/posts.js'), Posts: require(__dirname+'/posts.js'),
Boards: require(__dirname+'/boards.js'), Boards: require(__dirname+'/boards.js'),
Webring: require(__dirname+'/webring.js'),
Stats: require(__dirname+'/stats.js'), Stats: require(__dirname+'/stats.js'),
Accounts: require(__dirname+'/accounts.js'), Accounts: require(__dirname+'/accounts.js'),
Bans: require(__dirname+'/bans.js'), Bans: require(__dirname+'/bans.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({});
},
}

@ -814,7 +814,7 @@ table.boardtable th:nth-child(3),table.boardtable th:nth-child(4),table.boardtab
} }
.form-post { .form-post {
width: calc(100% - 10px)!important; width: 100%;
} }
.form-login { .form-login {

@ -33,7 +33,7 @@ const gulp = require('gulp')
async function wipe() { async function wipe() {
const Mongo = require(__dirname+'/db/db.js'); const Mongo = require(__dirname+'/db/db.js');
await Mongo.connect(); await Mongo.connect();
const { Boards, Posts, Captchas, Ratelimits, const { Webring, Boards, Posts, Captchas, Ratelimits,
Accounts, Files, Stats, Modlogs, Bans } = require(__dirname+'/db/'); Accounts, Files, Stats, Modlogs, Bans } = require(__dirname+'/db/');
//wipe db shit //wipe db shit
@ -43,6 +43,7 @@ async function wipe() {
Accounts.deleteAll(), Accounts.deleteAll(),
Posts.deleteAll(), Posts.deleteAll(),
Boards.deleteAll(), Boards.deleteAll(),
Webring.deleteAll(),
Bans.deleteAll(), Bans.deleteAll(),
Files.deleteAll(), Files.deleteAll(),
Stats.deleteAll(), Stats.deleteAll(),
@ -69,6 +70,7 @@ async function wipe() {
}) })
//add indexes - should profiled and changed at some point if necessary //add indexes - should profiled and changed at some point if necessary
, Stats.db.createIndex({board:1, hour:1}) , 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}) , Boards.db.createIndex({ips: 1, pph:1, sequence_value:1})
, Bans.db.dropIndexes() , Bans.db.dropIndexes()
, Captchas.db.dropIndexes() , Captchas.db.dropIndexes()

@ -1,9 +1,8 @@
'use strict'; 'use strict';
const cache = require(__dirname+'/../../redis.js') const { enableWebring } = require(__dirname+'/../../configs/main.json')
, { enableWebring } = require(__dirname+'/../../configs/main.json') , { Boards, Webring } = require(__dirname+'/../../db/')
, { Boards } = require(__dirname+'/../../db/') , limit = 1;
, limit = 20;
module.exports = async (req, res, next) => { module.exports = async (req, res, next) => {
@ -17,27 +16,57 @@ module.exports = async (req, res, next) => {
page = 1; page = 1;
} }
const offset = (page-1) * limit; 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 { try {
[ boards, webringBoards, localPages ] = await Promise.all([ [ localBoards, localPages, webringBoards, webringPages ] = await Promise.all([
Boards.boardSort(offset, limit), Boards.boardSort(offset, limit, sort, filter),
enableWebring ? cache.get('webring:boards') : null,
Boards.count(), 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) { } catch (err) {
return next(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); const maxPage = Math.max(localPages, webringPages);
return res.render('boardlist', { return res.render('boardlist', {
boards, localBoards,
webringBoards, webringBoards,
page, page,
maxPage maxPage,
sort,
search: !Array.isArray(search) ? search : null,
}); });
} }

@ -3,7 +3,7 @@
const fetch = require('node-fetch') const fetch = require('node-fetch')
, { meta } = require(__dirname+'/../configs/main.json') , { meta } = require(__dirname+'/../configs/main.json')
, { logos, following, blacklist } = require(__dirname+'/../configs/webring.json') , { logos, following, blacklist } = require(__dirname+'/../configs/webring.json')
, { Boards } = require(__dirname+'/../db/') , { Boards, Webring } = require(__dirname+'/../db/')
, { outputFile } = require('fs-extra') , { outputFile } = require('fs-extra')
, cache = require(__dirname+'/../redis.js') , cache = require(__dirname+'/../redis.js')
, uploadDirectory = require(__dirname+'/../helpers/files/uploadDirectory.js') , uploadDirectory = require(__dirname+'/../helpers/files/uploadDirectory.js')
@ -38,9 +38,9 @@ module.exports = async () => {
} }
const known = [...new Set(found.concat(fetchWebring))] const known = [...new Set(found.concat(fetchWebring))]
.filter(site => !blacklist.some(x => site.includes(x)) && !site.includes(meta.url)); .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: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 //now update the webring json with board list and known sites
const boards = await Boards.boardSort(0, 0); //does not include unlisted boards const boards = await Boards.boardSort(0, 0); //does not include unlisted boards
const json = { const json = {

@ -6,10 +6,31 @@ block head
block content block content
h1.board-title Board List 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 h4.board-description Local Boards
include ../includes/boardtable.pug include ../includes/boardtable.pug
each board in boards each board in localBoards
tr tr
td td
if board.settings.sfw if board.settings.sfw
@ -34,5 +55,5 @@ block content
td #{board.postsPerHour || '-'} td #{board.postsPerHour || '-'}
td #{board.uniqueUsers || '-'} td #{board.uniqueUsers || '-'}
td #{board.totalPosts || '-'} td #{board.totalPosts || '-'}
nav.pages.text-center nav.pages.text-center.mt-5
include ../includes/boardlistpages.pug include ../includes/boardlistpages.pug

Loading…
Cancel
Save