Commit 4e97f824 authored by Nicolas Wavrant's avatar Nicolas Wavrant

erp5_web_jabber_client: implement desktop notifications

parent 027fb6a3
/*global window, rJS, RSVP, jIO, document, loopEventListener */ /*global window, rJS, RSVP, jIO, Notification, document, loopEventListener */
/*jslint indent: 2, maxerr: 3, nomen: true */ /*jslint indent: 2, maxerr: 3, nomen: true */
(function (window, rJS, RSVP, jIO, document, loopEventListener) { (function (window, rJS, RSVP, jIO, Notification, document, loopEventListener) {
"use strict"; "use strict";
var CONNECTION_GADGET_SCOPE = "connection", var CONNECTION_GADGET_SCOPE = "connection",
CONNECTION_GADGET_URL = "connection/", CONNECTION_GADGET_URL = "connection/",
JIO_GADGET_URL = "gadget_jio.html"; JIO_GADGET_URL = "gadget_jio.html";
function dropNotification() { function dropNotification(gadget) {
var contact;
document.querySelector("link[rel='shortcut icon']").setAttribute("href", "gadget_jabberclient_notification_ok.ico"); document.querySelector("link[rel='shortcut icon']").setAttribute("href", "gadget_jabberclient_notification_ok.ico");
for (contact in gadget.state_parameter_dict.notification_per_contact) {
if (gadget.state_parameter_dict.notification_per_contact.hasOwnProperty(contact)) {
gadget.state_parameter_dict.notification_per_contact[contact].close();
delete gadget.state_parameter_dict.notification_per_contact[contact];
}
}
} }
function wrapJioCall(gadget, method_name, argument_list) { function wrapJioCall(gadget, method_name, argument_list) {
...@@ -272,7 +279,8 @@ ...@@ -272,7 +279,8 @@
document.querySelector("link[rel='shortcut icon']").setAttribute("href", "gadget_jabberclient_notification_warning.ico"); document.querySelector("link[rel='shortcut icon']").setAttribute("href", "gadget_jabberclient_notification_warning.ico");
} }
var gadget = this; var gadget = this,
from = argument_list[0];
return addLog(this, argument_list[0], argument_list[2], true) return addLog(this, argument_list[0], argument_list[2], true)
.push(function () { .push(function () {
return initializeContact(gadget, argument_list[0]); return initializeContact(gadget, argument_list[0]);
...@@ -281,6 +289,17 @@ ...@@ -281,6 +289,17 @@
doc.notification = true; doc.notification = true;
return gadget.state_parameter_dict.volatile_jio.put(argument_list[0], doc); return gadget.state_parameter_dict.volatile_jio.put(argument_list[0], doc);
}) })
.push(function () {
if (!document.hasFocus() && Notification.permission === "granted") {
gadget.state_parameter_dict.notification_per_contact[from] = new Notification(
"New message from " + from,
{
silent: true,
tag: 'jabberclient:' + from
}
);
}
})
.push(function () { .push(function () {
return gadget.refresh(); return gadget.refresh();
}); });
...@@ -361,9 +380,10 @@ ...@@ -361,9 +380,10 @@
connected: false, connected: false,
server: 'https://mail.nexedi.net/chat/http-bind/', server: 'https://mail.nexedi.net/chat/http-bind/',
jio: '', jio: '',
passwd: '' passwd: '',
notification_per_contact: {}
}; };
dropNotification(); dropNotification(gadget);
}) })
.ready(function (gadget) { .ready(function (gadget) {
return gadget.getDeclaredGadget('persistent_jio') return gadget.getDeclaredGadget('persistent_jio')
...@@ -485,12 +505,16 @@ ...@@ -485,12 +505,16 @@
}) })
.declareService(function () { .declareService(function () {
var gadget = this;
function _dropNotification() {
return dropNotification(gadget);
}
return loopEventListener( return loopEventListener(
window, window,
'focus', 'focus',
false, false,
dropNotification _dropNotification
); );
}); });
}(window, rJS, RSVP, jIO, document, loopEventListener)); }(window, rJS, RSVP, jIO, Notification, document, loopEventListener));
\ No newline at end of file \ No newline at end of file
...@@ -236,7 +236,7 @@ ...@@ -236,7 +236,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>967.46266.46579.39065</string> </value> <value> <string>988.26490.15321.9130</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -254,7 +254,7 @@ ...@@ -254,7 +254,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1526979580.7</float> <float>1606985968.14</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
/*global window, rJS*/ /*global window, rJS, Notification*/
/*jslint nomen: true, indent: 2, maxerr: 3 */ /*jslint nomen: true, indent: 2, maxerr: 3 */
(function (window, rJS) { (function (window, rJS, Notification) {
"use strict"; "use strict";
rJS(window) rJS(window)
...@@ -102,7 +102,16 @@ ...@@ -102,7 +102,16 @@
]] ]]
} }
}); });
})
.push(function () {
return gadget.requestNotificationPermission();
}); });
})
.declareJob("requestNotificationPermission", function () {
if (Notification !== undefined && Notification.permission !== "denied") {
return Notification.requestPermission();
}
}); });
}(window, rJS)); }(window, rJS, Notification));
\ No newline at end of file \ No newline at end of file
...@@ -236,7 +236,7 @@ ...@@ -236,7 +236,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>968.4124.44320.51336</string> </value> <value> <string>988.37813.29181.47069</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -254,7 +254,7 @@ ...@@ -254,7 +254,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1528383833.91</float> <float>1607665366.11</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