326:8ab8308cb161
Anton Shestakov <av6@dwimlabs.net>, Sat, 14 Jul 2018 22:33:05 +0800
index: only get entity if contact is still online

previous change 322:7dfdf32e8577

js/index.js

Permissions: -rw-r--r--

Other formats: Feeds:
// Generated by CoffeeScript 1.12.7
(function() {
var $form, _saveEntityCache, _saveSettings, connectfn, enableCSI, enableCarbons, getChat, getClientEntity, getContact, getProfile, getServerEntity, getServerVersion, getStamp, getText, loadEntityCache, loadSettings, onAnyError, onChatMessage, onChatState, onConnected, onDisconnected, onGetLast, onGetTime, onGetVersion, onPing, onPresence, onWebRTC, origTitle, saveEntityCache, saveSettings, sendMessage,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
Strophe.addNamespace('CAPS', 'http://jabber.org/protocol/caps');
Strophe.addNamespace('CARBONS', 'urn:xmpp:carbons:2');
Strophe.addNamespace('CHATSTATES', 'http://jabber.org/protocol/chatstates');
Strophe.addNamespace('CSI', 'urn:xmpp:csi:0');
Strophe.addNamespace('DELAY', 'urn:xmpp:delay');
Strophe.addNamespace('FORWARD', 'urn:xmpp:forward:0');
Strophe.addNamespace('LAST', 'jabber:iq:last');
Strophe.addNamespace('MESSAGE_CORRECT', 'urn:xmpp:message-correct:0');
Strophe.addNamespace('PRIVATE', 'jabber:iq:private');
Strophe.addNamespace('TIME', 'urn:xmpp:time');
window.contacts = new Tram.Contacts();
window.contactsApp = new Tram.ContactsApp({
el: $('[data-app="contacts"]'),
collection: contacts
});
window.calls = new Tram.Calls();
window.callsApp = new Tram.CallsApp({
el: $('[data-app="calls"]'),
collection: calls
});
window.settings = new Tram.ClientSettings();
window.serverInfo = new Tram.ServerInfo();
window.clientState = new Tram.ClientState();
window.faviconApp = new Tram.FaviconApp({
model: clientState
});
window.sidebarApp = new Tram.SidebarApp({
el: $('[data-app="sidebar"]'),
model: clientState
});
window.progressApp = new Tram.ProgressApp({
el: $('[data-app="progress"]'),
model: clientState
});
window.chats = {};
window.profiles = new Tram.Profiles();
window.entities = new Tram.Entities();
contacts.on('change:show', function(model) {
if (model.get('type') === 'self') {
return clientState.set('show', model.get('show'));
}
});
origTitle = document.title;
contacts.on('change:d/unread', function() {
var total;
total = contacts.reduce(function(memo, c) {
var ref;
return memo += (ref = c.get('d/unread')) != null ? ref : 0;
}, 0);
if (total > 0) {
return document.title = "(" + total + ") " + origTitle;
} else {
return document.title = origTitle;
}
});
contacts.on('change:chatstate/self', function(contact, cs) {
var msg, ref, ref1;
if (settings.get('chatstates') && (ref = Strophe.NS.CHATSTATES, indexOf.call((ref1 = contact != null ? contact.get('features') : void 0) != null ? ref1 : [], ref) >= 0)) {
msg = $msg({
to: contact.get('jid'),
from: X.conn.jid,
type: 'chat'
}).c(cs, {
xmlns: Strophe.NS.CHATSTATES
});
return X.conn.send(msg.tree());
}
});
contacts.on('change:presence', function(contact) {
var c, dupes, offline;
dupes = contacts.where({
bjid: contact.get('bjid')
});
offline = (function() {
var i, len, results;
results = [];
for (i = 0, len = dupes.length; i < len; i++) {
c = dupes[i];
if (c.get('presence') === 'unavailable') {
results.push(c);
}
}
return results;
})();
if (offline.length === dupes.length) {
offline = (function() {
var i, len, results;
results = [];
for (i = 0, len = offline.length; i < len; i++) {
c = offline[i];
if (c.get('jid') !== contact.get('jid')) {
results.push(c);
}
}
return results;
})();
}
return contacts.remove(offline);
});
clientState.on('action/show', function(show, status) {
return X.sendPresence({
show: show,
status: status,
priority: 1
});
});
clientState.on('action/disconnect', function() {
return X.disconnect('Logged out');
});
clientState.on('change:contact', function() {
var $logs, contact, previous;
previous = clientState.previous('contact');
if (previous != null) {
previous.set('chatstate/self', 'inactive');
previous.unset('active');
}
contact = clientState.get('contact');
if (contact != null) {
contact.set('chatstate/self', 'active');
contact.set('active', true);
_(contacts.where({
bjid: contact.get('bjid')
})).each(function(contact) {
return contact.unset('d/unread');
});
}
$logs = $('[data-app="logs"]');
$logs.children().detach();
$logs.append(contact.chat.render().el);
contact.chat.scroll();
return $('[data-form="send"]').toggleClass('uk-hidden', contact == null);
});
clientState.on('change:csi', function() {
var state;
state = clientState.get('csi');
return X.conn.send($build(state, {
xmlns: Strophe.NS.CSI
}).tree());
});
onConnected = function() {
var dCache, dCarbons, dInfo, dRoster, dSettings, dVersion;
X.conn.addHandler(onPresence, null, 'presence');
X.conn.addHandler(onChatMessage, null, 'message', 'chat');
X.conn.addHandler(onChatState, Strophe.NS.CHATSTATES, 'message');
X.conn.addHandler(onWebRTC, Tram.NS.WEBRTC, 'message', 'chat');
X.conn.addHandler(onAnyError, null, null, 'error');
X.conn.addHandler(onGetLast, Strophe.NS.LAST, 'iq', 'get');
X.conn.addHandler(onGetTime, Strophe.NS.TIME, 'iq', 'get');
X.conn.addHandler(onGetVersion, Strophe.NS.VERSION, 'iq', 'get');
X.conn.ping.addPingHandler(onPing);
X.conn.disco.addIdentity('client', 'web', Tram.info.client);
X.conn.disco.addFeature(Strophe.NS.CAPS);
X.conn.disco.addFeature(Strophe.NS.CARBONS);
X.conn.disco.addFeature(Strophe.NS.CHATSTATES);
X.conn.disco.addFeature(Strophe.NS.CSI);
X.conn.disco.addFeature(Strophe.NS.DISCO_INFO);
X.conn.disco.addFeature(Strophe.NS.LAST);
X.conn.disco.addFeature(Strophe.NS.MESSAGE_CORRECT);
X.conn.disco.addFeature(Strophe.NS.PING);
X.conn.disco.addFeature(Strophe.NS.PRIVATE);
X.conn.disco.addFeature(Strophe.NS.TIME);
X.conn.disco.addFeature(Strophe.NS.VERSION);
X.conn.disco.addFeature(Strophe.NS.XHTML_IM);
X.conn.disco.addFeature(Tram.NS.WEBRTC);
dSettings = $.Deferred();
dCache = $.Deferred();
dVersion = $.Deferred();
dInfo = $.Deferred();
dCarbons = $.Deferred();
dRoster = $.Deferred();
loadSettings(dSettings.resolve, dSettings.reject);
loadEntityCache(dCache.resolve, dCache.reject);
getServerVersion(dVersion.resolve, dVersion.reject);
X.conn.roster.get(dRoster.resolve);
$.when(dCache).always(function() {
return getServerEntity(dInfo.resolve, dInfo.reject);
});
$.when(dInfo).always(function() {
var hasCSI, ref, ref1;
if (ref = Strophe.NS.CARBONS, indexOf.call(serverInfo.get('features'), ref) >= 0) {
enableCarbons(dCarbons.resolve, dCarbons.reject);
} else {
dCarbons.reject();
}
hasCSI = ((ref1 = X.conn.features) != null ? ref1.getElementsByTagNameNS(Strophe.NS.CSI, 'csi')[0] : void 0) != null;
serverInfo.set('csi', hasCSI);
if (hasCSI) {
return enableCSI();
}
});
return $.when(dSettings, dVersion, dInfo, dRoster, dCarbons).always(function() {
X.sendPresence({
status: 'Online',
priority: 1
});
entities.on('add change', saveEntityCache);
return settings.on('change', saveSettings);
});
};
onDisconnected = function() {
return location.reload();
};
getStamp = function(stanza) {
var delay, ref;
delay = stanza.getElementsByTagNameNS(Strophe.NS.DELAY, 'delay')[0];
return new Date((ref = delay != null ? delay.getAttribute('stamp') : void 0) != null ? ref : Date.now());
};
getText = function(el) {
var i, len, node, ref, str;
if (!el) {
return null;
}
str = '';
if (el.childNodes.length === 0 && el.nodeType === Strophe.ElementType.TEXT) {
str += el.nodeValue;
}
ref = el.childNodes;
for (i = 0, len = ref.length; i < len; i++) {
node = ref[i];
if (node.nodeType === Strophe.ElementType.TEXT) {
str += node.nodeValue;
}
}
return str;
};
getServerVersion = function(doneCallback, failCallback) {
var failcb, iq, okcb;
iq = $iq({
type: 'get',
id: X.conn.getUniqueId('version'),
to: X.conn.domain
}).c('query', {
xmlns: Strophe.NS.VERSION
});
okcb = function(stanza) {
serverInfo.set({
name: getText(stanza.getElementsByTagName('name')[0]),
version: getText(stanza.getElementsByTagName('version')[0]),
os: getText(stanza.getElementsByTagName('os')[0])
});
return typeof doneCallback === "function" ? doneCallback() : void 0;
};
failcb = function(stanza) {
console.error("couldn't get version", stanza != null ? stanza.innerHTML : void 0);
return typeof failCallback === "function" ? failCallback() : void 0;
};
return X.conn.sendIQ(iq.tree(), okcb, failcb, Tram.config.iqTimeout);
};
enableCarbons = function(doneCallback, failCallback) {
var failcb, iq, okcb;
iq = $iq({
type: 'set',
id: X.conn.getUniqueId('enable')
}).c('enable', {
xmlns: Strophe.NS.CARBONS
});
okcb = function(stanza) {
serverInfo.set('carbons', true);
return typeof doneCallback === "function" ? doneCallback() : void 0;
};
failcb = function(stanza) {
serverInfo.set('carbons', false);
return typeof failCallback === "function" ? failCallback() : void 0;
};
return X.conn.sendIQ(iq.tree(), okcb, failcb, Tram.config.iqTimeout);
};
enableCSI = function() {
var csiTimer;
csiTimer = null;
$(window).on('focus.tram', function() {
if (csiTimer != null) {
window.clearTimeout(csiTimer);
csiTimer = null;
}
return clientState.set('csi', 'active');
});
return $(window).on('blur.tram', function() {
if (csiTimer != null) {
window.clearTimeout(csiTimer);
}
return csiTimer = window.setTimeout(function() {
return clientState.set('csi', 'inactive');
}, Tram.config.inactiveTime);
});
};
loadSettings = function(doneCallback, failCallback) {
var failcb, iq, okcb;
iq = $iq({
type: 'get',
id: X.conn.getUniqueId('settings')
}).c('query', {
xmlns: Strophe.NS.PRIVATE
}).c('settings', {
xmlns: Tram.NS.PRIVATE
});
okcb = function(stanza) {
var data, el;
el = stanza.getElementsByTagName('settings')[0];
if ((el != null ? el.firstChild : void 0) != null) {
try {
data = JSON.parse(el.firstChild.nodeValue);
} catch (error) {
if (typeof failCallback === "function") {
failCallback();
}
return;
}
}
if (data != null) {
settings.set(data);
}
return typeof doneCallback === "function" ? doneCallback() : void 0;
};
failcb = function(stanza) {
console.error("couldn't load settings", stanza);
return typeof failCallback === "function" ? failCallback() : void 0;
};
return X.conn.sendIQ(iq.tree(), okcb, failcb, Tram.config.iqTimeout);
};
_saveSettings = function(doneCallback, failCallback) {
var failcb, iq, okcb, payload;
payload = JSON.stringify(settings.toJSON());
iq = $iq({
type: 'set',
id: X.conn.getUniqueId('settings')
}).c('query', {
xmlns: Strophe.NS.PRIVATE
}).c('settings', {
xmlns: Tram.NS.PRIVATE
}).t(payload);
okcb = function(stanza) {
return typeof doneCallback === "function" ? doneCallback() : void 0;
};
failcb = function(stanza) {
console.warn("couldn't save settings", stanza);
return typeof failCallback === "function" ? failCallback() : void 0;
};
return X.conn.sendIQ(iq.tree(), okcb, failcb, Tram.config.iqTimeout);
};
saveSettings = _(_saveSettings).debounce(1000);
loadEntityCache = function(doneCallback, failCallback) {
var failcb, iq, okcb;
iq = $iq({
type: 'get',
id: X.conn.getUniqueId('entity-cache')
}).c('query', {
xmlns: Strophe.NS.PRIVATE
}).c('entity-cache', {
xmlns: Tram.NS.PRIVATE
});
okcb = function(stanza) {
var data, el;
el = stanza.getElementsByTagName('entity-cache')[0];
if ((el != null ? el.firstChild : void 0) != null) {
try {
data = JSON.parse(el.firstChild.nodeValue);
} catch (error) {
if (typeof failCallback === "function") {
failCallback();
}
return;
}
}
if (data != null) {
entities.add(data);
}
return typeof doneCallback === "function" ? doneCallback() : void 0;
};
failcb = function(stanza) {
console.error("couldn't load caps cache", stanza);
return typeof failCallback === "function" ? failCallback() : void 0;
};
return X.conn.sendIQ(iq.tree(), okcb, failcb, Tram.config.iqTimeout);
};
_saveEntityCache = function(doneCallback, failCallback) {
var failcb, iq, okcb, payload;
payload = JSON.stringify(entities.chain().filter(function(e) {
return e.has('id');
}).map(function(e) {
return e.toJSON();
}).value());
iq = $iq({
type: 'set',
id: X.conn.getUniqueId('entity-cache')
}).c('query', {
xmlns: Strophe.NS.PRIVATE
}).c('entity-cache', {
xmlns: Tram.NS.PRIVATE
}).t(payload);
okcb = function(stanza) {
return typeof doneCallback === "function" ? doneCallback() : void 0;
};
failcb = function(stanza) {
console.warn("couldn't save entity cache", stanza);
return typeof failCallback === "function" ? failCallback() : void 0;
};
return X.conn.sendIQ(iq.tree(), okcb, failcb, Tram.config.iqTimeout);
};
saveEntityCache = _(_saveEntityCache).debounce(1000);
onAnyError = function(stanza) {
console.warn('got an error:', stanza, stanza.outerHTML);
return true;
};
onGetLast = function(stanza) {
var from, id, iq;
id = stanza.getAttribute('id');
from = stanza.getAttribute('from');
iq = $iq({
to: from,
type: 'result',
id: id
}).c('query', {
xmlns: Strophe.NS.LAST,
seconds: '0'
});
X.conn.send(iq.tree());
return true;
};
onGetTime = function(stanza) {
var from, id, iq, now;
now = moment();
id = stanza.getAttribute('id');
from = stanza.getAttribute('from');
iq = $iq({
to: from,
type: 'result',
id: id
}).c('time', {
xmlns: Strophe.NS.TIME
}).c('tzo').t(now.format('Z')).up().c('utc').t(now.toISOString());
X.conn.send(iq.tree());
return true;
};
onGetVersion = function(stanza) {
var from, id, iq;
id = stanza.getAttribute('id');
from = stanza.getAttribute('from');
iq = $iq({
to: from,
type: 'result',
id: id
}).c('query', {
xmlns: Strophe.NS.VERSION
}).c('name').t(Tram.info.client).up().c('version').t(Tram.info.version);
X.conn.send(iq.tree());
return true;
};
onPing = function(stanza) {
X.conn.ping.pong(stanza);
return true;
};
getChat = function(jid) {
return chats[jid] != null ? chats[jid] : chats[jid] = new Tram.LogApp({
collection: new Tram.Messages()
});
};
getContact = function(jid, node, ver, presence) {
var bjid, contact, self;
contact = contacts.get(jid);
if (contact != null) {
return contact;
}
self = jid === X.conn.jid;
if (self) {
contacts.each(function(model) {
return model.set('type', 'contact');
});
}
bjid = Strophe.getBareJidFromJid(jid);
contact = contacts.add({
jid: jid,
bjid: bjid,
type: self ? 'self' : 'contact'
});
contact.set('profile', getProfile(bjid));
if (presence !== 'unavailable' && (Strophe.getResourceFromJid(jid) != null)) {
getClientEntity(contact, node, ver);
}
contact.chat = getChat(bjid);
contact.chat.collection.on('add', function(model) {
var ref, ref1;
if (model.get('type') === 'chat' && model.get('from') === contact.get('jid') && ((ref = clientState.get('contact')) != null ? ref.get('bjid') : void 0) !== bjid) {
return contact.set('d/unread', ((ref1 = contact.get('d/unread')) != null ? ref1 : 0) + 1);
}
});
contact.on('action/chat', function() {
return clientState.set('contact', contact);
});
contact.on('action/authorize', function() {
X.conn.roster.authorize(bjid);
if (!X.conn.roster.findItem(bjid)) {
X.conn.roster.add(bjid, null, [], function() {
return X.conn.roster.subscribe(bjid, 'Subscribing back', null);
});
}
return contacts.remove(contact);
});
contact.on('action/unauthorize', function() {
X.conn.roster.unauthorize(bjid);
return contacts.remove(contact);
});
contact.on('action/remove', function() {
if (X.conn.roster.findItem(bjid)) {
return X.conn.roster.remove(bjid, function() {
return contacts.remove(contacts.where({
bjid: bjid
}));
});
} else {
if (contacts.find({
bjid: bjid,
presence: 'subscribe'
}) != null) {
X.conn.roster.unauthorize(bjid);
}
return contacts.remove(contacts.where({
bjid: bjid
}));
}
});
contact.w = new Tram.WebRTCInterface(contact);
contact.on('action/call', function(media) {
contact.set('callstate', 'outgoing');
return contact.w.init(true, {
audio: indexOf.call(media, 'a') >= 0,
video: indexOf.call(media, 'v') >= 0
});
});
contact.on('action/accept', function(media) {
return contact.w.init(false, {
audio: indexOf.call(media, 'a') >= 0,
video: indexOf.call(media, 'v') >= 0
});
});
contact.on('action/decline action/hangup', function() {
contact.w.sendIntent('terminate');
contact.w.disconnect();
return contact.unset('callstate');
});
return contact;
};
getProfile = function(bjid) {
var failcb, okcb, profile, to;
profile = profiles.get(bjid);
if (profile != null) {
return profile;
}
profile = profiles.add({
bjid: bjid
});
okcb = function(stanza) {
var avatar, data, vcard;
vcard = stanza.getElementsByTagNameNS(Strophe.NS.VCARD, 'vCard')[0];
if (vcard == null) {
console.warn("no vcard in response", stanza);
return;
}
data = {
nickname: getText(vcard.querySelector('NICKNAME')),
fullname: getText(vcard.querySelector('FN')),
firstname: getText(vcard.querySelector('N > GIVEN')),
lastname: getText(vcard.querySelector('N > FAMILY'))
};
avatar = {
mime: getText(vcard.querySelector('PHOTO > TYPE')),
data: getText(vcard.querySelector('PHOTO > BINVAL'))
};
if (avatar.mime && avatar.data) {
data.avatar = avatar;
}
return profile.set(data);
};
failcb = function(stanza) {
return console.warn("couldn't get vcard", stanza);
};
to = bjid;
if (to === Strophe.getBareJidFromJid(X.conn.jid)) {
to = null;
}
X.conn.vcard.get(okcb, to, failcb);
return profile;
};
getClientEntity = function(contact, node, ver) {
var entity, failcb, jid, nodever, okcb;
jid = contact.get('jid');
if ((node != null) && (ver != null)) {
nodever = node + "#" + ver;
entity = entities.get(nodever);
if (entity != null) {
contact.set('entity', entity);
contact.set('features', entity.get('features'));
return entity;
}
entity = entities.add({
id: nodever
});
} else {
entity = entities.add({
jid: jid
});
}
contact.set('entity', entity);
okcb = function(stanza) {
var el, features, identities;
identities = (function() {
var i, len, ref, results;
ref = stanza.getElementsByTagName('identity');
results = [];
for (i = 0, len = ref.length; i < len; i++) {
el = ref[i];
results.push({
type: el.getAttribute('type'),
name: el.getAttribute('name'),
category: el.getAttribute('category')
});
}
return results;
})();
features = (function() {
var i, len, ref, results;
ref = stanza.getElementsByTagName('feature');
results = [];
for (i = 0, len = ref.length; i < len; i++) {
el = ref[i];
results.push(el.getAttribute('var'));
}
return results;
})();
entity.set({
client: _(identities).findWhere({
category: 'client'
}),
identities: identities,
features: features
});
return contact.set('features', features);
};
failcb = function(stanza) {
return console.warn("couldn't get client info", stanza);
};
X.conn.disco.info(jid, nodever, okcb, failcb, Tram.config.iqTimeout);
return entity;
};
getServerEntity = function(doneCallback, failCallback) {
var caps, entity, failcb, jid, node, nodever, okcb, ver;
jid = X.conn.domain;
caps = X.conn.features.getElementsByTagNameNS(Strophe.NS.CAPS, 'c')[0];
node = caps != null ? caps.getAttribute('node') : void 0;
ver = caps != null ? caps.getAttribute('ver') : void 0;
if ((node != null) && (ver != null)) {
nodever = node + "#" + ver;
entity = entities.get(nodever);
if (entity != null) {
serverInfo.set('entity', entity);
serverInfo.set('features', entity.get('features'));
if (typeof doneCallback === "function") {
doneCallback();
}
return entity;
}
entity = entities.add({
id: nodever
});
} else {
entity = entities.add({
jid: jid
});
}
serverInfo.set('entity', entity);
okcb = function(stanza) {
var el, features, identities;
identities = (function() {
var i, len, ref, results;
ref = stanza.getElementsByTagName('identity');
results = [];
for (i = 0, len = ref.length; i < len; i++) {
el = ref[i];
results.push({
type: el.getAttribute('type'),
name: el.getAttribute('name'),
category: el.getAttribute('category')
});
}
return results;
})();
features = (function() {
var i, len, ref, results;
ref = stanza.getElementsByTagName('feature');
results = [];
for (i = 0, len = ref.length; i < len; i++) {
el = ref[i];
results.push(el.getAttribute('var'));
}
return results;
})();
entity.set({
client: _(identities).findWhere({
category: 'client'
}),
identities: identities,
features: features
});
serverInfo.set('features', features);
return typeof doneCallback === "function" ? doneCallback() : void 0;
};
failcb = function(stanza) {
console.warn("couldn't get server info", stanza);
return typeof failCallback === "function" ? failCallback() : void 0;
};
X.conn.disco.info(jid, nodever, okcb, failcb, Tram.config.iqTimeout);
return entity;
};
onPresence = function(stanza) {
var bjid, caps, contact, from, node, priority, ref, show, stamp, status, type, ver;
type = (ref = stanza.getAttribute('type')) != null ? ref : 'available';
from = stanza.getAttribute('from');
bjid = Strophe.getBareJidFromJid(from);
switch (type) {
case 'subscribe':
if (X.conn.roster.findItem(bjid)) {
X.conn.roster.authorize(bjid);
return true;
}
break;
case 'subscribed':
X.conn.roster.subscribe(bjid);
return true;
case 'unsubscribe':
X.conn.roster.unauthorize(bjid);
return true;
case 'unsubscribed':
X.conn.roster.unsubscribe(bjid);
return true;
case 'error':
return true;
}
caps = stanza.getElementsByTagNameNS(Strophe.NS.CAPS, 'c')[0];
node = caps != null ? caps.getAttribute('node') : void 0;
ver = caps != null ? caps.getAttribute('ver') : void 0;
contact = getContact(from, node, ver, type);
show = getText(stanza.getElementsByTagName('show')[0]);
status = getText(stanza.getElementsByTagName('status')[0]);
priority = getText(stanza.getElementsByTagName('priority')[0]);
switch (type) {
case 'available':
if (show == null) {
show = 'online';
}
if (priority == null) {
priority = '0';
}
break;
case 'unavailable':
show = 'offline';
priority = '0';
}
contact.set({
presence: type,
show: show,
status: status,
priority: priority
});
if (type === 'unavailable') {
contact.w.disconnect();
}
if ((type === 'available' || type === 'unavailable') && contact.get('type') !== 'self') {
stamp = getStamp(stanza);
contact.chat.collection.add({
id: stanza.getAttribute('id'),
type: 'presence',
cls: 'presence',
from: from,
stamp: stamp,
contact: contact,
presence: type,
show: show,
status: status,
priority: priority,
text: status != null ? status : "is now " + show
});
}
return true;
};
onChatMessage = function(stanza) {
var body, carbon, chat, cls, contact, forwarded, from, fromSelf, html, id, message, msg, received, ref, replace, rid, sent, stamp, subject, text, thread, to, type, xhtml;
sent = stanza.getElementsByTagNameNS(Strophe.NS.CARBONS, 'sent')[0];
received = stanza.getElementsByTagNameNS(Strophe.NS.CARBONS, 'received')[0];
carbon = (sent != null) || (received != null);
if (sent != null) {
forwarded = sent.getElementsByTagNameNS(Strophe.NS.FORWARD, 'forwarded')[0];
} else if (received != null) {
forwarded = received.getElementsByTagNameNS(Strophe.NS.FORWARD, 'forwarded')[0];
}
if (forwarded != null) {
message = forwarded.getElementsByTagName('message')[0];
if (carbon) {
onChatState(message);
}
} else {
message = stanza;
}
text = getText(message.getElementsByTagName('body')[0]);
xhtml = message.getElementsByTagNameNS(Strophe.NS.XHTML_IM, 'html')[0];
if (!(text || xhtml)) {
return true;
}
id = message.getAttribute('id');
to = message.getAttribute('to');
from = message.getAttribute('from');
type = (ref = message.getAttribute('type')) != null ? ref : 'normal';
thread = getText(message.getElementsByTagName('thread')[0]);
subject = getText(message.getElementsByTagName('subject')[0]);
cls = type;
stamp = getStamp(stanza);
if (type === 'error') {
return true;
}
contact = getContact(from);
fromSelf = contact.get('bjid') === Strophe.getBareJidFromJid(X.conn.jid);
if ((xhtml != null) && (fromSelf || X.conn.roster.findItem(contact.get('bjid')))) {
body = xhtml.getElementsByTagNameNS(Strophe.NS.XHTML, 'body')[0];
html = Strophe.createHtml(body).innerHTML;
}
if ((forwarded != null) && fromSelf) {
chat = getContact(to).chat;
cls = 'self';
} else {
chat = contact.chat;
}
replace = stanza.getElementsByTagNameNS(Strophe.NS.MESSAGE_CORRECT, 'replace')[0];
rid = replace != null ? replace.getAttribute('id') : void 0;
if (rid != null) {
msg = chat.collection.get(rid);
if ((msg != null) && msg.get('from') === from) {
msg.set({
id: id,
corrected: true,
carbon: carbon,
thread: thread,
subject: subject,
text: text,
html: html
});
return true;
}
}
chat.collection.add({
id: id,
type: type,
cls: cls,
to: to,
from: from,
contact: contact,
stamp: stamp,
carbon: carbon,
thread: thread,
subject: subject,
text: text,
html: html
});
return true;
};
onChatState = function(stanza) {
var contact, delay, elements, from;
delay = stanza.getElementsByTagNameNS(Strophe.NS.DELAY, 'delay')[0];
if (delay != null) {
return true;
}
from = stanza.getAttribute('from');
contact = getContact(from);
elements = stanza.getElementsByTagNameNS(Strophe.NS.CHATSTATES, '*');
if (elements[0] != null) {
contact.set('chatstate', elements[0].tagName.toLowerCase());
}
return true;
};
onWebRTC = function(stanza) {
var contact, from, intent, payload, stamp;
from = stanza.getAttribute('from');
contact = getContact(from);
intent = stanza.getElementsByTagNameNS(Tram.NS.WEBRTC, 'intent')[0];
payload = stanza.getElementsByTagNameNS(Tram.NS.WEBRTC, 'payload')[0];
stamp = getStamp(stanza);
if (intent != null) {
switch (getText(intent)) {
case 'initiate':
contact.set('callstate', 'incoming');
contact.chat.collection.add({
id: stanza.getAttribute('id'),
type: 'call',
cls: 'call',
from: from,
stamp: stamp,
contact: contact,
text: 'incoming call'
});
break;
case 'terminate':
contact.w.disconnect();
contact.unset('callstate');
}
}
if (payload != null) {
contact.w.onPayload(stanza);
}
return true;
};
window.X = new Tram.XMPPInterface();
X.on('connecting', function() {
return connForm.unset('auth-errors');
});
X.on('authfail', function() {
return connForm.set('auth-errors', ['Invalid username or password.']);
});
X.on('disconnected', function() {
$('[data-step="login"]').removeClass('uk-hidden');
$('[data-step="main"]').addClass('uk-hidden');
return onDisconnected();
});
X.on('connected attached', function() {
$('[data-step="login"]').addClass('uk-hidden');
$('[data-step="main"]').removeClass('uk-hidden');
return onConnected();
});
X.on('status', function(status) {
switch (status) {
case Strophe.Status.CONNECTING:
return clientState.set('progress', 0);
case Strophe.Status.CONNECTED:
return clientState.set('progress', 100);
default:
return clientState.unset('progress');
}
});
$(window).on('beforeunload unload', function() {
return X.disconnect('Window closed');
});
window.connForm = new Tram.ConnectionForm();
connectfn = function() {
if (connForm.isValid()) {
return X.connect(connForm.get('username').trim(), connForm.get('password'));
}
};
$form = $('[data-form="connect"]');
$form.streamline();
window.connRivet = rivets.bind($form, {
form: connForm,
connect: connectfn
});
sendMessage = function() {
var contact, msg, ref, ref1, text;
text = $('#msg').val();
if (text !== '' && clientState.has('contact')) {
contact = clientState.get('contact');
msg = $msg({
to: contact.get('jid'),
from: X.conn.jid,
type: 'chat'
}).c('body').t(text).up();
if (settings.get('chatstates') && (ref = Strophe.NS.CHATSTATES, indexOf.call((ref1 = contact.get('features')) != null ? ref1 : [], ref) >= 0)) {
msg.c('active', {
xmlns: Strophe.NS.CHATSTATES
});
contact.set('chatstate/self', 'active', {
silent: true
});
}
X.conn.send(msg.tree());
contact.chat.collection.add({
type: 'chat',
cls: 'self',
from: X.conn.jid,
to: contact.get('jid'),
contact: contacts.findWhere({
type: 'self'
}),
text: text
});
contact.chat.scroll();
return $('#msg').val('');
}
};
$('body').on('dragover dragenter', function(event) {
event.stopPropagation();
return event.preventDefault();
});
$('body').on('drop', function(event) {
event.stopPropagation();
return event.preventDefault();
});
$('[data-send-button]').on('click', sendMessage);
$('#msg').on('keypress', function(e) {
var ref;
if (e.keyCode === 13) {
return sendMessage();
} else {
return (ref = clientState.get('contact')) != null ? ref.set('chatstate/self', 'composing') : void 0;
}
});
$('#msg').on('blur', function() {
var ref, ref1;
if (((ref = clientState.get('contact')) != null ? ref.get('chatstate/self') : void 0) === 'composing') {
return (ref1 = clientState.get('contact')) != null ? ref1.set('chatstate/self', 'paused') : void 0;
}
});
$('[data-add-button]').on('click', function() {
var jid;
jid = $('#new-contact').val().trim();
if (jid === '') {
return;
}
if (indexOf.call(jid, '@') < 0) {
jid = jid + "@" + Tram.config.domain;
}
$('#new-contact').val('');
return X.conn.roster.add(jid, null, [], function() {
return X.conn.roster.subscribe(jid, 'I want to chat', null);
});
});
$('#new-contact').on('keypress', function(e) {
if (e.keyCode === 13) {
return $('[data-add-button]').trigger('click');
}
});
$('[data-unregister-button]').on('click', function() {
return X.unregister();
});
$('#settings-modal').one('show.uk.modal', function() {
window.settingsRivet = rivets.bind($('[data-app="settings"]'), {
settings: settings
});
return window.serverRivet = rivets.bind($('[data-app="server-info"]'), {
server: serverInfo
});
});
$('#profile-modal').one('show.uk.modal', function() {
return window.profileRivet = rivets.bind($('[data-app="profile"]'), {
contact: contacts.get(X.conn.jid)
});
});
window.setInterval(function() {
return $('time[datetime]').each(function() {
var $this;
$this = $(this);
return $this.text(moment($this.attr('datetime')).fromNow());
});
}, 15 * 1000);
}).call(this);
//# sourceMappingURL=index.js.map