From 7065e40913ad2b6c1c0f16d003f6a27735ab0ee0 Mon Sep 17 00:00:00 2001 From: GabbasovDinar Date: Wed, 28 Aug 2024 16:25:29 +0500 Subject: [PATCH] [IMP] web_notify:Allow notify with sound --- web_notify/README.rst | 25 ++++-- web_notify/__manifest__.py | 5 +- web_notify/models/res_users.py | 27 +++++-- web_notify/readme/USAGE.md | 19 ++++- web_notify/static/description/index.html | 42 ++++++---- .../static/src/components/audio_player.esm.js | 78 +++++++++++++++++++ .../static/src/components/audio_player.xml | 10 +++ .../js/services/notification_services.esm.js | 38 +++++---- .../static/src/services/effect_service.esm.js | 43 ++++++++++ .../notification_sound_service.esm.js | 58 ++++++++++++++ web_notify/tests/test_res_users.py | 5 ++ 11 files changed, 303 insertions(+), 47 deletions(-) create mode 100644 web_notify/static/src/components/audio_player.esm.js create mode 100644 web_notify/static/src/components/audio_player.xml create mode 100644 web_notify/static/src/services/effect_service.esm.js create mode 100644 web_notify/static/src/services/notification_sound_service.esm.js diff --git a/web_notify/README.rst b/web_notify/README.rst index 435be4179ac0..ce6d3323f738 100644 --- a/web_notify/README.rst +++ b/web_notify/README.rst @@ -1,7 +1,3 @@ -.. image:: https://odoo-community.org/readme-banner-image - :target: https://odoo-community.org/get-involved?utm_source=readme - :alt: Odoo Community Association - ========== Web Notify ========== @@ -17,7 +13,7 @@ Web Notify .. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png :target: https://odoo-community.org/page/development-status :alt: Production/Stable -.. |badge2| image:: https://img.shields.io/badge/license-LGPL--3-blue.png +.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html :alt: License: LGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github @@ -89,6 +85,25 @@ or self.env.user.notify_default(message='My default message') +You can also add sound to your notifications by using the ``sound`` +parameter. The sound parameter expects a string containing the URL path +to the audio file that should be played when the notification is +displayed. + +.. code:: python + + self.env.user.notify_success(message='My success message', sound='//static/audio/success.mp3') + +or + +.. code:: python + + self.env.user.notify_info(message='My information message', sound='//static/audio/info.mp3') + +The sound parameter can be used with any notification type (success, +danger, warning, info, or default). If the sound parameter is not +provided, the notification will be displayed without any sound. + The notifications can bring interactivity with some buttons. - One allowing to refresh the active view diff --git a/web_notify/__manifest__.py b/web_notify/__manifest__.py index 4e6daac71960..52adab90e772 100644 --- a/web_notify/__manifest__.py +++ b/web_notify/__manifest__.py @@ -8,13 +8,14 @@ Send notification messages to user""", "version": "17.0.1.1.0", "license": "LGPL-3", - "author": "ACSONE SA/NV," "AdaptiveCity," "Odoo Community Association (OCA)", + "author": "ACSONE SA/NV,AdaptiveCity,Odoo Community Association (OCA)", "development_status": "Production/Stable", "website": "https://github.com/OCA/web", "depends": ["web", "bus", "base", "mail"], "assets": { "web.assets_backend": [ - "web_notify/static/src/js/services/*.js", + "web_notify/static/src/**/*.js", + "web_notify/static/src/**/*.xml", ] }, "demo": ["views/res_users_demo.xml"], diff --git a/web_notify/models/res_users.py b/web_notify/models/res_users.py index 3f2af3f69122..c98e13208516 100644 --- a/web_notify/models/res_users.py +++ b/web_notify/models/res_users.py @@ -50,9 +50,12 @@ def notify_success( target=None, action=None, params=None, + sound=None, ): title = title or _("Success") - self._notify_channel(SUCCESS, message, title, sticky, target, action, params) + self._notify_channel( + SUCCESS, message, title, sticky, target, action, params, sound + ) def notify_danger( self, @@ -62,9 +65,12 @@ def notify_danger( target=None, action=None, params=None, + sound=None, ): title = title or _("Danger") - self._notify_channel(DANGER, message, title, sticky, target, action, params) + self._notify_channel( + DANGER, message, title, sticky, target, action, params, sound + ) def notify_warning( self, @@ -74,9 +80,12 @@ def notify_warning( target=None, action=None, params=None, + sound=None, ): title = title or _("Warning") - self._notify_channel(WARNING, message, title, sticky, target, action, params) + self._notify_channel( + WARNING, message, title, sticky, target, action, params, sound + ) def notify_info( self, @@ -86,9 +95,12 @@ def notify_info( target=None, action=None, params=None, + sound=None, ): title = title or _("Information") - self._notify_channel(INFO, message, title, sticky, target, action, params) + self._notify_channel( + INFO, message, title, sticky, target, action, params, sound + ) def notify_default( self, @@ -98,9 +110,12 @@ def notify_default( target=None, action=None, params=None, + sound=None, ): title = title or _("Default") - self._notify_channel(DEFAULT, message, title, sticky, target, action, params) + self._notify_channel( + DEFAULT, message, title, sticky, target, action, params, sound + ) def _notify_channel( self, @@ -111,6 +126,7 @@ def _notify_channel( target=None, action=None, params=None, + sound=None, ): if not (self.env.user._is_admin() or self.env.su) and any( user.id != self.env.uid for user in self @@ -129,6 +145,7 @@ def _notify_channel( "sticky": sticky, "action": action, "params": dict(params or []), + "sound": sound, } notifications = [[partner, "web.notify", [bus_message]] for partner in target] diff --git a/web_notify/readme/USAGE.md b/web_notify/readme/USAGE.md index b6922cbf2c10..04f0f0e52f9f 100644 --- a/web_notify/readme/USAGE.md +++ b/web_notify/readme/USAGE.md @@ -29,6 +29,23 @@ or self.env.user.notify_default(message='My default message') ``` +You can also add sound to your notifications by using the `sound` parameter. +The sound parameter expects a string containing the URL path to the audio file +that should be played when the notification is displayed. + +``` python +self.env.user.notify_success(message='My success message', sound='//static/audio/success.mp3') +``` + +or + +``` python +self.env.user.notify_info(message='My information message', sound='//static/audio/info.mp3') +``` + +The sound parameter can be used with any notification type (success, danger, warning, info, or +default). If the sound parameter is not provided, the notification will be displayed without any sound. + The notifications can bring interactivity with some buttons. - One allowing to refresh the active view @@ -59,4 +76,4 @@ module in a demo database. Access the users form through Settings -\> Users & Companies. You'll see a tab called "Test web notify", here you'll find two buttons that'll allow you test the module. -![](../static/img/test_notifications_demo.png) +![](../static/img/test_notifications_demo.png) \ No newline at end of file diff --git a/web_notify/static/description/index.html b/web_notify/static/description/index.html index d4635f60132c..759dd1a5c608 100644 --- a/web_notify/static/description/index.html +++ b/web_notify/static/description/index.html @@ -3,7 +3,7 @@ -README.rst +Web Notify -
+
+

