Refactor messenger to use commands from UI to store

This commit is contained in:
yflory
2018-11-27 16:55:56 +01:00
parent cc3e6f3924
commit ca45ff31d1
10 changed files with 158 additions and 480 deletions

View File

@@ -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;
}
});
};