Enable messenger in async store

pull/1/head
yflory 7 years ago
parent bd85f1b003
commit 4903bd0757

@ -12,7 +12,7 @@ var map = {
var messages = {}; var messages = {};
var LS_LANG = "CRYPTPAD_LANG"; 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 getBrowserLanguage = function () { return navigator.language || navigator.userLanguage || ''; };
var getLanguage = messages._getLanguage = function () { var getLanguage = messages._getLanguage = function () {
if (window.cryptpadLanguage) { return window.cryptpadLanguage; } if (window.cryptpadLanguage) { return window.cryptpadLanguage; }
@ -24,19 +24,17 @@ var getLanguage = messages._getLanguage = function () {
}; };
var language = getLanguage(); 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'); } 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'; map.en = 'English';
var defaultLanguage = 'en'; var defaultLanguage = 'en';
if (!Language || language === defaultLanguage || !map[language]) { Util.extend(messages, Default);
messages = $.extend(true, messages, Default); if (Language && language !== defaultLanguage) {
}
else {
// Add the translated keys to the returned object // Add the translated keys to the returned object
messages = $.extend(true, messages, Default, Language); Util.extend(messages, Language);
} }
messages._languages = map; messages._languages = map;

@ -5,7 +5,8 @@ define([
'/common/common-hash.js', '/common/common-hash.js',
'/common/common-util.js', '/common/common-util.js',
'/common/common-realtime.js', '/common/common-realtime.js',
], function ($, Crypto, Curve, Hash, Util, Realtime) { '/common/common-constants.js',
], function ($, Crypto, Curve, Hash, Util, Realtime, Constants) {
'use strict'; 'use strict';
var Msg = { var Msg = {
inputs: [], inputs: [],
@ -28,7 +29,7 @@ define([
var createData = Msg.createData = function (proxy, hash) { var createData = Msg.createData = function (proxy, hash) {
return { return {
channel: hash || Hash.createChannelId(), channel: hash || Hash.createChannelId(),
displayName: proxy['cryptpad.username'], displayName: proxy[Constants.displayNameKey],
profile: proxy.profile && proxy.profile.view, profile: proxy.profile && proxy.profile.view,
edPublic: proxy.edPublic, edPublic: proxy.edPublic,
curvePublic: proxy.curvePublic, curvePublic: proxy.curvePublic,
@ -56,7 +57,7 @@ define([
}); });
}; };
Msg.messenger = function (common) { Msg.messenger = function (store) {
var messenger = { var messenger = {
handlers: { handlers: {
message: [], message: [],
@ -89,9 +90,9 @@ define([
var joining = {}; var joining = {};
// declare common variables // declare common variables
var network = common.getNetwork(); var network = store.network;
var proxy = common.getProxy(); var proxy = store.proxy;
var realtime = common.getRealtime(); var realtime = store.realtime;
Msg.hk = network.historyKeeper; Msg.hk = network.historyKeeper;
var friends = getFriendList(proxy); var friends = getFriendList(proxy);
@ -626,18 +627,10 @@ define([
}); });
}; };
// TODO displayName
messenger.getMyInfo = function (cb) { messenger.getMyInfo = function (cb) {
cb(void 0, { cb(void 0, {
curvePublic: proxy.curvePublic, curvePublic: proxy.curvePublic,
displayName: '' //common.getDisplayName(), displayName: proxy[Constants.displayNameKey]
});
};
messenger.clearOwnedChannel = function (channel, cb) {
common.clearOwnedChannel(channel, function (e) {
if (e) { return void cb(e); }
cb();
}); });
}; };

@ -209,6 +209,43 @@ define([], function () {
xhr.send(); 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; return Util;
}); });
}(self)); }(self));

@ -47,6 +47,15 @@ define([
var PINNING_ENABLED = AppConfig.enablePinning; var PINNING_ENABLED = AppConfig.enablePinning;
// COMMON
common.getLanguage = function () {
return Messages._languageUsed;
};
common.setLanguage = function (l, cb) {
Language.setLanguage(l, null, cb);
};
// RESTRICTED // RESTRICTED
// Settings only // Settings only
common.getUserObject = function (cb) { common.getUserObject = function (cb) {
@ -119,14 +128,125 @@ define([
}; };
// REFACTOR pull language directly? // RPC
common.getLanguage = function () { common.pinPads = function (pads, cb) {
return Messages._languageUsed; 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) { common.getMetadata = function (cb) {
postMessage("GET_METADATA", null, function (obj) { postMessage("GET_METADATA", null, function (obj) {
if (obj && obj.error) { return void cb(obj.error); } if (obj && obj.error) { return void cb(obj.error); }
@ -138,7 +258,6 @@ define([
postMessage("SET_DISPLAY_NAME", value, cb); postMessage("SET_DISPLAY_NAME", value, cb);
}; };
// STORAGE
common.setPadAttribute = function (attr, value, cb, href) { common.setPadAttribute = function (attr, value, cb, href) {
href = Hash.getRelativeHref(href || window.location.href); href = Hash.getRelativeHref(href || window.location.href);
postMessage("SET_PAD_ATTRIBUTE", { postMessage("SET_PAD_ATTRIBUTE", {
@ -178,7 +297,6 @@ define([
}); });
}; };
// Tags // Tags
common.resetTags = function (href, tags, cb) { common.resetTags = function (href, tags, cb) {
// set pad attribute // set pad attribute
@ -320,133 +438,52 @@ define([
}); });
}; };
common.pinPads = function (pads, cb) { // Messaging (manage friends from the userlist)
postMessage("PIN_PADS", pads, function (obj) { common.inviteFromUserlist = function (netfluxId, cb) {
if (obj && obj.error) { return void cb(obj.error); } postMessage("INVITE_FROM_USERLIST", {
cb(null, obj.hash); netfluxId: netfluxId,
}); href: window.location.href
};
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
}, function (obj) { }, function (obj) {
if (obj && obj.error) { return void cb(obj.error); } if (obj && obj.error) { return void cb(obj.error); }
cb(null, obj); cb();
}); });
}; };
common.getFileSize = function (href, cb) { // Messenger
postMessage("GET_FILE_SIZE", {href: href}, function (obj) { var messenger = common.messenger = {};
if (obj && obj.error) { return void cb(obj.error); } messenger.getFriendList = function (cb) {
cb(undefined, obj.size); postMessage("CONTACTS_GET_FRIEND_LIST", null, cb);
});
}; };
messenger.getMyInfo = function (cb) {
// TODO not used anymore? postMessage("CONTACTS_GET_MY_INFO", null, cb);
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.getFriendInfo = function (curvePublic, cb) {
common.updatePinLimit = function (cb) { postMessage("CONTACTS_GET_FRIEND_INFO", curvePublic, 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.removeFriend = function (curvePublic, cb) {
common.getPinLimit = function (cb) { postMessage("CONTACTS_REMOVE_FRIEND", curvePublic, 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.openFriendChannel = function (curvePublic, cb) {
common.isOverPinLimit = function (cb) { postMessage("CONTACTS_OPEN_FRIEND_CHANNEL", curvePublic, 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.getFriendStatus = function (curvePublic, cb) {
common.clearOwnedChannel = function (channel, cb) { postMessage("CONTACTS_GET_FRIEND_STATUS", curvePublic, cb);
postMessage("CLEAR_OWNED_CHANNEL", {channel: channel}, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
cb(null, obj);
});
}; };
messenger.getMoreHistory = function (data, cb) {
common.uploadChunk = function (data, cb) { postMessage("CONTACTS_GET_MORE_HISTORY", data, cb);
postMessage("UPLOAD_CHUNK", {chunk: data}, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
cb(null, obj);
});
}; };
messenger.sendMessage = function (data, cb) {
common.uploadComplete = function (cb) { postMessage("CONTACTS_SEND_MESSAGE", data, cb);
postMessage("UPLOAD_COMPLETE", null, function (obj) {
if (obj && obj.error) { return void cb(obj.error); }
cb(null, obj);
});
}; };
messenger.setChannelHead = function (data, cb) {
common.uploadStatus = function (size, cb) { postMessage("CONTACTS_SET_CHANNEL_HEAD", data, 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.onMessageEvent = Util.mkEvent();
messenger.onJoinEvent = Util.mkEvent();
messenger.onLeaveEvent = Util.mkEvent();
messenger.onUpdateEvent = Util.mkEvent();
messenger.onFriendEvent = Util.mkEvent();
messenger.onUnfriendEvent = Util.mkEvent();
// HERE // HERE
common.getShareHashes = function (secret, cb) { common.getShareHashes = function (secret, cb) {
@ -540,6 +577,25 @@ define([
common.onFriendComplete(data); common.onFriendComplete(data);
break; 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 env = {};
var initialized = false; var initialized = false;
return function (f) { return function (f, rdyCfg) {
if (initialized) { if (initialized) {
return void setTimeout(function () { f(void 0, env); }); return void setTimeout(function () { f(void 0, env); });
} }
@ -583,7 +639,9 @@ define([
query: onMessage, // TODO temporary, will be replaced by a webworker channel query: onMessage, // TODO temporary, will be replaced by a webworker channel
userHash: LocalStore.getUserHash(), userHash: LocalStore.getUserHash(),
anonHash: LocalStore.getFSHash(), 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]) { if (sessionStorage[Constants.newPadPathKey]) {
cfg.initialPath = sessionStorage[Constants.newPadPathKey]; cfg.initialPath = sessionStorage[Constants.newPadPathKey];

@ -7,12 +7,14 @@ define([
'/common/common-feedback.js', '/common/common-feedback.js',
'/common/common-realtime.js', '/common/common-realtime.js',
'/common/common-messaging.js', '/common/common-messaging.js',
'/common/common-messenger.js',
'/common/outer/network-config.js', '/common/outer/network-config.js',
'/bower_components/chainpad-crypto/crypto.js?v=0.1.5', '/bower_components/chainpad-crypto/crypto.js?v=0.1.5',
'/bower_components/chainpad/chainpad.dist.js', '/bower_components/chainpad/chainpad.dist.js',
'/bower_components/chainpad-listmap/chainpad-listmap.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) { Crypto, ChainPad, Listmap) {
var Store = {}; var Store = {};
@ -164,9 +166,8 @@ define([
Store.clearOwnedChannel = function (data, cb) { Store.clearOwnedChannel = function (data, cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); } if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
store.rpc.clearOwnedChannel(data.channel, function (err, res) { store.rpc.clearOwnedChannel(data, function (err) {
if (err) { return void cb({error:err}); } cb({error:err});
cb(res);
}); });
}; };
@ -333,10 +334,25 @@ define([
cb(JSON.parse(JSON.stringify(metadata))); cb(JSON.parse(JSON.stringify(metadata)));
}; };
// Reset the drive part of the userObject (from settings) var makePad = function (href, title) {
Store.resetDrive = function (data, cb) { var now = +new Date();
store.proxy.drive = store.fo.getStructure(); return {
onSync(cb); 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); 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 * Settings & pad attributes
* data * data
@ -463,27 +485,6 @@ define([
cb(all); 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 // Templates
Store.getTemplates = function (data, cb) { Store.getTemplates = function (data, cb) {
var templateFiles = store.userObject.getFiles(['template']); var templateFiles = store.userObject.getFiles(['template']);
@ -606,20 +607,7 @@ define([
cb(list); cb(list);
}; };
// Get hashes for the share button // Messaging (manage friends from the userlist)
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
var getMessagingCfg = function () { var getMessagingCfg = function () {
return { return {
proxy: store.proxy, proxy: store.proxy,
@ -642,6 +630,91 @@ define([
Messaging.inviteFromUserlist(messagingCfg, data, cb); 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 onReady = function (returned, cb) {
var proxy = store.proxy; var proxy = store.proxy;
var userObject = store.userObject = UserObject.init(proxy.drive, { var userObject = store.userObject = UserObject.init(proxy.drive, {
@ -791,7 +864,7 @@ define([
initialized = true; initialized = true;
postMessage = function (cmd, d, cb) { postMessage = function (cmd, d, cb) {
setTimeout(function () { 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(); var messagingCfg = getMessagingCfg();
Messaging.addDirectMessageHandler(messagingCfg); 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,
});
});
}
}); });
}; };

@ -118,7 +118,34 @@ define([
case 'INVITE_FROM_USERLIST': { case 'INVITE_FROM_USERLIST': {
Store.inviteFromUserlist(data, cb); break; 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: { default: {
break; break;

@ -33,7 +33,6 @@ define([
'/common/cryptget.js', '/common/cryptget.js',
'/common/sframe-channel.js', '/common/sframe-channel.js',
'/filepicker/main.js', '/filepicker/main.js',
//'/common/common-messenger.js',
'/common/common-messaging.js', '/common/common-messaging.js',
'/common/common-notifier.js', '/common/common-notifier.js',
'/common/common-hash.js', '/common/common-hash.js',
@ -45,7 +44,7 @@ define([
'/common/outer/network-config.js', '/common/outer/network-config.js',
'/bower_components/netflux-websocket/netflux-client.js', '/bower_components/netflux-websocket/netflux-client.js',
], waitFor(function (_CpNfOuter, _Cryptpad, _Crypto, _Cryptget, _SFrameChannel, ], 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) { _Constants, _Feedback, _LocalStore, NetConfig, Netflux) {
CpNfOuter = _CpNfOuter; CpNfOuter = _CpNfOuter;
Cryptpad = _Cryptpad; Cryptpad = _Cryptpad;
@ -53,7 +52,6 @@ define([
Cryptget = _Cryptget; Cryptget = _Cryptget;
SFrameChannel = _SFrameChannel; SFrameChannel = _SFrameChannel;
FilePicker = _FilePicker; FilePicker = _FilePicker;
//Messenger = _Messenger;
Messaging = _Messaging; Messaging = _Messaging;
Notifier = _Notifier; Notifier = _Notifier;
Utils.Hash = _Hash; Utils.Hash = _Hash;
@ -87,7 +85,9 @@ define([
SFrameChannel.create($('#sbox-iframe')[0].contentWindow, waitFor(function (sfc) { SFrameChannel.create($('#sbox-iframe')[0].contentWindow, waitFor(function (sfc) {
sframeChan = sfc; sframeChan = sfc;
}), false, { cache: cache, localStore: localStore, language: Cryptpad.getLanguage() }); }), false, { cache: cache, localStore: localStore, language: Cryptpad.getLanguage() });
Cryptpad.ready(waitFor()); Cryptpad.ready(waitFor(), {
messenger: cfg.messaging
});
if (!cfg.newNetwork) { if (!cfg.newNetwork) {
Netflux.connect(NetConfig.getWebsocketURL()).then(waitFor(function (nw) { Netflux.connect(NetConfig.getWebsocketURL()).then(waitFor(function (nw) {
@ -505,119 +505,59 @@ define([
} }
if (cfg.messaging) { 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) { sframeChan.on('Q_CONTACTS_GET_FRIEND_LIST', function (data, cb) {
messenger.getFriendList(function (e, keys) { Cryptpad.messenger.getFriendList(cb);
cb({
error: e,
data: keys,
});
});
}); });
sframeChan.on('Q_CONTACTS_GET_MY_INFO', function (data, cb) { sframeChan.on('Q_CONTACTS_GET_MY_INFO', function (data, cb) {
messenger.getMyInfo(function (e, info) { Cryptpad.messenger.getMyInfo(cb);
cb({
error: e,
data: info,
});
});
}); });
sframeChan.on('Q_CONTACTS_GET_FRIEND_INFO', function (curvePublic, cb) { sframeChan.on('Q_CONTACTS_GET_FRIEND_INFO', function (curvePublic, cb) {
messenger.getFriendInfo(curvePublic, function (e, info) { Cryptpad.messenger.getFriendInfo(curvePublic, cb);
cb({
error: e,
data: info,
});
});
}); });
sframeChan.on('Q_CONTACTS_REMOVE_FRIEND', function (curvePublic, cb) { sframeChan.on('Q_CONTACTS_REMOVE_FRIEND', function (curvePublic, cb) {
messenger.removeFriend(curvePublic, function (e, info) { Cryptpad.messenger.removeFriend(curvePublic, cb);
cb({
error: e,
data: info,
});
});
}); });
sframeChan.on('Q_CONTACTS_OPEN_FRIEND_CHANNEL', function (curvePublic, cb) { sframeChan.on('Q_CONTACTS_OPEN_FRIEND_CHANNEL', function (curvePublic, cb) {
messenger.openFriendChannel(curvePublic, function (e) { Cryptpad.messenger.openFriendChannel(curvePublic, cb);
cb({ error: e, });
});
}); });
sframeChan.on('Q_CONTACTS_GET_STATUS', function (curvePublic, cb) { sframeChan.on('Q_CONTACTS_GET_STATUS', function (curvePublic, cb) {
messenger.getStatus(curvePublic, function (e, online) { Cryptpad.messenger.getFriendStatus(curvePublic, cb);
cb({
error: e,
data: online,
});
});
}); });
sframeChan.on('Q_CONTACTS_GET_MORE_HISTORY', function (opt, cb) { sframeChan.on('Q_CONTACTS_GET_MORE_HISTORY', function (opt, cb) {
messenger.getMoreHistory(opt.curvePublic, opt.sig, opt.count, function (e, history) { Cryptpad.messenger.getMoreHistory(opt, cb);
cb({
error: e,
data: history,
});
});
}); });
sframeChan.on('Q_CONTACTS_SEND_MESSAGE', function (opt, cb) { sframeChan.on('Q_CONTACTS_SEND_MESSAGE', function (opt, cb) {
messenger.sendMessage(opt.curvePublic, opt.content, function (e) { Cryptpad.messenger.sendMessage(opt, cb);
cb({
error: e,
});
});
}); });
sframeChan.on('Q_CONTACTS_SET_CHANNEL_HEAD', function (opt, cb) { sframeChan.on('Q_CONTACTS_SET_CHANNEL_HEAD', function (opt, cb) {
messenger.setChannelHead(opt.curvePublic, opt.sig, function (e) { Cryptpad.messenger.setChannelHead(opt, cb);
cb({
error: e
});
});
}); });
sframeChan.on('Q_CONTACTS_CLEAR_OWNED_CHANNEL', function (channel, cb) { sframeChan.on('Q_CONTACTS_CLEAR_OWNED_CHANNEL', function (channel, cb) {
messenger.clearOwnedChannel(channel, function (e) { Cryptpad.clearOwnedChannel(channel, cb);
cb({
error: e,
});
});
}); });
messenger.on('message', function (message) { Cryptpad.messenger.onMessageEvent.reg(function (data) {
sframeChan.event('EV_CONTACTS_MESSAGE', message); sframeChan.event('EV_CONTACTS_MESSAGE', data);
}); });
messenger.on('join', function (curvePublic, channel) { Cryptpad.messenger.onJoinEvent.reg(function (data) {
sframeChan.event('EV_CONTACTS_JOIN', { sframeChan.event('EV_CONTACTS_JOIN', data);
curvePublic: curvePublic,
channel: channel,
});
}); });
messenger.on('leave', function (curvePublic, channel) { Cryptpad.messenger.onLeaveEvent.reg(function (data) {
sframeChan.event('EV_CONTACTS_LEAVE', { sframeChan.event('EV_CONTACTS_LEAVE', data);
curvePublic: curvePublic,
channel: channel,
});
}); });
messenger.on('update', function (info, curvePublic) { Cryptpad.messenger.onUpdateEvent.reg(function (data) {
sframeChan.event('EV_CONTACTS_UPDATE', { sframeChan.event('EV_CONTACTS_UPDATE', data);
curvePublic: curvePublic,
info: info,
});
}); });
messenger.on('friend', function (curvePublic) { Cryptpad.messenger.onFriendEvent.reg(function (data) {
sframeChan.event('EV_CONTACTS_FRIEND', { sframeChan.event('EV_CONTACTS_FRIEND', data);
curvePublic: curvePublic, });
}); 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(); sframeChan.ready();

Loading…
Cancel
Save