Web Notify

- - -Odoo Community Association - -
-

Web Notify

-

Production/Stable License: LGPL-3 OCA/web Translate me on Weblate Try me on Runboat

+

Production/Stable License: LGPL-3 OCA/web Translate me on Weblate Try me on Runboat

Send instant notification messages to the user in live.

This technical module allows you to send instant notification messages from the server to the user in live. Two kinds of notification are @@ -401,12 +396,12 @@

Web Notify

-

Installation

+

Installation

This module is based on the Instant Messaging Bus. To work properly, the server must be launched in gevent mode.

-

Usage

+

Usage

To send a notification to the user you just need to call one of the new methods defined on res.users:

@@ -428,6 +423,20 @@ 

Usage

 self.env.user.notify_default(message='My default message')
 
+

You can also add sound to your notifications by using the sound +parameter. The sound parameter expects a string containing the URL path +to the audio file that should be played when the notification is +displayed.

+
+self.env.user.notify_success(message='My success message', sound='/<YOUR_MODULE>/static/audio/success.mp3')
+
+

or

+
+self.env.user.notify_info(message='My information message', sound='/<YOUR_MODULE>/static/audio/info.mp3')
+
+

The sound parameter can be used with any notification type (success, +danger, warning, info, or default). If the sound parameter is not +provided, the notification will be displayed without any sound.

The notifications can bring interactivity with some buttons.

  • One allowing to refresh the active view
  • @@ -456,7 +465,7 @@

    Usage

    image2

-

Bug Tracker

+

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed @@ -464,16 +473,16 @@

Bug Tracker

Do not contact contributors directly about support or help with technical issues.

-

Credits

+

Credits

-

Authors

+

Authors

  • ACSONE SA/NV
  • AdaptiveCity
-

Contributors

+

Contributors

-

Maintainers

+

Maintainers

This module is maintained by the OCA.

Odoo Community Association @@ -502,6 +511,5 @@

Maintainers

