Commit 48bd58c6 authored by Boris Kocherov's avatar Boris Kocherov Committed by Cédric Le Ninivin

erp5_web_jabber_client: work for for avoiding messages loss

Add Semaphore functionality on adding Log to history.

Implementation of Delivery Receipt sending. Delivery Receipt hadnling is
not implemented yet.
http://xmpp.org/extensions/xep-0184.html

clients which support xep-0184
adnroid: https://conversations.im/ https://www.xabber.com/
linux: http://psi-plus.com/

/reviewed-on nexedi/erp5!76
parents b094e983 2b5b0939
......@@ -22,6 +22,7 @@
);
}
};
Strophe.addNamespace('RECEIPTS', 'urn:xmpp:receipts');
var gadget_klass = rJS(window);
......@@ -50,14 +51,29 @@
}
function disconnectOnbeforeunload(connection) {
return function (event) {
/* XXX it can be interfere with changed warning
if (changed && $('button.save')) {
return unsaved_warn_message;
}*/
connection.sync = true;
connection.disconnect();
connection.flush();
};
}
function deferOnMessageStanza(message) {
var gadget = this;
enqueueDefer(gadget, function () {
var to = Strophe.getBareJidFromJid(message.getAttribute('to')),
from = Strophe.getBareJidFromJid(message.getAttribute('from')),
from = message.getAttribute('from'),
id = message.getAttribute('id'),
type = message.getAttribute('type'),
body = message.querySelector('body');
body = message.querySelector('body'),
req = message.getElementsByTagName('request'),
connection = gadget.props.connection;
if (type !== "chat") {
throw new Error("Unsupported message type: " + type);
......@@ -66,8 +82,18 @@
throw new Error("Expected message to: " + to);
}
if (body !== null) {
return gadget.notifyXMPPMessageTextReceived(from, to,
body.textContent);
if (connection !== undefined && id !== null && req.length > 0) {
// xep-0184 send delivery receipt
connection.send(
$msg({from: connection.jid, to: from})
.c("received", {xmlns: Strophe.NS.RECEIPTS, id: id})
);
}
return gadget.notifyXMPPMessageTextReceived(
Strophe.getBareJidFromJid(from),
to,
body.textContent
);
}
});
return true;
......@@ -148,27 +174,29 @@
// Try to auto connection
if (gadget.props.server !== undefined) {
gadget.props.connection = new Strophe.Connection(gadget.props.server);
// gadget.props.connection.rawInput = function (data) {
// console.log("RECEIVING SOMETHING");
// console.log(data);
// };
// gadget.props.connection.rawOutput = function (data) {
// console.log("SENDING SOMETHING");
// console.log(data);
// };
gadget.props.connection.connect(
var connection = gadget.props.connection;
// connection.rawInput = function (data) {
// console.log("RECEIVING SOMETHING");
// console.log(data);
// };
// connection.rawOutput = function (data) {
// console.log("SENDING SOMETHING");
// console.log(data);
// };
connection.connect(
gadget.props.jid,
gadget.props.passwd,
handleConnectionCallback
);
gadget.props.connection.addHandler(
window.onbeforeunload = disconnectOnbeforeunload(connection);
connection.addHandler(
deferOnPresenceStanza.bind(gadget),
null,
"presence"
);
gadget.props.connection.addHandler(
connection.addHandler(
deferOnMessageStanza.bind(gadget),
null,
"message",
......@@ -342,9 +370,10 @@
})
.declareMethod('sendMessage', function (jid, text) {
this.props.connection.send(
$msg({to: jid, type: "chat"}).c('body').t(text)
);
var connection = this.props.connection;
connection.send($msg({id: connection.getUniqueId(), to: jid, type: "chat"})
.c('body').t(text).up()
.c('request', {'xmlns': Strophe.NS.RECEIPTS}));
});
}(window, rJS, Strophe, $iq, $pres, $msg, RSVP));
\ No newline at end of file
......@@ -230,7 +230,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>949.35404.19073.41659</string> </value>
<value> <string>949.56116.24375.61559</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -248,7 +248,7 @@
</tuple>
<state>
<tuple>
<float>1456844917.63</float>
<float>1458087635.15</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -43,6 +43,30 @@
return gadget.state_parameter_dict.my_jid + '-' + jid;
}
function enqueueDefer(gadget, callback) {
var deferred = gadget.props.current_deferred;
// Unblock queue
if (deferred !== undefined) {
deferred.resolve("Another event added");
}
// Add next callback
try {
gadget.props.service_queue.push(callback);
} catch (error) {
throw new Error("Connection gadget already crashed... " +
gadget.props.service_queue.rejectedReason.toString());
}
// Block the queue
deferred = RSVP.defer();
gadget.props.current_deferred = deferred;
gadget.props.service_queue.push(function () {
return deferred.promise;
});
}
function getLog(gadget, jid, options) {
return wrapJioCall(gadget, 'getAttachment', [getStorageIdFromJid(gadget, jid), 'enclosure', options])
.push(undefined, function (error) {
......@@ -55,10 +79,20 @@
}
function addLog(gadget, jid, text, is_incoming) {
var deferred = RSVP.defer();
enqueueDefer(gadget, function () {
return getLog(gadget, jid, {format: 'text'})
.push(function (result) {
var new_history = result + getLogString(text, is_incoming);
return wrapJioCall(gadget, 'putAttachment', [getStorageIdFromJid(gadget, jid), 'enclosure', new_history]);
})
.push(function () {
deferred.resolve();
return true;
});
});
return new RSVP.Queue().push(function () {
return deferred.promise;
});
}
......@@ -160,6 +194,9 @@
}
rJS(window)
.ready(function (g) {
g.props = {};
})
.allowPublicAcquisition("notifyXMPPConnecting", function () {
return;
......@@ -431,6 +468,22 @@
throw new Error('Unsupported putAttachment: ' + id + ' ' + name);
})
.declareService(function () {
// queue for addLog
var context = this;
context.props.service_queue = new RSVP.Queue();
enqueueDefer(context);
return new RSVP.Queue()
.push(function () {
return context.props.service_queue;
})
.push(function () {
throw new Error("Service should not have been stopped!");
});
})
.declareService(function () {
return loopEventListener(
window,
......
......@@ -230,7 +230,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>949.36462.22852.4232</string> </value>
<value> <string>949.57481.62457.42496</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -248,7 +248,7 @@
</tuple>
<state>
<tuple>
<float>1457082886.14</float>
<float>1458172700.61</float>
<string>UTC</string>
</tuple>
</state>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment