This commit is contained in:
barisusakli 2016-02-26 12:57:29 +02:00
parent db7b85ebde
commit 9eaead7574
7 changed files with 338 additions and 0 deletions

14
.gitignore vendored Normal file
View file

@ -0,0 +1,14 @@
npm-debug.log
node_modules/
sftp-config.json
public/css/*.css
*.sublime-project
*.sublime-workspace
.project
.idea
*.swp
Vagrantfile
.vagrant
provision.sh
*.komodoproject

86
.jshintrc Normal file
View file

@ -0,0 +1,86 @@
{
// JSHint Default Configuration File (as on JSHint website)
// See http://jshint.com/docs/ for more details

"maxerr" : 50, // {int} Maximum error before stopping

// Enforcing
"bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.)
"camelcase" : false, // true: Identifiers must be in camelCase
"curly" : true, // true: Require {} for every new block or scope
"eqeqeq" : true, // true: Require triple equals (===) for comparison
"forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty()
"immed" : false, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());`
"indent" : 4, // {int} Number of spaces to use for indentation
"latedef" : false, // true: Require variables/functions to be defined before being used
"newcap" : false, // true: Require capitalization of all constructor functions e.g. `new F()`
"noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee`
"noempty" : true, // true: Prohibit use of empty blocks
"nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment)
"plusplus" : false, // true: Prohibit use of `++` & `--`
"quotmark" : false, // Quotation mark consistency:
// false : do nothing (default)
// true : ensure whatever is used is consistent
// "single" : require single quotes
// "double" : require double quotes
"undef" : true, // true: Require all non-global variables to be declared (prevents global leaks)
"unused" : true, // true: Require all defined variables be used
"strict" : true, // true: Requires all functions run in ES5 Strict Mode
"trailing" : false, // true: Prohibit trailing whitespaces
"maxparams" : false, // {int} Max number of formal params allowed per function
"maxdepth" : false, // {int} Max depth of nested blocks (within functions)
"maxstatements" : false, // {int} Max number statements per function
"maxcomplexity" : false, // {int} Max cyclomatic complexity per function
"maxlen" : false, // {int} Max number of characters per line

// Relaxing
"asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons)
"boss" : false, // true: Tolerate assignments where comparisons would be expected
"debug" : false, // true: Allow debugger statements e.g. browser breakpoints.
"eqnull" : false, // true: Tolerate use of `== null`
"es5" : false, // true: Allow ES5 syntax (ex: getters and setters)
"esnext" : false, // true: Allow ES.next (ES6) syntax (ex: `const`)
"moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features)
// (ex: `for each`, multiple try/catch, function expression…)
"evil" : false, // true: Tolerate use of `eval` and `new Function()`
"expr" : false, // true: Tolerate `ExpressionStatement` as Programs
"funcscope" : false, // true: Tolerate defining variables inside control statements"
"globalstrict" : false, // true: Allow global "use strict" (also enables 'strict')
"iterator" : false, // true: Tolerate using the `__iterator__` property
"lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block
"laxbreak" : false, // true: Tolerate possibly unsafe line breakings
"laxcomma" : false, // true: Tolerate comma-first style coding
"loopfunc" : false, // true: Tolerate functions being defined in loops
"multistr" : false, // true: Tolerate multi-line strings
"proto" : false, // true: Tolerate using the `__proto__` property
"scripturl" : false, // true: Tolerate script-targeted URLs
"smarttabs" : false, // true: Tolerate mixed tabs/spaces when used for alignment
"shadow" : false, // true: Allows re-define variables later in code e.g. `var x=1; x=2;`
"sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation
"supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;`
"validthis" : false, // true: Tolerate using this in a non-constructor function

// Environments
"browser" : true, // Web Browser (window, document, etc)
"couch" : false, // CouchDB
"devel" : true, // Development/debugging (alert, confirm, etc)
"dojo" : false, // Dojo Toolkit
"jquery" : true, // jQuery
"mootools" : false, // MooTools
"node" : true, // Node.js
"nonstandard" : false, // Widely adopted globals (escape, unescape, etc)
"prototypejs" : false, // Prototype and Scriptaculous
"rhino" : false, // Rhino
"worker" : false, // Web Workers
"wsh" : false, // Windows Scripting Host
"yui" : false, // Yahoo User Interface

// Legacy
"nomen" : false, // true: Prohibit dangling `_` in variables
"onevar" : false, // true: Allow only one `var` statement per function
"passfail" : false, // true: Stop on first error
"white" : false, // true: Check against strict whitespace and indentation rules

// Custom Globals
"globals" : {} // additional predefined global variables
}

11
README.md Normal file
View file

@ -0,0 +1,11 @@
# NodeBB Plugin Leaderboard

A plugin that creates a /leaderboard route based on daily/weekly/monthly reputation

## Installation

npm install nodebb-plugin-leaderboard OR install it from your ACP




170
index.js Normal file
View file

@ -0,0 +1,170 @@

'use strict';

var async = require.main.require('async');
var cron = require.main.require('cron').CronJob;
var nconf = require.main.require('nconf');

var db = require.main.require('./src/database');
var helpers = require.main.require('./src/routes/helpers');
var controllersHelpers = require.main.require('./src/controllers/helpers');
var usersController = require.main.require('./src/controllers/users');
var pubsub = require.main.require('./src/pubsub');

var plugin = {};

var cronJobs = [];

cronJobs.push(new cron('0 0 * * *', function() {db.delete('users:reputation:daily');}, null, false));
cronJobs.push(new cron('0 0 * * 0', function() {db.delete('users:reputation:weekly');}, null, false));
cronJobs.push(new cron('0 0 1 * *', function() {db.delete('users:reputation:monthly');}, null, false));

plugin.init = function(params, callback) {
var middlewares = [params.middleware.checkGlobalPrivacySettings];
helpers.setupPageRoute(params.router, '/leaderboard/:term?', params.middleware, middlewares, plugin.renderLeaderboard);
reStartCronJobs();
callback();
};

plugin.renderLeaderboard = function(req, res, next) {
var term = req.params.term || '';
if (term === 'alltime') {
term = '';
}
var set = 'users:reputation' + (term ? ':' + term : '');

var userData;
async.waterfall([
function (next) {
usersController.getUsers(set, req.uid, req.query.page, next);
},
function (_userData, next) {
userData = _userData;
var uids = userData.users.map(function(user) {
return user && user.uid;
});
db.sortedSetScores(set, uids, next);
},
function (scores, next) {
userData.users.forEach(function(user, index) {
if (user) {
user.reputation = scores[index] || 0;
}
});
next(null, userData);
}
], function(err, userData) {
if (err) {
return next(err);
}
var breadcrumbs = [{text: term ? (term.charAt(0).toUpperCase() + term.slice(1)) : 'Leaderboard'}];

if (term) {
breadcrumbs.unshift({text: 'Leaderboard', url: '/leaderboard'});
userData[term] = true;
}

userData.breadcrumbs = controllersHelpers.buildBreadcrumbs(breadcrumbs);
userData['route_users:reputation'] = true;
userData.title = 'Leaderboard';
res.render('leaderboard', userData);
});
};

plugin.getNavigation = function(core, callback) {
core.push([
{
route: '/leaderboard',
title: 'Leaderboard',
enabled: true,
iconClass: 'fa-star',
textClass: 'visible-xs-inline',
text: '',
properties: { },
core: true
}
]);
callback(null, core);
};

plugin.onUpvote = function(data) {
var change = 0;
if (data.current === 'unvote') {
change = 1;
} else if (data.current === 'downvote') {
change = 2;
}

updateLeaderboards(change, data.owner);
};

plugin.onDownvote = function(data) {
var change = 0;
if (data.current === 'unvote') {
change = -1;
} else if (data.current === 'upvote') {
change = -2;
}
updateLeaderboards(change, data.owner);
};

plugin.onUnvote = function(data) {
var change = 0;
if (data.current === 'upvote') {
change = -1;
} else if (data.current === 'downvote') {
change = 1;
}
updateLeaderboards(change, data.owner);
};

function updateLeaderboards(change, owner) {
if (change) {
db.sortedSetIncrBy('users:reputation:daily', change, owner);
db.sortedSetIncrBy('users:reputation:weekly', change, owner);
db.sortedSetIncrBy('users:reputation:monthly', change, owner);
}
}

plugin.activate = function(id) {
if (id === 'nodebb-plugin-leaderboard') {
pubsub.publish('nodebb-plugin-leaderboard:activate');
}
};

plugin.deactivate = function(id) {
if (id === 'nodebb-plugin-leaderboard') {
pubsub.publish('nodebb-plugin-leaderboard:deactivate');
}
};

pubsub.on('nodebb-plugin-leaderboard:activate', function() {
reStartCronJobs();
});

pubsub.on('nodebb-plugin-leaderboard:deactivate', function() {
stopCronJobs();
});


function reStartCronJobs() {
if (nconf.get('isPrimary') === 'true') {
stopCronJobs();
cronJobs.forEach(function(job) {
job.start();
});
}
}

function stopCronJobs() {
if (nconf.get('isPrimary') === 'true') {
cronJobs.forEach(function(job) {
job.stop();
});
}
}



module.exports = plugin;

26
package.json Normal file
View file

@ -0,0 +1,26 @@
{
"name": "nodebb-plugin-leaderboard",
"version": "1.0.0",
"description": "Adds a daily/weekly/monthly leaderboard based on reputation",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/NodeBB/nodebb-plugin-leaderboard"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodebb",
"plugin",
"leaderboard"
],
"author": "Baris Usakli <baris@nodebb.org>",
"license": "MIT",
"dependencies": {

},
"nbbpm": {
"compatibility": "^0.7.0 || ^0.8.0 || ^0.9.0"
}
}

13
plugin.json Normal file
View file

@ -0,0 +1,13 @@
{
"library": "./index.js",
"hooks": [
{ "hook": "static:app.load", "method": "init"},
{ "hook": "action:post.upvote", "method": "onUpvote"},
{ "hook": "action:post.downvote", "method": "onDownvote"},
{ "hook": "action:post.unvote", "method": "onUnvote"},
{ "hook": "action:plugin.activate", "method": "activate"},
{ "hook": "action:plugin.deactivate", "method": "deactivate"},
{ "hook": "filter:navigation.available", "method": "getNavigation"}
],
"templates": "static/templates"
}

View file

@ -0,0 +1,18 @@
<div class="users">

<!-- IMPORT partials/breadcrumbs.tpl -->

<div class="row">
<div class="col-lg-6">
<ul class="nav nav-pills">
<li class="<!-- IF daily -->active<!-- ENDIF daily -->"><a href='{config.relative_path}/leaderboard/daily'>Daily</a></li>
<li class="<!-- IF weekly -->active<!-- ENDIF weekly -->"><a href='{config.relative_path}/leaderboard/weekly'>Weekly</a></li>
<li class="<!-- IF monthly -->active<!-- ENDIF monthly -->"><a href='{config.relative_path}/leaderboard/monthly'>Monthly</a></li>
</ul>
</div>
</div>

<ul id="users-container" class="users-container">
<!-- IMPORT partials/users_list.tpl -->
</ul>
</div>