-
diff --git a/web_notify/static/src/components/audio_player.esm.js b/web_notify/static/src/components/audio_player.esm.js new file mode 100644 index 000000000000..13117f4bc4c7 --- /dev/null +++ b/web_notify/static/src/components/audio_player.esm.js @@ -0,0 +1,78 @@ +/** @odoo-module alias=web_notify.AudioPlayer **/ + +import {Component, useState} from "@odoo/owl"; + +/** + * @typedef AudioPlayerProps + * @property {string} src URL of the audio file to be played + * @property {number} [volume=1.0] Volume level of the audio (from 0.0 to 1.0) + * @property {boolean} [loop=false] Whether the audio should loop + * @property {Function} [onEnded] Callback function to be called when the audio ends + */ + +/** + * The AudioPlayer component is responsible for playing audio files with + * specified settings like volume and looping. It also provides the ability + * to trigger actions when the audio playback ends. + */ +export class AudioPlayer extends Component { + setup() { + this.state = useState({isPlaying: false}); + this.audioElement = new Audio(this.props.src); + + // Set audio properties + this.audioElement.volume = this.props.volume || 1.0; + this.audioElement.loop = this.props.loop || false; + + // Start playing the audio + this.audioElement + .play() + .then(() => { + this.state.isPlaying = true; + }) + .catch((error) => { + console.error("Audio playback failed:", error); + }); + + // Listen for the end of the audio playback + this.audioElement.addEventListener("ended", this.onAudioEnded.bind(this)); + } + + /** + * Stops the audio playback and triggers the onEnded callback if provided. + */ + stopAudio() { + this.audioElement.pause(); + this.audioElement.currentTime = 0; + this.state.isPlaying = false; + + if (this.props.onEnded) { + this.props.onEnded(); + } + } + + /** + * Handler for when the audio playback ends. + */ + onAudioEnded() { + if (!this.props.loop) { + this.stopAudio(); + } + } + + willUnmount() { + // Clean up the audio element and listeners + this.audioElement.removeEventListener("ended", this.onAudioEnded); + this.audioElement.pause(); + } +} + +AudioPlayer.props = { + src: {type: String}, + volume: {type: Number, optional: true}, + loop: {type: Boolean, optional: true}, + onEnded: {type: Function, optional: true}, + close: {type: Function, optional: true}, +}; + +AudioPlayer.template = "web_notify.AudioPlayer"; diff --git a/web_notify/static/src/components/audio_player.xml b/web_notify/static/src/components/audio_player.xml new file mode 100644 index 000000000000..5db199427dcd --- /dev/null +++ b/web_notify/static/src/components/audio_player.xml @@ -0,0 +1,10 @@ + + + + + + + + + diff --git a/web_notify/static/src/js/services/notification_services.esm.js b/web_notify/static/src/js/services/notification_services.esm.js index c37cc174f450..c24d260fea69 100644 --- a/web_notify/static/src/js/services/notification_services.esm.js +++ b/web_notify/static/src/js/services/notification_services.esm.js @@ -5,12 +5,12 @@ import {browser} from "@web/core/browser/browser"; import {registry} from "@web/core/registry"; export const webNotificationService = { - dependencies: ["bus_service", "notification", "action"], + dependencies: ["bus_service", "action", "notification_sound"], - start(env, {bus_service, notification, action}) { + start(env, {bus_service, action, notification_sound}) { let webNotifTimeouts = {}; /** - * Displays the web notification on user's screen + * Displays the web notification with sound on user's screen * @param {*} notifications */ function displaywebNotification(notifications) { @@ -35,20 +35,24 @@ export const webNotificationService = { }, ]; } - const notificationRemove = notification.add(markup(notif.message), { - title: notif.title, - type: notif.type, - sticky: notif.sticky, - className: notif.className, - buttons: buttons.map((button) => { - const onClick = button.onClick; - button.onClick = async () => { - await onClick(); - notificationRemove(); - }; - return button; - }), - }); + const notificationRemove = notification_sound.add( + markup(notif.message), + { + title: notif.title, + type: notif.type, + sticky: notif.sticky, + className: notif.className, + buttons: buttons.map((button) => { + const onClick = button.onClick; + button.onClick = async () => { + await onClick(); + notificationRemove(); + }; + return button; + }), + sound: notif.sound, + } + ); }); }); } diff --git a/web_notify/static/src/services/effect_service.esm.js b/web_notify/static/src/services/effect_service.esm.js new file mode 100644 index 000000000000..95d9fe5d76e8 --- /dev/null +++ b/web_notify/static/src/services/effect_service.esm.js @@ -0,0 +1,43 @@ +/** @odoo-module **/ + +import {registry} from "@web/core/registry"; +import {AudioPlayer} from "../components/audio_player.esm"; +const effectRegistry = registry.category("effects"); + +// ----------------------------------------------------------------------------- +// Audio effect +// ----------------------------------------------------------------------------- + +/** + * Handles effect of type "audio_effect". It returns the AudioPlayer component + * with the given audio source URL and other properties. + * + * @param {Object} env + * @param {Object} [params={}] + * @param {string} params.src + * The URL of the audio file to play. + * @param {number} [params.volume=1.0] Volume level of the audio (from 0.0 to 1.0) + * @param {boolean} [params.loop=false] Whether the audio should loop + * @param {Function} [params.onEnded] Callback function to be called when the audio ends + */ + +function audioEffect(env, params = {}) { + if (!params.src) { + console.warn( + "Audio effect requires a 'src' parameter with the URL of the audio file." + ); + return; + } + + return { + Component: AudioPlayer, + props: { + src: params.src, + volume: params.volume || 1.0, + loop: params.loop || false, + onEnded: params.onEnded, + }, + }; +} + +effectRegistry.add("audio_effect", audioEffect); diff --git a/web_notify/static/src/services/notification_sound_service.esm.js b/web_notify/static/src/services/notification_sound_service.esm.js new file mode 100644 index 000000000000..46ea7ee2a767 --- /dev/null +++ b/web_notify/static/src/services/notification_sound_service.esm.js @@ -0,0 +1,58 @@ +/** @odoo-module **/ + +import {registry} from "@web/core/registry"; + +/** + * The notificationSoundService is responsible for handling the playback of audio + * notifications when a new notification is added. This service integrates with + * the notification system and the effect service to provide audible feedback + * based on the type of notification. + * + * Dependencies: + * - notification: The service responsible for displaying notifications on the UI. + * - effect: The service that handles visual and auditory effects in the application. + */ + +export const notificationSoundService = { + dependencies: ["notification", "effect"], + + /** + * Starts the notification sound service, enabling sound playback for notifications. + * + * @param {Object} env The environment object, providing access to various services. + * @param {Object} services An object containing the dependencies (notification, effect). + * @returns {Object} The add function, used to add notifications with sound. + */ + start(env, {notification, effect}) { + /** + * Adds a notification with an associated sound effect. + * + * @param {String} message The message to be displayed in the notification. + * @param {Object} [options={}] Additional options for the notification, such as type, sound and etc + * @returns {Function} A function to close the notification. + */ + function add(message, options = {}) { + const sound = options.sound || false; + delete options.sound; // Remove sound option from the options before passing to notification + + const closeFn = notification.add(message, options); + + if (sound) + // Trigger the audio effect. + effect.add({ + type: "audio_effect", + src: sound, + volume: 0.8, + loop: false, + onEnded: () => { + // Placeholder for any action after sound ends + }, + }); + return closeFn; + } + return {add}; + }, +}; + +// Register the notification sound service in the service registry +registry.category("services").add("notification_sound", notificationSoundService); diff --git a/web_notify/tests/test_res_users.py b/web_notify/tests/test_res_users.py index 976bfd96b5a6..e4ca9870cb4e 100644 --- a/web_notify/tests/test_res_users.py +++ b/web_notify/tests/test_res_users.py @@ -20,6 +20,7 @@ def test_notify_success(self): "sticky": True, "action": None, "params": {}, + "sound": "/mail/static/src/audio/ting.mp3", } self.env.user.notify_success(**test_msg) self.env.cr.precommit.run() # trigger the creation of bus.bus records @@ -39,6 +40,7 @@ def test_notify_danger(self): "sticky": True, "action": None, "params": {}, + "sound": "/mail/static/src/audio/ting.mp3", } self.env.user.notify_danger(**test_msg) self.env.cr.precommit.run() @@ -58,6 +60,7 @@ def test_notify_warning(self): "sticky": True, "action": None, "params": {}, + "sound": "/mail/static/src/audio/ting.mp3", } self.env.user.notify_warning(**test_msg) self.env.cr.precommit.run() @@ -77,6 +80,7 @@ def test_notify_info(self): "sticky": True, "action": None, "params": {}, + "sound": "/mail/static/src/audio/ting.mp3", } self.env.user.notify_info(**test_msg) self.env.cr.precommit.run() @@ -96,6 +100,7 @@ def test_notify_default(self): "sticky": True, "action": None, "params": {}, + "sound": "/mail/static/src/audio/ting.mp3", } self.env.user.notify_default(**test_msg) self.env.cr.precommit.run()