diff --git a/www/common/common-messaging.js b/www/common/common-messaging.js index 8a846482a..f92eaed33 100644 --- a/www/common/common-messaging.js +++ b/www/common/common-messaging.js @@ -2,8 +2,42 @@ define([ 'jquery', '/bower_components/chainpad-crypto/crypto.js', '/common/curve.js', + '/common/common-hash.js', + '/bower_components/marked/marked.min.js', -], function ($, Crypto, Curve, Marked) { + '/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: [], }; @@ -28,11 +62,10 @@ define([ return Marked(content); }; - var createData = Msg.createData = function (common, hash) { - var proxy = common.getProxy(); + var createData = Msg.createData = function (proxy, hash) { return { - channel: hash || common.createChannelId(), - displayName: proxy[common.displayNameKey], + channel: hash || Hash.createChannelId(), + displayName: proxy['cryptpad.username'], profile: proxy.profile && proxy.profile.view, edPublic: proxy.edPublic, curvePublic: proxy.curvePublic, @@ -40,39 +73,39 @@ define([ }; }; - var getFriend = function (common, pubkey) { - var proxy = common.getProxy(); + var getFriend = function (proxy, pubkey) { if (pubkey === proxy.curvePublic) { - var data = createData(common); + var data = createData(proxy); 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 removeFromFriendList = function (proxy, realtime, curvePublic, cb) { + if (!proxy.friends) { return; } var friends = proxy.friends; delete friends[curvePublic]; - common.whenRealtimeSyncs(common.getRealtime(), cb); + Realtime.whenRealtimeSyncs(realtime, cb); }; - // TODO set this up as an observable data structure - var getFriendList = Msg.getFriendList = function (common) { - var proxy = common.getProxy(); + var getFriendList = Msg.getFriendList = function (proxy) { if (!proxy.friends) { proxy.friends = {}; } return proxy.friends; }; - Msg.getFriendChannelsList = function (common) { - var friends = getFriendList(common); + 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 = []; - Object.keys(friends).forEach(function (key) { - if (key === "me") { return; } - list.push(friends[key].channel); + eachFriend(proxy, function (friend) { + list.push(friend.channel); }); return list; }; @@ -85,131 +118,6 @@ define([ 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 = $('