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); }; // TODO set this up as an observable data structure 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 = {}; // TODO extract into UI method var createChatBox = function (common, $container, curvePublic, ui) { var data = getFriend(common, curvePublic); // Input var channel = channels[data.channel]; var $header = $('
', { 'class': 'header', }).appendTo($container); var $avatar = $('
', {'class': 'avatar'}).appendTo($header); // more history... $('', { 'class': 'more-history', }) .text('get more history') .click(function () { console.log("GETTING HISTORY"); channel.getPreviousMessages(); }) .appendTo($header); var $removeHistory = $('', { 'class': 'remove-history fa fa-eraser', title: common.Messages.contacts_removeHistoryTitle }) .click(function () { common.confirm(common.Messages.contacts_confirmRemoveHistory, function (yes) { if (!yes) { return; } common.clearOwnedChannel(data.channel, function (e) { if (e) { console.error(e); common.alert(common.Messages.contacts_removeHistoryServerError); return; } }); }); }); $removeHistory.appendTo($header); $('
', {'class': 'messages'}).appendTo($container); var $inputBlock = $('
', {'class': 'input'}).appendTo($container); var $input = $('