nodebb-plugin-shoutbox
Advanced tools
Comparing version 0.0.2 to 0.0.3
100
library.js
@@ -10,2 +10,3 @@ var async = require('async'), | ||
winston = module.parent.require('winston'), | ||
SocketIndex = module.parent.require('./socket.io/index'), | ||
ModulesSockets = module.parent.require('./socket.io/modules'); | ||
@@ -143,72 +144,56 @@ | ||
Shoutbox.sockets = { | ||
"get": function(callback) { | ||
"get": function(socket, data, callback) { | ||
Shoutbox.backend.getShouts(function(err, messages) { | ||
try { | ||
if (err) | ||
return callback(null); | ||
if (err) | ||
return callback(null, []); | ||
callback(messages); | ||
} catch (e) { | ||
winston.error("Someone did a no-no!: " + e.message); | ||
} | ||
callback(null, messages); | ||
}); | ||
}, | ||
"send": function(data, sessionData) { | ||
try { | ||
if (sessionData.uid === 0) { | ||
"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(sessionData.uid, 'username', function(err, username) { | ||
if(err) { | ||
return; | ||
} | ||
Shoutbox.backend.parse(sessionData.uid, username, msg, function(parsed) { | ||
Shoutbox.backend.addShout(sessionData.uid, msg, function(err, message) { | ||
sessionData.server.sockets.in('global').emit('event:shoutbox.receive', { | ||
fromuid: sessionData.uid, | ||
username: username, | ||
content: parsed, | ||
timestamp: Date.now() | ||
}); | ||
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 | ||
}); | ||
}); | ||
}); | ||
} catch (e) { | ||
winston.error("Someone did a no-no!: " + e.message); | ||
} | ||
}); | ||
}, | ||
"remove": function(data, callback, sessionData) { | ||
"remove": function(socket, data, callback) { | ||
db.getObjectField('shout:' + data.sid, 'fromuid', function(err, uid) { | ||
try { | ||
if (err) { | ||
return callback("Unknown error", false); | ||
} | ||
if (uid === sessionData.uid) { | ||
Shoutbox.backend.markRemoved(data.sid, function(err, result) { | ||
try { | ||
if (err) { | ||
return callback("Unknown error", false); | ||
} | ||
return callback(null, true); | ||
} catch (e) { | ||
winston.error("Someone did a no-no!: " + e.message); | ||
} | ||
}); | ||
} else { | ||
return callback("Shout does not belong to you", false); | ||
} | ||
} catch (e) { | ||
winston.error("Someone did a no-no!: " + e.message); | ||
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(data, callback, sessionData){ | ||
"get_users": function(socket, data, callback){ | ||
try { | ||
var users = []; | ||
for(var i in sessionData.userSockets) { | ||
if (sessionData.userSockets.hasOwnProperty((i))) { | ||
for(var i in socket.userSockets) { | ||
if (socket.userSockets.hasOwnProperty((i))) { | ||
users.push(i); | ||
@@ -220,5 +205,5 @@ } | ||
if(err) { | ||
return callback([]); | ||
return callback(null, []); | ||
} | ||
return callback(usersData); | ||
return callback(null, usersData); | ||
} catch (e) { | ||
@@ -252,2 +237,3 @@ winston.error("Someone did a no-no!: " + e.message); | ||
Shoutbox.backend.updateShoutTime(fromuid); | ||
shout.sid = sid; | ||
callback(null, shout); | ||
@@ -277,3 +263,3 @@ }); | ||
User.getUserField(message.fromuid, 'username', function(err, username) { | ||
Shoutbox.backend.parse(message.fromuid, username, message.content, function(parsed) { | ||
Shoutbox.backend.parse(message.fromuid, username, message.content, function(err, parsed) { | ||
message.content = parsed; | ||
@@ -307,3 +293,3 @@ message.sid = sid; | ||
var result = username + parsed; | ||
callback(result); | ||
callback(null, result); | ||
}); | ||
@@ -310,0 +296,0 @@ }); |
{ | ||
"name": "nodebb-plugin-shoutbox", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "NodeBB Shoutbox Plugin", | ||
@@ -5,0 +5,0 @@ "main": "library.js", |
@@ -6,4 +6,4 @@ $(document).ready(function() { | ||
], function(shoutBox) { | ||
shoutBox.init(); | ||
shoutBox.showUsers(); | ||
shoutBox.base.init(); | ||
shoutBox.base.showUserPanel(); | ||
}); | ||
@@ -18,3 +18,3 @@ } | ||
], function(shoutBox) { | ||
shoutBox.init(); | ||
shoutBox.base.init(); | ||
}); | ||
@@ -21,0 +21,0 @@ } |
define(['string'], function(S) { | ||
var userCheckIntervalId = 0, | ||
loaded = false, | ||
socketEntries = { | ||
"get": "api:modules.shoutbox.get", | ||
"send": "api:modules.shoutbox.send", | ||
"remove" : "api:modules.shoutbox.remove", | ||
"get_users": "api:modules.shoutbox.get_users", | ||
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!" | ||
}; | ||
module = {}; | ||
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.hasLoaded = function() { | ||
return loaded; | ||
} | ||
"showUserPanel": function() { | ||
module.base.getUsersPanel().parent().removeClass('hidden'); | ||
box.utils.startUserPoll(); | ||
box.base.updateUsers(); | ||
}, | ||
module.getShoutPanel = function() { | ||
return $('#shoutbox'); | ||
} | ||
"hasLoaded": function() { | ||
return box.vars.loaded; | ||
}, | ||
module.getUsersPanel = function() { | ||
return $('#shoutbox-users'); | ||
} | ||
"getShoutPanel": function() { | ||
return $('#shoutbox'); | ||
}, | ||
module.init = function(callback) { | ||
var anon = checkForAnon(); | ||
if (!anon) { | ||
addSendHandler(module.getShoutPanel()); | ||
addButtonHandlers(module.getShoutPanel()); | ||
"getUsersPanel": function() { | ||
return $('#shoutbox-users'); | ||
} | ||
registerSocket(); | ||
getShouts(module.getShoutPanel()); | ||
}; | ||
loaded = true; | ||
if (callback) | ||
callback(); | ||
} | ||
module.appendShout = function(shoutBox, shout) { | ||
var shoutContent = shoutBox.find('#shoutbox-content'); | ||
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>'; | ||
module.box = { | ||
"addShout": function(shoutBox, shout) { | ||
var shoutContent = shoutBox.find('#shoutbox-content'); | ||
shoutContent.append(box.base.parseShout(shout)); | ||
box.base.scrollToBottom(shoutContent); | ||
} | ||
var shoutHTML = "<div id='shoutbox-shout-" + shout.sid + "'>" + options + S(prefix + shout.content).stripTags('p').s + "</div>"; | ||
}; | ||
shoutContent.append(shoutHTML); | ||
scrollToBottom(shoutContent); | ||
} | ||
module.showUsers = function() { | ||
$('#shoutbox-users').parent().removeClass('hidden'); | ||
checkUsers(); | ||
checkOnlineUsers(); | ||
} | ||
function checkForAnon() { | ||
if (app.uid === null) { | ||
$('#shoutbox .input-group').hide(); | ||
$('#shoutbox .btn-group').hide(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
function scrollToBottom(shoutContent) { | ||
if (module.getShoutPanel().length > 0) { | ||
shoutContent.scrollTop( | ||
shoutContent[0].scrollHeight - shoutContent.height() | ||
); | ||
} | ||
} | ||
function getShouts(shoutBox) { | ||
socket.emit(socketEntries.get, function(shouts) { | ||
for(var i = 0; i<shouts.length; ++i) { | ||
module.appendShout(shoutBox, shouts[i]); | ||
} | ||
}); | ||
} | ||
function registerSocket() { | ||
if (socket.listeners(socketEntries.receive).length === 0) { | ||
socket.on(socketEntries.receive, function(data) { | ||
if (module.hasLoaded) { | ||
module.appendShout(module.getShoutPanel(), data); | ||
app.alternatingTitle('[' + data.username + '] - new shout!'); | ||
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 + ')'); | ||
}); | ||
} | ||
} | ||
}; | ||
function addSendHandler(shoutBox) { | ||
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.utils = { | ||
"checkAnon": function(callback) { | ||
if (app.uid === null) { | ||
return callback(true); | ||
} | ||
}); | ||
shoutBox.find('#shoutbox-message-send-btn').off('click'); | ||
shoutBox.find('#shoutbox-message-send-btn').on('click', function(e){ | ||
sendMessage(shoutBox); | ||
return false; | ||
}); | ||
} | ||
function removeShout(sid, node) { | ||
socket.emit(socketEntries.remove, {"sid": sid}, function (err, result) { | ||
if (result === true) { | ||
node.remove(); | ||
} else if (err) { | ||
app.alertError("Error deleting shout: " + err, 3000); | ||
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(); | ||
} | ||
} | ||
} | ||
}; | ||
function addButtonHandlers(shoutBox) { | ||
//START DELETE BUTTON | ||
shoutBox.find('button.close').off('click'); | ||
shoutBox.on('click', 'button.close', function(e) { | ||
removeShout(e.currentTarget.parentNode.id.match(/\d+/), e.currentTarget.parentNode); | ||
}); | ||
//END DELETE BUTTON | ||
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); | ||
} | ||
}); | ||
//START GIST BUTTON | ||
var gistModal = $('#create-gist-modal'); | ||
shoutBox.find('#create-gist-button').off('click'); | ||
shoutBox.find('#create-gist-button').on('click', function(e) { | ||
gistModal.modal('show'); | ||
}); | ||
gistModal.find('#create-gist-submit').off('click'); | ||
gistModal.find('#create-gist-submit').on('click', function(e) { | ||
createGist(gistModal.find('textarea').val(), gistModal); | ||
}); | ||
//END GIST BUTTON | ||
shoutBox.find('#view-archive-button').off('click'); | ||
shoutBox.find('#view-archive-button').on('click', function(e) { | ||
app.alertError("Not implemented!", 3000); | ||
}); | ||
//START ARCHIVE BUTTON | ||
//END ARCHIVE BUTTON | ||
} | ||
function createGist(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 | ||
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(''); | ||
} | ||
} | ||
} | ||
$.post("https://api.github.com/gists", JSON.stringify(json), function(data) { | ||
gistModal.modal('hide'); | ||
var input = $('.shoutbox').find('.shoutbox-message-input'); | ||
var link = data.html_url; | ||
if (input.val().length > 0) { | ||
link = " " + link; | ||
}, | ||
"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); | ||
} | ||
}); | ||
} | ||
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); | ||
}); | ||
} | ||
}, | ||
"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() { | ||
function sendMessage(shoutBox) { | ||
var msg = S(shoutBox.find('#shoutbox-message-input').val()).stripTags().s; | ||
if(msg.length) { | ||
socket.emit(socketEntries.send, {message:msg}); | ||
shoutBox.find('#shoutbox-message-input').val(''); | ||
} | ||
} | ||
} | ||
}; | ||
function checkOnlineUsers() { | ||
if(userCheckIntervalId === 0) { | ||
userCheckIntervalId = setInterval(function() { | ||
checkUsers(); | ||
}, 10000); | ||
} | ||
} | ||
function checkUsers() { | ||
socket.emit(socketEntries.get_users, {}, function(data) { | ||
var userCount = data.length; | ||
var usernames = []; | ||
for(var i = 0; i < userCount; i++) { | ||
var uname = data[i].username; | ||
if (uname === null ) { | ||
uname = 'Anonymous'; | ||
box.sockets = { | ||
"receive": { | ||
"register": function() { | ||
if (socket.listeners(box.vars.sockets.receive).length === 0) { | ||
socket.on(box.vars.sockets.receive, this.handle); | ||
} | ||
usernames.push(uname); | ||
}, | ||
"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 userString = usernames.join("; "); | ||
module.getUsersPanel().find('.panel-body').text(userString); | ||
module.getUsersPanel().find('.panel-title').text('Users (' + userCount + ')'); | ||
}); | ||
} | ||
} | ||
@@ -213,0 +264,0 @@ |
27190
592