From 89ab6659235bd64c0282913313638944a436170c Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Sun, 8 Nov 2020 13:16:49 -0800 Subject: [PATCH] Add user new device notification setting for initial stream only --- data/interfaces/default/settings.html | 9 +++++++++ plexpy/config.py | 1 + plexpy/datafactory.py | 14 ++++++++++++-- plexpy/notification_handler.py | 3 ++- plexpy/webserve.py | 2 ++ 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/data/interfaces/default/settings.html b/data/interfaces/default/settings.html index 4a0cdc7a..7b7293b4 100644 --- a/data/interfaces/default/settings.html +++ b/data/interfaces/default/settings.html @@ -997,6 +997,15 @@

The number of concurrent streams by a single user for Tautulli to trigger a notification. Minimum 2.

+
+ +

+ Enable to only send a new device notification the first time a user streams from a new device. + Disable to send a new device notification everytime a user streams from the device until it is recorded in history (i.e. exceeds the ignore interval). +

+
diff --git a/plexpy/config.py b/plexpy/config.py index 4e2c5762..25189527 100644 --- a/plexpy/config.py +++ b/plexpy/config.py @@ -167,6 +167,7 @@ _CONFIG_DEFINITIONS = { 'NOTIFY_REMOTE_ACCESS_THRESHOLD': (int, 'Monitoring', 60), 'NOTIFY_CONCURRENT_BY_IP': (int, 'Monitoring', 0), 'NOTIFY_CONCURRENT_THRESHOLD': (int, 'Monitoring', 2), + 'NOTIFY_NEW_DEVICE_INITIAL_ONLY': (int, 'Monitoring', 1), 'PLEXPY_AUTO_UPDATE': (int, 'General', 0), 'REFRESH_LIBRARIES_INTERVAL': (int, 'Monitoring', 12), 'REFRESH_LIBRARIES_ON_STARTUP': (int, 'Monitoring', 1), diff --git a/plexpy/datafactory.py b/plexpy/datafactory.py index 23dc25dc..ef918bd2 100644 --- a/plexpy/datafactory.py +++ b/plexpy/datafactory.py @@ -1841,12 +1841,22 @@ class DataFactory(object): logger.warn("Tautulli DataFactory :: Unable to execute database query for delete_newsletter_log: %s." % e) return False - def get_user_devices(self, user_id=''): + def get_user_devices(self, user_id='', history_only=True): monitor_db = database.MonitorDatabase() if user_id: + if history_only: + query = 'SELECT machine_id FROM session_history ' \ + 'WHERE user_id = ? ' \ + 'GROUP BY machine_id' + else: + query = 'SELECT * FROM (' \ + 'SELECT user_id, machine_id FROM session_history ' \ + 'UNION SELECT user_id, machine_id from sessions_continued) ' \ + 'WHERE user_id = ? ' \ + 'GROUP BY machine_id' + try: - query = 'SELECT machine_id FROM session_history WHERE user_id = ? GROUP BY machine_id' result = monitor_db.select(query=query, args=[user_id]) except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for get_user_devices: %s." % e) diff --git a/plexpy/notification_handler.py b/plexpy/notification_handler.py index 4ae81d1a..6a846316 100644 --- a/plexpy/notification_handler.py +++ b/plexpy/notification_handler.py @@ -193,7 +193,8 @@ def notify_conditions(notify_action=None, stream_data=None, timeline_data=None): elif notify_action == 'on_newdevice': data_factory = datafactory.DataFactory() - user_devices = data_factory.get_user_devices(user_id=stream_data['user_id']) + user_devices = data_factory.get_user_devices(user_id=stream_data['user_id'], + history_only=not plexpy.CONFIG.NOTIFY_NEW_DEVICE_INITIAL_ONLY) evaluated = stream_data['machine_id'] not in user_devices elif stream_data['media_type'] in ('movie', 'episode', 'clip'): diff --git a/plexpy/webserve.py b/plexpy/webserve.py index 39f02546..2aca3fe9 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -3136,6 +3136,7 @@ class WebInterface(object): "notify_concurrent_by_ip": checked(plexpy.CONFIG.NOTIFY_CONCURRENT_BY_IP), "notify_concurrent_threshold": plexpy.CONFIG.NOTIFY_CONCURRENT_THRESHOLD, "notify_continued_session_threshold": plexpy.CONFIG.NOTIFY_CONTINUED_SESSION_THRESHOLD, + "notify_new_device_initial_only": checked(plexpy.CONFIG.NOTIFY_NEW_DEVICE_INITIAL_ONLY), "home_sections": json.dumps(plexpy.CONFIG.HOME_SECTIONS), "home_stats_cards": json.dumps(plexpy.CONFIG.HOME_STATS_CARDS), "home_library_cards": json.dumps(plexpy.CONFIG.HOME_LIBRARY_CARDS), @@ -3199,6 +3200,7 @@ class WebInterface(object): "refresh_libraries_on_startup", "refresh_users_on_startup", "notify_consecutive", "notify_recently_added_upgrade", "notify_group_recently_added_grandparent", "notify_group_recently_added_parent", + "notify_new_device_initial_only", "monitor_pms_updates", "get_file_sizes", "log_blacklist", "http_hash_password", "allow_guest_access", "cache_images", "http_proxy", "http_basic_auth", "notify_concurrent_by_ip", "history_table_activity", "plexpy_auto_update",