nodebb-plugin-shoutbox
Advanced tools
Comparing version 2.1.3 to 2.1.4
@@ -0,3 +1,7 @@ | ||
/* eslint-disable no-await-in-loop */ | ||
'use strict'; | ||
const _ = require('lodash'); | ||
const packageInfo = require('../package.json'); | ||
@@ -79,2 +83,32 @@ const pluginInfo = require('../plugin.json'); | ||
}, | ||
getRandomUser: async function (socket, data) { | ||
let done = false; | ||
let start = -49; | ||
let stop = start + 48; | ||
const oneMinuteMs = 60 * 1000; | ||
const cutoffMinutes = parseInt(data.cutoffMinutes, 10) || 30; | ||
const cutoff = Date.now() - (cutoffMinutes * oneMinuteMs); | ||
const foundShouts = []; | ||
do { | ||
const sids = await NodeBB.db.getListRange('shouts', start, stop); | ||
if (!sids.length) { | ||
done = true; | ||
} else { | ||
const allShouts = await NodeBB.db.getObjects(sids.map(sid => `shout:${sid}`)); | ||
const shouts = allShouts.filter(s => s && s.timestamp >= cutoff); | ||
const allBeforeCutoff = allShouts.every(s => s && s.timestamp < cutoff); | ||
if (allBeforeCutoff) { | ||
done = true; | ||
} | ||
foundShouts.push(...shouts); | ||
} | ||
start -= 50; | ||
stop = start + 49; | ||
} while (!done); | ||
if (!foundShouts.length) { | ||
throw new Error(`No users found in the past ${cutoffMinutes} minutes`); | ||
} | ||
const randomUid = _.sample(_.uniq(foundShouts.map(s => s.fromuid))); | ||
return await NodeBB.User.getUserFields(randomUid, ['uid', 'username', 'userslug', 'picture']); | ||
}, | ||
}; | ||
@@ -81,0 +115,0 @@ |
{ | ||
"name": "nodebb-plugin-shoutbox", | ||
"version": "2.1.3", | ||
"version": "2.1.4", | ||
"description": "NodeBB Shoutbox Plugin", | ||
@@ -24,3 +24,5 @@ "main": "library.js", | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"lodash": "^4.17.21" | ||
}, | ||
"devDependencies": { | ||
@@ -27,0 +29,0 @@ "eslint": "7.32.0", |
@@ -54,2 +54,13 @@ 'use strict'; | ||
} | ||
$('[component="shoutbox/random-user"]').on('click', function () { | ||
const cutoff = $(this).attr('data-cutoff'); | ||
socket.emit('admin.plugins.shoutbox.getRandomUser', { cutoffMinutes: cutoff }, async function (err, user) { | ||
if (err) { | ||
return Shoutbox.alert('error', err); | ||
} | ||
const bootbox = await app.require('bootbox'); | ||
bootbox.alert(`Picked user <a href="${config.relative_path}/user/${user.userslug}">${user.username}</a>`); | ||
}); | ||
}); | ||
}; | ||
@@ -56,0 +67,0 @@ |
494295
1802
1
+ Addedlodash@^4.17.21
+ Addedlodash@4.17.21(transitive)