Commit 8e4e9185 authored by JC Brand's avatar JC Brand

Replace lodash methods with built-in ones and add jsdoc strings

parent 2d44144b
...@@ -8,9 +8,10 @@ ...@@ -8,9 +8,10 @@
* @module converse-notification * @module converse-notification
*/ */
import converse from "@converse/headless/converse-core"; import converse from "@converse/headless/converse-core";
import { get } from "lodash";
const { Strophe, _, sizzle } = converse.env, const { Strophe, sizzle } = converse.env;
u = converse.env.utils; const u = converse.env.utils;
converse.plugins.add('converse-notification', { converse.plugins.add('converse-notification', {
...@@ -56,7 +57,7 @@ converse.plugins.add('converse-notification', { ...@@ -56,7 +57,7 @@ converse.plugins.add('converse-notification', {
} }
const mentioned = (new RegExp(`\\b${room.get('nick')}\\b`)).test(body.textContent); const mentioned = (new RegExp(`\\b${room.get('nick')}\\b`)).test(body.textContent);
notify_all = notify_all === true || notify_all = notify_all === true ||
(Array.isArray(notify_all) && _.includes(notify_all, room_jid)); (Array.isArray(notify_all) && notify_all.includes(room_jid));
if (sender === room.get('nick') || (!notify_all && !mentioned)) { if (sender === room.get('nick') || (!notify_all && !mentioned)) {
return false; return false;
} }
...@@ -93,12 +94,12 @@ converse.plugins.add('converse-notification', { ...@@ -93,12 +94,12 @@ converse.plugins.add('converse-notification', {
}; };
/**
* Plays a notification sound
* @private
* @method _converse#playSoundNotification
*/
_converse.playSoundNotification = function () { _converse.playSoundNotification = function () {
/* Plays a sound to notify that a new message was recieved.
*/
// XXX Eventually this can be refactored to use Notification's sound
// feature, but no browser currently supports it.
// https://developer.mozilla.org/en-US/docs/Web/API/notification/sound
if (_converse.play_sounds && window.Audio !== undefined) { if (_converse.play_sounds && window.Audio !== undefined) {
const audioOgg = new Audio(_converse.sounds_path+"msg_received.ogg"); const audioOgg = new Audio(_converse.sounds_path+"msg_received.ogg");
const canPlayOgg = audioOgg.canPlayType('audio/ogg'); const canPlayOgg = audioOgg.canPlayType('audio/ogg');
...@@ -123,10 +124,13 @@ converse.plugins.add('converse-notification', { ...@@ -123,10 +124,13 @@ converse.plugins.add('converse-notification', {
Notification.permission === "granted"; Notification.permission === "granted";
}; };
/**
* Shows an HTML5 Notification with the passed in message
* @private
* @method _converse#showMessageNotification
* @param { String } message
*/
_converse.showMessageNotification = function (message) { _converse.showMessageNotification = function (message) {
/* Shows an HTML5 Notification to indicate that a new chat
* message was received.
*/
if (!_converse.areDesktopNotificationsEnabled()) { if (!_converse.areDesktopNotificationsEnabled()) {
return; return;
} }
...@@ -134,12 +138,12 @@ converse.plugins.add('converse-notification', { ...@@ -134,12 +138,12 @@ converse.plugins.add('converse-notification', {
const full_from_jid = message.getAttribute('from'), const full_from_jid = message.getAttribute('from'),
from_jid = Strophe.getBareJidFromJid(full_from_jid); from_jid = Strophe.getBareJidFromJid(full_from_jid);
if (message.getAttribute('type') === 'headline') { if (message.getAttribute('type') === 'headline') {
if (!_.includes(from_jid, '@') || _converse.allow_non_roster_messaging) { if (!from_jid.includes('@') || _converse.allow_non_roster_messaging) {
title = __("Notification from %1$s", from_jid); title = __("Notification from %1$s", from_jid);
} else { } else {
return; return;
} }
} else if (!_.includes(from_jid, '@')) { } else if (!from_jid.includes('@')) {
// workaround for Prosody which doesn't give type "headline" // workaround for Prosody which doesn't give type "headline"
title = __("Notification from %1$s", from_jid); title = __("Notification from %1$s", from_jid);
} else if (message.getAttribute('type') === 'groupchat') { } else if (message.getAttribute('type') === 'groupchat') {
...@@ -166,7 +170,7 @@ converse.plugins.add('converse-notification', { ...@@ -166,7 +170,7 @@ converse.plugins.add('converse-notification', {
// the message... // the message...
const body = sizzle(`encrypted[xmlns="${Strophe.NS.OMEMO}"]`, message).length ? const body = sizzle(`encrypted[xmlns="${Strophe.NS.OMEMO}"]`, message).length ?
__('OMEMO Message received') : __('OMEMO Message received') :
_.get(message.querySelector('body'), 'textContent'); get(message.querySelector('body'), 'textContent');
if (!body) { if (!body) {
return; return;
} }
...@@ -185,7 +189,7 @@ converse.plugins.add('converse-notification', { ...@@ -185,7 +189,7 @@ converse.plugins.add('converse-notification', {
/* Creates an HTML5 Notification to inform of a change in a /* Creates an HTML5 Notification to inform of a change in a
* contact's chat state. * contact's chat state.
*/ */
if (_.includes(_converse.chatstate_notification_blacklist, contact.jid)) { if (_converse.chatstate_notification_blacklist.includes(contact.jid)) {
// Don't notify if the user is being ignored. // Don't notify if the user is being ignored.
return; return;
} }
...@@ -274,8 +278,7 @@ converse.plugins.add('converse-notification', { ...@@ -274,8 +278,7 @@ converse.plugins.add('converse-notification', {
}; };
_converse.requestPermission = function () { _converse.requestPermission = function () {
if (_converse.supports_html5_notification && if (_converse.supports_html5_notification && !['denied', 'granted'].includes(Notification.permission)) {
! _.includes(['denied', 'granted'], Notification.permission)) {
// Ask user to enable HTML5 notifications // Ask user to enable HTML5 notifications
Notification.requestPermission(); Notification.requestPermission();
} }
...@@ -293,4 +296,3 @@ converse.plugins.add('converse-notification', { ...@@ -293,4 +296,3 @@ converse.plugins.add('converse-notification', {
}); });
} }
}); });
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