add mam support for master tab

Esse commit está contido em:
sualko
2017-03-17 15:59:41 +01:00
commit 52588a1437
9 arquivos alterados com 259 adições e 4 exclusões
+2
Ver Arquivo
@@ -25,3 +25,5 @@ lib/i18next/
lib/jquery-i18next/
lib/magnific-popup/
.idea
/lib/strophejs-plugin-mam
/lib/strophejs-plugin-rsm
+3 -1
Ver Arquivo
@@ -34,6 +34,8 @@
"strophe.chatstates": "strophe/strophejs-plugin-chatstates",
"jquery-i18next": "^1.2.0",
"i18next": "^5.0.0",
"magnific-popup": "^1.1.0"
"magnific-popup": "^1.1.0",
"strophejs-plugin-mam": "strophe/strophejs-plugin-mam#27e8d25",
"strophejs-plugin-rsm": "strophe/strophejs-plugin-rsm#fa16b3b"
}
}
+12
Ver Arquivo
@@ -47,6 +47,18 @@
"license": "MIT",
"url": "https://github.com/strophe/strophejs-plugins/tree/master/chatstates"
},
{
"name": "strophe.js/mam",
"file": "lib/strophejs-plugin-mam/strophe.mam.js",
"license": "MIT",
"url": "https://github.com/strophe/strophejs-plugin-mam"
},
{
"name": "strophe.js/rsm",
"file": "lib/strophejs-plugin-rsm/strophe.rsm.js",
"license": "MIT",
"url": "https://github.com/strophe/strophejs-plugin-rsm"
},
{
"name": "strophe.jinglejs",
"file": "lib/strophe.jinglejs/strophe.jinglejs-bundle.js",
+27
Ver Arquivo
@@ -232,6 +232,7 @@
.jsxc_fade {
position: relative;
overflow: hidden;
.jsxc_overlay {
display: none;
@@ -302,6 +303,31 @@
}
}
}
.jsxc_mam-load-more {
text-align: center;
font-size: 0.8em;
font-style: italic;
position: absolute;
top: -42px;
left: 0;
right: 0;
height: 42px;
cursor: pointer;
z-index: 80;
line-height: 42px;
opacity: 0;
transition: opacity 0.5s, top 0.5s;
&.jsxc_show {
top: 0;
opacity: 0.7;
&:hover {
opacity: 1;
}
}
}
}
.jsxc_avatar {
@@ -312,6 +338,7 @@
width: 100%;
overflow: hidden;
padding: 3px;
padding-top: 42px;
}
.slimScrollDiv {
+14 -3
Ver Arquivo
@@ -2717,8 +2717,12 @@ jsxc.gui.window = {
$('[data-bid="' + bid + '"]').find('.jsxc_lastmsg .jsxc_text').html(msg);
}
if (jsxc.Message.getDOM(uid).length > 0) {
jsxc.Message.getDOM(uid).replaceWith(msgDiv);
var currentMessageElement = jsxc.Message.getDOM(uid);
if (currentMessageElement.length > 0) {
if (currentMessageElement.attr('data-queryId')) {
msgDiv.attr('data-queryId', currentMessageElement.attr('data-queryId'));
}
currentMessageElement.replaceWith(msgDiv);
} else {
win.find('.jsxc_textarea').append(msgDiv);
}
@@ -2763,7 +2767,9 @@ jsxc.gui.window = {
jsxc.gui.detectUriScheme(win);
jsxc.gui.detectEmail(win);
jsxc.gui.window.scrollDown(bid);
if (!message.forwarded) {
jsxc.gui.window.scrollDown(bid);
}
},
/**
@@ -2836,6 +2842,11 @@ jsxc.gui.window = {
if (win.length > 0) {
win.find('.jsxc_textarea').empty();
}
var buddyData = jsxc.storage.getUserItem('buddy', bid) || {};
delete buddyData.lastArchiveUid;
delete buddyData.archiveExhausted;
jsxc.storage.setUserItem('buddy', bid, buddyData);
},
/**
+5
Ver Arquivo
@@ -301,5 +301,10 @@ jsxc.options = {
screenMediaExtension: {
firefox: '',
chrome: ''
},
mam: {
enable: true,
max: null
}
};
+2
Ver Arquivo
@@ -35,6 +35,7 @@ jsxc.otr = {
});
} else {
jsxc.gui.window.postMessage({
_uid: d._uid,
bid: bid,
direction: jsxc.Message.IN,
msg: d.msg,
@@ -209,6 +210,7 @@ jsxc.otr = {
// Receive message
jsxc.otr.objects[bid].on('ui', function(msg, encrypted, meta) {
jsxc.otr.receiveMessage({
_uid: meta._uid,
bid: bid,
msg: msg,
encrypted: encrypted === true,
+3
Ver Arquivo
@@ -967,12 +967,15 @@ jsxc.xmpp = {
if (jsxc.otr.objects.hasOwnProperty(bid) && body) {
// @TODO check for file upload url after decryption
jsxc.otr.objects[bid].receiveMsg(body, {
_uid: mid,
foo: 'bar',
stamp: stamp,
forwarded: forwarded,
attachment: attachment
});
} else {
jsxc.gui.window.postMessage({
_uid: mid,
bid: bid,
direction: jsxc.Message.IN,
msg: body,
+191
Ver Arquivo
@@ -0,0 +1,191 @@
/**
* Implements XEP-0313: Message Archive Management.
*
* @namespace jsxc.xmpp.mam
* @see {@link https://xmpp.org/extensions/xep-0313.html}
*/
jsxc.xmpp.mam = {
conn: null
};
jsxc.xmpp.mam.init = function() {
var self = jsxc.xmpp.mam;
self.conn = jsxc.xmpp.conn;
};
jsxc.xmpp.mam.isEnabled = function() {
var self = jsxc.xmpp.mam;
var mamOptions = jsxc.options.get('mam') || {};
// prosody does not announce mam:2, while it is supported
var hasFeatureMam0 = jsxc.xmpp.hasFeatureByJid(self.conn.domain, 'urn:xmpp:mam:0');
var hasFeatureMam2 = jsxc.xmpp.hasFeatureByJid(self.conn.domain, Strophe.NS.MAM);
return (hasFeatureMam0 || hasFeatureMam2) && mamOptions.enable;
};
jsxc.xmpp.mam.nextMessages = function(bid) {
var self = jsxc.xmpp.mam;
var buddyData = jsxc.storage.getUserItem('buddy', bid) || {};
var lastArchiveUid = buddyData.lastArchiveUid;
var queryId = self.conn.getUniqueId();
var mamOptions = jsxc.options.get('mam') || {};
var history = jsxc.storage.getUserItem('history', bid) || [];
if (buddyData.archiveExhausted) {
jsxc.debug('No more archived messages.');
return;
}
var queryOptions = {
queryid: queryId,
before: lastArchiveUid || '',
with: bid,
onMessage: function() {
var args = Array.from(arguments);
args.unshift(bid);
self.onMessage.apply(this, args);
return true;
},
onComplete: function() {
var args = Array.from(arguments);
args.unshift(bid);
self.onComplete.apply(this, args);
return true;
}
};
var oldestMessageId = history[history.length - 1];
if (oldestMessageId && !lastArchiveUid) {
var oldestMessage = new jsxc.Message(oldestMessageId);
queryOptions.end = (new Date(oldestMessage.stamp)).toISOString();
}
if (mamOptions.max) {
queryOptions.max = mamOptions.max;
}
self.conn.mam.query(undefined, queryOptions);
};
jsxc.xmpp.mam.onMessage = function(bid, stanza) {
stanza = $(stanza);
var result = stanza.find('result[xmlns="' + Strophe.NS.MAM + '"]');
var queryId = result.attr('queryid');
if (result.length !== 1) {
return;
}
var forwarded = result.find('forwarded[xmlns="' + jsxc.CONST.NS.FORWARD + '"]');
var message = forwarded.find('message');
var messageId = $(message).attr('id');
if (message.length !== 1) {
return;
}
var from = message.attr('from');
var to = message.attr('to');
if (jsxc.jidToBid(from) !== bid && jsxc.jidToBid(to) !== bid) {
return;
}
var delay = forwarded.find('delay[xmlns="urn:xmpp:delay"]');
var stamp = (delay.length > 0) ? new Date(delay.attr('stamp')) : new Date();
stamp = stamp.getTime();
var body = $(message).find('body:first').text();
if (!body || body.match(/\?OTR/i)) {
return true;
}
var direction = (jsxc.jidToBid(to) === bid) ? jsxc.Message.OUT : jsxc.Message.IN;
var win = jsxc.gui.window.get(bid);
var textarea = win.find('.jsxc_textarea');
if (textarea.find('[id="' + messageId + '"]').length === 0) {
var pseudoChatElement = $('<div>');
pseudoChatElement.attr('id', messageId.replace(/:/g, '-'));
pseudoChatElement.attr('data-queryId', queryId);
var lastMessage = textarea.find('[data-queryId="' + queryId + '"]').last();
var history = jsxc.storage.getUserItem('history', bid) || [];
if (history.indexOf(messageId) < 0) {
if (lastMessage.length === 0) {
textarea.prepend(pseudoChatElement);
history.push(messageId);
} else {
lastMessage.after(pseudoChatElement);
history.splice(history.indexOf(lastMessage.attr('id').replace(/-/g, ':')), 0, messageId);
}
}
jsxc.storage.setUserItem('history', bid, history);
}
jsxc.gui.window.postMessage({
_uid: messageId,
bid: bid,
direction: direction,
msg: body,
encrypted: false,
forwarded: true,
stamp: stamp
});
};
jsxc.xmpp.mam.onComplete = function(bid, stanza) {
stanza = $(stanza);
var fin = stanza.find('fin[xmlns="' + Strophe.NS.MAM + '"]');
var buddyData = jsxc.storage.getUserItem('buddy', bid) || {};
buddyData.archiveExhausted = fin.attr('complete') === 'true';
buddyData.lastArchiveUid = fin.find('first').text();
jsxc.storage.setUserItem('buddy', bid, buddyData);
};
jsxc.xmpp.mam.initWindow = function(ev, win) {
var self = jsxc.xmpp.mam;
if (!jsxc.xmpp.conn && jsxc.master) {
$(document).one('attached.jsxc', function() {
self.initWindow(null, win);
});
return;
}
if (!jsxc.xmpp.mam.isEnabled()) {
return;
}
var bid = win.attr('data-bid');
var buddyData = jsxc.storage.getUserItem('buddy', bid) || {};
var element = $('<div>');
element.addClass('jsxc_mam-load-more');
element.appendTo(win.find('.slimScrollDiv'));
element.click(function() {
jsxc.xmpp.mam.nextMessages(bid);
});
element.text($.t('Load_older_messages'));
win.find('.jsxc_textarea').scroll(function() {
if (this.scrollTop < 42 && !buddyData.archiveExhausted) {
element.addClass('jsxc_show');
} else {
element.removeClass('jsxc_show');
}
});
win.find('.jsxc_textarea').scroll();
};
$(document).on('attached.jsxc', jsxc.xmpp.mam.init);
$(document).on('init.window.jsxc', jsxc.xmpp.mam.initWindow);