define([ 'jquery', '/bower_components/chainpad-crypto/crypto.js', '/common/curve.js', '/bower_components/marked/marked.min.js', ], function ($, Crypto, Curve, Marked) { var Msg = { inputs: [], }; var Types = { message: 'MSG', update: 'UPDATE', unfriend: 'UNFRIEND', mapId: 'MAP_ID', mapIdAck: 'MAP_ID_ACK' }; // TODO // - mute a channel (hide notifications or don't open it?) var ready = []; var pending = {}; var pendingRequests = []; var parseMessage = function (content) { return Marked(content); }; var createData = Msg.createData = function (common, hash) { var proxy = common.getProxy(); return { channel: hash || common.createChannelId(), displayName: proxy[common.displayNameKey], profile: proxy.profile && proxy.profile.view, edPublic: proxy.edPublic, curvePublic: proxy.curvePublic, avatar: proxy.profile && proxy.profile.avatar }; }; var getFriend = function (common, pubkey) { var proxy = common.getProxy(); if (pubkey === proxy.curvePublic) { var data = createData(common); delete data.channel; return data; } return proxy.friends ? proxy.friends[pubkey] : undefined; }; var removeFromFriendList = Msg.removeFromFriendList = function (common, curvePublic, cb) { var proxy = common.getProxy(); if (!proxy.friends) { return; } var friends = proxy.friends; delete friends[curvePublic]; common.whenRealtimeSyncs(common.getRealtime(), cb); }; var getFriendList = Msg.getFriendList = function (common) { var proxy = common.getProxy(); if (!proxy.friends) { proxy.friends = {}; } return proxy.friends; }; Msg.getFriendChannelsList = function (common) { var friends = getFriendList(common); var list = []; Object.keys(friends).forEach(function (key) { if (key === "me") { return; } list.push(friends[key].channel); }); return list; }; // Messaging tools var avatars = {}; // TODO make this internal to the messenger var channels = Msg.channels = window.channels = {}; var UI = Msg.UI = {}; UI.init = function (common, $listContainer, $msgContainer) { var ui = { containers: { friendList: $listContainer, messages: $msgContainer, }, }; ui.setFriendList = function (display, remove) { UI.getFriendList(common, display, remove).appendTo($listContainer); }; ui.notify = function (curvePublic) { var $friend = $listContainer.find('.friend').filter(function (idx, el) { return $(el).data('key') === curvePublic; }); $friend.addClass('notify'); }; ui.unnotify = function (curvePublic) { var $friend = $listContainer.find('.friend').filter(function (idx, el) { return $(el).data('key') === curvePublic; }); $friend.removeClass('notify'); }; ui.update = function (curvePublic, types) { var data = getFriend(common, curvePublic); var chan = channels[data.channel]; if (!chan.ready) { chan.updateOnReady = (chan.updateOnReady || []).concat(types); return; } var $friend = $listContainer.find('.friend').filter(function (idx, el) { return $(el).data('key') === curvePublic; }); if (types.indexOf('displayName') >= 0) { $friend.find('.name').text(data.displayName); } if (types.indexOf('avatar') >= 0) { $friend.find('.default').remove(); $friend.find('media-tag').remove(); if (data.avatar && avatars[data.avatar]) { $friend.prepend(avatars[data.avatar]); } else { common.displayAvatar($friend, data.avatar, data.displayName, function ($img) { if (data.avatar && $img) { avatars[data.avatar] = $img[0].outerHTML; } }); } } }; ui.updateStatus = function (curvePublic) { var data = getFriend(common, curvePublic); var chan = channels[data.channel]; // FIXME mixes logic and presentation var $friend = $listContainer.find('.friend').filter(function (idx, el) { return $(el).data('key') === curvePublic; }); var status = chan.userList.some(function (nId) { return chan.mapId[nId] === curvePublic; }); var statusText = status ? 'online' : 'offline'; $friend.find('.status').attr('class', 'status '+statusText); }; ui.getChannel = function (curvePublic) { // TODO extract into UI method var $chat = $msgContainer.find('.chat').filter(function (idx, el) { return $(el).data('key') === curvePublic; }); return $chat.length? $chat: null; }; ui.hideInfo = function () { $msgContainer.find('.info').hide(); }; ui.showInfo = function () { $msgContainer.find('.info').show(); }; ui.createChat = function (curvePublic) { return $('