1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: jsxc.lib.xmpp.mam.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: jsxc.lib.xmpp.mam.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* 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 mamOptions = jsxc.options.get('mam') || {};
var features = jsxc.storage.getUserItem('features') || [];
var hasFeatureMam1 = features.indexOf('urn:xmpp:mam:1') >= 0;
var hasFeatureMam2 = features.indexOf('urn:xmpp:mam:2') >= 0;
if (hasFeatureMam1 && !hasFeatureMam2) {
Strophe.addNamespace('MAM', 'urn:xmpp:mam:1');
}
return (hasFeatureMam1 || 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) || {};
var win = jsxc.gui.window.get(bid);
buddyData.archiveExhausted = fin.attr('complete') === 'true';
buddyData.lastArchiveUid = fin.find('first').text();
if (buddyData.archiveExhausted) {
win.find('.jsxc_fade').removeClass('jsxc_mam-enable');
}
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.master) {
return;
}
$(document).on('features.jsxc', function() {
jsxc.xmpp.mam.addLoadButton(win);
});
var features = jsxc.storage.getUserItem('features');
if (features !== null) {
// features.jsxc was already fired
jsxc.xmpp.mam.addLoadButton(win);
}
};
jsxc.xmpp.mam.addLoadButton = function(win) {
if (!jsxc.xmpp.mam.isEnabled()) {
return;
}
var classNameShow = 'jsxc_show';
var classNameMamEnable = 'jsxc_mam-enable';
var bid = win.attr('data-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() {
var buddyData = jsxc.storage.getUserItem('buddy', bid) || {};
if (this.scrollTop < 42 && !buddyData.archiveExhausted) {
element.addClass(classNameShow);
} else {
element.removeClass(classNameShow);
}
if (!buddyData.archiveExhausted) {
win.find('.jsxc_fade').addClass(classNameMamEnable);
}
});
win.find('.jsxc_textarea').scroll();
};
$(document).on('attached.jsxc', jsxc.xmpp.mam.init);
$(document).on('init.window.jsxc', jsxc.xmpp.mam.initWindow);
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="jsxc.Message.html">Message</a></li></ul><h3>Namespaces</h3><ul><li><a href="jsxc.html">jsxc</a></li><li><a href="jsxc.fileTransfer.html">fileTransfer</a></li><li><a href="jsxc.gui.html">gui</a></li><li><a href="jsxc.gui.dialog.html">dialog</a></li><li><a href="jsxc.gui.queryActions.html">queryActions</a></li><li><a href="jsxc.gui.roster.html">roster</a></li><li><a href="jsxc.gui.window.html">window</a></li><li><a href="jsxc.muc.html">muc</a></li><li><a href="jsxc.notification.html">notification</a></li><li><a href="jsxc.options.html">options</a></li><li><a href="jsxc.otr.html">otr</a></li><li><a href="jsxc.storage.html">storage</a></li><li><a href="jsxc.tab.html">tab</a></li><li><a href="jsxc.webrtc.html">webrtc</a></li><li><a href="jsxc.xmpp.html">xmpp</a></li><li><a href="jsxc.xmpp.bookmarks.html">bookmarks</a></li><li><a href="jsxc.xmpp.carbons.html">carbons</a></li><li><a href="jsxc.xmpp.chatState.html">chatState</a></li><li><a href="jsxc.xmpp.httpUpload.html">httpUpload</a></li><li><a href="jsxc.xmpp.mam.html">mam</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri May 10 2019 12:36:11 GMT+0200 (Central European Summer Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
|