nodebb-plugin-shoutbox
Advanced tools
Comparing version 0.0.3 to 0.0.4
618
library.js
var async = require('async'), | ||
fs = require('fs'), | ||
path = require('path'), | ||
S = require('string'), | ||
User = module.parent.require('./user'), | ||
Meta = module.parent.require('./meta'), | ||
Plugins = module.parent.parent.require('./plugins'), | ||
db = module.parent.require('./database'), | ||
winston = module.parent.require('winston'), | ||
SocketIndex = module.parent.require('./socket.io/index'), | ||
ModulesSockets = module.parent.require('./socket.io/modules'); | ||
fs = require('fs'), | ||
path = require('path'), | ||
S = require('string'), | ||
User = module.parent.require('./user'), | ||
Meta = module.parent.require('./meta'), | ||
Plugins = module.parent.parent.require('./plugins'), | ||
db = module.parent.require('./database'), | ||
winston = module.parent.require('winston'), | ||
webserver = module.parent.require('./webserver'), | ||
SocketIndex = module.parent.require('./socket.io/index'), | ||
ModulesSockets = module.parent.require('./socket.io/modules'); | ||
var constants = Object.freeze({ | ||
'name': "Shoutbox", | ||
'global': { | ||
'route': '/shoutbox' | ||
}, | ||
'admin': { | ||
'route': '/plugins/shoutbox', | ||
'icon': 'fa-bullhorn' | ||
}, | ||
'config_keys': ['headerlink'], | ||
'config_defaults': { | ||
'headerlink': '0' | ||
} | ||
'name': "Shoutbox", | ||
'global': { | ||
'route': '/shoutbox' | ||
}, | ||
'admin': { | ||
'route': '/plugins/shoutbox', | ||
'icon': 'fa-bullhorn' | ||
}, | ||
'config_keys': ['headerlink','pageposition','shoutlimit'], | ||
'config_defaults': { | ||
'headerlink': '0', | ||
'pageposition': 'top', | ||
'shoutlimit': 25 | ||
} | ||
}); | ||
var Shoutbox = {}; | ||
Shoutbox.config = {}; | ||
Shoutbox.config = { | ||
'get': function(key) { | ||
if (constants.config_keys.indexOf(key) !== -1) { | ||
return Meta.config['shoutbox:' + key] || constants.config_defaults[key]; | ||
} | ||
} | ||
}; | ||
Shoutbox.init = { | ||
"setup": function() { | ||
var dbkeys = constants.config_keys.map(function (key) { | ||
return "shoutbox:" + key; | ||
}); | ||
db.getObjectFields('config', dbkeys, function(err, values) { | ||
dbkeys.forEach(function(dbkey) { | ||
var realkey = dbkey.split(":")[1]; | ||
Shoutbox.config[realkey] = values[dbkey] || constants.config_defaults[realkey]; | ||
}); | ||
}); | ||
}, | ||
"load": function() { | ||
ModulesSockets.shoutbox = Shoutbox.sockets; | ||
}, | ||
"global": { | ||
addNavigation: function(custom_header, callback) { | ||
if (Shoutbox.config.headerlink === '1') { | ||
custom_header.navigation.push({ | ||
"class": "", | ||
"iconClass": "fa fa-fw fa-bullhorn", | ||
"route": constants.global.route, | ||
"text": constants.name | ||
}); | ||
} | ||
return custom_header; | ||
}, | ||
addRoute: function(custom_routes, callback) { | ||
function getBaseTemplate(next) { | ||
fs.readFile(path.resolve(__dirname, '../../public/templates/home.tpl'), function (err, template) { | ||
next(err, template); | ||
}); | ||
} | ||
"load": function() { | ||
ModulesSockets.shoutbox = Shoutbox.sockets; | ||
}, | ||
"global": { | ||
addNavigation: function(custom_header, callback) { | ||
if (Shoutbox.config.get('headerlink') === '1') { | ||
custom_header.navigation.push({ | ||
"class": "", | ||
"iconClass": "fa fa-fw fa-bullhorn", | ||
"route": constants.global.route, | ||
"text": constants.name | ||
}); | ||
} | ||
return custom_header; | ||
}, | ||
addRoute: function(custom_routes, callback) { | ||
function getBaseTemplate(next) { | ||
fs.readFile(path.resolve(__dirname, '../../public/templates/home.tpl'), function (err, template) { | ||
next(err, template); | ||
}); | ||
} | ||
function getPartial(next) { | ||
fs.readFile(path.resolve(__dirname, './partials/shoutbox.tpl'), function (err, template) { | ||
next(err, template); | ||
}); | ||
} | ||
function getPartial(next) { | ||
fs.readFile(path.resolve(__dirname, './partials/shoutbox.tpl'), function (err, template) { | ||
next(err, template); | ||
}); | ||
} | ||
async.parallel([getBaseTemplate, getPartial], function(err, results) { | ||
var template = results[0], | ||
partial = results[1]; | ||
async.parallel([getBaseTemplate, getPartial], function(err, results) { | ||
var template = results[0], | ||
partial = results[1]; | ||
// todo: this line should become a templates.js method, ie. templates.replaceBLock(blockname, partial); | ||
template = template.toString().replace(/<div class="row home"/g, partial + "$&"); | ||
// todo: this line should become a templates.js method, ie. templates.replaceBLock(blockname, partial); | ||
if (Shoutbox.config.get('pageposition') === "top") { | ||
template = template.toString().replace(/<div class="row home"/g, partial + "$&"); | ||
} else if (Shoutbox.config.get('pageposition') === "bottom") { | ||
template = template.toString().replace(/<div class="row footer-stats"/g, partial + "$&"); | ||
} else { | ||
template = template; | ||
} | ||
custom_routes.templates.push({ | ||
"template": "home.tpl", | ||
"content": template | ||
}); | ||
fs.readFile(path.resolve(__dirname, './partials/shoutbox.tpl'), function (err, tpl) { | ||
custom_routes.routes.push({ | ||
route: constants.global.route, | ||
method: "get", | ||
options: function(req, res, callback) { | ||
callback({ | ||
req: req, | ||
res: res, | ||
route: constants.global.route, | ||
name: constants.name, | ||
content: tpl | ||
}); | ||
} | ||
}); | ||
custom_routes.templates.push({ | ||
"template": "home.tpl", | ||
"content": template | ||
}); | ||
callback(null, custom_routes); | ||
}); | ||
}); | ||
fs.readFile(path.resolve(__dirname, './partials/shoutbox.tpl'), function (err, tpl) { | ||
custom_routes.routes.push({ | ||
route: constants.global.route, | ||
method: "get", | ||
options: function(req, res, callback) { | ||
callback({ | ||
req: req, | ||
res: res, | ||
content: tpl | ||
}); | ||
} | ||
}); | ||
callback(null, custom_routes); | ||
}); | ||
}); | ||
}, | ||
addScripts: function(scripts, callback) { | ||
return scripts.concat([ | ||
'plugins/nodebb-plugin-shoutbox/js/main.js' | ||
]); | ||
} | ||
}, | ||
"admin": { | ||
addNavigation: function(custom_header, callback) { | ||
custom_header.plugins.push({ | ||
"route": constants.admin.route, | ||
"icon": constants.admin.icon, | ||
"name": constants.name | ||
}); | ||
return custom_header; | ||
}, | ||
addRoute: function(custom_routes, callback) { | ||
fs.readFile(path.join(__dirname, './partials/admin.tpl'), function(err, tpl) { | ||
custom_routes.routes.push({ | ||
route: constants.admin.route, | ||
method: "get", | ||
options: function(req, res, callback) { | ||
callback({ | ||
req: req, | ||
res: res, | ||
route: constants.admin.route, | ||
name: constants.name, | ||
content: tpl | ||
}); | ||
} | ||
}); | ||
}, | ||
addScripts: function(scripts, callback) { | ||
return scripts.concat([ | ||
'plugins/nodebb-plugin-shoutbox/js/main.js' | ||
]); | ||
} | ||
}, | ||
"admin": { | ||
addNavigation: function(custom_header, callback) { | ||
custom_header.plugins.push({ | ||
"route": constants.admin.route, | ||
"icon": constants.admin.icon, | ||
"name": constants.name | ||
}); | ||
callback(null, custom_routes); | ||
}); | ||
} | ||
} | ||
return custom_header; | ||
}, | ||
addRoute: function(custom_routes, callback) { | ||
fs.readFile(path.join(__dirname, './partials/admin.tpl'), function(err, tpl) { | ||
custom_routes.routes.push({ | ||
route: constants.admin.route, | ||
method: "get", | ||
options: function(req, res, callback) { | ||
callback({ | ||
req: req, | ||
res: res, | ||
route: constants.admin.route, | ||
name: constants.name, | ||
content: tpl | ||
}); | ||
} | ||
}); | ||
callback(null, custom_routes); | ||
}); | ||
} | ||
} | ||
} | ||
Shoutbox.init.setup(); | ||
Shoutbox.sockets = { | ||
"get": function(socket, data, callback) { | ||
Shoutbox.backend.getShouts(function(err, messages) { | ||
if (err) | ||
return callback(null, []); | ||
"get": function(socket, data, callback) { | ||
var start, end; | ||
if (data && (data.start && data.end)) { | ||
start = parseInt(data.start, 10); | ||
end = parseInt(data.end, 10); | ||
} else { | ||
start = -(parseInt(Shoutbox.config.get('shoutlimit'), 10) - 1); | ||
end = -1; | ||
} | ||
Shoutbox.backend.getShouts(start, end, function(err, messages) { | ||
if (err) | ||
return callback(null, []); | ||
callback(null, messages); | ||
}); | ||
}, | ||
"send": function(socket, data, callback) { | ||
if (socket.uid === 0) { | ||
return; | ||
} | ||
callback(null, messages); | ||
}); | ||
}, | ||
"send": function(socket, data, callback) { | ||
if (socket.uid === 0) { | ||
return; | ||
} | ||
var msg = S(data.message).stripTags().s; | ||
User.getUserField(socket.uid, 'username', function(err, username) { | ||
if(err) { | ||
return; | ||
} | ||
var msg = S(data.message).stripTags().s; | ||
User.getUserField(socket.uid, 'username', function(err, username) { | ||
if(err) { | ||
return; | ||
} | ||
Shoutbox.backend.parse(socket.uid, username, msg, function(err, parsed) { | ||
Shoutbox.backend.addShout(socket.uid, msg, function(err, message) { | ||
SocketIndex.server.sockets.in('global').emit('event:shoutbox.receive', { | ||
fromuid: message.fromuid, | ||
username: username, | ||
content: parsed, | ||
sid: message.sid, | ||
timestamp: message.timestamp | ||
}); | ||
}); | ||
}); | ||
}); | ||
}, | ||
"remove": function(socket, data, callback) { | ||
db.getObjectField('shout:' + data.sid, 'fromuid', function(err, uid) { | ||
if (err) { | ||
return callback("Unknown error", false); | ||
} | ||
if (uid === socket.uid) { | ||
Shoutbox.backend.markRemoved(data.sid, function(err, result) { | ||
if (err) { | ||
return callback("Unknown error", false); | ||
} | ||
return callback(null, true); | ||
}); | ||
} else { | ||
return callback("Shout does not belong to you", false); | ||
} | ||
}); | ||
}, | ||
"get_users": function(socket, data, callback){ | ||
try { | ||
var users = []; | ||
for(var i in socket.userSockets) { | ||
if (socket.userSockets.hasOwnProperty((i))) { | ||
users.push(i); | ||
} | ||
} | ||
User.getMultipleUserFields(users, ['username'], function(err, usersData) { | ||
try { | ||
if(err) { | ||
return callback(null, []); | ||
} | ||
return callback(null, usersData); | ||
} catch (e) { | ||
winston.error("Someone did a no-no!: " + e.message); | ||
} | ||
}); | ||
} catch (e) { | ||
winston.error("Someone did a no-no!: " + e.message); | ||
} | ||
} | ||
Shoutbox.backend.parse(socket.uid, username, msg, function(err, parsed) { | ||
Shoutbox.backend.addShout(socket.uid, msg, function(err, message) { | ||
SocketIndex.server.sockets.in('global').emit('event:shoutbox.receive', { | ||
fromuid: message.fromuid, | ||
username: username, | ||
content: parsed, | ||
sid: message.sid, | ||
timestamp: message.timestamp | ||
}); | ||
}); | ||
}); | ||
}); | ||
}, | ||
"remove": function(socket, data, callback) { | ||
Shoutbox.backend.removeShout(data.sid, socket.uid, callback); | ||
}, | ||
"removeAll": function(socket, data, callback) { | ||
if (data !== null && data !== undefined) { | ||
if (typeof(data.which) === "string") { | ||
if (data.which === 'deleted') { | ||
return Shoutbox.backend.pruneDeleted(socket.uid, callback); | ||
} else if (data.which ==='all') { | ||
return Shoutbox.backend.removeAll(socket.uid, callback); | ||
} | ||
} | ||
} | ||
return callback(null, false); | ||
}, | ||
"get_users": function(socket, data, callback){ | ||
var users = Object.keys(SocketIndex.getConnectedClients()); | ||
User.getMultipleUserFields(users, ['username'], function(err, usersData) { | ||
if(err) { | ||
return callback(null, []); | ||
} | ||
return callback(null, usersData); | ||
}); | ||
}, | ||
"getConfig": function(socket, data, callback) { | ||
User.isAdministrator(socket.uid, function(err, isAdmin) { | ||
callback(null, { | ||
'maxShouts': parseInt(Shoutbox.config.get('shoutlimit'), 10), | ||
'isAdmin': isAdmin | ||
}); | ||
}); | ||
} | ||
} | ||
Shoutbox.backend = { | ||
"addShout": function(fromuid, content, callback) { | ||
db.incrObjectField('global', 'nextSid', function(err, sid) { | ||
if (err) { | ||
return callback(err, null); | ||
} | ||
"addShout": function(fromuid, content, callback) { | ||
db.incrObjectField('global', 'nextSid', function(err, sid) { | ||
if (err) { | ||
return callback(err, null); | ||
} | ||
var shout = { | ||
content: content, | ||
timestamp: Date.now(), | ||
fromuid: fromuid, | ||
deleted: '0' | ||
}; | ||
var shout = { | ||
content: content, | ||
timestamp: Date.now(), | ||
fromuid: fromuid, | ||
deleted: '0' | ||
}; | ||
db.setObject('shout:' + sid, shout); | ||
db.listAppend('shouts', sid); | ||
db.setObject('shout:' + sid, shout); | ||
db.listAppend('shouts', sid); | ||
Shoutbox.backend.updateShoutTime(fromuid); | ||
shout.sid = sid; | ||
callback(null, shout); | ||
}); | ||
}, | ||
"getShouts": function(callback) { | ||
db.getListRange('shouts', -((Meta.config.shoutsToDisplay || 25) - 1), -1, function(err, sids) { | ||
if (err) { | ||
return callback(err, null); | ||
} | ||
Shoutbox.backend.updateShoutTime(fromuid); | ||
shout.sid = sid; | ||
callback(null, shout); | ||
}); | ||
}, | ||
"getShouts": function(start, end, callback) { | ||
db.getListRange('shouts', start, end, function(err, sids) { | ||
if (err) { | ||
return callback(err, null); | ||
} | ||
if (!sids || !sids.length) { | ||
return callback(null, []); | ||
} | ||
if (!sids || !sids.length) { | ||
return callback(null, []); | ||
} | ||
var messages = []; | ||
var messages = []; | ||
function getShout(sid, next) { | ||
db.getObject('shout:' + sid, function(err, message) { | ||
if (err) { | ||
return next(err); | ||
} | ||
if (message.deleted === '1') { | ||
return next(null); | ||
} | ||
User.getUserField(message.fromuid, 'username', function(err, username) { | ||
Shoutbox.backend.parse(message.fromuid, username, message.content, function(err, parsed) { | ||
message.content = parsed; | ||
message.sid = sid; | ||
messages.push(message); | ||
next(null); | ||
}); | ||
}); | ||
}); | ||
} | ||
function getShout(sid, next) { | ||
db.getObject('shout:' + sid, function(err, message) { | ||
if (err) { | ||
return next(err); | ||
} | ||
if (message.deleted === '1') { | ||
return next(null); | ||
} | ||
User.getUserField(message.fromuid, 'username', function(err, username) { | ||
Shoutbox.backend.parse(message.fromuid, username, message.content, function(err, parsed) { | ||
message.content = parsed; | ||
message.sid = sid; | ||
messages.push(message); | ||
next(null); | ||
}); | ||
}); | ||
}); | ||
} | ||
async.eachSeries(sids, getShout, function(err) { | ||
if (err) { | ||
return callback(err, null); | ||
} | ||
callback(null, messages); | ||
}); | ||
}); | ||
}, | ||
"parse": function (uid, username, message, callback) { | ||
Plugins.fireHook('filter:post.parse', message, function(err, parsed) { | ||
User.isAdministrator(uid, function(err, isAdmin) { | ||
if (isAdmin) { | ||
username = "<span class='shoutbox-user-admin'>" + username + "</span>: "; | ||
//putTogether(username, message); | ||
} else { | ||
username = "<span class='shoutbox-user'>" + username + "</span>: "; | ||
} | ||
//var result = parsed.replace("<p>", "<p>" + username + ": "); | ||
var result = username + parsed; | ||
callback(null, result); | ||
}); | ||
}); | ||
}, | ||
"markRemoved": function (sid, callback) { | ||
db.setObjectField('shout:' + sid, 'deleted', '1', function (err, result) { | ||
callback(err, result); | ||
}); | ||
}, | ||
"updateShoutTime": function(uid, callback) { | ||
db.sortedSetAdd('uid:' + uid + ':shouts', Date.now(), 0, function(err) { | ||
if (callback) { | ||
callback(err); | ||
} | ||
}); | ||
} | ||
async.eachSeries(sids, getShout, function(err) { | ||
if (err) { | ||
return callback(err, null); | ||
} | ||
callback(null, messages); | ||
}); | ||
}); | ||
}, | ||
"parse": function (uid, username, message, callback) { | ||
Plugins.fireHook('filter:post.parse', message, function(err, parsed) { | ||
User.isAdministrator(uid, function(err, isAdmin) { | ||
if (isAdmin) { | ||
username = "<span class='shoutbox-user-admin'>" + username + "</span>: "; | ||
//putTogether(username, message); | ||
} else { | ||
username = "<span class='shoutbox-user'>" + username + "</span>: "; | ||
} | ||
//var result = parsed.replace("<p>", "<p>" + username + ": "); | ||
var result = username + parsed; | ||
callback(null, result); | ||
}); | ||
}); | ||
}, | ||
"removeShout": function(sid, uid, callback) { | ||
User.isAdministrator(uid, function(err, isAdmin) { | ||
db.getObjectField('shout:' + sid, 'fromuid', function(err, fromuid) { | ||
if (err) { | ||
return callback("Unknown error", false); | ||
} | ||
if (fromuid === uid || isAdmin) { | ||
db.setObjectField('shout:' + sid, 'deleted', '1', function (err, result) { | ||
if (err) { | ||
return callback("Unknown error", false); | ||
} | ||
return callback(null, true); | ||
}); | ||
} else { | ||
return callback("Shout does not belong to you", false); | ||
} | ||
}); | ||
}); | ||
}, | ||
"pruneDeleted": function(uid, callback) { | ||
User.isAdministrator(uid, function(err, isAdmin) { | ||
if (isAdmin === true) { | ||
db.getListRange('shouts', 0, -1, function(err, sids) { | ||
if (err || !sids || !sids.length) { | ||
return callback(err, false); | ||
} | ||
var removedSids = []; | ||
function deleteShout(sid, next) { | ||
db.getObjectField('shout:' + sid, 'deleted', function(err, isDeleted) { | ||
if (isDeleted === '1') { | ||
db.delete('shout:' + sid); | ||
db.listRemoveAll('shouts', sid); | ||
next() | ||
} | ||
next(null); | ||
}); | ||
} | ||
async.eachSeries(sids, deleteShout, function(err) { | ||
if (err) { | ||
return callback(err, false); | ||
} | ||
return callback(null, true); | ||
}); | ||
}); | ||
} else { | ||
return callback("Not allowed", false); | ||
} | ||
}); | ||
}, | ||
"removeAll": function(uid, callback) { | ||
User.isAdministrator(uid, function(err, isAdmin) { | ||
if (isAdmin === true) { | ||
db.getListRange('shouts', 0, -1, function(err, sids) { | ||
if (err || !sids || !sids.length) { | ||
return callback(err, false); | ||
} | ||
function deleteShout(sid, next) { | ||
db.delete('shout:' + sid); | ||
db.listRemoveAll('shouts', sid); | ||
next(); | ||
} | ||
async.eachSeries(sids, deleteShout, function(err) { | ||
db.setObjectField('global', 'nextSid', 0, function(err, result) { | ||
if (err) { | ||
return callback(err, null); | ||
} | ||
return callback(null, true); | ||
}); | ||
}); | ||
}); | ||
} else { | ||
return callback("Not allowed", false); | ||
} | ||
}); | ||
}, | ||
"updateShoutTime": function(uid, callback) { | ||
db.sortedSetAdd('uid:' + uid + ':shouts', Date.now(), 0, function(err) { | ||
if (callback) { | ||
callback(err); | ||
} | ||
}); | ||
} | ||
} | ||
module.exports = Shoutbox; |
{ | ||
"name": "nodebb-plugin-shoutbox", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "NodeBB Shoutbox Plugin", | ||
@@ -5,0 +5,0 @@ "main": "library.js", |
@@ -8,13 +8,13 @@ { | ||
"hooks": [ | ||
{ "hook": "filter:admin.header.build", "method": "init.admin.addNavigation", "callbacked": false }, | ||
{ "hook": "filter:admin.create_routes", "method": "init.admin.addRoute", "callbacked": true }, | ||
{ "hook": "filter:header.build", "method": "init.global.addNavigation", "callbacked": false }, | ||
{ "hook": "filter:server.create_routes", "method": "init.global.addRoute", "callbacked": true }, | ||
{ "hook": "filter:scripts.get", "method": "init.global.addScripts", "callbacked": false }, | ||
{ "hook": "action:app.load", "method": "init.load", "callbacked": false } | ||
{ "hook": "filter:admin.header.build", "method": "init.admin.addNavigation", "callbacked": false }, | ||
{ "hook": "filter:admin.create_routes", "method": "init.admin.addRoute", "callbacked": true }, | ||
{ "hook": "filter:header.build", "method": "init.global.addNavigation", "callbacked": false }, | ||
{ "hook": "filter:server.create_routes", "method": "init.global.addRoute", "callbacked": true }, | ||
{ "hook": "filter:scripts.get", "method": "init.global.addScripts", "callbacked": false }, | ||
{ "hook": "action:app.load", "method": "init.load", "callbacked": false } | ||
], | ||
"staticDir": "./public", | ||
"css": [ | ||
"css/style.css" | ||
] | ||
"staticDir": "./public", | ||
"css": [ | ||
"css/style.css" | ||
] | ||
} |
$(document).ready(function() { | ||
if (document.location.pathname === "/shoutbox") { | ||
requirejs([ | ||
'plugins/nodebb-plugin-shoutbox/js/shoutbox.js' | ||
], function(shoutBox) { | ||
shoutBox.base.init(); | ||
shoutBox.base.showUserPanel(); | ||
}); | ||
} | ||
if (document.location.pathname === "/shoutbox") { | ||
requirejs([ | ||
'plugins/nodebb-plugin-shoutbox/js/shoutbox.js' | ||
], function(shoutBox) { | ||
shoutBox.base.init(); | ||
shoutBox.base.showUserPanel(); | ||
}); | ||
} | ||
$(document).bind('DOMNodeInserted', function(event) { | ||
// todo improve this | ||
if (event.target.className == 'row shoutbox-row') { | ||
requirejs([ | ||
'plugins/nodebb-plugin-shoutbox/js/shoutbox.js' | ||
], function(shoutBox) { | ||
shoutBox.base.init(); | ||
}); | ||
} | ||
}); | ||
$(document).bind('DOMNodeInserted', function(event) { | ||
// todo improve this | ||
if (event.target.className == 'row shoutbox-row') { | ||
requirejs([ | ||
'plugins/nodebb-plugin-shoutbox/js/shoutbox.js' | ||
], function(shoutBox) { | ||
shoutBox.base.init(); | ||
}); | ||
} | ||
}); | ||
}); |
define(['string'], function(S) { | ||
var box = {}, | ||
module = {}; | ||
var box = {}, | ||
module = {}; | ||
box.vars = { | ||
"loaded": false, | ||
"userCheckIntervalId": 0, | ||
"sockets": { | ||
"get": "modules.shoutbox.get", | ||
"send": "modules.shoutbox.send", | ||
"remove" : "modules.shoutbox.remove", | ||
"get_users": "modules.shoutbox.get_users", | ||
"receive": "event:shoutbox.receive" | ||
}, | ||
"titleAlert": "[ %u ] - new shout!", | ||
"anonMessage": "You must be logged in to view the shoutbox!" | ||
}; | ||
box.vars = { | ||
"config": { | ||
"maxShouts": 0 | ||
}, | ||
"loaded": false, | ||
"userCheckIntervalId": 0, | ||
"lastSid": 0, | ||
"sockets": { | ||
"get": "modules.shoutbox.get", | ||
"send": "modules.shoutbox.send", | ||
"remove" : "modules.shoutbox.remove", | ||
"get_users": "modules.shoutbox.get_users", | ||
"receive": "event:shoutbox.receive" | ||
}, | ||
"titleAlert": "[ %u ] - new shout!", | ||
"anonMessage": "You must be logged in to view the shoutbox!" | ||
}; | ||
module.base = { | ||
"init": function(callback) { | ||
box.utils.checkAnon(function(isAnon) { | ||
var shoutBox = module.base.getShoutPanel(); | ||
if (isAnon) { | ||
box.utils.hideInputs(); | ||
box.utils.showAnonMessage(shoutBox); | ||
} else { | ||
box.utils.registerHandlers(shoutBox); | ||
box.base.getShouts(shoutBox); | ||
} | ||
box.vars.loaded = true; | ||
if (callback) { | ||
callback(); | ||
} | ||
}); | ||
}, | ||
module.base = { | ||
"init": function(callback) { | ||
box.utils.checkAnon(function(isAnon) { | ||
var shoutBox = module.base.getShoutPanel(); | ||
if (isAnon) { | ||
box.utils.hideInputs(); | ||
box.utils.showAnonMessage(shoutBox); | ||
} else { | ||
box.utils.getConfig(); | ||
box.utils.registerHandlers(shoutBox); | ||
box.base.getShouts(shoutBox); | ||
} | ||
box.vars.loaded = true; | ||
if (callback) { | ||
callback(); | ||
} | ||
}); | ||
}, | ||
"showUserPanel": function() { | ||
module.base.getUsersPanel().parent().removeClass('hidden'); | ||
box.utils.startUserPoll(); | ||
box.base.updateUsers(); | ||
}, | ||
"showUserPanel": function() { | ||
module.base.getUsersPanel().parent().removeClass('hidden'); | ||
box.utils.startUserPoll(); | ||
box.base.updateUsers(); | ||
}, | ||
"hasLoaded": function() { | ||
return box.vars.loaded; | ||
}, | ||
"hasLoaded": function() { | ||
return box.vars.loaded; | ||
}, | ||
"getShoutPanel": function() { | ||
return $('#shoutbox'); | ||
}, | ||
"getShoutPanel": function() { | ||
return $('#shoutbox'); | ||
}, | ||
"getUsersPanel": function() { | ||
return $('#shoutbox-users'); | ||
} | ||
}; | ||
"getUsersPanel": function() { | ||
return $('#shoutbox-users'); | ||
} | ||
}; | ||
module.box = { | ||
"addShout": function(shoutBox, shout) { | ||
var shoutContent = shoutBox.find('#shoutbox-content'); | ||
shoutContent.append(box.base.parseShout(shout)); | ||
box.base.scrollToBottom(shoutContent); | ||
} | ||
}; | ||
module.box = { | ||
"addShout": function(shoutBox, shout) { | ||
var shoutContent = shoutBox.find('#shoutbox-content'); | ||
shoutContent.append(box.base.parseShout(shout)); | ||
box.base.scrollToBottom(shoutContent); | ||
box.vars.lastSid = shout.sid; | ||
} | ||
}; | ||
box.base = { | ||
"getShouts": function(shoutBox) { | ||
console.log("Getting"); | ||
socket.emit(box.vars.sockets.get, function(err, shouts) { | ||
for(var i = 0; i<shouts.length; ++i) { | ||
module.box.addShout(shoutBox, shouts[i]); | ||
} | ||
}); | ||
}, | ||
"parseShout": function(shout) { | ||
var date = new Date(parseInt(shout.timestamp, 10)); | ||
var prefix = '<span class="shoutbox-timestamp">' + date.toLocaleTimeString() + '</span> '; | ||
var options = ''; | ||
if (shout.fromuid === app.uid) { | ||
options = '<button type="button" class="close pull-right" aria-hidden="true">×</button>'; | ||
} | ||
return "<div id='shoutbox-shout-" + shout.sid + "'>" + options + S(prefix + shout.content).stripTags('p').s + "</div>"; | ||
}, | ||
"scrollToBottom": function(shoutContent) { | ||
if(shoutContent[0]) { | ||
shoutContent.scrollTop( | ||
shoutContent[0].scrollHeight - shoutContent.height() | ||
); | ||
} | ||
}, | ||
"updateUsers": function() { | ||
socket.emit(box.vars.sockets.get_users, {}, function(err, data) { | ||
var userCount = data.length; | ||
var usernames = data.map(function(i) { | ||
return (i.username === null ? 'Anonymous' : i.username); | ||
}); | ||
var userString = usernames.join("; "); | ||
module.base.getUsersPanel().find('.panel-body').text(userString); | ||
module.base.getUsersPanel().find('.panel-title').text('Users (' + userCount + ')'); | ||
}); | ||
} | ||
}; | ||
box.base = { | ||
"getShouts": function(shoutBox) { | ||
socket.emit(box.vars.sockets.get, function(err, shouts) { | ||
for(var i = 0; i<shouts.length; ++i) { | ||
module.box.addShout(shoutBox, shouts[i]); | ||
} | ||
}); | ||
}, | ||
"parseShout": function(shout) { | ||
var date = new Date(parseInt(shout.timestamp, 10)); | ||
var prefix = '<span class="shoutbox-timestamp">' + date.toLocaleTimeString() + '</span> '; | ||
var options = ''; | ||
if (shout.fromuid === app.uid || box.vars.config.isAdmin === true) { | ||
options = '<button type="button" class="close pull-right" aria-hidden="true">×</button>'; | ||
} | ||
return "<div id='shoutbox-shout-" + shout.sid + "'>" + options + S(prefix + shout.content).stripTags('p').s + "</div>"; | ||
}, | ||
"scrollToBottom": function(shoutContent) { | ||
if(shoutContent[0]) { | ||
shoutContent.scrollTop( | ||
shoutContent[0].scrollHeight - shoutContent.height() | ||
); | ||
} | ||
}, | ||
"updateUsers": function() { | ||
socket.emit(box.vars.sockets.get_users, {}, function(err, data) { | ||
var userCount = data.length; | ||
var usernames = data.map(function(i) { | ||
return (i.username === null ? 'Anonymous' : i.username); | ||
}); | ||
var userString = usernames.join("; "); | ||
module.base.getUsersPanel().find('.panel-body').text(userString); | ||
module.base.getUsersPanel().find('.panel-title').text('Users (' + userCount + ')'); | ||
}); | ||
} | ||
}; | ||
box.utils = { | ||
"checkAnon": function(callback) { | ||
if (app.uid === null) { | ||
return callback(true); | ||
} | ||
return callback(false); | ||
}, | ||
"showAnonMessage": function(shoutBox) { | ||
shoutBox.find('#shoutbox-content').html(box.vars.anonMessage); | ||
}, | ||
"startUserPoll": function() { | ||
if(box.vars.userCheckIntervalId === 0) { | ||
box.vars.userCheckIntervalId = setInterval(function() { | ||
box.base.updateUsers(); | ||
}, 10000); | ||
} | ||
}, | ||
"hideInputs": function() { | ||
$('#shoutbox').find('.btn-group, .input-group').hide(); | ||
}, | ||
"registerHandlers": function(shoutBox) { | ||
box.utils.addActionHandlers(shoutBox); | ||
box.utils.addSocketHandlers(); | ||
}, | ||
"addActionHandlers": function(shoutBox) { | ||
var actions = box.actions; | ||
for (var a in actions) { | ||
if (actions.hasOwnProperty(a)) { | ||
actions[a].register(shoutBox); | ||
} | ||
} | ||
}, | ||
"addSocketHandlers": function() { | ||
var sockets = box.sockets; | ||
for (var s in sockets) { | ||
if (sockets.hasOwnProperty(s)) { | ||
sockets[s].register(); | ||
} | ||
} | ||
} | ||
}; | ||
box.utils = { | ||
"checkAnon": function(callback) { | ||
if (app.uid === null) { | ||
return callback(true); | ||
} | ||
return callback(false); | ||
}, | ||
"showAnonMessage": function(shoutBox) { | ||
shoutBox.find('#shoutbox-content').html(box.vars.anonMessage); | ||
}, | ||
"getConfig": function() { | ||
socket.emit('modules.shoutbox.getConfig', function(err, config) { | ||
box.vars.config = config; | ||
}); | ||
}, | ||
"startUserPoll": function() { | ||
if(box.vars.userCheckIntervalId === 0) { | ||
box.vars.userCheckIntervalId = setInterval(function() { | ||
box.base.updateUsers(); | ||
}, 10000); | ||
} | ||
}, | ||
"hideInputs": function() { | ||
$('#shoutbox').find('.btn-group, .input-group').hide(); | ||
}, | ||
"registerHandlers": function(shoutBox) { | ||
box.utils.addActionHandlers(shoutBox); | ||
box.utils.addSocketHandlers(); | ||
}, | ||
"addActionHandlers": function(shoutBox) { | ||
var actions = box.actions; | ||
for (var a in actions) { | ||
if (actions.hasOwnProperty(a)) { | ||
actions[a].register(shoutBox); | ||
} | ||
} | ||
}, | ||
"addSocketHandlers": function() { | ||
var sockets = box.sockets; | ||
for (var s in sockets) { | ||
if (sockets.hasOwnProperty(s)) { | ||
sockets[s].register(); | ||
} | ||
} | ||
} | ||
}; | ||
box.actions = { | ||
"send": { | ||
"register": function(shoutBox) { | ||
var sendMessage = this.handle; | ||
shoutBox.find('#shoutbox-message-input').off('keypress'); | ||
shoutBox.find('#shoutbox-message-input').on('keypress', function(e) { | ||
if(e.which === 13 && !e.shiftKey) { | ||
sendMessage(shoutBox); | ||
} | ||
}); | ||
box.actions = { | ||
"send": { | ||
"register": function(shoutBox) { | ||
var sendMessage = this.handle; | ||
shoutBox.find('#shoutbox-message-input').off('keypress').on('keypress', function(e) { | ||
if(e.which === 13 && !e.shiftKey) { | ||
sendMessage(shoutBox); | ||
} | ||
}); | ||
shoutBox.find('#shoutbox-message-send-btn').off('click'); | ||
shoutBox.find('#shoutbox-message-send-btn').on('click', function(e){ | ||
sendMessage(shoutBox); | ||
return false; | ||
}); | ||
}, | ||
"handle": function(shoutBox) { | ||
var msg = S(shoutBox.find('#shoutbox-message-input').val()).stripTags().s; | ||
if(msg.length) { | ||
socket.emit(box.vars.sockets.send, {message:msg}); | ||
shoutBox.find('#shoutbox-message-input').val(''); | ||
} | ||
} | ||
}, | ||
"delete": { | ||
"register": function(shoutBox) { | ||
shoutBox.find('button.close').off('click'); | ||
shoutBox.on('click', 'button.close', this.handle); | ||
}, | ||
"handle": function(e) { | ||
var sid = e.currentTarget.parentNode.id.match(/\d+/), | ||
node = e.currentTarget.parentNode; | ||
socket.emit(box.vars.sockets.remove, {"sid": sid}, function (err, result) { | ||
if (result === true) { | ||
node.remove(); | ||
} else if (err) { | ||
app.alertError("Error deleting shout: " + err, 3000); | ||
} | ||
}); | ||
} | ||
}, | ||
"gist": { | ||
"register": function(shoutBox) { | ||
var show = this.handle.show, | ||
create = this.handle.create, | ||
gistModal = $('#create-gist-modal'); | ||
shoutBox.find('#create-gist-button').off('click'); | ||
shoutBox.find('#create-gist-button').on('click', function(e) { | ||
show(gistModal); | ||
}); | ||
gistModal.find('#create-gist-submit').off('click'); | ||
gistModal.find('#create-gist-submit').on('click', function(e) { | ||
create(gistModal.find('textarea').val(), gistModal); | ||
}); | ||
}, | ||
"handle": { | ||
"show": function(gistModal) { | ||
gistModal.modal('show'); | ||
}, | ||
"create": function(code, gistModal) { | ||
if (app.uid === null) { | ||
gistModal.modal('hide'); | ||
app.alertError('Only registered users can create Gists!', 3000); | ||
return; | ||
} | ||
var json = { | ||
"description": "Gist created from BitBangers shoutbox", | ||
"public": true, | ||
"files": { | ||
"file1.txt": { | ||
"content": code | ||
} | ||
} | ||
} | ||
$.post("https://api.github.com/gists", JSON.stringify(json), function(data) { | ||
gistModal.modal('hide'); | ||
var input = module.base.getShoutPanel().find('#shoutbox-message-input'); | ||
var link = data.html_url; | ||
if (input.val().length > 0) { | ||
link = " " + link; | ||
} | ||
input.val(input.val() + link); | ||
app.alertSuccess("Successfully created Gist!", 3000); | ||
gistModal.find('textarea').val(''); | ||
}).fail(function(data) { | ||
gistModal.modal('hide'); | ||
app.alertError("Error while creating Gist, try again later!", 3000); | ||
}); | ||
} | ||
} | ||
}, | ||
"archive": { | ||
"register": function(shoutBox) { | ||
shoutBox.find('#view-archive-button').off('click'); | ||
shoutBox.find('#view-archive-button').on('click', function(e) { | ||
app.alertError("Not implemented!", 3000); | ||
}); | ||
}, | ||
"handle": function() { | ||
shoutBox.find('#shoutbox-message-send-btn').off('click').on('click', function(e){ | ||
sendMessage(shoutBox); | ||
return false; | ||
}); | ||
}, | ||
"handle": function(shoutBox) { | ||
var msg = S(shoutBox.find('#shoutbox-message-input').val()).stripTags().s; | ||
if(msg.length) { | ||
socket.emit(box.vars.sockets.send, {message:msg}); | ||
shoutBox.find('#shoutbox-message-input').val(''); | ||
} | ||
} | ||
}, | ||
"delete": { | ||
"register": function(shoutBox) { | ||
shoutBox.off('click', 'button.close').on('click', 'button.close', this.handle); | ||
}, | ||
"handle": function(e) { | ||
var sid = e.currentTarget.parentNode.id.match(/\d+/), | ||
node = e.currentTarget.parentNode; | ||
socket.emit(box.vars.sockets.remove, {"sid": sid}, function (err, result) { | ||
if (result === true) { | ||
node.remove(); | ||
} else if (err) { | ||
app.alertError("Error deleting shout: " + err, 3000); | ||
} | ||
}); | ||
} | ||
}, | ||
"gist": { | ||
"register": function(shoutBox) { | ||
var show = this.handle.show, | ||
create = this.handle.create, | ||
gistModal = $('#shoutbox-modal-gist'); | ||
shoutBox.find('#shoutbox-button-create-gist').off('click').on('click', function(e) { | ||
show(gistModal); | ||
}); | ||
gistModal.find('#shoutbox-button-create-gist-submit').off('click').on('click', function(e) { | ||
create(gistModal.find('textarea').val(), gistModal); | ||
}); | ||
}, | ||
"handle": { | ||
"show": function(gistModal) { | ||
gistModal.modal('show'); | ||
}, | ||
"create": function(code, gistModal) { | ||
if (app.uid === null) { | ||
gistModal.modal('hide'); | ||
app.alertError('Only registered users can create Gists!', 3000); | ||
return; | ||
} | ||
var json = { | ||
"description": "Gist created from BitBangers shoutbox", | ||
"public": true, | ||
"files": { | ||
"file1.txt": { | ||
"content": code | ||
} | ||
} | ||
} | ||
$.post("https://api.github.com/gists", JSON.stringify(json), function(data) { | ||
gistModal.modal('hide'); | ||
var input = module.base.getShoutPanel().find('#shoutbox-message-input'); | ||
var link = data.html_url; | ||
if (input.val().length > 0) { | ||
link = " " + link; | ||
} | ||
input.val(input.val() + link); | ||
app.alertSuccess("Successfully created Gist!", 3000); | ||
gistModal.find('textarea').val(''); | ||
}).fail(function(data) { | ||
gistModal.modal('hide'); | ||
app.alertError("Error while creating Gist, try again later!", 3000); | ||
}); | ||
} | ||
} | ||
}, | ||
"archive": { | ||
"register": function(shoutBox) { | ||
var handle = this.handle, | ||
show = this.handle.show, | ||
prev = this.handle.prev, | ||
next = this.handle.next, | ||
archiveModal = $('#shoutbox-archive-modal'); | ||
shoutBox.find('#shoutbox-button-archive').off('click').on('click', function(e) { | ||
show(archiveModal, handle); | ||
}); | ||
} | ||
} | ||
}; | ||
archiveModal.find('#shoutbox-button-archive-prev').off('click').on('click', function(e) { | ||
prev(archiveModal, handle); | ||
}); | ||
archiveModal.find('#shoutbox-button-archive-next').off('click').on('click', function(e) { | ||
next(archiveModal, handle); | ||
}); | ||
}, | ||
"handle": { | ||
"show": function(archiveModal, handle) { | ||
archiveModal.modal('show'); | ||
if (!archiveModal.data('start')) { | ||
archiveModal.data('start', (-(box.vars.config.maxShouts - 1)).toString()); | ||
archiveModal.data('end', "-1"); | ||
} | ||
handle.get(archiveModal, handle); | ||
}, | ||
"prev": function(archiveModal, handle) { | ||
var curStart = parseInt(archiveModal.data('start'), 10); | ||
var curEnd = parseInt(archiveModal.data('end')); | ||
box.sockets = { | ||
"receive": { | ||
"register": function() { | ||
if (socket.listeners(box.vars.sockets.receive).length === 0) { | ||
socket.on(box.vars.sockets.receive, this.handle); | ||
} | ||
}, | ||
"handle": function(data) { | ||
if (module.base.hasLoaded) { | ||
module.box.addShout(module.base.getShoutPanel(), data); | ||
app.alternatingTitle(box.vars.titleAlert.replace(/%u/g, data.username)); | ||
} | ||
} | ||
} | ||
} | ||
var newStart = curStart - box.vars.config.maxShouts; | ||
var newEnd = curEnd - box.vars.config.maxShouts; | ||
return module; | ||
if (Math.abs(newStart) < (parseInt(box.vars.lastSid, 10) + box.vars.config.maxShouts)) { | ||
archiveModal.data('start', newStart); | ||
archiveModal.data('end', newEnd); | ||
handle.get(archiveModal, handle); | ||
} | ||
}, | ||
"next": function(archiveModal, handle) { | ||
var curStart = parseInt(archiveModal.data('start'), 10); | ||
var curEnd = parseInt(archiveModal.data('end')); | ||
var newStart = curStart + box.vars.config.maxShouts; | ||
var newEnd = curEnd + box.vars.config.maxShouts; | ||
var startLimit = -(box.vars.config.maxShouts - 1); | ||
if (newStart <= startLimit && newEnd < 0) { | ||
archiveModal.data('start', newStart); | ||
archiveModal.data('end', newEnd); | ||
handle.get(archiveModal, handle); | ||
} | ||
}, | ||
"get": function(archiveModal, handle) { | ||
archiveModal.find('#shoutbox-archive-content').html(''); | ||
var curStart = archiveModal.data('start'); | ||
var curEnd = archiveModal.data('end'); | ||
var addShout = handle.addShout; | ||
socket.emit(box.vars.sockets.get, {'start': curStart, 'end': curEnd}, function(err, shouts) { | ||
for(var i = 0; i<shouts.length; ++i) { | ||
addShout(archiveModal, shouts[i]); | ||
} | ||
}); | ||
}, | ||
"addShout": function(archiveModal, shout) { | ||
var archiveContent = archiveModal.find('#shoutbox-archive-content'); | ||
archiveContent.append(box.base.parseShout(shout)); | ||
box.base.scrollToBottom(archiveContent); | ||
} | ||
} | ||
} | ||
}; | ||
box.sockets = { | ||
"receive": { | ||
"register": function() { | ||
if (socket.listeners(box.vars.sockets.receive).length === 0) { | ||
socket.on(box.vars.sockets.receive, this.handle); | ||
} | ||
}, | ||
"handle": function(data) { | ||
if (module.base.hasLoaded) { | ||
module.box.addShout(module.base.getShoutPanel(), data); | ||
app.alternatingTitle(box.vars.titleAlert.replace(/%u/g, data.username)); | ||
} | ||
} | ||
} | ||
} | ||
return module; | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
30325
11
760