diff --git a/public/sound/chat-incoming.wav b/public/sound/chat-incoming.wav deleted file mode 100644 index 4ad54e4a67..0000000000 Binary files a/public/sound/chat-incoming.wav and /dev/null differ diff --git a/public/sound/notification.wav b/public/sounds/notification.wav similarity index 100% rename from public/sound/notification.wav rename to public/sounds/notification.wav diff --git a/public/sounds/waterdrop-high.wav b/public/sounds/waterdrop-high.wav new file mode 100644 index 0000000000..581addfc89 Binary files /dev/null and b/public/sounds/waterdrop-high.wav differ diff --git a/public/sound/chat-outgoing.wav b/public/sounds/waterdrop-low.wav similarity index 100% rename from public/sound/chat-outgoing.wav rename to public/sounds/waterdrop-low.wav diff --git a/public/src/modules/chat.js b/public/src/modules/chat.js index 476718aa9a..38ecf7974f 100644 --- a/public/src/modules/chat.js +++ b/public/src/modules/chat.js @@ -1,8 +1,7 @@ - -'use strict'; +"use strict"; /* globals app, config, define, socket, translator, templates, utils */ -define(['taskbar', 'string', 'sound'], function(taskbar, S, sound) { +define(['taskbar', 'string', 'sounds'], function(taskbar, S, sounds) { var module = {}; @@ -53,9 +52,8 @@ define(['taskbar', 'string', 'sound'], function(taskbar, S, sound) { }); socket.on('event:chats.receive', function(data) { - - if (module.modalExists(data.fromuid)) { - var modal = module.getModal(data.fromuid); + if (module.modalExists(data.uid)) { + var modal = module.getModal(data.uid); module.appendChatMessage(modal, data.message, data.timestamp); if (modal.is(":visible")) { @@ -71,13 +69,15 @@ define(['taskbar', 'string', 'sound'], function(taskbar, S, sound) { app.alternatingTitle(data.username + ' has messaged you'); } } else { - module.createModal(data.username, data.fromuid, function(modal) { + module.createModal(data.username, data.uid, function(modal) { module.toggleNew(modal.attr('UUID'), true); app.alternatingTitle(data.username + ' has messaged you'); }); } - sound.play('chat-incoming'); + if (parseInt(app.uid, 10) !== parseInt(data.fromUid, 10)) { + sounds.play('chat-incoming'); + } }); }; @@ -229,7 +229,8 @@ define(['taskbar', 'string', 'sound'], function(taskbar, S, sound) { msg = msg +'\n'; socket.emit('modules.chats.send', { touid:chatModal.touid, message:msg}); chatModal.find('#chat-message-input').val(''); - sound.play('chat-outgoing'); + console.log('outgoing'); + sounds.play('chat-outgoing'); } } diff --git a/public/src/modules/notifications.js b/public/src/modules/notifications.js index 37bb81329d..fcb6143f14 100644 --- a/public/src/modules/notifications.js +++ b/public/src/modules/notifications.js @@ -1,4 +1,4 @@ -define(['sound'], function(sound) { +define(['sounds'], function(sound) { var Notifications = {}; Notifications.prepareDOM = function() { diff --git a/public/src/modules/sound.js b/public/src/modules/sound.js deleted file mode 100644 index 0c51cdf9a6..0000000000 --- a/public/src/modules/sound.js +++ /dev/null @@ -1,43 +0,0 @@ -"use strict"; - -define(['buzz'], function(buzz) { - var Sound = {}; - - Sound.initialised = false; - Sound.loaded = {}; - - Sound.init = function(callback) { - var sounds = { - notification: RELATIVE_PATH + '/sound/notification.wav', - 'chat-outgoing': RELATIVE_PATH + '/sound/chat-outgoing.wav', - 'chat-incoming': RELATIVE_PATH + '/sound/chat-incoming.wav' - }; - - for(var name in sounds) { - if (sounds.hasOwnProperty(name)) { - var path = sounds[name]; - - Sound.loaded[name] = new buzz.sound(path); - } - } - - this.initialised = true; - - callback(); - }; - - Sound.play = function(name) { - var ready = function() { - if (Sound.loaded[name]) { - Sound.loaded[name].play(); - } else { - console.log('[sound] Not found:', name); - } - }; - - if (!this.initialised) this.init(ready); - else ready(); - }; - - return Sound; -}); \ No newline at end of file diff --git a/public/src/modules/sounds.js b/public/src/modules/sounds.js new file mode 100644 index 0000000000..13b18e7364 --- /dev/null +++ b/public/src/modules/sounds.js @@ -0,0 +1,74 @@ +"use strict"; + +define(['buzz'], function(buzz) { + var Sounds = {}; + + Sounds.initialised = false; + Sounds.loaded = {}; + Sounds.mapping = {}; + + Sounds.init = function(callback) { + var ready = false, + onComplete = function() { + callback(); + }; + + loadFiles(function() { + if (ready) { + onComplete(); + } else { + ready = true; + } + }); + + loadMapping(function() { + if (ready) { + onComplete(); + } else { + ready = true; + } + }); + }; + + var loadFiles = function(callback) { + socket.emit('modules.sounds.getSounds', function(err, sounds) { + if (err) { + return console.log('[sounds] Could not initialise!'); + } + + for(var name in sounds) { + if (sounds.hasOwnProperty(name)) { + var path = sounds[name]; + + Sounds.loaded[name] = new buzz.sound(path); + } + } + + this.initialised = true; + + callback(); + }); + }; + + var loadMapping = function(callback) { + socket.emit('modules.sounds.getMapping', function(err, mapping) { + Sounds.mapping = mapping; + callback(); + }); + }; + + Sounds.play = function(name) { + var ready = function() { + if (Sounds.mapping[name] && Sounds.loaded[Sounds.mapping[name]]) { + Sounds.loaded[Sounds.mapping[name]].play(); + } else { + console.log('[sounds] Not found:', name); + } + }; + + if (!this.initialised) this.init(ready); + else ready(); + }; + + return Sounds; +}); \ No newline at end of file diff --git a/src/meta.js b/src/meta.js index c08b4a1766..971233286b 100644 --- a/src/meta.js +++ b/src/meta.js @@ -325,6 +325,47 @@ var fs = require('fs'), } }; + /* Sounds */ + Meta.sounds = {}; + + // todo: Possibly move these into a bundled module? + Meta.sounds.getLocal = function(callback) { + fs.readdir(path.join(__dirname, '../public/sounds'), function(err, files) { + var localList = {}; + + if (err) { + winston.error('Could not get local sound files:' + err.message); + console.log(err.stack); + return callback(null, []); + } + + // Return proper paths + files.forEach(function(filename) { + localList[filename] = nconf.get('url') + '/sounds/' + filename; + }); + + callback(null, localList); + }); + }; + + Meta.sounds.getMapping = function(callback) { + db.getObject('sounds', function(err, sounds) { + if (err || !sounds) { + // Send default sounds + var defaults = { + notification: 'notification.wav', + 'chat-incoming': 'waterdrop-high.wav', + 'chat-outgoing': 'waterdrop-low.wav' + }; + + return callback(null, defaults); + } + + callback.apply(null, arguments); + }); + }; + + /* Assorted */ Meta.css = { cache: undefined }; diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js index 7148347fc6..9feef8b1a3 100644 --- a/src/socket.io/modules.js +++ b/src/socket.io/modules.js @@ -198,7 +198,8 @@ SocketModules.chats.send = function(socket, data) { server.getUserSockets(touid).forEach(function(s) { s.emit('event:chats.receive', { - fromuid: socket.uid, + uid: socket.uid, + fromUid: socket.uid, username: username, message: parsed, timestamp: Date.now() @@ -207,7 +208,8 @@ SocketModules.chats.send = function(socket, data) { server.getUserSockets(socket.uid).forEach(function(s) { s.emit('event:chats.receive', { - fromuid: touid, + uid: touid, + fromUid: socket.uid, username: toUsername, message: parsed, timestamp: Date.now() @@ -234,4 +236,17 @@ SocketModules.notifications.mark_all_read = function(socket, data, callback) { notifications.mark_all_read(socket.uid, callback); }; +/* Sounds */ + +SocketModules.sounds = {}; + +SocketModules.sounds.getSounds = function(socket, data, callback) { + // Read sounds from local directory + meta.sounds.getLocal(callback); +}; + +SocketModules.sounds.getMapping = function(socket, data, callback) { + meta.sounds.getMapping(callback); +}; + module.exports = SocketModules; \ No newline at end of file