nodebb-plugin-shoutbox
Advanced tools
Comparing version 0.0.1 to 0.0.2
144
library.js
@@ -9,2 +9,3 @@ var async = require('async'), | ||
db = module.parent.require('./database'), | ||
winston = module.parent.require('winston'), | ||
ModulesSockets = module.parent.require('./socket.io/modules'); | ||
@@ -19,3 +20,7 @@ | ||
'route': '/plugins/shoutbox', | ||
'icon': 'fa-edit' | ||
'icon': 'fa-bullhorn' | ||
}, | ||
'config_keys': ['headerlink'], | ||
'config_defaults': { | ||
'headerlink': '0' | ||
} | ||
@@ -28,2 +33,13 @@ }); | ||
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() { | ||
@@ -34,8 +50,10 @@ ModulesSockets.shoutbox = Shoutbox.sockets; | ||
addNavigation: function(custom_header, callback) { | ||
custom_header.navigation.push({ | ||
"class": "", | ||
"route": constants.global.route, | ||
"text": constants.name | ||
}); | ||
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; | ||
@@ -68,3 +86,3 @@ }, | ||
fs.readFile(path.resolve(__dirname, './partials/page.tpl'), function (err, tpl) { | ||
fs.readFile(path.resolve(__dirname, './partials/shoutbox.tpl'), function (err, tpl) { | ||
custom_routes.routes.push({ | ||
@@ -127,48 +145,90 @@ route: constants.global.route, | ||
} | ||
Shoutbox.init.setup(); | ||
Shoutbox.sockets = { | ||
"get": function(callback) { | ||
Shoutbox.backend.getShouts(function(err, messages) { | ||
if (err) | ||
return callback(null); | ||
try { | ||
if (err) | ||
return callback(null); | ||
callback(messages); | ||
callback(messages); | ||
} catch (e) { | ||
winston.error("Someone did a no-no!: " + e.message); | ||
} | ||
}); | ||
}, | ||
"send": function(data, sessionData) { | ||
if (sessionData.uid === 0) { | ||
return; | ||
} | ||
var msg = S(data.message).stripTags().s; | ||
User.getUserField(sessionData.uid, 'username', function(err, username) { | ||
if(err) { | ||
try { | ||
if (sessionData.uid === 0) { | ||
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, | ||
message: parsed, | ||
timestamp: Date.now() | ||
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() | ||
}); | ||
}); | ||
}); | ||
}); | ||
} catch (e) { | ||
winston.error("Someone did a no-no!: " + e.message); | ||
} | ||
}, | ||
"remove": function(data, callback, sessionData) { | ||
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); | ||
} | ||
}); | ||
}, | ||
"get_users": function(data, callback, sessionData){ | ||
var users = []; | ||
for(var i in sessionData.userSockets) { | ||
if (sessionData.userSockets.hasOwnProperty((i))) { | ||
users.push(i); | ||
try { | ||
var users = []; | ||
for(var i in sessionData.userSockets) { | ||
if (sessionData.userSockets.hasOwnProperty((i))) { | ||
users.push(i); | ||
} | ||
} | ||
User.getMultipleUserFields(users, ['username'], function(err, usersData) { | ||
try { | ||
if(err) { | ||
return callback([]); | ||
} | ||
return callback(usersData); | ||
} catch (e) { | ||
winston.error("Someone did a no-no!: " + e.message); | ||
} | ||
}); | ||
} catch (e) { | ||
winston.error("Someone did a no-no!: " + e.message); | ||
} | ||
User.getMultipleUserFields(users, ['username'], function(err, usersData) { | ||
if(err) { | ||
return callback([]); | ||
} | ||
return callback(usersData); | ||
}); | ||
} | ||
@@ -187,3 +247,4 @@ } | ||
timestamp: Date.now(), | ||
fromuid: fromuid | ||
fromuid: fromuid, | ||
deleted: '0' | ||
}; | ||
@@ -215,5 +276,9 @@ | ||
} | ||
if (message.deleted === '1') { | ||
return next(null); | ||
} | ||
User.getUserField(message.fromuid, 'username', function(err, username) { | ||
Shoutbox.backend.parse(message.fromuid, username, message.content, function(parsed) { | ||
message.content = parsed; | ||
message.sid = sid; | ||
messages.push(message); | ||
@@ -249,2 +314,7 @@ next(null); | ||
}, | ||
"markRemoved": function (sid, callback) { | ||
db.setObjectField('shout:' + sid, 'deleted', '1', function (err, result) { | ||
callback(err, result); | ||
}); | ||
}, | ||
"updateShoutTime": function(uid, callback) { | ||
@@ -251,0 +321,0 @@ db.sortedSetAdd('uid:' + uid + ':shouts', Date.now(), 0, function(err) { |
{ | ||
"name": "nodebb-plugin-shoutbox", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "NodeBB Shoutbox Plugin", | ||
@@ -5,0 +5,0 @@ "main": "library.js", |
$(document).ready(function() { | ||
if (document.location.pathname === "/shoutbox") { | ||
requirejs([ | ||
'plugins/nodebb-plugin-shoutbox/js/shoutbox.js' | ||
], function(shoutBox) { | ||
shoutBox.init(); | ||
shoutBox.showUsers(); | ||
}); | ||
} | ||
$(document).bind('DOMNodeInserted', function(event) { | ||
@@ -3,0 +12,0 @@ // todo improve this |
@@ -7,2 +7,3 @@ define(['string'], function(S) { | ||
"send": "api:modules.shoutbox.send", | ||
"remove" : "api:modules.shoutbox.remove", | ||
"get_users": "api:modules.shoutbox.get_users", | ||
@@ -27,6 +28,10 @@ "receive": "event:shoutbox.receive" | ||
module.init = function(callback) { | ||
addSendHandler(module.getShoutPanel()); | ||
addButtonHandlers(module.getShoutPanel()); | ||
var anon = checkForAnon(); | ||
if (!anon) { | ||
addSendHandler(module.getShoutPanel()); | ||
addButtonHandlers(module.getShoutPanel()); | ||
} | ||
registerSocket(); | ||
getShouts(module.getShoutPanel()); | ||
registerSocket(); | ||
loaded = true; | ||
@@ -37,10 +42,14 @@ if (callback) | ||
module.appendShout = function(shoutBox, message, timestamp) { | ||
module.appendShout = function(shoutBox, shout) { | ||
var shoutContent = shoutBox.find('#shoutbox-content'); | ||
var date = new Date(parseInt(timestamp, 10)); | ||
var date = new Date(parseInt(shout.timestamp, 10)); | ||
var prefix = '<span class="shoutbox-timestamp">' + date.toLocaleTimeString() + '</span> '; | ||
var shout = "<div>" + S(prefix + message).stripTags('p').s + "</div>"; | ||
var options = ''; | ||
if (shout.fromuid === app.uid) { | ||
options = '<button type="button" class="close pull-right" aria-hidden="true">×</button>'; | ||
} | ||
var shoutHTML = "<div id='shoutbox-shout-" + shout.sid + "'>" + options + S(prefix + shout.content).stripTags('p').s + "</div>"; | ||
shoutContent.append(shout); | ||
shoutContent.append(shoutHTML); | ||
scrollToBottom(shoutContent); | ||
@@ -55,2 +64,11 @@ } | ||
function checkForAnon() { | ||
if (app.uid === null) { | ||
$('#shoutbox .input-group').hide(); | ||
$('#shoutbox .btn-group').hide(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
function scrollToBottom(shoutContent) { | ||
@@ -67,3 +85,3 @@ if (module.getShoutPanel().length > 0) { | ||
for(var i = 0; i<shouts.length; ++i) { | ||
module.appendShout(shoutBox, shouts[i].content, shouts[i].timestamp); | ||
module.appendShout(shoutBox, shouts[i]); | ||
} | ||
@@ -74,9 +92,10 @@ }); | ||
function registerSocket() { | ||
socket.removeListener(socketEntries.receive); | ||
socket.on(socketEntries.receive, function(data) { | ||
if (module.hasLoaded) { | ||
module.appendShout(module.getShoutPanel(), data.message, data.timestamp); | ||
app.alternatingTitle('[' + data.username + '] - new shout!'); | ||
} | ||
}); | ||
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!'); | ||
} | ||
}); | ||
} | ||
} | ||
@@ -99,3 +118,20 @@ | ||
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); | ||
} | ||
}); | ||
} | ||
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 | ||
//START GIST BUTTON | ||
@@ -112,2 +148,9 @@ var gistModal = $('#create-gist-modal'); | ||
//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 | ||
} | ||
@@ -114,0 +157,0 @@ |
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
24783
543