|
|
@ -10,49 +10,81 @@ define([
|
|
|
|
], function ($, Messages, Util, UI, Notifier, h, Marked, MediaTag) {
|
|
|
|
], function ($, Messages, Util, UI, Notifier, h, Marked, MediaTag) {
|
|
|
|
'use strict';
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var debug = console.log;
|
|
|
|
|
|
|
|
debug = function () {};
|
|
|
|
|
|
|
|
|
|
|
|
var MessengerUI = {};
|
|
|
|
var MessengerUI = {};
|
|
|
|
|
|
|
|
|
|
|
|
var dataQuery = function (curvePublic) {
|
|
|
|
var dataQuery = function (id) {
|
|
|
|
return '[data-key="' + curvePublic + '"]';
|
|
|
|
return '[data-key="' + id + '"]';
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var userQuery = function (curve) {
|
|
|
|
|
|
|
|
return '[data-user="' + curve + '"]';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var initChannel = function (state, curvePublic, info) {
|
|
|
|
var initChannel = function (state, info) {
|
|
|
|
console.log('initializing channel for [%s]', curvePublic);
|
|
|
|
console.log('initializing channel for [%s]', info.id);
|
|
|
|
state.channels[curvePublic] = {
|
|
|
|
state.channels[info.id] = {
|
|
|
|
messages: [],
|
|
|
|
messages: [],
|
|
|
|
|
|
|
|
name: info.name,
|
|
|
|
|
|
|
|
isFriendChat: info.isFriendChat,
|
|
|
|
|
|
|
|
curvePublic: info.curvePublic,
|
|
|
|
HEAD: info.lastKnownHash,
|
|
|
|
HEAD: info.lastKnownHash,
|
|
|
|
TAIL: null,
|
|
|
|
TAIL: null,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
MessengerUI.create = function (messenger, $userlist, $messages, common) {
|
|
|
|
MessengerUI.create = function (messenger, $container, common) {
|
|
|
|
var origin = common.getMetadataMgr().getPrivateData().origin;
|
|
|
|
var sframeChan = common.getSframeChannel();
|
|
|
|
|
|
|
|
var metadataMgr = common.getMetadataMgr();
|
|
|
|
|
|
|
|
var origin = metadataMgr.getPrivateData().origin;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$container.addClass('cp-app-contacts-initializing');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var spinner = '<span class="fa fa-spinner fa-pulse fa-4x fa-fw"></span>';
|
|
|
|
|
|
|
|
var messaging = h('div#cp-app-contacts-messaging', [
|
|
|
|
|
|
|
|
h('span.fa.fa-spinner.fa-pulse.fa-4x.fa-fw.cp-app-contacts-spinner'),
|
|
|
|
|
|
|
|
h('div.cp-app-contacts-info', [
|
|
|
|
|
|
|
|
h('h2', Messages.contacts_info1),
|
|
|
|
|
|
|
|
h('ul', [
|
|
|
|
|
|
|
|
h('li', Messages.contacts_info2),
|
|
|
|
|
|
|
|
h('li', Messages.contacts_info3),
|
|
|
|
|
|
|
|
])
|
|
|
|
|
|
|
|
])
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var friendList = h('div#cp-app-contacts-friendlist', [
|
|
|
|
|
|
|
|
h('span.fa.fa-spinner.fa-pulse.fa-4x.fa-fw.cp-app-contacts-spinner'),
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var $userlist = $(friendList).appendTo($container);
|
|
|
|
|
|
|
|
var $messages = $(messaging).appendTo($container);
|
|
|
|
|
|
|
|
|
|
|
|
var state = window.state = {
|
|
|
|
var state = window.state = {
|
|
|
|
active: '',
|
|
|
|
active: '',
|
|
|
|
|
|
|
|
channels: {}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
state.channels = {};
|
|
|
|
var contactsData = state.contactsData = {};
|
|
|
|
var displayNames = state.displayNames = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var avatars = state.avatars = {};
|
|
|
|
var avatars = state.avatars = {};
|
|
|
|
var setActive = function (curvePublic) {
|
|
|
|
var setActive = function (id) {
|
|
|
|
state.active = curvePublic;
|
|
|
|
state.active = id;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
var isActive = function (curvePublic) {
|
|
|
|
var isActive = function (id) {
|
|
|
|
return curvePublic === state.active;
|
|
|
|
return id === state.active;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var find = {};
|
|
|
|
var find = {};
|
|
|
|
find.inList = function (curvePublic) {
|
|
|
|
find.inList = function (id) {
|
|
|
|
return $userlist.find(dataQuery(curvePublic));
|
|
|
|
return $userlist.find(dataQuery(id));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var notify = function (curvePublic) {
|
|
|
|
var notify = function (id) {
|
|
|
|
find.inList(curvePublic).addClass('cp-app-contacts-notify');
|
|
|
|
find.inList(id).addClass('cp-app-contacts-notify');
|
|
|
|
};
|
|
|
|
};
|
|
|
|
var unnotify = function (curvePublic) {
|
|
|
|
var unnotify = function (id) {
|
|
|
|
find.inList(curvePublic).removeClass('cp-app-contacts-notify');
|
|
|
|
find.inList(id).removeClass('cp-app-contacts-notify');
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var m = function (md) {
|
|
|
|
var m = function (md) {
|
|
|
@ -83,24 +115,24 @@ define([
|
|
|
|
var markup = {};
|
|
|
|
var markup = {};
|
|
|
|
markup.message = function (msg) {
|
|
|
|
markup.message = function (msg) {
|
|
|
|
var curvePublic = msg.author;
|
|
|
|
var curvePublic = msg.author;
|
|
|
|
var name = displayNames[msg.author];
|
|
|
|
var name = contactsData[msg.author].displayName;
|
|
|
|
return h('div.cp-app-contacts-message', {
|
|
|
|
return h('div.cp-app-contacts-message', {
|
|
|
|
title: msg.time? new Date(msg.time).toLocaleString(): '?',
|
|
|
|
title: msg.time? new Date(msg.time).toLocaleString(): '?',
|
|
|
|
'data-key': curvePublic,
|
|
|
|
'data-user': curvePublic,
|
|
|
|
}, [
|
|
|
|
}, [
|
|
|
|
name? h('div.cp-app-contacts-sender', name): undefined,
|
|
|
|
name? h('div.cp-app-contacts-sender', name): undefined,
|
|
|
|
m(msg.text),
|
|
|
|
m(msg.text),
|
|
|
|
]);
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var getChat = function (curvePublic) {
|
|
|
|
var getChat = function (id) {
|
|
|
|
return $messages.find(dataQuery(curvePublic));
|
|
|
|
return $messages.find(dataQuery(id));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var normalizeLabels = function ($messagebox) {
|
|
|
|
var normalizeLabels = function ($messagebox) {
|
|
|
|
$messagebox.find('div.cp-app-contacts-message').toArray().reduce(function (a, b) {
|
|
|
|
$messagebox.find('div.cp-app-contacts-message').toArray().reduce(function (a, b) {
|
|
|
|
var $b = $(b);
|
|
|
|
var $b = $(b);
|
|
|
|
if ($(a).data('key') === $b.data('key')) {
|
|
|
|
if ($(a).data('user') === $b.data('user')) {
|
|
|
|
$b.find('.cp-app-contacts-sender').hide();
|
|
|
|
$b.find('.cp-app-contacts-sender').hide();
|
|
|
|
return a;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -108,29 +140,31 @@ define([
|
|
|
|
}, []);
|
|
|
|
}, []);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
markup.chatbox = function (curvePublic, data) {
|
|
|
|
markup.chatbox = function (id, data, curvePublic) {
|
|
|
|
var moreHistory = h('span.cp-app-contacts-more-history.fa.fa-history', {
|
|
|
|
var moreHistory = h('span.cp-app-contacts-more-history.fa.fa-history', {
|
|
|
|
title: Messages.contacts_fetchHistory,
|
|
|
|
title: Messages.contacts_fetchHistory,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
var displayName = data.displayName;
|
|
|
|
|
|
|
|
|
|
|
|
var chan = state.channels[id];
|
|
|
|
|
|
|
|
var displayName = chan.name;
|
|
|
|
|
|
|
|
|
|
|
|
var fetching = false;
|
|
|
|
var fetching = false;
|
|
|
|
var $moreHistory = $(moreHistory).click(function () {
|
|
|
|
var $moreHistory = $(moreHistory).click(function () {
|
|
|
|
if (fetching) { return; }
|
|
|
|
if (fetching) { return; }
|
|
|
|
|
|
|
|
|
|
|
|
// get oldest known message...
|
|
|
|
// get oldest known message...
|
|
|
|
var channel = state.channels[curvePublic];
|
|
|
|
var channel = state.channels[id];
|
|
|
|
|
|
|
|
|
|
|
|
if (channel.exhausted) {
|
|
|
|
if (channel.exhausted) {
|
|
|
|
return void $moreHistory.addClass('cp-app-contacts-faded');
|
|
|
|
return void $moreHistory.addClass('cp-app-contacts-faded');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
console.log('getting history');
|
|
|
|
debug('getting history');
|
|
|
|
var sig = channel.TAIL || channel.HEAD;
|
|
|
|
var sig = channel.TAIL || channel.HEAD;
|
|
|
|
|
|
|
|
|
|
|
|
fetching = true;
|
|
|
|
fetching = true;
|
|
|
|
var $messagebox = $(getChat(curvePublic)).find('.cp-app-contacts-messages');
|
|
|
|
var $messagebox = $(getChat(id)).find('.cp-app-contacts-messages');
|
|
|
|
messenger.getMoreHistory(curvePublic, sig, 10, function (e, history) {
|
|
|
|
messenger.getMoreHistory(id, sig, 10, function (e, history) {
|
|
|
|
fetching = false;
|
|
|
|
fetching = false;
|
|
|
|
if (e) { return void console.error(e); }
|
|
|
|
if (e) { return void console.error(e); }
|
|
|
|
|
|
|
|
|
|
|
@ -145,7 +179,6 @@ define([
|
|
|
|
if (msg.sig === channel.TAIL) {
|
|
|
|
if (msg.sig === channel.TAIL) {
|
|
|
|
console.error('No more messages to fetch');
|
|
|
|
console.error('No more messages to fetch');
|
|
|
|
channel.exhausted = true;
|
|
|
|
channel.exhausted = true;
|
|
|
|
console.log(channel);
|
|
|
|
|
|
|
|
return void $moreHistory.addClass('cp-app-contacts-faded');
|
|
|
|
return void $moreHistory.addClass('cp-app-contacts-faded');
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
channel.TAIL = msg.sig;
|
|
|
|
channel.TAIL = msg.sig;
|
|
|
@ -156,9 +189,11 @@ define([
|
|
|
|
if (msg.type !== 'MSG') { return; }
|
|
|
|
if (msg.type !== 'MSG') { return; }
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME Schlameil the painter (performance does not scale well)
|
|
|
|
// FIXME Schlameil the painter (performance does not scale well)
|
|
|
|
|
|
|
|
// XXX trust the server?
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (channel.messages.some(function (old) {
|
|
|
|
if (channel.messages.some(function (old) {
|
|
|
|
return msg.sig === old.sig;
|
|
|
|
return msg.sig === old.sig;
|
|
|
|
})) { return; }
|
|
|
|
})) { return; }*/
|
|
|
|
|
|
|
|
|
|
|
|
channel.messages.unshift(msg);
|
|
|
|
channel.messages.unshift(msg);
|
|
|
|
var el_message = markup.message(msg);
|
|
|
|
var el_message = markup.message(msg);
|
|
|
@ -173,15 +208,19 @@ define([
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
$(removeHistory).click(function () {
|
|
|
|
$(removeHistory).click(function () {
|
|
|
|
|
|
|
|
// XXX
|
|
|
|
|
|
|
|
console.error("TODO: only display clear button if owned");
|
|
|
|
|
|
|
|
return;
|
|
|
|
UI.confirm(Messages.contacts_confirmRemoveHistory, function (yes) {
|
|
|
|
UI.confirm(Messages.contacts_confirmRemoveHistory, function (yes) {
|
|
|
|
if (!yes) { return; }
|
|
|
|
if (!yes) { return; }
|
|
|
|
|
|
|
|
|
|
|
|
messenger.clearOwnedChannel(data.channel, function (e) {
|
|
|
|
messenger.clearOwnedChannel(id, function (e) {
|
|
|
|
if (e) {
|
|
|
|
if (e) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e);
|
|
|
|
UI.alert(Messages.contacts_removeHistoryServerError);
|
|
|
|
UI.alert(Messages.contacts_removeHistoryServerError);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX clear the UI?
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
@ -205,30 +244,33 @@ define([
|
|
|
|
]);
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
var $avatar = $(avatar);
|
|
|
|
var $avatar = $(avatar);
|
|
|
|
if (data.avatar && avatars[data.avatar]) {
|
|
|
|
if (data.isFriendChat) {
|
|
|
|
$avatar.append(avatars[data.avatar]).append(rightCol);
|
|
|
|
var friend = contactsData[curvePublic];
|
|
|
|
|
|
|
|
if (friend.avatar && avatars[friend.avatar]) {
|
|
|
|
|
|
|
|
$avatar.append(avatars[friend.avatar]).append(rightCol);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
common.displayAvatar($avatar, data.avatar, data.displayName, function ($img) {
|
|
|
|
common.displayAvatar($avatar, friend.avatar, friend.displayName, function ($img) {
|
|
|
|
if (data.avatar && $img) {
|
|
|
|
if (friend.avatar && $img) {
|
|
|
|
avatars[data.avatar] = $img[0].outerHTML;
|
|
|
|
avatars[friend.avatar] = $img[0].outerHTML;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$(rightCol).insertAfter($avatar);
|
|
|
|
$(rightCol).insertAfter($avatar);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var sending = false;
|
|
|
|
var sending = false;
|
|
|
|
var send = function (content) {
|
|
|
|
var send = function (content) {
|
|
|
|
if (typeof(content) !== 'string' || !content.trim()) { return; }
|
|
|
|
if (typeof(content) !== 'string' || !content.trim()) { return; }
|
|
|
|
if (sending) { return false; }
|
|
|
|
if (sending) { return false; }
|
|
|
|
sending = true;
|
|
|
|
sending = true;
|
|
|
|
messenger.sendMessage(curvePublic, content, function (e) {
|
|
|
|
messenger.sendMessage(id, content, function (e) {
|
|
|
|
if (e) {
|
|
|
|
if (e) {
|
|
|
|
// failed to send
|
|
|
|
// failed to send
|
|
|
|
return void console.error('failed to send');
|
|
|
|
return void console.error('failed to send');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
input.value = '';
|
|
|
|
input.value = '';
|
|
|
|
sending = false;
|
|
|
|
sending = false;
|
|
|
|
console.log('sent successfully');
|
|
|
|
debug('sent successfully');
|
|
|
|
var $messagebox = $(messages);
|
|
|
|
var $messagebox = $(messages);
|
|
|
|
|
|
|
|
|
|
|
|
var height = $messagebox[0].scrollHeight;
|
|
|
|
var height = $messagebox[0].scrollHeight;
|
|
|
@ -268,7 +310,8 @@ define([
|
|
|
|
$(sendButton).click(function () { send(input.value); });
|
|
|
|
$(sendButton).click(function () { send(input.value); });
|
|
|
|
|
|
|
|
|
|
|
|
return h('div.cp-app-contacts-chat', {
|
|
|
|
return h('div.cp-app-contacts-chat', {
|
|
|
|
'data-key': curvePublic,
|
|
|
|
'data-key': id,
|
|
|
|
|
|
|
|
'data-user': data.isFriendChat && curvePublic
|
|
|
|
}, [
|
|
|
|
}, [
|
|
|
|
header,
|
|
|
|
header,
|
|
|
|
messages,
|
|
|
|
messages,
|
|
|
@ -283,16 +326,19 @@ define([
|
|
|
|
$messages.find('.cp-app-contacts-info').hide();
|
|
|
|
$messages.find('.cp-app-contacts-info').hide();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var updateStatus = function (curvePublic) {
|
|
|
|
var showInfo = function () {
|
|
|
|
var $status = find.inList(curvePublic).find('.cp-app-contacts-status');
|
|
|
|
$messages.find('.cp-app-contacts-info').show();
|
|
|
|
// FIXME this stopped working :(
|
|
|
|
};
|
|
|
|
messenger.getStatus(curvePublic, function (e, online) {
|
|
|
|
|
|
|
|
|
|
|
|
var updateStatus = function (id) {
|
|
|
|
|
|
|
|
var $status = find.inList(id).find('.cp-app-contacts-status');
|
|
|
|
|
|
|
|
messenger.getStatus(id, function (e, online) {
|
|
|
|
// if error maybe you shouldn't display this friend...
|
|
|
|
// if error maybe you shouldn't display this friend...
|
|
|
|
if (e) {
|
|
|
|
if (e) {
|
|
|
|
find.inList(curvePublic).hide();
|
|
|
|
find.inList(id).hide();
|
|
|
|
getChat(curvePublic).hide();
|
|
|
|
getChat(id).hide();
|
|
|
|
|
|
|
|
|
|
|
|
return void console.error(curvePublic, e);
|
|
|
|
return void console.error(id, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (online) {
|
|
|
|
if (online) {
|
|
|
|
return void $status
|
|
|
|
return void $status
|
|
|
@ -302,20 +348,20 @@ define([
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var display = function (curvePublic) {
|
|
|
|
var display = function (chanId) {
|
|
|
|
var channel = state.channels[curvePublic];
|
|
|
|
var channel = state.channels[chanId];
|
|
|
|
var lastMsg = channel.messages.slice(-1)[0];
|
|
|
|
var lastMsg = channel.messages.slice(-1)[0];
|
|
|
|
|
|
|
|
|
|
|
|
if (lastMsg) {
|
|
|
|
if (lastMsg) {
|
|
|
|
channel.HEAD = lastMsg.sig;
|
|
|
|
channel.HEAD = lastMsg.sig;
|
|
|
|
messenger.setChannelHead(curvePublic, channel.HEAD, function (e) {
|
|
|
|
messenger.setChannelHead(chanId, channel.HEAD, function (e) {
|
|
|
|
if (e) { console.error(e); }
|
|
|
|
if (e) { console.error(e); }
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setActive(curvePublic);
|
|
|
|
setActive(chanId);
|
|
|
|
unnotify(curvePublic);
|
|
|
|
unnotify(chanId);
|
|
|
|
var $chat = getChat(curvePublic);
|
|
|
|
var $chat = getChat(chanId);
|
|
|
|
hideInfo();
|
|
|
|
hideInfo();
|
|
|
|
$messages.find('div.cp-app-contacts-chat[data-key]').hide();
|
|
|
|
$messages.find('div.cp-app-contacts-chat[data-key]').hide();
|
|
|
|
if ($chat.length) {
|
|
|
|
if ($chat.length) {
|
|
|
@ -325,26 +371,21 @@ define([
|
|
|
|
$more.click();
|
|
|
|
$more.click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return void $chat.show();
|
|
|
|
return void $chat.show();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.error("Chat is missing... Please reload the page and try again.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
messenger.getFriendInfo(curvePublic, function (e, info) {
|
|
|
|
|
|
|
|
if (e) { return void console.error(e); } // FIXME
|
|
|
|
|
|
|
|
var chatbox = markup.chatbox(curvePublic, info);
|
|
|
|
|
|
|
|
$messages.append(chatbox);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var removeFriend = function (curvePublic) {
|
|
|
|
var removeFriend = function (curvePublic) {
|
|
|
|
messenger.removeFriend(curvePublic, function (e /*, removed */) {
|
|
|
|
messenger.removeFriend(curvePublic, function (e /*, removed */) {
|
|
|
|
if (e) { return void console.error(e); }
|
|
|
|
if (e) { return void console.error(e); }
|
|
|
|
find.inList(curvePublic).remove();
|
|
|
|
|
|
|
|
//console.log(removed);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
markup.friend = function (data) {
|
|
|
|
markup.room = function (id, room, userlist) {
|
|
|
|
var curvePublic = data.curvePublic;
|
|
|
|
var roomEl = h('div.cp-app-contacts-friend.cp-avatar', {
|
|
|
|
var friend = h('div.cp-app-contacts-friend.cp-avatar', {
|
|
|
|
'data-key': id,
|
|
|
|
'data-key': curvePublic,
|
|
|
|
'data-user': room.isFriendChat ? userlist[0].curvePublic : ''
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var remove = h('span.cp-app-contacts-remove.fa.fa-user-times', {
|
|
|
|
var remove = h('span.cp-app-contacts-remove.fa.fa-user-times', {
|
|
|
@ -352,22 +393,26 @@ define([
|
|
|
|
});
|
|
|
|
});
|
|
|
|
var status = h('span.cp-app-contacts-status');
|
|
|
|
var status = h('span.cp-app-contacts-status');
|
|
|
|
var rightCol = h('span.cp-app-contacts-right-col', [
|
|
|
|
var rightCol = h('span.cp-app-contacts-right-col', [
|
|
|
|
h('span.cp-app-contacts-name', [data.displayName]),
|
|
|
|
h('span.cp-app-contacts-name', [room.name]),
|
|
|
|
remove,
|
|
|
|
remove,
|
|
|
|
]);
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
var $friend = $(friend)
|
|
|
|
var friendData = room.isFriendChat ? userlist[0] : {};
|
|
|
|
.click(function () {
|
|
|
|
|
|
|
|
display(curvePublic);
|
|
|
|
var $room = $(roomEl).click(function () {
|
|
|
|
})
|
|
|
|
display(id);
|
|
|
|
.dblclick(function () {
|
|
|
|
}).dblclick(function () {
|
|
|
|
if (data.profile) { window.open(origin + '/profile/#' + data.profile); }
|
|
|
|
if (friendData.profile) { window.open(origin + '/profile/#' + friendData.profile); }
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
$(remove).click(function (e) {
|
|
|
|
$(remove).click(function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
var channel = state.channels[id];
|
|
|
|
|
|
|
|
if (channel.isFriendChat) {
|
|
|
|
|
|
|
|
var curvePublic = channel.curvePublic;
|
|
|
|
|
|
|
|
var friend = contactsData[curvePublic] || friendData;
|
|
|
|
UI.confirm(Messages._getKey('contacts_confirmRemove', [
|
|
|
|
UI.confirm(Messages._getKey('contacts_confirmRemove', [
|
|
|
|
Util.fixHTML(data.displayName)
|
|
|
|
Util.fixHTML(friend.name)
|
|
|
|
]), function (yes) {
|
|
|
|
]), function (yes) {
|
|
|
|
if (!yes) { return; }
|
|
|
|
if (!yes) { return; }
|
|
|
|
removeFriend(curvePublic, function (e) {
|
|
|
|
removeFriend(curvePublic, function (e) {
|
|
|
@ -377,21 +422,24 @@ define([
|
|
|
|
// FIXME seems to trigger EJOINED from netflux-websocket (from server);
|
|
|
|
// FIXME seems to trigger EJOINED from netflux-websocket (from server);
|
|
|
|
// (tried to join a channel in which you were already present)
|
|
|
|
// (tried to join a channel in which you were already present)
|
|
|
|
}, undefined, true);
|
|
|
|
}, undefined, true);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// TODO room remove room
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (data.avatar && avatars[data.avatar]) {
|
|
|
|
if (friendData.avatar && avatars[friendData.avatar]) {
|
|
|
|
$friend.append(avatars[data.avatar]);
|
|
|
|
$room.append(avatars[friendData.avatar]);
|
|
|
|
$friend.append(rightCol);
|
|
|
|
$room.append(rightCol);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
common.displayAvatar($friend, data.avatar, data.displayName, function ($img) {
|
|
|
|
common.displayAvatar($room, friendData.avatar, room.name, function ($img) {
|
|
|
|
if (data.avatar && $img) {
|
|
|
|
if (friendData.avatar && $img) {
|
|
|
|
avatars[data.avatar] = $img[0].outerHTML;
|
|
|
|
avatars[friendData.avatar] = $img[0].outerHTML;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$friend.append(rightCol);
|
|
|
|
$room.append(rightCol);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$friend.append(status);
|
|
|
|
$room.append(status);
|
|
|
|
return $friend;
|
|
|
|
return $room;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var isBottomedOut = function ($elem) {
|
|
|
|
var isBottomedOut = function ($elem) {
|
|
|
@ -401,19 +449,23 @@ define([
|
|
|
|
var initializing = true;
|
|
|
|
var initializing = true;
|
|
|
|
messenger.on('message', function (message) {
|
|
|
|
messenger.on('message', function (message) {
|
|
|
|
if (!initializing) { Notifier.notify(); }
|
|
|
|
if (!initializing) { Notifier.notify(); }
|
|
|
|
var curvePublic = message.curve;
|
|
|
|
var chanId = message.channel;
|
|
|
|
|
|
|
|
|
|
|
|
var name = displayNames[curvePublic];
|
|
|
|
var chat = getChat(chanId);
|
|
|
|
var chat = getChat(curvePublic, name);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(message);
|
|
|
|
debug(message);
|
|
|
|
|
|
|
|
|
|
|
|
var el_message = markup.message(message);
|
|
|
|
var el_message = markup.message(message);
|
|
|
|
|
|
|
|
|
|
|
|
state.channels[curvePublic].messages.push(message);
|
|
|
|
var channel = state.channels[chanId];
|
|
|
|
|
|
|
|
if (!channel) {
|
|
|
|
|
|
|
|
console.error('expected channel [%s] to be open', chanId);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var $chat = $(chat);
|
|
|
|
channel.messages.push(message);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var $chat = $(chat);
|
|
|
|
if (!$chat.length) {
|
|
|
|
if (!$chat.length) {
|
|
|
|
console.error("Got a message but the chat isn't open");
|
|
|
|
console.error("Got a message but the chat isn't open");
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -428,110 +480,173 @@ define([
|
|
|
|
}
|
|
|
|
}
|
|
|
|
normalizeLabels($messagebox);
|
|
|
|
normalizeLabels($messagebox);
|
|
|
|
|
|
|
|
|
|
|
|
var channel = state.channels[curvePublic];
|
|
|
|
if (isActive(chanId)) {
|
|
|
|
if (!channel) {
|
|
|
|
|
|
|
|
console.error('expected channel [%s] to be open', curvePublic);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isActive(curvePublic)) {
|
|
|
|
|
|
|
|
channel.HEAD = message.sig;
|
|
|
|
channel.HEAD = message.sig;
|
|
|
|
messenger.setChannelHead(curvePublic, message.sig, function (e) {
|
|
|
|
messenger.setChannelHead(chanId, message.sig, function (e) {
|
|
|
|
if (e) { return void console.error(e); }
|
|
|
|
if (e) { return void console.error(e); }
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var lastMsg = channel.messages.slice(-1)[0];
|
|
|
|
var lastMsg = channel.messages.slice(-1)[0];
|
|
|
|
if (lastMsg.sig !== channel.HEAD) {
|
|
|
|
if (lastMsg.sig !== channel.HEAD) {
|
|
|
|
return void notify(curvePublic);
|
|
|
|
return void notify(chanId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unnotify(curvePublic);
|
|
|
|
unnotify(chanId);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
messenger.on('join', function (curvePublic, channel) {
|
|
|
|
messenger.on('join', function (data, channel) {
|
|
|
|
channel = channel;
|
|
|
|
if (data.curvePublic) {
|
|
|
|
updateStatus(curvePublic);
|
|
|
|
contactsData[data.curvePublic] = data;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
updateStatus(channel);
|
|
|
|
|
|
|
|
// TODO room refresh online userlist
|
|
|
|
});
|
|
|
|
});
|
|
|
|
messenger.on('leave', function (curvePublic, channel) {
|
|
|
|
messenger.on('leave', function (data, channel) {
|
|
|
|
channel = channel;
|
|
|
|
if (contactsData[data.curvePublic]) {
|
|
|
|
updateStatus(curvePublic);
|
|
|
|
delete contactsData[data.curvePublic];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
updateStatus(channel);
|
|
|
|
|
|
|
|
// TODO room refresh online userlist
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// change in your friend list
|
|
|
|
// change in your friend list
|
|
|
|
messenger.on('update', function (info, curvePublic) {
|
|
|
|
messenger.on('update', function (info, types) {
|
|
|
|
var name = displayNames[curvePublic] = info.displayName;
|
|
|
|
if (!info || !info.curvePublic) { return; }
|
|
|
|
|
|
|
|
var curvePublic = info.curvePublic;
|
|
|
|
|
|
|
|
contactsData[curvePublic] = info;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (types.indexOf('displayName') !== -1) {
|
|
|
|
|
|
|
|
var name = info.displayName;
|
|
|
|
|
|
|
|
|
|
|
|
// update label in friend list
|
|
|
|
// update label in friend list
|
|
|
|
find.inList(curvePublic).find('.cp-app-contacts-name').text(name);
|
|
|
|
$userlist.find(userQuery(curvePublic)).find('.cp-app-contacts-name').text(name);
|
|
|
|
|
|
|
|
|
|
|
|
// update title bar and messages
|
|
|
|
// update title bar and messages
|
|
|
|
$messages.find(dataQuery(curvePublic) + ' .cp-app-contacts-header ' +
|
|
|
|
$messages.find(userQuery(curvePublic) + ' .cp-app-contacts-header ' +
|
|
|
|
'.cp-app-contacts-name, div.cp-app-contacts-message'+
|
|
|
|
'.cp-app-contacts-name, div.cp-app-contacts-message'+
|
|
|
|
dataQuery(curvePublic) + ' div.cp-app-contacts-sender').text(name).text(name);
|
|
|
|
userQuery(curvePublic) + ' div.cp-app-contacts-sender').text(name);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// XXX update name in state.channels[id] ??
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO room
|
|
|
|
|
|
|
|
// Update name in room userlist
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (types.indexOf('profile') !== -1) {
|
|
|
|
|
|
|
|
// update dblclick event in friend list
|
|
|
|
|
|
|
|
$userlist.find(userQuery(curvePublic)).off('dblclick').dblclick(function () {
|
|
|
|
|
|
|
|
if (info.profile) { window.open(origin + '/profile/#' + info.profile); }
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var connectToFriend = function (curvePublic, cb) {
|
|
|
|
if (types.indexOf('avatar') !== -1) {
|
|
|
|
messenger.getFriendInfo(curvePublic, function (e, info) {
|
|
|
|
var $mAvatar = $messages
|
|
|
|
if (e) { return void console.error(e); }
|
|
|
|
.find(userQuery(curvePublic) +' .cp-app-contacts-header .cp-avatar');
|
|
|
|
var name = displayNames[curvePublic] = info.displayName;
|
|
|
|
var $lAvatar = $userlist.find(userQuery(curvePublic));
|
|
|
|
initChannel(state, curvePublic, info);
|
|
|
|
$lAvatar.find('.cp-avatar-default, media-tag').remove();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var $div = $('<div>');
|
|
|
|
|
|
|
|
common.displayAvatar($div, info.avatar, info.displayName, function ($img) {
|
|
|
|
|
|
|
|
if (info.avatar && $img) {
|
|
|
|
|
|
|
|
avatars[info.avatar] = $img[0].outerHTML;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$mAvatar.html($div.html());
|
|
|
|
|
|
|
|
$lAvatar.find('.cp-app-contacts-right-col').before($div.html());
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var chatbox = markup.chatbox(curvePublic, info);
|
|
|
|
var execCommand = function (cmd, data, cb) {
|
|
|
|
|
|
|
|
sframeChan.query('Q_CHAT_COMMAND', {cmd: cmd, data: data}, function (err, obj) {
|
|
|
|
|
|
|
|
if (err || obj.error) { return void cb(err || obj.error); }
|
|
|
|
|
|
|
|
cb(void 0, obj);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var initializeRoom = function (room) {
|
|
|
|
|
|
|
|
var id = room.id;
|
|
|
|
|
|
|
|
initChannel(state, room);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
execCommand('GET_USERLIST', {id: id}, function (e, list) {
|
|
|
|
|
|
|
|
if (e || list.error) { return void console.error(e || list.error); }
|
|
|
|
|
|
|
|
if (!Array.isArray(list) || !list.length) {
|
|
|
|
|
|
|
|
return void console.error("Empty room!");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
debug('userlist: ' + JSON.stringify(list));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This is a friend, the userlist is only one user.
|
|
|
|
|
|
|
|
var friend = list[0];
|
|
|
|
|
|
|
|
contactsData[friend.curvePublic] = friend;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var chatbox = markup.chatbox(id, room, friend.curvePublic);
|
|
|
|
$(chatbox).hide();
|
|
|
|
$(chatbox).hide();
|
|
|
|
$messages.append(chatbox);
|
|
|
|
$messages.append(chatbox);
|
|
|
|
|
|
|
|
|
|
|
|
var friend = markup.friend(info, name);
|
|
|
|
var roomEl = markup.room(id, room, list);
|
|
|
|
$userlist.append(friend);
|
|
|
|
$userlist.append(roomEl);
|
|
|
|
messenger.openFriendChannel(curvePublic, function (e) {
|
|
|
|
|
|
|
|
if (e) { return void console.error(e); }
|
|
|
|
updateStatus(id);
|
|
|
|
cb();
|
|
|
|
|
|
|
|
updateStatus(curvePublic);
|
|
|
|
|
|
|
|
// don't add friends that are already in your userlist
|
|
|
|
|
|
|
|
//if (friendExistsInUserList(k)) { return; }
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO room group chat
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
messenger.on('friend', function (curvePublic) {
|
|
|
|
messenger.on('friend', function (curvePublic) {
|
|
|
|
console.log('new friend: ', curvePublic);
|
|
|
|
debug('new friend: ', curvePublic);
|
|
|
|
//console.error("TODO redraw user list");
|
|
|
|
execCommand('GET_ROOMS', curvePublic, function (err, rooms) {
|
|
|
|
//console.error("TODO connect to new friend");
|
|
|
|
if (err) { return void console.error(err); }
|
|
|
|
// FIXME this doesn't work right now because the friend hasn't been fully added?
|
|
|
|
debug('rooms: ' + JSON.stringify(rooms));
|
|
|
|
connectToFriend(curvePublic, function () {
|
|
|
|
rooms.forEach(initializeRoom);
|
|
|
|
//console.error('connected');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
messenger.on('unfriend', function (curvePublic, removedByMe) {
|
|
|
|
|
|
|
|
var channel = state.channels[state.active];
|
|
|
|
|
|
|
|
$userlist.find(userQuery(curvePublic)).remove();
|
|
|
|
|
|
|
|
$messages.find(userQuery(curvePublic)).remove();
|
|
|
|
|
|
|
|
if (channel && channel.curvePublic === curvePublic) {
|
|
|
|
|
|
|
|
showInfo();
|
|
|
|
|
|
|
|
// TODO UI.alert if this is triggered by the other guy
|
|
|
|
|
|
|
|
// (we need more data on the 'unfriend' event to determine its origin)
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
messenger.on('unfriend', function (curvePublic) {
|
|
|
|
|
|
|
|
console.log('unfriend', curvePublic);
|
|
|
|
|
|
|
|
find.inList(curvePublic).remove();
|
|
|
|
|
|
|
|
console.error('TODO remove chatbox');
|
|
|
|
|
|
|
|
console.error('TODO show something if that chatbox was active');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
common.getMetadataMgr().onChange(function () {
|
|
|
|
// TODO room
|
|
|
|
//messenger.checkNewFriends();
|
|
|
|
// messenger.on('joinroom', function (chanid))
|
|
|
|
messenger.updateMyData();
|
|
|
|
// messenger.on('leaveroom', function (chanid))
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME dirty hack
|
|
|
|
|
|
|
|
messenger.getMyInfo(function (e, info) {
|
|
|
|
messenger.getMyInfo(function (e, info) {
|
|
|
|
displayNames[info.curvePublic] = info.displayName;
|
|
|
|
contactsData[info.curvePublic] = info;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
messenger.getFriendList(function (e, keys) {
|
|
|
|
var ready = false;
|
|
|
|
var count = keys.length + 1;
|
|
|
|
var onMessengerReady = function () {
|
|
|
|
var ready = function () {
|
|
|
|
if (ready) { return; }
|
|
|
|
count--;
|
|
|
|
ready = true;
|
|
|
|
if (count === 0) {
|
|
|
|
|
|
|
|
initializing = false;
|
|
|
|
execCommand('GET_ROOMS', null, function (err, rooms) {
|
|
|
|
UI.removeLoadingScreen();
|
|
|
|
if (err) { return void console.error(err); }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
debug('rooms: ' + JSON.stringify(rooms));
|
|
|
|
|
|
|
|
rooms.forEach(initializeRoom);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$container.removeClass('cp-app-contacts-initializing');
|
|
|
|
};
|
|
|
|
};
|
|
|
|
ready();
|
|
|
|
|
|
|
|
keys.forEach(function (curvePublic) {
|
|
|
|
// Initialize chat when outer is ready (all channels loaded)
|
|
|
|
connectToFriend(curvePublic, ready);
|
|
|
|
// TODO: try again in outer if fail to load a channel
|
|
|
|
|
|
|
|
execCommand('IS_READY', null, function (err, yes) {
|
|
|
|
|
|
|
|
if (yes) { onMessengerReady(); }
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
sframeChan.on('EV_CHAT_EVENT', function (data) {
|
|
|
|
|
|
|
|
if (data.ev === 'READY') {
|
|
|
|
|
|
|
|
onMessengerReady();
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|