diff --git a/customize.dist/messages.js b/customize.dist/messages.js index 7cb43762d..7e84dd01d 100644 --- a/customize.dist/messages.js +++ b/customize.dist/messages.js @@ -12,7 +12,7 @@ var map = { var messages = {}; var LS_LANG = "CRYPTPAD_LANG"; -var getStoredLanguage = function () { return localStorage.getItem(LS_LANG); }; +var getStoredLanguage = function () { return localStorage && localStorage.getItem(LS_LANG); }; var getBrowserLanguage = function () { return navigator.language || navigator.userLanguage || ''; }; var getLanguage = messages._getLanguage = function () { if (window.cryptpadLanguage) { return window.cryptpadLanguage; } @@ -24,19 +24,17 @@ var getLanguage = messages._getLanguage = function () { }; var language = getLanguage(); -var req = ['jquery', '/customize/translations/messages.js']; +var req = ['/common/common-util.js', '/customize/translations/messages.js']; if (language && map[language]) { req.push('/customize/translations/messages.' + language + '.js'); } -define(req, function($, Default, Language) { +define(req, function(Util, Default, Language) { map.en = 'English'; var defaultLanguage = 'en'; - if (!Language || language === defaultLanguage || !map[language]) { - messages = $.extend(true, messages, Default); - } - else { + Util.extend(messages, Default); + if (Language && language !== defaultLanguage) { // Add the translated keys to the returned object - messages = $.extend(true, messages, Default, Language); + Util.extend(messages, Language); } messages._languages = map; diff --git a/www/common/common-messenger.js b/www/common/common-messenger.js index 09a527bfb..9efe72586 100644 --- a/www/common/common-messenger.js +++ b/www/common/common-messenger.js @@ -5,7 +5,8 @@ define([ '/common/common-hash.js', '/common/common-util.js', '/common/common-realtime.js', -], function ($, Crypto, Curve, Hash, Util, Realtime) { + '/common/common-constants.js', +], function ($, Crypto, Curve, Hash, Util, Realtime, Constants) { 'use strict'; var Msg = { inputs: [], @@ -28,7 +29,7 @@ define([ var createData = Msg.createData = function (proxy, hash) { return { channel: hash || Hash.createChannelId(), - displayName: proxy['cryptpad.username'], + displayName: proxy[Constants.displayNameKey], profile: proxy.profile && proxy.profile.view, edPublic: proxy.edPublic, curvePublic: proxy.curvePublic, @@ -56,7 +57,7 @@ define([ }); }; - Msg.messenger = function (common) { + Msg.messenger = function (store) { var messenger = { handlers: { message: [], @@ -89,9 +90,9 @@ define([ var joining = {}; // declare common variables - var network = common.getNetwork(); - var proxy = common.getProxy(); - var realtime = common.getRealtime(); + var network = store.network; + var proxy = store.proxy; + var realtime = store.realtime; Msg.hk = network.historyKeeper; var friends = getFriendList(proxy); @@ -626,18 +627,10 @@ define([ }); }; - // TODO displayName messenger.getMyInfo = function (cb) { cb(void 0, { curvePublic: proxy.curvePublic, - displayName: '' //common.getDisplayName(), - }); - }; - - messenger.clearOwnedChannel = function (channel, cb) { - common.clearOwnedChannel(channel, function (e) { - if (e) { return void cb(e); } - cb(); + displayName: proxy[Constants.displayNameKey] }); }; diff --git a/www/common/common-util.js b/www/common/common-util.js index 759730663..030536751 100644 --- a/www/common/common-util.js +++ b/www/common/common-util.js @@ -209,6 +209,43 @@ define([], function () { xhr.send(); }; + // Check if an element is a plain object + Util.isObject = function (o) { + return typeof (o) === "object" && + Object.prototype.toString.call(o) === '[object Object]'; + }; + + Util.isCircular = function (o) { + try { + JSON.stringify(o); + return false; + } catch (e) { return true; } + }; + + /* recursively adds the properties of an object 'b' to 'a' + arrays are only shallow copies, so references to the original + might still be present. Be mindful if you will modify 'a' in the future */ + Util.extend = function (a, b) { + if (!Util.isObject(a) || !Util.isObject(b)) { + return void console.log("Extend only works with 2 objects"); + } + if (Util.isCircular(b)) { + return void console.log("Extend doesn't accept circular objects"); + } + for (var k in b) { + if (Util.isObject(b[k])) { + a[k] = {}; + Util.extend(a[k], b[k]); + continue; + } + if (Array.isArray(b[k])) { + a[k] = b[k].slice(); + continue; + } + a[k] = b[k]; + } + }; + return Util; }); }(self)); diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index dc8a37025..ee04ee19b 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -47,6 +47,15 @@ define([ var PINNING_ENABLED = AppConfig.enablePinning; + // COMMON + common.getLanguage = function () { + return Messages._languageUsed; + }; + common.setLanguage = function (l, cb) { + Language.setLanguage(l, null, cb); + }; + + // RESTRICTED // Settings only common.getUserObject = function (cb) { @@ -119,14 +128,125 @@ define([ }; - // REFACTOR pull language directly? - common.getLanguage = function () { - return Messages._languageUsed; + // RPC + common.pinPads = function (pads, cb) { + postMessage("PIN_PADS", pads, function (obj) { + if (obj && obj.error) { return void cb(obj.error); } + cb(null, obj.hash); + }); }; - common.setLanguage = function (l, cb) { - Language.setLanguage(l, null, cb); + + common.unpinPads = function (pads, cb) { + postMessage("UNPIN_PADS", pads, function (obj) { + if (obj && obj.error) { return void cb(obj.error); } + cb(null, obj.hash); + }); + }; + + common.getPinnedUsage = function (cb) { + postMessage("GET_PINNED_USAGE", null, function (obj) { + if (obj.error) { return void cb(obj.error); } + cb(null, obj.bytes); + }); + }; + + common.updatePinLimit = function (cb) { + postMessage("UPDATE_PIN_LIMIT", null, function (obj) { + if (obj.error) { return void cb(obj.error); } + cb(undefined, obj.limit, obj.plan, obj.note); + }); + }; + + common.getPinLimit = function (cb) { + postMessage("GET_PIN_LIMIT", null, function (obj) { + if (obj.error) { return void cb(obj.error); } + cb(undefined, obj.limit, obj.plan, obj.note); + }); }; + common.isOverPinLimit = function (cb) { + if (!LocalStore.isLoggedIn()) { return void cb(null, false); } + var usage; + var andThen = function (e, limit, plan) { + if (e) { return void cb(e); } + var data = {usage: usage, limit: limit, plan: plan}; + if (usage > limit) { + return void cb (null, true, data); + } + return void cb (null, false, data); + }; + var todo = function (e, used) { + if (e) { return void cb(e); } + usage = used; + common.getPinLimit(andThen); + }; + common.getPinnedUsage(todo); + }; + + common.clearOwnedChannel = function (channel, cb) { + postMessage("CLEAR_OWNED_CHANNEL", channel, cb); + }; + + common.uploadComplete = function (cb) { + postMessage("UPLOAD_COMPLETE", null, function (obj) { + if (obj && obj.error) { return void cb(obj.error); } + cb(null, obj); + }); + }; + + common.uploadStatus = function (size, cb) { + postMessage("UPLOAD_STATUS", {size: size}, function (obj) { + if (obj && obj.error) { return void cb(obj.error); } + cb(null, obj); + }); + }; + + common.uploadCancel = function (cb) { + postMessage("UPLOAD_CANCEL", null, function (obj) { + if (obj && obj.error) { return void cb(obj.error); } + cb(null, obj); + }); + }; + + common.uploadChunk = function (data, cb) { + postMessage("UPLOAD_CHUNK", {chunk: data}, function (obj) { + if (obj && obj.error) { return void cb(obj.error); } + cb(null, obj); + }); + }; + + // ANON RPC + + // SFRAME: talk to anon_rpc from the iframe + common.anonRpcMsg = function (msg, data, cb) { + if (!msg) { return; } + postMessage("ANON_RPC_MESSAGE", { + msg: msg, + data: data + }, function (obj) { + if (obj && obj.error) { return void cb(obj.error); } + cb(null, obj); + }); + }; + + common.getFileSize = function (href, cb) { + postMessage("GET_FILE_SIZE", {href: href}, function (obj) { + if (obj && obj.error) { return void cb(obj.error); } + cb(undefined, obj.size); + }); + }; + + common.getMultipleFileSize = function (files, cb) { + postMessage("GET_MULTIPLE_FILE_SIZE", {files:files}, function (obj) { + if (obj.error) { return void cb(obj.error); } + cb(undefined, obj.size); + }); + }; + + // Store + + + common.getMetadata = function (cb) { postMessage("GET_METADATA", null, function (obj) { if (obj && obj.error) { return void cb(obj.error); } @@ -138,7 +258,6 @@ define([ postMessage("SET_DISPLAY_NAME", value, cb); }; - // STORAGE common.setPadAttribute = function (attr, value, cb, href) { href = Hash.getRelativeHref(href || window.location.href); postMessage("SET_PAD_ATTRIBUTE", { @@ -178,7 +297,6 @@ define([ }); }; - // Tags common.resetTags = function (href, tags, cb) { // set pad attribute @@ -320,133 +438,52 @@ define([ }); }; - common.pinPads = function (pads, cb) { - postMessage("PIN_PADS", pads, function (obj) { - if (obj && obj.error) { return void cb(obj.error); } - cb(null, obj.hash); - }); - }; - - common.unpinPads = function (pads, cb) { - postMessage("UNPIN_PADS", pads, function (obj) { - if (obj && obj.error) { return void cb(obj.error); } - cb(null, obj.hash); - }); - }; - - common.getPinnedUsage = function (cb) { - postMessage("GET_PINNED_USAGE", null, function (obj) { - if (obj.error) { return void cb(obj.error); } - cb(null, obj.bytes); - }); - }; - - // SFRAME: talk to anon_rpc from the iframe - common.anonRpcMsg = function (msg, data, cb) { - if (!msg) { return; } - postMessage("ANON_RPC_MESSAGE", { - msg: msg, - data: data + // Messaging (manage friends from the userlist) + common.inviteFromUserlist = function (netfluxId, cb) { + postMessage("INVITE_FROM_USERLIST", { + netfluxId: netfluxId, + href: window.location.href }, function (obj) { if (obj && obj.error) { return void cb(obj.error); } - cb(null, obj); + cb(); }); }; - common.getFileSize = function (href, cb) { - postMessage("GET_FILE_SIZE", {href: href}, function (obj) { - if (obj && obj.error) { return void cb(obj.error); } - cb(undefined, obj.size); - }); + // Messenger + var messenger = common.messenger = {}; + messenger.getFriendList = function (cb) { + postMessage("CONTACTS_GET_FRIEND_LIST", null, cb); }; - - // TODO not used anymore? - common.getMultipleFileSize = function (files, cb) { - postMessage("GET_MULTIPLE_FILE_SIZE", {files:files}, function (obj) { - if (obj.error) { return void cb(obj.error); } - cb(undefined, obj.size); - }); + messenger.getMyInfo = function (cb) { + postMessage("CONTACTS_GET_MY_INFO", null, cb); }; - - common.updatePinLimit = function (cb) { - postMessage("UPDATE_PIN_LIMIT", null, function (obj) { - if (obj.error) { return void cb(obj.error); } - cb(undefined, obj.limit, obj.plan, obj.note); - }); + messenger.getFriendInfo = function (curvePublic, cb) { + postMessage("CONTACTS_GET_FRIEND_INFO", curvePublic, cb); }; - - common.getPinLimit = function (cb) { - postMessage("GET_PIN_LIMIT", null, function (obj) { - if (obj.error) { return void cb(obj.error); } - cb(undefined, obj.limit, obj.plan, obj.note); - }); + messenger.removeFriend = function (curvePublic, cb) { + postMessage("CONTACTS_REMOVE_FRIEND", curvePublic, cb); }; - - common.isOverPinLimit = function (cb) { - if (!LocalStore.isLoggedIn()) { return void cb(null, false); } - var usage; - var andThen = function (e, limit, plan) { - if (e) { return void cb(e); } - var data = {usage: usage, limit: limit, plan: plan}; - if (usage > limit) { - return void cb (null, true, data); - } - return void cb (null, false, data); - }; - var todo = function (e, used) { - if (e) { return void cb(e); } - usage = used; - common.getPinLimit(andThen); - }; - common.getPinnedUsage(todo); + messenger.openFriendChannel = function (curvePublic, cb) { + postMessage("CONTACTS_OPEN_FRIEND_CHANNEL", curvePublic, cb); }; - - common.clearOwnedChannel = function (channel, cb) { - postMessage("CLEAR_OWNED_CHANNEL", {channel: channel}, function (obj) { - if (obj && obj.error) { return void cb(obj.error); } - cb(null, obj); - }); + messenger.getFriendStatus = function (curvePublic, cb) { + postMessage("CONTACTS_GET_FRIEND_STATUS", curvePublic, cb); }; - - common.uploadChunk = function (data, cb) { - postMessage("UPLOAD_CHUNK", {chunk: data}, function (obj) { - if (obj && obj.error) { return void cb(obj.error); } - cb(null, obj); - }); + messenger.getMoreHistory = function (data, cb) { + postMessage("CONTACTS_GET_MORE_HISTORY", data, cb); }; - - common.uploadComplete = function (cb) { - postMessage("UPLOAD_COMPLETE", null, function (obj) { - if (obj && obj.error) { return void cb(obj.error); } - cb(null, obj); - }); + messenger.sendMessage = function (data, cb) { + postMessage("CONTACTS_SEND_MESSAGE", data, cb); }; - - common.uploadStatus = function (size, cb) { - postMessage("UPLOAD_STATUS", {size: size}, function (obj) { - if (obj && obj.error) { return void cb(obj.error); } - cb(null, obj); - }); - }; - - common.uploadCancel = function (cb) { - postMessage("UPLOAD_CANCEL", null, function (obj) { - if (obj && obj.error) { return void cb(obj.error); } - cb(null, obj); - }); - }; - - // Messaging - common.inviteFromUserlist = function (netfluxId, cb) { - postMessage("INVITE_FROM_USERLIST", { - netfluxId: netfluxId, - href: window.location.href - }, function (obj) { - if (obj && obj.error) { return void cb(obj.error); } - cb(); - }); + messenger.setChannelHead = function (data, cb) { + postMessage("CONTACTS_SET_CHANNEL_HEAD", data, cb); }; - + messenger.onMessageEvent = Util.mkEvent(); + messenger.onJoinEvent = Util.mkEvent(); + messenger.onLeaveEvent = Util.mkEvent(); + messenger.onUpdateEvent = Util.mkEvent(); + messenger.onFriendEvent = Util.mkEvent(); + messenger.onUnfriendEvent = Util.mkEvent(); // HERE common.getShareHashes = function (secret, cb) { @@ -540,6 +577,25 @@ define([ common.onFriendComplete(data); break; } + // Messenger + case 'CONTACTS_MESSAGE': { + common.messenger.onMessageEvent.fire(data); break; + } + case 'CONTACTS_JOIN': { + common.messenger.onJoinEvent.fire(data); break; + } + case 'CONTACTS_LEAVE': { + common.messenger.onLeaveEvent.fire(data); break; + } + case 'CONTACTS_UPDATE': { + common.messenger.onUpdateEvent.fire(data); break; + } + case 'CONTACTS_FRIEND': { + common.messenger.onFriendEvent.fire(data); break; + } + case 'CONTACTS_UNFRIEND': { + common.messenger.onUnfriendEvent.fire(data); break; + } } }; @@ -547,7 +603,7 @@ define([ var env = {}; var initialized = false; - return function (f) { + return function (f, rdyCfg) { if (initialized) { return void setTimeout(function () { f(void 0, env); }); } @@ -583,7 +639,9 @@ define([ query: onMessage, // TODO temporary, will be replaced by a webworker channel userHash: LocalStore.getUserHash(), anonHash: LocalStore.getFSHash(), - localToken: tryParsing(localStorage.getItem(Constants.tokenKey)) + localToken: tryParsing(localStorage.getItem(Constants.tokenKey)), + language: common.getLanguage(), + messenger: rdyCfg.messenger }; if (sessionStorage[Constants.newPadPathKey]) { cfg.initialPath = sessionStorage[Constants.newPadPathKey]; diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index b67153830..fe94d1a38 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -7,12 +7,14 @@ define([ '/common/common-feedback.js', '/common/common-realtime.js', '/common/common-messaging.js', + '/common/common-messenger.js', '/common/outer/network-config.js', '/bower_components/chainpad-crypto/crypto.js?v=0.1.5', '/bower_components/chainpad/chainpad.dist.js', '/bower_components/chainpad-listmap/chainpad-listmap.js', -], function (UserObject, Migrate, Hash, Util, Constants, Feedback, Realtime, Messaging, NetConfig, +], function (UserObject, Migrate, Hash, Util, Constants, Feedback, Realtime, Messaging, Messenger, + NetConfig, Crypto, ChainPad, Listmap) { var Store = {}; @@ -164,9 +166,8 @@ define([ Store.clearOwnedChannel = function (data, cb) { if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); } - store.rpc.clearOwnedChannel(data.channel, function (err, res) { - if (err) { return void cb({error:err}); } - cb(res); + store.rpc.clearOwnedChannel(data, function (err) { + cb({error:err}); }); }; @@ -333,10 +334,25 @@ define([ cb(JSON.parse(JSON.stringify(metadata))); }; - // Reset the drive part of the userObject (from settings) - Store.resetDrive = function (data, cb) { - store.proxy.drive = store.fo.getStructure(); - onSync(cb); + var makePad = function (href, title) { + var now = +new Date(); + return { + href: href, + atime: now, + ctime: now, + title: title || Hash.getDefaultName(Hash.parsePadUrl(href)), + }; + }; + + Store.addPad = function (data, cb) { + if (!data.href) { return void cb({error:'NO_HREF'}); } + var pad = makePad(data.href, data.title); + store.userObject.pushData(pad, function (e, id) { + if (e) { return void cb({error: "Error while adding a template:"+ e}); } + var path = data.path || ['root']; + store.userObject.add(id, path); + onSync(cb); + }); }; /** @@ -414,6 +430,12 @@ define([ onSync(cb); }; + // Reset the drive part of the userObject (from settings) + Store.resetDrive = function (data, cb) { + store.proxy.drive = store.fo.getStructure(); + onSync(cb); + }; + /** * Settings & pad attributes * data @@ -463,27 +485,6 @@ define([ cb(all); }; - var makePad = function (href, title) { - var now = +new Date(); - return { - href: href, - atime: now, - ctime: now, - title: title || Hash.getDefaultName(Hash.parsePadUrl(href)), - }; - }; - - Store.addPad = function (data, cb) { - if (!data.href) { return void cb({error:'NO_HREF'}); } - var pad = makePad(data.href, data.title); - store.userObject.pushData(pad, function (e, id) { - if (e) { return void cb({error: "Error while adding a template:"+ e}); } - var path = data.path || ['root']; - store.userObject.add(id, path); - onSync(cb); - }); - }; - // Templates Store.getTemplates = function (data, cb) { var templateFiles = store.userObject.getFiles(['template']); @@ -606,20 +607,7 @@ define([ cb(list); }; - // Get hashes for the share button - Store.getStrongerHash = function (data, cb) { - var allPads = Util.find(store.proxy, ['drive', 'filesData']) || {}; - - // If we have a stronger version in drive, add it and add a redirect button - var stronger = Hash.findStronger(data.href, allPads); - if (stronger) { - var parsed2 = Hash.parsePadUrl(stronger); - return void cb(parsed2.hash); - } - cb(); - }; - - // Messaging + // Messaging (manage friends from the userlist) var getMessagingCfg = function () { return { proxy: store.proxy, @@ -642,6 +630,91 @@ define([ Messaging.inviteFromUserlist(messagingCfg, data, cb); }; + // Messenger + + // Get hashes for the share button + Store.getStrongerHash = function (data, cb) { + var allPads = Util.find(store.proxy, ['drive', 'filesData']) || {}; + + // If we have a stronger version in drive, add it and add a redirect button + var stronger = Hash.findStronger(data.href, allPads); + if (stronger) { + var parsed2 = Hash.parsePadUrl(stronger); + return void cb(parsed2.hash); + } + cb(); + }; + + Store.messenger = { + getFriendList: function (data, cb) { + store.messenger.getFriendList(function (e, keys) { + cb({ + error: e, + data: keys, + }); + }); + }, + getMyInfo: function (data, cb) { + store.messenger.getMyInfo(function (e, info) { + cb({ + error: e, + data: info, + }); + }); + }, + getFriendInfo: function (data, cb) { + store.messenger.getFriendInfo(data, function (e, info) { + cb({ + error: e, + data: info, + }); + }); + }, + removeFriend: function (data, cb) { + store.messenger.removeFriend(data, function (e, info) { + cb({ + error: e, + data: info, + }); + }); + }, + openFriendChannel: function (data, cb) { + store.messenger.openFriendChannel(data, function (e) { + cb({ error: e, }); + }); + }, + getFriendStatus: function (data, cb) { + store.messenger.getStatus(data, function (e, online) { + cb({ + error: e, + data: online, + }); + }); + }, + getMoreHistory: function (data, cb) { + store.messenger.getMoreHistory(data.curvePublic, data.sig, data.count, function (e, history) { + cb({ + error: e, + data: history, + }); + }); + }, + sendMessage: function (data, cb) { + store.messenger.sendMessage(data.curvePublic, data.content, function (e) { + cb({ + error: e, + }); + }); + }, + setChannelHead: function (data, cb) { + store.messenger.setChannelHead(data.curvePublic, data.sig, function (e) { + cb({ + error: e + }); + }); + } + }; + var onReady = function (returned, cb) { var proxy = store.proxy; var userObject = store.userObject = UserObject.init(proxy.drive, { @@ -791,7 +864,7 @@ define([ initialized = true; postMessage = function (cmd, d, cb) { setTimeout(function () { - data.query(cmd, d, cb); // TODO temporary, will be rzplaced by webworker channel + data.query(cmd, d, cb); // TODO temporary, will be replaced by webworker channel }); }; @@ -806,7 +879,40 @@ define([ var messagingCfg = getMessagingCfg(); Messaging.addDirectMessageHandler(messagingCfg); - + if (data.messenger) { + var messenger = store.messenger = Messenger.messenger(store); // TODO + messenger.on('message', function (message) { + postMessage('CONTACTS_MESSAGE', message); + }); + messenger.on('join', function (curvePublic, channel) { + postMessage('CONTACTS_JOIN', { + curvePublic: curvePublic, + channel: channel, + }); + }); + messenger.on('leave', function (curvePublic, channel) { + postMessage('CONTACTS_LEAVE', { + curvePublic: curvePublic, + channel: channel, + }); + }); + messenger.on('update', function (info, curvePublic) { + postMessage('CONTACTS_UPDATE', { + curvePublic: curvePublic, + info: info, + }); + }); + messenger.on('friend', function (curvePublic) { + postMessage('CONTACTS_FRIEND', { + curvePublic: curvePublic, + }); + }); + messenger.on('unfriend', function (curvePublic) { + postMessage('CONTACTS_UNFRIEND', { + curvePublic: curvePublic, + }); + }); + } }); }; diff --git a/www/common/outer/store-rpc.js b/www/common/outer/store-rpc.js index 0c0c71221..c0b435079 100644 --- a/www/common/outer/store-rpc.js +++ b/www/common/outer/store-rpc.js @@ -118,7 +118,34 @@ define([ case 'INVITE_FROM_USERLIST': { Store.inviteFromUserlist(data, cb); break; } - + // Messenger + case 'CONTACTS_GET_FRIEND_LIST': { + Store.messenger.getFriendList(data, cb); break; + } + case 'CONTACTS_GET_MY_INFO': { + Store.messenger.getMyInfo(data, cb); break; + } + case 'CONTACTS_GET_FRIEND_INFO': { + Store.messenger.getFriendInfo(data, cb); break; + } + case 'CONTACTS_REMOVE_FRIEND': { + Store.messenger.removeFriend(data, cb); break; + } + case 'CONTACTS_OPEN_FRIEND_CHANNEL': { + Store.messenger.openFriendChannel(data, cb); break; + } + case 'CONTACTS_GET_FRIEND_STATUS': { + Store.messenger.getFriendStatus(data, cb); break; + } + case 'CONTACTS_GET_MORE_HISTORY': { + Store.messenger.getMoreHistory(data, cb); break; + } + case 'CONTACTS_SEND_MESSAGE': { + Store.messenger.sendMessage(data, cb); break; + } + case 'CONTACTS_SET_CHANNEL_HEAD': { + Store.messenger.setChannelHead(data, cb); break; + } default: { break; diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index 76510078b..c9c6c9526 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -33,7 +33,6 @@ define([ '/common/cryptget.js', '/common/sframe-channel.js', '/filepicker/main.js', - //'/common/common-messenger.js', '/common/common-messaging.js', '/common/common-notifier.js', '/common/common-hash.js', @@ -45,7 +44,7 @@ define([ '/common/outer/network-config.js', '/bower_components/netflux-websocket/netflux-client.js', ], waitFor(function (_CpNfOuter, _Cryptpad, _Crypto, _Cryptget, _SFrameChannel, - _FilePicker, /*_Messenger,*/ _Messaging, _Notifier, _Hash, _Util, _Realtime, + _FilePicker, _Messaging, _Notifier, _Hash, _Util, _Realtime, _Constants, _Feedback, _LocalStore, NetConfig, Netflux) { CpNfOuter = _CpNfOuter; Cryptpad = _Cryptpad; @@ -53,7 +52,6 @@ define([ Cryptget = _Cryptget; SFrameChannel = _SFrameChannel; FilePicker = _FilePicker; - //Messenger = _Messenger; Messaging = _Messaging; Notifier = _Notifier; Utils.Hash = _Hash; @@ -87,7 +85,9 @@ define([ SFrameChannel.create($('#sbox-iframe')[0].contentWindow, waitFor(function (sfc) { sframeChan = sfc; }), false, { cache: cache, localStore: localStore, language: Cryptpad.getLanguage() }); - Cryptpad.ready(waitFor()); + Cryptpad.ready(waitFor(), { + messenger: cfg.messaging + }); if (!cfg.newNetwork) { Netflux.connect(NetConfig.getWebsocketURL()).then(waitFor(function (nw) { @@ -505,119 +505,59 @@ define([ } if (cfg.messaging) { - // TODO make messenger work with async store - /*var messenger = Messenger.messenger(Cryptpad); - sframeChan.on('Q_CONTACTS_GET_FRIEND_LIST', function (data, cb) { - messenger.getFriendList(function (e, keys) { - cb({ - error: e, - data: keys, - }); - }); + Cryptpad.messenger.getFriendList(cb); }); sframeChan.on('Q_CONTACTS_GET_MY_INFO', function (data, cb) { - messenger.getMyInfo(function (e, info) { - cb({ - error: e, - data: info, - }); - }); + Cryptpad.messenger.getMyInfo(cb); }); sframeChan.on('Q_CONTACTS_GET_FRIEND_INFO', function (curvePublic, cb) { - messenger.getFriendInfo(curvePublic, function (e, info) { - cb({ - error: e, - data: info, - }); - }); + Cryptpad.messenger.getFriendInfo(curvePublic, cb); }); sframeChan.on('Q_CONTACTS_REMOVE_FRIEND', function (curvePublic, cb) { - messenger.removeFriend(curvePublic, function (e, info) { - cb({ - error: e, - data: info, - }); - }); + Cryptpad.messenger.removeFriend(curvePublic, cb); }); sframeChan.on('Q_CONTACTS_OPEN_FRIEND_CHANNEL', function (curvePublic, cb) { - messenger.openFriendChannel(curvePublic, function (e) { - cb({ error: e, }); - }); + Cryptpad.messenger.openFriendChannel(curvePublic, cb); }); sframeChan.on('Q_CONTACTS_GET_STATUS', function (curvePublic, cb) { - messenger.getStatus(curvePublic, function (e, online) { - cb({ - error: e, - data: online, - }); - }); + Cryptpad.messenger.getFriendStatus(curvePublic, cb); }); sframeChan.on('Q_CONTACTS_GET_MORE_HISTORY', function (opt, cb) { - messenger.getMoreHistory(opt.curvePublic, opt.sig, opt.count, function (e, history) { - cb({ - error: e, - data: history, - }); - }); + Cryptpad.messenger.getMoreHistory(opt, cb); }); sframeChan.on('Q_CONTACTS_SEND_MESSAGE', function (opt, cb) { - messenger.sendMessage(opt.curvePublic, opt.content, function (e) { - cb({ - error: e, - }); - }); + Cryptpad.messenger.sendMessage(opt, cb); }); sframeChan.on('Q_CONTACTS_SET_CHANNEL_HEAD', function (opt, cb) { - messenger.setChannelHead(opt.curvePublic, opt.sig, function (e) { - cb({ - error: e - }); - }); + Cryptpad.messenger.setChannelHead(opt, cb); }); sframeChan.on('Q_CONTACTS_CLEAR_OWNED_CHANNEL', function (channel, cb) { - messenger.clearOwnedChannel(channel, function (e) { - cb({ - error: e, - }); - }); + Cryptpad.clearOwnedChannel(channel, cb); }); - messenger.on('message', function (message) { - sframeChan.event('EV_CONTACTS_MESSAGE', message); + Cryptpad.messenger.onMessageEvent.reg(function (data) { + sframeChan.event('EV_CONTACTS_MESSAGE', data); }); - messenger.on('join', function (curvePublic, channel) { - sframeChan.event('EV_CONTACTS_JOIN', { - curvePublic: curvePublic, - channel: channel, - }); + Cryptpad.messenger.onJoinEvent.reg(function (data) { + sframeChan.event('EV_CONTACTS_JOIN', data); }); - messenger.on('leave', function (curvePublic, channel) { - sframeChan.event('EV_CONTACTS_LEAVE', { - curvePublic: curvePublic, - channel: channel, - }); + Cryptpad.messenger.onLeaveEvent.reg(function (data) { + sframeChan.event('EV_CONTACTS_LEAVE', data); }); - messenger.on('update', function (info, curvePublic) { - sframeChan.event('EV_CONTACTS_UPDATE', { - curvePublic: curvePublic, - info: info, - }); + Cryptpad.messenger.onUpdateEvent.reg(function (data) { + sframeChan.event('EV_CONTACTS_UPDATE', data); }); - messenger.on('friend', function (curvePublic) { - sframeChan.event('EV_CONTACTS_FRIEND', { - curvePublic: curvePublic, - }); + Cryptpad.messenger.onFriendEvent.reg(function (data) { + sframeChan.event('EV_CONTACTS_FRIEND', data); + }); + Cryptpad.messenger.onUnfriendEvent.reg(function (data) { + sframeChan.event('EV_CONTACTS_UNFRIEND', data); }); - messenger.on('unfriend', function (curvePublic) { - sframeChan.event('EV_CONTACTS_UNFRIEND', { - curvePublic: curvePublic, - }); - });*/ } sframeChan.ready();