Refactor messenger to use commands from UI to store

pull/1/head
yflory 6 years ago
parent cc3e6f3924
commit ca45ff31d1

@ -62,12 +62,6 @@ define([
Msg.messenger = function (store) {
var messenger = {
handlers: {
message: [],
join: [],
leave: [],
update: [],
friend: [],
unfriend: [],
event: []
},
range_requests: {},
@ -136,12 +130,12 @@ define([
delete messenger.range_requests[txid];
};
messenger.getMoreHistory = function (chanId, hash, count, cb) {
var getMoreHistory = function (chanId, hash, count, cb) {
if (typeof(cb) !== 'function') { return; }
if (typeof(hash) !== 'string') {
// Channel is empty!
return void cb(void 0, []);
return void cb([]);
}
var chan = getChannel(chanId);
@ -189,17 +183,17 @@ define([
}
};*/
messenger.setChannelHead = function (id, hash, cb) {
var setChannelHead = function (id, hash, cb) {
var channel = getChannel(id);
if (channel.isFriendChat) {
var friend = getFriendFromChannel(id);
if (!friend) { return void cb('NO_SUCH_FRIEND'); }
if (!friend) { return void cb({error: 'NO_SUCH_FRIEND'}); }
friend.lastKnownHash = hash;
} else if (channel.isPadChat) {
// Nothing to do
} else {
// TODO room
return void cb('NOT_IMPLEMENTED');
return void cb({error: 'NOT_IMPLEMENTED'});
}
cb();
};
@ -217,8 +211,10 @@ define([
}
});
eachHandler('update', function (f) {
f(clone(data), types, channel);
emit('UPDATE_DATA', {
info: clone(data),
types: types,
channel: channel
});
};
@ -263,8 +259,9 @@ define([
if (parsed[2] !== sender || !parsed[1]) { return; }
channel.mapId[sender] = parsed[1];
checkFriendData(parsed[1].curvePublic, parsed[1], channel.id);
eachHandler('join', function (f) {
f(parsed[1], channel.id);
emit('JOIN', {
info: parsed[1],
id: channel.id
});
if (parsed[0] !== Types.mapId) { return; } // Don't send your key if it's already an ACK
@ -321,9 +318,7 @@ define([
channel.messages.push(res);
if (!joining[channel.id]) {
// Channel is ready
eachHandler('message', function (f) {
f(res);
});
emit('MESSAGE', res);
}
return true;
@ -342,8 +337,9 @@ define([
removeFromFriendList(curvePublic, function () {
channel.wc.leave(Types.unfriend);
delete channels[channel.id];
eachHandler('unfriend', function (f) {
f(curvePublic, false);
emit('UNFRIEND', {
curvePublic: curvePublic,
fromMe: false
});
});
return;
@ -380,8 +376,10 @@ define([
console.error(err);
});
});
eachHandler('update', function (f) {
f(myData, ['displayName', 'profile', 'avatar']);
emit('UPDATE', {
info: myData,
types: ['displayName', 'profile', 'avatar'],
});
friends.me = myData;
}
@ -490,7 +488,7 @@ define([
});
orderMessages(channel, decrypted);
req.cb(void 0, decrypted);
req.cb(decrypted);
return deleteRangeRequest(txid);
} else {
console.log(parsed);
@ -505,14 +503,14 @@ define([
// Error in initial history
// History cleared while we're in the channel
if (parsed.error === 'ECLEARED') {
messenger.setChannelHead(parsed.channel, '', function () {});
setChannelHead(parsed.channel, '', function () {});
emit('CLEAR_CHANNEL', parsed.channel);
return;
}
// History cleared while we were offline
// ==> we asked for an invalid last known hash
if (parsed.error && parsed.error === "EINVAL") {
messenger.setChannelHead(parsed.channel, '', function () {
setChannelHead(parsed.channel, '', function () {
getChannelMessagesSince(getChannel(parsed.channel), {}, {});
});
return;
@ -551,19 +549,19 @@ define([
onDirectMessage(msg, sender);
});
messenger.removeFriend = function (curvePublic, cb) {
var removeFriend = function (curvePublic, cb) {
if (typeof(cb) !== 'function') { throw new Error('NO_CALLBACK'); }
var data = getFriend(proxy, curvePublic);
if (!data) {
// friend is not valid
console.error('friend is not valid');
return void cb('INVALID_FRIEND');
return void cb({error: 'INVALID_FRIEND'});
}
var channel = channels[data.channel];
if (!channel) {
return void cb("NO_SUCH_CHANNEL");
return void cb({error: "NO_SUCH_CHANNEL"});
}
if (!network.webChannels.some(function (wc) {
@ -580,17 +578,18 @@ define([
channel.wc.bcast(cryptMsg).then(function () {
removeFromFriendList(curvePublic, function () {
delete channels[channel.id];
eachHandler('unfriend', function (f) {
f(curvePublic, true);
emit('UNFRIEND', {
curvePublic: curvePublic,
fromMe: true
});
cb();
});
}, function (err) {
console.error(err);
cb(err);
cb({error: err});
});
} catch (e) {
cb(e);
cb({error: e});
}
};
@ -644,8 +643,9 @@ define([
})) { return; }
// Send the notification
eachHandler('leave', function (f) {
f(otherData, channel.id);
emit('LEAVE', {
info: otherData,
id: channel.id
});
};
@ -689,27 +689,13 @@ define([
}));
};
/*messenger.openFriendChannel = function (curvePublic, cb) {
if (typeof(curvePublic) !== 'string') { return void cb('INVALID_ID'); }
if (typeof(cb) !== 'function') { throw new Error('expected callback'); }
var friend = clone(friends[curvePublic]);
if (typeof(friend) !== 'object') {
return void cb('NO_FRIEND_DATA');
}
var channel = friend.channel;
if (!channel) { return void cb('E_NO_CHANNEL'); }
joining[channel] = cb;
openFriendChannel(friend, curvePublic);
};*/
messenger.sendMessage = function (id, payload, cb) {
var sendMessage = function (id, payload, cb) {
var channel = getChannel(id);
if (!channel) { return void cb('NO_CHANNEL'); }
if (!channel) { return void cb({error: 'NO_CHANNEL'}); }
if (!network.webChannels.some(function (wc) {
if (wc.id === channel.wc.id) { return true; }
})) {
return void cb('NO_SUCH_CHANNEL');
return void cb({error: 'NO_SUCH_CHANNEL'});
}
var msg = [Types.message, proxy.curvePublic, +new Date(), payload];
@ -725,11 +711,11 @@ define([
pushMsg(channel, cryptMsg);
cb();
}, function (err) {
cb(err);
cb({error: err});
});
};
messenger.getStatus = function (chanId, cb) {
var getStatus = function (chanId, cb) {
// Display green status if one member is not me
var channel = getChannel(chanId);
if (!channel) { return void cb('NO_SUCH_CHANNEL'); }
@ -738,26 +724,11 @@ define([
if (!data) { return false; }
return data.curvePublic !== proxy.curvePublic;
});
cb(void 0, online);
cb(online);
};
messenger.getFriendInfo = function (channel, cb) {
setTimeout(function () {
var friend;
for (var k in friends) {
if (friends[k].channel === channel) {
friend = friends[k];
break;
}
}
if (!friend) { return void cb('NO_SUCH_FRIEND'); }
// this clone will be redundant when ui uses postmessage
cb(void 0, clone(friend));
});
};
messenger.getMyInfo = function (cb) {
cb(void 0, {
var getMyInfo = function (cb) {
cb({
curvePublic: proxy.curvePublic,
displayName: proxy[Constants.displayNameKey]
});
@ -792,8 +763,8 @@ define([
var channel = friend.channel;
if (!channel) { return; }
loadFriend(friend, function () {
eachHandler('friend', function (f) {
f(curvePublic);
emit('FRIEND', {
curvePublic: curvePublic,
});
});
return;
@ -810,8 +781,9 @@ define([
var channel = channels[o];
channel.wc.leave(Types.unfriend);
delete channels[channel.id];
eachHandler('unfriend', function (f) {
f(curvePublic, true);
emit('UNFRIEND', {
curvePublic: curvePublic,
fromMe: true
});
});
@ -822,8 +794,8 @@ define([
var channel = friend.channel;
if (!channel) { return; }
loadFriend(friend, function () {
eachHandler('friend', function (f) {
f(friend.curvePublic);
emit('FRIEND', {
curvePublic: friend.curvePublic,
});
});
};
@ -982,6 +954,24 @@ define([
if (cmd === 'OPEN_PAD_CHAT') {
return void openPadChat(data, cb);
}
if (cmd === 'GET_MY_INFO') {
return void getMyInfo(cb);
}
if (cmd === 'REMOVE_FRIEND') {
return void removeFriend(data, cb);
}
if (cmd === 'GET_STATUS') {
return void getStatus(data, cb);
}
if (cmd === 'GET_MORE_HISTORY') {
return void getMoreHistory(data.id, data.sig, data.count, cb);
}
if (cmd === 'SEND_MESSAGE') {
return void sendMessage(data.id, data.content, cb);
}
if (cmd === 'SET_CHANNEL_HEAD') {
return void setChannelHead(data.id, data.sig, cb);
}
};
Object.freeze(messenger);

@ -619,45 +619,10 @@ define([
// Messenger
var messenger = common.messenger = {};
messenger.getFriendList = function (cb) {
postMessage("CONTACTS_GET_FRIEND_LIST", null, cb);
};
messenger.getMyInfo = function (cb) {
postMessage("CONTACTS_GET_MY_INFO", null, cb);
};
messenger.getFriendInfo = function (curvePublic, cb) {
postMessage("CONTACTS_GET_FRIEND_INFO", curvePublic, cb);
};
messenger.removeFriend = function (curvePublic, cb) {
postMessage("CONTACTS_REMOVE_FRIEND", curvePublic, cb);
};
messenger.openFriendChannel = function (curvePublic, cb) {
postMessage("CONTACTS_OPEN_FRIEND_CHANNEL", curvePublic, cb);
};
messenger.getFriendStatus = function (curvePublic, cb) {
postMessage("CONTACTS_GET_FRIEND_STATUS", curvePublic, cb);
};
messenger.getMoreHistory = function (data, cb) {
postMessage("CONTACTS_GET_MORE_HISTORY", data, cb);
};
messenger.sendMessage = function (data, cb) {
postMessage("CONTACTS_SEND_MESSAGE", data, cb);
};
messenger.setChannelHead = function (data, cb) {
postMessage("CONTACTS_SET_CHANNEL_HEAD", data, cb);
};
messenger.execCommand = function (data, cb) {
postMessage("CHAT_COMMAND", data, cb);
};
messenger.onEvent = Util.mkEvent();
messenger.onMessageEvent = Util.mkEvent();
messenger.onJoinEvent = Util.mkEvent();
messenger.onLeaveEvent = Util.mkEvent();
messenger.onUpdateEvent = Util.mkEvent();
messenger.onFriendEvent = Util.mkEvent();
messenger.onUnfriendEvent = Util.mkEvent();
// Pad RPC
var pad = common.padRpc = {};
@ -1082,13 +1047,6 @@ define([
common.onNetworkReconnect.fire(data);
});
},
// Messenger
CONTACTS_MESSAGE: common.messenger.onMessageEvent.fire,
CONTACTS_JOIN: common.messenger.onJoinEvent.fire,
CONTACTS_LEAVE: common.messenger.onLeaveEvent.fire,
CONTACTS_UPDATE: common.messenger.onUpdateEvent.fire,
CONTACTS_FRIEND: common.messenger.onFriendEvent.fire,
CONTACTS_UNFRIEND: common.messenger.onUnfriendEvent.fire,
// Chat
CHAT_EVENT: common.messenger.onEvent.fire,
// Pad

@ -898,83 +898,6 @@ define([
};
Store.messenger = {
getFriendList: function (clientId, data, cb) {
if (!store.messenger) { return void cb({error: 'Messenger is disabled'}); }
store.messenger.getFriendList(function (e, keys) {
cb({
error: e,
data: keys,
});
});
},
getMyInfo: function (clientId, data, cb) {
if (!store.messenger) { return void cb({error: 'Messenger is disabled'}); }
store.messenger.getMyInfo(function (e, info) {
cb({
error: e,
data: info,
});
});
},
getFriendInfo: function (clientId, data, cb) {
if (!store.messenger) { return void cb({error: 'Messenger is disabled'}); }
store.messenger.getFriendInfo(data, function (e, info) {
cb({
error: e,
data: info,
});
});
},
removeFriend: function (clientId, data, cb) {
if (!store.messenger) { return void cb({error: 'Messenger is disabled'}); }
store.messenger.removeFriend(data, function (e, info) {
cb({
error: e,
data: info,
});
});
},
openFriendChannel: function (clientId, data, cb) {
if (!store.messenger) { return void cb({error: 'Messenger is disabled'}); }
store.messenger.openFriendChannel(data, function (e) {
cb({ error: e, });
});
},
getFriendStatus: function (clientId, data, cb) {
if (!store.messenger) { return void cb({error: 'Messenger is disabled'}); }
store.messenger.getStatus(data, function (e, online) {
cb({
error: e,
data: online,
});
});
},
getMoreHistory: function (clientId, data, cb) {
if (!store.messenger) { return void cb({error: 'Messenger is disabled'}); }
store.messenger.getMoreHistory(data.curvePublic, data.sig, data.count, function (e, history) {
cb({
error: e,
data: history,
});
});
},
sendMessage: function (clientId, data, cb) {
if (!store.messenger) { return void cb({error: 'Messenger is disabled'}); }
store.messenger.sendMessage(data.curvePublic, data.content, function (e) {
cb({
error: e,
});
});
},
setChannelHead: function (clientId, data, cb) {
if (!store.messenger) { return void cb({error: 'Messenger is disabled'}); }
store.messenger.setChannelHead(data.curvePublic, data.sig, function (e) {
cb({
error: e
});
});
},
execCommand: function (clientId, data, cb) {
if (!store.messenger) { return void cb({error: 'Messenger is disabled'}); }
store.messenger.execCommand(data, cb);
@ -1360,39 +1283,6 @@ define([
var loadMessenger = function () {
if (AppConfig.availablePadTypes.indexOf('contacts') === -1) { return; }
var messenger = store.messenger = Messenger.messenger(store);
messenger.on('message', function (message) {
sendMessengerEvent('CONTACTS_MESSAGE', message);
});
messenger.on('join', function (curvePublic, channel) {
sendMessengerEvent('CONTACTS_JOIN', {
curvePublic: curvePublic,
channel: channel,
});
});
messenger.on('leave', function (curvePublic, channel) {
sendMessengerEvent('CONTACTS_LEAVE', {
curvePublic: curvePublic,
channel: channel,
});
});
messenger.on('update', function (info, types, channel) {
sendMessengerEvent('CONTACTS_UPDATE', {
types: types,
info: info,
channel: channel
});
});
messenger.on('friend', function (curvePublic) {
sendMessengerEvent('CONTACTS_FRIEND', {
curvePublic: curvePublic,
});
});
messenger.on('unfriend', function (curvePublic, removedByMe) {
sendMessengerEvent('CONTACTS_UNFRIEND', {
curvePublic: curvePublic,
removedByMe: removedByMe
});
});
messenger.on('event', function (ev, data) {
sendMessengerEvent('CHAT_EVENT', {
ev: ev,

@ -60,16 +60,6 @@ define([
// Messaging
INVITE_FROM_USERLIST: Store.inviteFromUserlist,
ADD_DIRECT_MESSAGE_HANDLERS: Store.addDirectMessageHandlers,
// Messenger
CONTACTS_GET_FRIEND_LIST: Store.messenger.getFriendList,
CONTACTS_GET_MY_INFO: Store.messenger.getMyInfo,
CONTACTS_GET_FRIEND_INFO: Store.messenger.getFriendInfo,
CONTACTS_REMOVE_FRIEND: Store.messenger.removeFriend,
CONTACTS_OPEN_FRIEND_CHANNEL: Store.messenger.openFriendChannel,
CONTACTS_GET_FRIEND_STATUS: Store.messenger.getFriendStatus,
CONTACTS_GET_MORE_HISTORY: Store.messenger.getMoreHistory,
CONTACTS_SEND_MESSAGE: Store.messenger.sendMessage,
CONTACTS_SET_CHANNEL_HEAD: Store.messenger.setChannelHead,
// Chat
CHAT_COMMAND: Store.messenger.execCommand,
// Pad

@ -713,7 +713,7 @@ define([
Cryptpad.setLanguage(data, cb);
});
sframeChan.on('Q_CONTACTS_CLEAR_OWNED_CHANNEL', function (channel, cb) {
sframeChan.on('Q_CLEAR_OWNED_CHANNEL', function (channel, cb) {
Cryptpad.clearOwnedChannel(channel, cb);
});
sframeChan.on('Q_REMOVE_OWNED_CHANNEL', function (channel, cb) {
@ -794,37 +794,6 @@ define([
if (cfg.messaging) {
Notifier.getPermission();
sframeChan.on('Q_CONTACTS_GET_FRIEND_LIST', function (data, cb) {
Cryptpad.messenger.getFriendList(cb);
});
sframeChan.on('Q_CONTACTS_GET_MY_INFO', function (data, cb) {
Cryptpad.messenger.getMyInfo(cb);
});
sframeChan.on('Q_CONTACTS_GET_FRIEND_INFO', function (curvePublic, cb) {
Cryptpad.messenger.getFriendInfo(curvePublic, cb);
});
sframeChan.on('Q_CONTACTS_REMOVE_FRIEND', function (curvePublic, cb) {
Cryptpad.messenger.removeFriend(curvePublic, cb);
});
sframeChan.on('Q_CONTACTS_OPEN_FRIEND_CHANNEL', function (curvePublic, cb) {
Cryptpad.messenger.openFriendChannel(curvePublic, cb);
});
sframeChan.on('Q_CONTACTS_GET_STATUS', function (curvePublic, cb) {
Cryptpad.messenger.getFriendStatus(curvePublic, cb);
});
sframeChan.on('Q_CONTACTS_GET_MORE_HISTORY', function (opt, cb) {
Cryptpad.messenger.getMoreHistory(opt, cb);
});
sframeChan.on('Q_CONTACTS_SEND_MESSAGE', function (opt, cb) {
Cryptpad.messenger.sendMessage(opt, cb);
});
sframeChan.on('Q_CONTACTS_SET_CHANNEL_HEAD', function (opt, cb) {
Cryptpad.messenger.setChannelHead(opt, cb);
});
sframeChan.on('Q_CHAT_OPENPADCHAT', function (data, cb) {
Cryptpad.messenger.execCommand({
@ -841,25 +810,6 @@ define([
Cryptpad.messenger.onEvent.reg(function (data) {
sframeChan.event('EV_CHAT_EVENT', data);
});
Cryptpad.messenger.onMessageEvent.reg(function (data) {
sframeChan.event('EV_CONTACTS_MESSAGE', data);
});
Cryptpad.messenger.onJoinEvent.reg(function (data) {
sframeChan.event('EV_CONTACTS_JOIN', data);
});
Cryptpad.messenger.onLeaveEvent.reg(function (data) {
sframeChan.event('EV_CONTACTS_LEAVE', data);
});
Cryptpad.messenger.onUpdateEvent.reg(function (data) {
sframeChan.event('EV_CONTACTS_UPDATE', data);
});
Cryptpad.messenger.onFriendEvent.reg(function (data) {
sframeChan.event('EV_CONTACTS_FRIEND', data);
});
Cryptpad.messenger.onUnfriendEvent.reg(function (data) {
sframeChan.event('EV_CONTACTS_UNFRIEND', data);
});
}
// Chrome 68 on Mac contains a bug resulting in the page turning white after a few seconds

@ -1,121 +0,0 @@
define([], function () {
var MI = {};
MI.create = function (sFrameChan) {
var messenger = {};
var _handlers = {
message: [],
join: [],
leave: [],
update: [],
friend: [],
unfriend: []
};
messenger.on = function (key, f) {
if (!_handlers[key]) { throw new Error('invalid event'); }
_handlers[key].push(f);
};
sFrameChan.on('EV_CONTACTS_MESSAGE', function (data) {
_handlers.message.forEach(function (f) {
f(data);
});
});
sFrameChan.on('EV_CONTACTS_JOIN', function (data) {
_handlers.join.forEach(function (f) {
f(data.curvePublic, data.channel);
});
});
sFrameChan.on('EV_CONTACTS_LEAVE', function (data) {
_handlers.leave.forEach(function (f) {
f(data.curvePublic, data.channel);
});
});
sFrameChan.on('EV_CONTACTS_UPDATE', function (data) {
_handlers.update.forEach(function (f) {
f(data.info, data.types, data.channel);
});
});
sFrameChan.on('EV_CONTACTS_FRIEND', function (data) {
_handlers.friend.forEach(function (f) {
f(data.curvePublic);
});
});
sFrameChan.on('EV_CONTACTS_UNFRIEND', function (data) {
_handlers.unfriend.forEach(function (f) {
f(data.curvePublic, data.removedByMe);
});
});
/*** QUERIES ***/
messenger.getFriendList = function (cb) {
sFrameChan.query('Q_CONTACTS_GET_FRIEND_LIST', null, function (err, data) {
cb(err || data.error, data.data);
});
};
messenger.getMyInfo = function (cb) {
sFrameChan.query('Q_CONTACTS_GET_MY_INFO', null, function (err, data) {
cb(err || data.error, data.data);
});
};
messenger.getFriendInfo = function (curvePublic, cb) {
sFrameChan.query('Q_CONTACTS_GET_FRIEND_INFO', curvePublic, function (err, data) {
cb(err || data.error, data.data);
//cb({ error: err, data: data, });
});
};
messenger.removeFriend = function (curvePublic, cb) {
sFrameChan.query('Q_CONTACTS_REMOVE_FRIEND', curvePublic, function (err, data) {
cb(err || data.error, data.data);
});
};
messenger.openFriendChannel = function (curvePublic, cb) {
sFrameChan.query('Q_CONTACTS_OPEN_FRIEND_CHANNEL', curvePublic, function (err, data) {
cb(err || data.error);
});
};
messenger.getStatus = function (curvePublic, cb) {
sFrameChan.query('Q_CONTACTS_GET_STATUS', curvePublic, function (err, data) {
cb(err || data.error, data.data);
});
};
messenger.getMoreHistory = function (curvePublic, sig, count, cb) {
sFrameChan.query('Q_CONTACTS_GET_MORE_HISTORY', {
curvePublic: curvePublic,
sig: sig,
count: count
}, function (err, data) {
cb(err || data.error, data.data);
});
};
messenger.sendMessage = function (curvePublic, content, cb) {
sFrameChan.query('Q_CONTACTS_SEND_MESSAGE', {
content: content,
curvePublic: curvePublic,
}, function (err, data) {
cb(err || data.error);
});
};
messenger.setChannelHead = function (curvePublic, sig, cb) {
sFrameChan.query('Q_CONTACTS_SET_CHANNEL_HEAD', {
curvePublic: curvePublic,
sig: sig,
}, function (e, data) {
cb(e || data.error);
});
};
messenger.clearOwnedChannel = function (channel, cb) {
sFrameChan.query('Q_CONTACTS_CLEAR_OWNED_CHANNEL', channel, function (e) {
cb(e);
});
};
return messenger;
};
return MI;
});

@ -153,24 +153,6 @@ define({
// Cache is wiped after each new release
'EV_CACHE_PUT': true,
// Contacts
'EV_CONTACTS_MESSAGE': true,
'EV_CONTACTS_JOIN': true,
'EV_CONTACTS_LEAVE': true,
'EV_CONTACTS_UPDATE': true,
'EV_CONTACTS_FRIEND': true,
'EV_CONTACTS_UNFRIEND': true,
'Q_CONTACTS_GET_FRIEND_LIST': true,
'Q_CONTACTS_GET_MY_INFO': true,
'Q_CONTACTS_GET_FRIEND_INFO': true,
'Q_CONTACTS_REMOVE_FRIEND': true,
'Q_CONTACTS_OPEN_FRIEND_CHANNEL': true,
'Q_CONTACTS_GET_STATUS': true,
'Q_CONTACTS_GET_MORE_HISTORY': true,
'Q_CONTACTS_SEND_MESSAGE': true,
'Q_CONTACTS_SET_CHANNEL_HEAD': true,
'Q_CONTACTS_CLEAR_OWNED_CHANNEL': true,
// Chat
'EV_CHAT_EVENT': true,
'Q_CHAT_COMMAND': true,
@ -228,6 +210,8 @@ define({
// Remove an owned pad from the server
'Q_REMOVE_OWNED_CHANNEL': true,
// Clear an owned pad from the server (preserve metadata)
'Q_CLEAR_OWNED_CHANNEL': true,
// Notifications about connection and disconnection from the network
'EV_NETWORK_DISCONNECT': true,

@ -6,11 +6,10 @@ define([
'/common/common-interface.js',
'/common/common-hash.js',
'/common/common-feedback.js',
'/common/sframe-messenger-inner.js',
'/contacts/messenger-ui.js',
'/customize/messages.js',
], function ($, Config, ApiConfig, UIElements, UI, Hash, Feedback,
Messenger, MessengerUI, Messages) {
MessengerUI, Messages) {
var Common;
var Bar = {
@ -425,9 +424,7 @@ Messenger, MessengerUI, Messages) {
id: 'cp-app-contacts-container',
'class': 'cp-app-contacts-inapp'
}).prependTo(toolbar.chatContent);
var sframeChan = Common.getSframeChannel();
var messenger = Messenger.create(sframeChan);
MessengerUI.create(messenger, $container, Common, toolbar);
MessengerUI.create($container, Common, toolbar);
};
var createChat = function (toolbar, config) {
if (!config.metadataMgr) {

@ -6,7 +6,6 @@ define([
'/common/sframe-common.js',
'/common/hyperscript.js',
'/contacts/messenger-ui.js',
'/common/sframe-messenger-inner.js',
'/customize/messages.js',
'/common/common-interface.js',
@ -21,7 +20,6 @@ define([
SFCommon,
h,
MessengerUI,
Messenger,
Messages,
UI
)
@ -56,9 +54,7 @@ define([
APP.toolbar = Toolbar.create(configTb);
APP.toolbar.$rightside.hide();
var messenger = Messenger.create(sFrameChan);
MessengerUI.create(messenger, $(appElement), common);
MessengerUI.create($(appElement), common);
UI.removeLoadingScreen();

@ -39,7 +39,7 @@ define([
};
};
MessengerUI.create = function (messenger, $container, common, toolbar) {
MessengerUI.create = function ($container, common, toolbar) {
var sframeChan = common.getSframeChannel();
var metadataMgr = common.getMetadataMgr();
var origin = metadataMgr.getPrivateData().origin;
@ -75,6 +75,13 @@ define([
]),
]);
var execCommand = function (cmd, data, cb) {
sframeChan.query('Q_CHAT_COMMAND', {cmd: cmd, data: data}, function (err, obj) {
if (err || (obj && obj.error)) { return void cb(err || (obj && obj.error)); }
cb(void 0, obj);
});
};
var $userlist = $(friendList).appendTo($container);
var $messages = $(messaging).appendTo($container);
@ -229,7 +236,11 @@ define([
fetching = true;
var $messagebox = $(getChat(id)).find('.cp-app-contacts-messages');
messenger.getMoreHistory(id, sig, 10, function (e, history) {
execCommand('GET_MORE_HISTORY', {
id: id,
sig: sig,
count: 10
}, function (e, history) {
fetching = false;
if (e) { return void console.error(e); }
@ -274,7 +285,7 @@ define([
UI.confirm(Messages.contacts_confirmRemoveHistory, function (yes) {
if (!yes) { return; }
messenger.clearOwnedChannel(id, function (e) {
sframeChan.query('Q_CLEAR_OWNED_CHANNEL', id, function (e) {
if (e) {
console.error(e);
UI.alert(Messages.contacts_removeHistoryServerError);
@ -341,7 +352,10 @@ define([
if (typeof(content) !== 'string' || !content.trim()) { return; }
if (sending) { return false; }
sending = true;
messenger.sendMessage(id, content, function (e) {
execCommand('SEND_MESSAGE', {
id: id,
content: content
}, function (e) {
if (e) {
// failed to send
return void console.error('failed to send');
@ -412,7 +426,7 @@ define([
var updateStatus = function (id) {
if (!state.channels[id]) { return; }
var $status = find.inList(id).find('.cp-app-contacts-status');
messenger.getStatus(id, function (e, online) {
execCommand('GET_STATUS', id, function (e, online) {
// if error maybe you shouldn't display this friend...
if (e) {
find.inList(id).hide();
@ -434,7 +448,10 @@ define([
if (lastMsg) {
channel.HEAD = lastMsg.sig;
messenger.setChannelHead(chanId, channel.HEAD, function (e) {
execCommand('SET_CHANNEL_HEAD', {
id: chanId,
sig: channel.HEAD
}, function (e) {
if (e) { console.error(e); }
});
}
@ -459,7 +476,7 @@ define([
};
var removeFriend = function (curvePublic) {
messenger.removeFriend(curvePublic, function (e /*, removed */) {
execCommand('REMOVE_FRIEND', curvePublic, function (e /*, removed */) {
if (e) { return void console.error(e); }
});
};
@ -533,7 +550,7 @@ define([
return ($elem[0].scrollHeight - $elem.scrollTop() === $elem.outerHeight());
};
messenger.on('message', function (message) {
var onMessage = function (message) {
var chanId = message.channel;
var channel = state.channels[chanId];
if (!channel) { return; }
@ -573,7 +590,10 @@ define([
if (isActive(chanId)) {
channel.HEAD = message.sig;
messenger.setChannelHead(chanId, message.sig, function (e) {
execCommand('SET_CHANNEL_HEAD', {
id: chanId,
sig: message.sig
}, function (e) {
if (e) { return void console.error(e); }
});
return;
@ -583,25 +603,30 @@ define([
return void notify(chanId, message);
}
unnotify(chanId);
});
};
messenger.on('join', function (data, channel) {
var onJoin = function (obj) {
var channel = obj.id;
var data = obj.info;
if (data.curvePublic) {
contactsData[data.curvePublic] = data;
}
updateStatus(channel);
// TODO room refresh online userlist
});
messenger.on('leave', function (data, channel) {
};
var onLeave = function (obj) {
var channel = obj.id;
var data = obj.info;
if (contactsData[data.curvePublic]) {
delete contactsData[data.curvePublic];
}
updateStatus(channel);
// TODO room refresh online userlist
});
};
// change in your friend list
messenger.on('update', function (info, types, channel) {
var onUpdateData = function (data) {
var info = data.info;
var types = data.types;
var channel = data.channel;
if (!info || !info.curvePublic) { return; }
// Make sure we don't store useless data (friends data in pad chat or the other way)
if (channel && !state.channels[channel]) { return; }
@ -620,9 +645,6 @@ define([
$messages.find(userQuery(curvePublic) + ' .cp-app-contacts-header ' +
'.cp-app-contacts-name, div.cp-app-contacts-message'+
userQuery(curvePublic) + ' div.cp-app-contacts-sender').text(name);
// TODO room
// Update name in room userlist
}
if (types.indexOf('profile') !== -1) {
@ -648,13 +670,6 @@ define([
});
}
});
var execCommand = function (cmd, data, cb) {
sframeChan.query('Q_CHAT_COMMAND', {cmd: cmd, data: data}, function (err, obj) {
if (err || (obj && obj.error)) { return void cb(err || (obj && obj.error)); }
cb(void 0, obj);
});
};
var initializeRoom = function (room) {
@ -710,7 +725,8 @@ define([
});
};
messenger.on('friend', function (curvePublic) {
var onFriend = function (obj) {
var curvePublic = obj.curvePublic;
if (isApp) { return; }
debug('new friend: ', curvePublic);
execCommand('GET_ROOMS', {curvePublic: curvePublic}, function (err, rooms) {
@ -718,9 +734,11 @@ define([
debug('rooms: ' + JSON.stringify(rooms));
rooms.forEach(initializeRoom);
});
});
};
messenger.on('unfriend', function (curvePublic, removedByMe) {
var onUnfriend = function (obj) {
var curvePublic = obj.curvePublic;
var removedByMe = obj.fromMe;
if (isApp) { return; }
var channel = state.channels[state.active];
$userlist.find(userQuery(curvePublic)).remove();
@ -731,7 +749,7 @@ define([
if (!removedByMe) {
// TODO UI.alert if this is triggered by the other guy
}
});
};
common.getMetadataMgr().onTitleChange(function () {
var padChat = common.getPadChat();
@ -754,11 +772,11 @@ define([
});
// TODO room
// messenger.on('joinroom', function (chanid))
// messenger.on('leaveroom', function (chanid))
// var onJoinRoom
// var onLeaveRoom
messenger.getMyInfo(function (e, info) {
execCommand('GET_MY_INFO', null, function (e, info) {
contactsData[info.curvePublic] = info;
});
@ -813,26 +831,52 @@ define([
});
}
sframeChan.on('EV_CHAT_EVENT', function (obj) {
if (obj.ev === 'READY') {
var cmd = obj.ev;
var data = obj.data;
if (cmd === 'READY') {
onMessengerReady();
return;
}
if (obj.ev === 'CLEAR_CHANNEL') {
clearChannel(obj.data);
if (cmd === 'CLEAR_CHANNEL') {
clearChannel(data);
return;
}
if (obj.ev === 'PADCHAT_READY') {
onPadChatReady(obj.data);
if (cmd === 'PADCHAT_READY') {
onPadChatReady(data);
return;
}
if (obj.ev === 'DISCONNECT') {
if (cmd === 'DISCONNECT') {
onDisconnect();
return;
}
if (obj.ev === 'RECONNECT') {
if (cmd === 'RECONNECT') {
onReconnect();
return;
}
if (cmd === 'UPDATE_DATA') {
onUpdateData(data);
return;
}
if (cmd === 'MESSAGE') {
onMessage(data);
return;
}
if (cmd === 'JOIN') {
onJoin(data);
return;
}
if (cmd === 'LEAVE') {
onLeave(data);
return;
}
if (cmd === 'FRIEND') {
onFriend(data);
return;
}
if (cmd === 'UNFRIEND') {
onUnfriend(data);
return;
}
});
};

Loading…
Cancel
Save