From 9f8c9e88220a9646d687900df5e270d69c27d7f8 Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Wed, 22 May 2013 15:35:54 -0400 Subject: [PATCH 1/6] derp --- public/css/style.less | 9 ++++ public/templates/admin/users.tpl | 2 +- public/templates/footer.tpl | 1 - public/templates/header.tpl | 12 +++++ public/templates/topic.tpl | 78 +++++++++++++++++++++++++++++++- src/websockets.js | 17 ++++++- 6 files changed, 115 insertions(+), 4 deletions(-) diff --git a/public/css/style.less b/public/css/style.less index 5cb9d0b0ff..5da5d4be06 100644 --- a/public/css/style.less +++ b/public/css/style.less @@ -733,4 +733,13 @@ body .navbar .nodebb-inline-block { font-size: 12px; color: red; } +} + +#chat-content { + width:95%; + height:200px; +} + +#chat-message-input { + width:95%; } \ No newline at end of file diff --git a/public/templates/admin/users.tpl b/public/templates/admin/users.tpl index 4d8df6ca3e..3f181571be 100644 --- a/public/templates/admin/users.tpl +++ b/public/templates/admin/users.tpl @@ -16,7 +16,7 @@
- +
{users.username} diff --git a/public/templates/footer.tpl b/public/templates/footer.tpl index 75d4086dce..c21eb5599f 100644 --- a/public/templates/footer.tpl +++ b/public/templates/footer.tpl @@ -81,7 +81,6 @@ } }); - // Post window events var postWindowEl = document.getElementById('post_window'), discardEl = document.getElementById('discard-post'); diff --git a/public/templates/header.tpl b/public/templates/header.tpl index 0a2caf095e..05d604b7d5 100644 --- a/public/templates/header.tpl +++ b/public/templates/header.tpl @@ -90,6 +90,18 @@
+ +
\ No newline at end of file diff --git a/public/templates/topic.tpl b/public/templates/topic.tpl index cef7b2a962..3e6eaa064c 100644 --- a/public/templates/topic.tpl +++ b/public/templates/topic.tpl @@ -52,7 +52,7 @@ -
  • +
  • @@ -66,6 +66,7 @@
    {posts.signature}
    +
    @@ -191,6 +192,7 @@ e.preventDefault(); moveThreadModal.modal('show'); }, false); + moveThreadModal.on('shown', function() { var loadingEl = document.getElementById('categories-loading'); if (loadingEl) { @@ -291,6 +293,79 @@ } }); + // CHAT, move to chat.js later? + + $('.post-container').delegate('.chat', 'click', function(e){ + + var username = $(this).parents('li').attr('data-username'); + var touid = $(this).parents('li').attr('data-uid'); + + var chatModal = $('#chat-modal-'+touid); + console.log($('#chat-modal-'+touid).length); + console.log('#chat-modal-'+touid); + if(!chatModal.length) { + var chatModal = $('#chat-modal').clone(); + chatModal.attr('id','#chat-modal-'+touid); + chatModal.appendTo($('body')); + chatModal.find('.close').on('click',function(e){ + chatModal.hide(); + }); + } + + chatModal.show(); + + chatModal.find('#chat-with-name').html(username); + + //addSendHandler(touid); + + }); + + $('#chat-modal').on('hide', function(){ + $('#chat-message-input').off('keypress'); + $('#chat-content').html(''); + }); + + socket.on('chatMessage', function(data){ + var username = data.username; + var fromuid = data.fromuid; + var message = data.message; + + $('#chat-modal').modal('show'); + $('#chat-with-name').html(username); + $('#chat-content').append(message); + + var chatContent = $('#chat-content'); + chatContent.scrollTop( + chatContent[0].scrollHeight - chatContent.height() + ); + + addSendHandler(fromuid); + }); + + function addSendHandler(touid) { + $('#chat-message-input').off('keypress'); + $('#chat-message-input').on('keypress', function(e) { + if(e.which === 13) { + sendMessage(touid); + } + }); + + $('#chat-message-send-btn').off('click'); + $('#chat-message-send-btn').on('click', function(e){ + sendMessage(touid); + return false; + }); + } + + function sendMessage(touid) { + var msg = $('#chat-message-input').val() + '\n'; + socket.emit('sendChatMessage', { touid:touid, message:msg}); + $('#chat-message-input').val(''); + $('#chat-content').append('You : '+ msg); + } + //end of chat + + ajaxify.register_events([ 'event:rep_up', 'event:rep_down', 'event:new_post', 'api:get_users_in_room', 'event:topic_deleted', 'event:topic_restored', 'event:topic:locked', @@ -299,6 +374,7 @@ 'api:posts.favourite' ]); + socket.on('api:get_users_in_room', function(users) { var anonymous = users.anonymous, usernames = users.usernames, diff --git a/src/websockets.js b/src/websockets.js index 502bea880a..aff4cb4fd3 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -10,7 +10,8 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}), (function(io) { var users = {}, - rooms = {} + userSockets = {}, + rooms = {} global.io = io; @@ -48,6 +49,7 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}), var hs = socket.handshake; var uid = users[hs.sessionID]; + userSockets[uid] = socket; user.go_online(uid); @@ -233,6 +235,19 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}), socket.on('api:posts.restore', function(data) { posts.restore(uid, data.pid); }); + + socket.on('sendChatMessage', function(data) { + var touid = data.touid; + + if(userSockets[touid]) { + var msg = data.message; + + user.getUserField(uid, 'username', function(username) { + var finalMessage = username + ' says : ' + msg; + userSockets[touid].emit('chatMessage', {fromuid:uid, username:username, message:finalMessage}); + }); + } + }); }); }(SocketIO)); From 8d0ac4cb077a76cfff6ef2e4bf59ffd86a74ea8a Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Wed, 22 May 2013 17:14:06 -0400 Subject: [PATCH 2/6] chat feature --- public/src/app.js | 12 +++++ public/templates/header.tpl | 3 +- public/templates/topic.tpl | 102 +++++++++++++++++++++--------------- src/websockets.js | 3 +- 4 files changed, 77 insertions(+), 43 deletions(-) diff --git a/public/src/app.js b/public/src/app.js index c5a2f58bc9..dbb39ecc45 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -50,6 +50,17 @@ var socket, return text.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"); } + // Willingly stolen from: http://phpjs.org/functions/strip_tags/ + app.strip_tags = function(input, allowed) { + allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); // making sure the allowed arg is a string containing only tags in lowercase (
    ) + var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi, + commentsAndPhpTags = /|<\?(?:php)?[\s\S]*?\?>/gi; + + return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) { + return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : ''; + }); + } + // use unique alert_id to have multiple alerts visible at a time, use the same alert_id to fade out the current instance // type : error, success, info, warning/notify // title = bolded title text @@ -114,6 +125,7 @@ var socket, app.open_post_window = function(post_mode, id, title, pid) { + submit_post_btn = submit_post_btn || document.getElementById('submit_post_btn'); post_title = post_title || document.getElementById('post_title'); reply_title = reply_title || document.getElementById('reply_title'); diff --git a/public/templates/header.tpl b/public/templates/header.tpl index e5518d3dbb..8d97d30a3e 100644 --- a/public/templates/header.tpl +++ b/public/templates/header.tpl @@ -9,6 +9,7 @@ + @@ -100,7 +101,7 @@
    diff --git a/public/templates/topic.tpl b/public/templates/topic.tpl index 3e6eaa064c..7243483639 100644 --- a/public/templates/topic.tpl +++ b/public/templates/topic.tpl @@ -300,68 +300,88 @@ var username = $(this).parents('li').attr('data-username'); var touid = $(this).parents('li').attr('data-uid'); + var chatModal = createModalIfDoesntExist(username, touid); + + chatModal.show(); + bringModalToTop(chatModal); + + }); + + function bringModalToTop(chatModal) { + var topZ = 0; + $('.modal').each(function(){ + var thisZ = parseInt($(this).css('zIndex'), 10); + if (thisZ > topZ){ + topZ = thisZ; + } + }); + chatModal.css('zIndex', topZ+1); + } + + function createModalIfDoesntExist(username, touid) { var chatModal = $('#chat-modal-'+touid); - console.log($('#chat-modal-'+touid).length); - console.log('#chat-modal-'+touid); + if(!chatModal.length) { var chatModal = $('#chat-modal').clone(); - chatModal.attr('id','#chat-modal-'+touid); + chatModal.attr('id','chat-modal-'+touid); chatModal.appendTo($('body')); + chatModal.draggable(); + chatModal.find('#chat-with-name').html(username); + chatModal.find('.close').on('click',function(e){ chatModal.hide(); }); + + chatModal.on('click', function(e){ + bringModalToTop(chatModal); + }); + + addSendHandler(chatModal, touid); } - chatModal.show(); + return chatModal; + } - chatModal.find('#chat-with-name').html(username); - - //addSendHandler(touid); - - }); + function addSendHandler(chatModal, touid) { + chatModal.find('#chat-message-input').off('keypress'); + chatModal.find('#chat-message-input').on('keypress', function(e) { + if(e.which === 13) { + sendMessage(chatModal, touid); + } + }); - $('#chat-modal').on('hide', function(){ - $('#chat-message-input').off('keypress'); - $('#chat-content').html(''); - }); + chatModal.find('#chat-message-send-btn').off('click'); + chatModal.find('#chat-message-send-btn').on('click', function(e){ + sendMessage(chatModal, touid); + return false; + }); + } + + function sendMessage(chatModal, touid) { + var msg = app.strip_tags(chatModal.find('#chat-message-input').val()) + '\n'; + socket.emit('sendChatMessage', { touid:touid, message:msg}); + chatModal.find('#chat-message-input').val(''); + appendChatMessage(chatModal, 'You : ' + msg); + } socket.on('chatMessage', function(data){ var username = data.username; var fromuid = data.fromuid; var message = data.message; - $('#chat-modal').modal('show'); - $('#chat-with-name').html(username); - $('#chat-content').append(message); + var chatModal = createModalIfDoesntExist(username, fromuid); + chatModal.show(); + bringModalToTop(chatModal); + + appendChatMessage(chatModal, message) + }); - var chatContent = $('#chat-content'); + function appendChatMessage(chatModal, message){ + var chatContent = chatModal.find('#chat-content'); + chatContent.append(message); chatContent.scrollTop( chatContent[0].scrollHeight - chatContent.height() ); - - addSendHandler(fromuid); - }); - - function addSendHandler(touid) { - $('#chat-message-input').off('keypress'); - $('#chat-message-input').on('keypress', function(e) { - if(e.which === 13) { - sendMessage(touid); - } - }); - - $('#chat-message-send-btn').off('click'); - $('#chat-message-send-btn').on('click', function(e){ - sendMessage(touid); - return false; - }); - } - - function sendMessage(touid) { - var msg = $('#chat-message-input').val() + '\n'; - socket.emit('sendChatMessage', { touid:touid, message:msg}); - $('#chat-message-input').val(''); - $('#chat-content').append('You : '+ msg); } //end of chat diff --git a/src/websockets.js b/src/websockets.js index aff4cb4fd3..1bdcb98180 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -5,6 +5,7 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}), config = require('../config.js'), user = require('./user.js'), posts = require('./posts.js'), + utils = require('./utils.js'), topics = require('./topics.js'), categories = require('./categories.js'); @@ -240,7 +241,7 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}), var touid = data.touid; if(userSockets[touid]) { - var msg = data.message; + var msg = utils.strip_tags(data.message); user.getUserField(uid, 'username', function(username) { var finalMessage = username + ' says : ' + msg; From 6e8a18f4752b5cd8e2a1dc951e99e2cb04ab744d Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Wed, 22 May 2013 17:28:30 -0400 Subject: [PATCH 3/6] fixed IE bug when chat message was sent post window was opening --- public/templates/topic.tpl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/public/templates/topic.tpl b/public/templates/topic.tpl index 7243483639..5fa8fa17a6 100644 --- a/public/templates/topic.tpl +++ b/public/templates/topic.tpl @@ -25,12 +25,12 @@

    {topic_name}

    - - - + + + - - + +

    @@ -83,9 +83,9 @@
    - +