define([ 'jquery', '/bower_components/chainpad-crypto/crypto.js', '/common/curve.js', '/common/common-hash.js', '/bower_components/marked/marked.min.js', '/common/common-realtime.js', // displayAvatar // whenRealtimeSyncs // getRealtime -> removeFromFriendList /* UI Messages confirm fixHTML displayAvatar clearOwnedChannel alert pushMsg removeFromFriendList onDirectMessage getNetwork getProxy pushMsg Init getNetwork getProxy onDirectMessage removeFromFriendList notify onMessage */ ], function ($, Crypto, Curve, Hash, Marked, Realtime) { 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 (proxy, hash) { return { channel: hash || Hash.createChannelId(), displayName: proxy['cryptpad.username'], profile: proxy.profile && proxy.profile.view, edPublic: proxy.edPublic, curvePublic: proxy.curvePublic, avatar: proxy.profile && proxy.profile.avatar }; }; var getFriend = function (proxy, pubkey) { if (pubkey === proxy.curvePublic) { var data = createData(proxy); delete data.channel; return data; } return proxy.friends ? proxy.friends[pubkey] : undefined; }; var removeFromFriendList = function (proxy, realtime, curvePublic, cb) { if (!proxy.friends) { return; } var friends = proxy.friends; delete friends[curvePublic]; Realtime.whenRealtimeSyncs(realtime, cb); }; var getFriendList = Msg.getFriendList = function (proxy) { if (!proxy.friends) { proxy.friends = {}; } return proxy.friends; }; var eachFriend = function (friends, cb) { Object.keys(friends).forEach(function (id) { if (id === 'me') { return; } cb(friends[id], id, friends); }); }; Msg.getFriendChannelsList = function (proxy) { var list = []; eachFriend(proxy, function (friend) { list.push(friend.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.addToFriendList = function (data, display, remove) { var $block = ui.containers.friendBlock; var $friend = $('