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 !76
parents b094e983 2b5b0939
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
); );
} }
}; };
Strophe.addNamespace('RECEIPTS', 'urn:xmpp:receipts');
var gadget_klass = rJS(window); var gadget_klass = rJS(window);
...@@ -50,14 +51,29 @@ ...@@ -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) { function deferOnMessageStanza(message) {
var gadget = this; var gadget = this;
enqueueDefer(gadget, function () { enqueueDefer(gadget, function () {
var to = Strophe.getBareJidFromJid(message.getAttribute('to')), 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'), type = message.getAttribute('type'),
body = message.querySelector('body'); body = message.querySelector('body'),
req = message.getElementsByTagName('request'),
connection = gadget.props.connection;
if (type !== "chat") { if (type !== "chat") {
throw new Error("Unsupported message type: " + type); throw new Error("Unsupported message type: " + type);
...@@ -66,8 +82,18 @@ ...@@ -66,8 +82,18 @@
throw new Error("Expected message to: " + to); throw new Error("Expected message to: " + to);
} }
if (body !== null) { if (body !== null) {
return gadget.notifyXMPPMessageTextReceived(from, to, if (connection !== undefined && id !== null && req.length > 0) {
body.textContent); // 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; return true;
...@@ -148,27 +174,29 @@ ...@@ -148,27 +174,29 @@
// Try to auto connection // Try to auto connection
if (gadget.props.server !== undefined) { if (gadget.props.server !== undefined) {
gadget.props.connection = new Strophe.Connection(gadget.props.server); gadget.props.connection = new Strophe.Connection(gadget.props.server);
var connection = gadget.props.connection;
// gadget.props.connection.rawInput = function (data) {
// console.log("RECEIVING SOMETHING"); // connection.rawInput = function (data) {
// console.log(data); // console.log("RECEIVING SOMETHING");
// }; // console.log(data);
// gadget.props.connection.rawOutput = function (data) { // };
// console.log("SENDING SOMETHING"); // connection.rawOutput = function (data) {
// console.log(data); // console.log("SENDING SOMETHING");
// }; // console.log(data);
// };
gadget.props.connection.connect(
connection.connect(
gadget.props.jid, gadget.props.jid,
gadget.props.passwd, gadget.props.passwd,
handleConnectionCallback handleConnectionCallback
); );
gadget.props.connection.addHandler( window.onbeforeunload = disconnectOnbeforeunload(connection);
connection.addHandler(
deferOnPresenceStanza.bind(gadget), deferOnPresenceStanza.bind(gadget),
null, null,
"presence" "presence"
); );
gadget.props.connection.addHandler( connection.addHandler(
deferOnMessageStanza.bind(gadget), deferOnMessageStanza.bind(gadget),
null, null,
"message", "message",
...@@ -342,9 +370,10 @@ ...@@ -342,9 +370,10 @@
}) })
.declareMethod('sendMessage', function (jid, text) { .declareMethod('sendMessage', function (jid, text) {
this.props.connection.send( var connection = this.props.connection;
$msg({to: jid, type: "chat"}).c('body').t(text) 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)); }(window, rJS, Strophe, $iq, $pres, $msg, RSVP));
\ No newline at end of file
...@@ -230,7 +230,7 @@ ...@@ -230,7 +230,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>949.35404.19073.41659</string> </value> <value> <string>949.56116.24375.61559</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -248,7 +248,7 @@ ...@@ -248,7 +248,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1456844917.63</float> <float>1458087635.15</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -43,6 +43,30 @@ ...@@ -43,6 +43,30 @@
return gadget.state_parameter_dict.my_jid + '-' + jid; 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) { function getLog(gadget, jid, options) {
return wrapJioCall(gadget, 'getAttachment', [getStorageIdFromJid(gadget, jid), 'enclosure', options]) return wrapJioCall(gadget, 'getAttachment', [getStorageIdFromJid(gadget, jid), 'enclosure', options])
.push(undefined, function (error) { .push(undefined, function (error) {
...@@ -55,11 +79,21 @@ ...@@ -55,11 +79,21 @@
} }
function addLog(gadget, jid, text, is_incoming) { function addLog(gadget, jid, text, is_incoming) {
return getLog(gadget, jid, {format: 'text'}) var deferred = RSVP.defer();
.push(function (result) { enqueueDefer(gadget, function () {
var new_history = result + getLogString(text, is_incoming); return getLog(gadget, jid, {format: 'text'})
return wrapJioCall(gadget, 'putAttachment', [getStorageIdFromJid(gadget, jid), 'enclosure', new_history]); .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;
});
} }
function dropConnectionGadget(gadget) { function dropConnectionGadget(gadget) {
...@@ -160,6 +194,9 @@ ...@@ -160,6 +194,9 @@
} }
rJS(window) rJS(window)
.ready(function (g) {
g.props = {};
})
.allowPublicAcquisition("notifyXMPPConnecting", function () { .allowPublicAcquisition("notifyXMPPConnecting", function () {
return; return;
...@@ -431,6 +468,22 @@ ...@@ -431,6 +468,22 @@
throw new Error('Unsupported putAttachment: ' + id + ' ' + name); 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 () { .declareService(function () {
return loopEventListener( return loopEventListener(
window, window,
......
...@@ -230,7 +230,7 @@ ...@@ -230,7 +230,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>949.36462.22852.4232</string> </value> <value> <string>949.57481.62457.42496</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -248,7 +248,7 @@ ...@@ -248,7 +248,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1457082886.14</float> <float>1458172700.61</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </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