Re-enable browser notifications

This commit is contained in:
JonnyWong16 2018-01-17 17:01:44 -08:00
parent 32b43202c2
commit 3e51310511
6 changed files with 54 additions and 44 deletions

View file

@ -2,6 +2,7 @@
import plexpy import plexpy
from plexpy import version from plexpy import version
from plexpy.helpers import anon_url from plexpy.helpers import anon_url
from plexpy.notifiers import BROWSER_NOTIFIERS
%> %>
<!doctype html> <!doctype html>
@ -283,6 +284,9 @@ ${next.modalIncludes()}
<script src="${http_root}js/pnotify.custom.min.js"></script> <script src="${http_root}js/pnotify.custom.min.js"></script>
<script src="${http_root}js/script.js${cache_param}"></script> <script src="${http_root}js/script.js${cache_param}"></script>
<script src="${http_root}js/jquery.qrcode.min.js"></script> <script src="${http_root}js/jquery.qrcode.min.js"></script>
% if _session['user_group'] == 'admin' and BROWSER_NOTIFIERS:
<script src="${http_root}js/ajaxNotifications.js"></script>
% endif
<script> <script>
% if _session['user_group'] == 'admin': % if _session['user_group'] == 'admin':
$('#updateDismiss').click(function() { $('#updateDismiss').click(function() {

View file

@ -2,7 +2,7 @@
PNotify.prototype.options.addclass = "stack-bottomright"; PNotify.prototype.options.addclass = "stack-bottomright";
PNotify.prototype.options.buttons.closer_hover = false; PNotify.prototype.options.buttons.closer_hover = false;
PNotify.prototype.options.desktop = { desktop: true, icon: 'images/logo.png' } PNotify.prototype.options.desktop = { desktop: true, icon: 'images/logo-circle.png' };
PNotify.prototype.options.history = false; PNotify.prototype.options.history = false;
PNotify.prototype.options.shadow = false; PNotify.prototype.options.shadow = false;
PNotify.prototype.options.stack = { dir1: 'up', dir2: 'left', firstpos1: 25, firstpos2: 25 }; PNotify.prototype.options.stack = { dir1: 'up', dir2: 'left', firstpos1: 25, firstpos2: 25 };
@ -21,7 +21,7 @@ function check_notifications() {
$.getJSON('get_browser_notifications', function (data) { $.getJSON('get_browser_notifications', function (data) {
if (data) { if (data) {
$.each(data, function (i, notification) { $.each(data, function (i, notification) {
if (notification.delay == 0) { if (notification.delay === 0) {
PNotify.prototype.options.hide = false; PNotify.prototype.options.hide = false;
} else { } else {
PNotify.prototype.options.hide = true; PNotify.prototype.options.hide = true;
@ -34,7 +34,7 @@ function check_notifications() {
setTimeout(function () { setTimeout(function () {
"use strict"; "use strict";
check_notifications(); check_notifications();
}, 3000); }, 5000);
} }
$(document).ready(function () { $(document).ready(function () {

View file

@ -333,7 +333,7 @@
$('#notifier-config-modal').unbind('hidden.bs.modal'); $('#notifier-config-modal').unbind('hidden.bs.modal');
// Need this for setting conditions since conditions contain the character " // Need this for setting conditions since conditions contain the character "
$('#custom_conditions').val('${notifier['custom_conditions'] | n}') $('#custom_conditions').val('${notifier["custom_conditions"] | n}');
$('#condition-widget').filterer({ $('#condition-widget').filterer({
parameters: ${parameters | n}, parameters: ${parameters | n},
@ -341,7 +341,7 @@
updateConditions: function(newConditions){ updateConditions: function(newConditions){
$('#custom_conditions').val(JSON.stringify(newConditions)); $('#custom_conditions').val(JSON.stringify(newConditions));
} }
}) });
function reloadModal() { function reloadModal() {
$.ajax({ $.ajax({
@ -359,7 +359,7 @@
if (jqXHR) { if (jqXHR) {
var result = $.parseJSON(jqXHR.responseText); var result = $.parseJSON(jqXHR.responseText);
var msg = result.message; var msg = result.message;
if (result.result == 'success') { if (result.result === 'success') {
showMsg('<i class="fa fa-check"></i> ' + msg, false, true, 5000) showMsg('<i class="fa fa-check"></i> ' + msg, false, true, 5000)
} else { } else {
showMsg('<i class="fa fa-times"></i> ' + msg, false, true, 5000, true) showMsg('<i class="fa fa-times"></i> ' + msg, false, true, 5000, true)
@ -419,7 +419,7 @@
% if notifier['agent_name'] == 'facebook': % if notifier['agent_name'] == 'facebook':
function disableFacebookRequest() { function disableFacebookRequest() {
if ($('#facebook_app_id').val() != '' && $('#facebook_app_secret').val() != '') { $('#facebook_facebookStep1').prop('disabled', false); } if ($('#facebook_app_id').val() !== '' && $('#facebook_app_secret').val() !== '') { $('#facebook_facebookStep1').prop('disabled', false); }
else { $('#facebook_facebookStep1').prop('disabled', true); } else { $('#facebook_facebookStep1').prop('disabled', true); }
} }
disableFacebookRequest(); disableFacebookRequest();
@ -433,19 +433,20 @@
$('#facebook_redirect_uri').val($('#facebook_redirect_uri').val().slice(0, -1)); $('#facebook_redirect_uri').val($('#facebook_redirect_uri').val().slice(0, -1));
} }
var facebook_token;
$.ajax({ $.ajax({
url: 'facebookStep1', url: 'facebookStep1',
data: { data: {
app_id: $('#facebook_app_id').val(), app_id: $('#facebook_app_id').val(),
app_secret: $('#facebook_app_secret').val(), app_secret: $('#facebook_app_secret').val(),
redirect_uri: $('#facebook_redirect_uri').val(), redirect_uri: $('#facebook_redirect_uri').val()
}, },
cache: false, cache: false,
async: true, async: true,
complete: function (xhr, status) { complete: function (xhr, status) {
var result = $.parseJSON(xhr.responseText); var result = $.parseJSON(xhr.responseText);
var msg = result.msg; var msg = result.msg;
if (result.result == 'success') { if (result.result === 'success') {
showMsg('<i class="fa fa-check"></i> ' + msg, false, true, 5000); showMsg('<i class="fa fa-check"></i> ' + msg, false, true, 5000);
window.open(result.url); window.open(result.url);
@ -484,18 +485,18 @@
$('#notifier-config-modal').on('hidden.bs.modal', function () { $('#notifier-config-modal').on('hidden.bs.modal', function () {
facebook_token = false; facebook_token = false;
}) });
% elif notifier['agent_name'] == 'browser': % elif notifier['agent_name'] == 'browser':
$('#browser_allow_browser').click(function () { $('#browser_allow_browser').click(function () {
PNotify.desktop.permission(); PNotify.desktop.permission();
}) });
% elif notifier['agent_name'] == 'osx': % elif notifier['agent_name'] == 'osx':
$('#osxnotifyregister').click(function () { $('#osxnotifyregister').click(function () {
var osx_notify_app = $('#osx_notify_app').val(); var osx_notify_app = $('#osx_notify_app').val();
$.get('osxnotifyregister', { 'app': osx_notify_app }, function (data) { showMsg('<i class="fa fa-check"></i> ' + data, false, true, 3000); }); $.get('osxnotifyregister', { 'app': osx_notify_app }, function (data) { showMsg('<i class="fa fa-check"></i> ' + data, false, true, 3000); });
}) });
% elif notifier['agent_name'] == 'email': % elif notifier['agent_name'] == 'email':
var REGEX_EMAIL = '([a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@' + var REGEX_EMAIL = '([a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@' +
@ -578,7 +579,6 @@
create: true create: true
}); });
var join_device_names = $join_device_names[0].selectize; var join_device_names = $join_device_names[0].selectize;
console.log(${json.dumps(next((c['value'] for c in notifier['config_options'] if c['name'] == 'join_device_names'), [])) | n});
join_device_names.setValue(${json.dumps(next((c['value'] for c in notifier['config_options'] if c['name'] == 'join_device_names'), [])) | n}); join_device_names.setValue(${json.dumps(next((c['value'] for c in notifier['config_options'] if c['name'] == 'join_device_names'), [])) | n});
% endif % endif
@ -710,7 +710,7 @@
}); });
function sendTestNotification() { function sendTestNotification() {
if ('${notifier["agent_name"]}' != 'browser') { if ('${notifier["agent_name"]}' !== 'browser') {
$.ajax({ $.ajax({
url: 'send_notification', url: 'send_notification',
data: { data: {
@ -734,7 +734,7 @@
} }
}); });
} else { } else {
if ($('#browser_auto_hide_delay').val() == "0") { if ($('#browser_auto_hide_delay').val() === "0") {
PNotify.prototype.options.hide = false; PNotify.prototype.options.hide = false;
} else { } else {
PNotify.prototype.options.hide = true; PNotify.prototype.options.hide = true;

View file

@ -410,6 +410,7 @@ def start():
# Start background notification thread # Start background notification thread
notification_handler.start_threads(num_threads=CONFIG.NOTIFICATION_THREADS) notification_handler.start_threads(num_threads=CONFIG.NOTIFICATION_THREADS)
notifiers.check_browser_enabled()
_STARTED = True _STARTED = True

View file

@ -64,6 +64,9 @@ import users
from plexpy.config import _BLACKLIST_KEYS, _WHITELIST_KEYS from plexpy.config import _BLACKLIST_KEYS, _WHITELIST_KEYS
BROWSER_NOTIFIERS = {}
AGENT_IDS = {'growl': 0, AGENT_IDS = {'growl': 0,
'prowl': 1, 'prowl': 1,
'xbmc': 2, 'xbmc': 2,
@ -551,6 +554,10 @@ def set_notifier_config(notifier_id=None, agent_id=None, **kwargs):
db.upsert(table_name='notifiers', key_dict=keys, value_dict=values) db.upsert(table_name='notifiers', key_dict=keys, value_dict=values)
logger.info(u"Tautulli Notifiers :: Updated notification agent: %s (notifier_id %s)." % (agent['label'], notifier_id)) logger.info(u"Tautulli Notifiers :: Updated notification agent: %s (notifier_id %s)." % (agent['label'], notifier_id))
blacklist_logger() blacklist_logger()
if agent['name'] == 'browser':
check_browser_enabled()
return True return True
except Exception as e: except Exception as e:
logger.warn(u"Tautulli Notifiers :: Unable to update notification agent: %s." % e) logger.warn(u"Tautulli Notifiers :: Unable to update notification agent: %s." % e)
@ -994,40 +1001,15 @@ class BROWSER(Notifier):
Browser notifications Browser notifications
""" """
NAME = 'Browser' NAME = 'Browser'
_DEFAULT_CONFIG = {'enabled': 0, _DEFAULT_CONFIG = {'auto_hide_delay': 5
'auto_hide_delay': 5
} }
def agent_notify(self, subject='', body='', action='', **kwargs): def agent_notify(self, subject='', body='', action='', **kwargs):
logger.info(u"Tautulli Notifiers :: {name} notification sent.".format(name=self.NAME)) logger.info(u"Tautulli Notifiers :: {name} notification sent.".format(name=self.NAME))
return True return True
def get_notifications(self):
if not self.config['enabled']:
return
db = database.MonitorDatabase()
result = db.select('SELECT subject_text, body_text FROM notify_log '
'WHERE agent_id = 17 AND timestamp >= ? ',
args=[time.time() - 3])
notifications = []
for item in result:
notification = {'subject_text': item['subject_text'],
'body_text': item['body_text'],
'delay': self.config['auto_hide_delay']}
notifications.append(notification)
return {'notifications': notifications}
def return_config_options(self): def return_config_options(self):
config_option = [{'label': 'Enable Browser Notifications', config_option = [{'label': 'Allow Notifications',
'value': self.config['enabled'],
'name': 'browser_enabled',
'description': 'Enable to display desktop notifications from your browser.',
'input_type': 'checkbox'
},
{'label': 'Allow Notifications',
'value': 'Allow Notifications', 'value': 'Allow Notifications',
'name': 'browser_allow_browser', 'name': 'browser_allow_browser',
'description': 'Click to allow browser notifications. You must click this button for each browser.', 'description': 'Click to allow browser notifications. You must click this button for each browser.',
@ -3544,3 +3526,27 @@ def upgrade_config_to_db():
notifier_id = add_notifier_config(agent_id=agent_id) notifier_id = add_notifier_config(agent_id=agent_id)
set_notifier_config(notifier_id=notifier_id, agent_id=agent_id, **notifier_config) set_notifier_config(notifier_id=notifier_id, agent_id=agent_id, **notifier_config)
def check_browser_enabled():
global BROWSER_NOTIFIERS
BROWSER_NOTIFIERS = {}
for n in get_notifiers():
if n['agent_id'] == 17 and n['active']:
notifier_config = get_notifier_config(n['id'])
BROWSER_NOTIFIERS[n['id']] = notifier_config['config']['auto_hide_delay']
def get_browser_notifications():
db = database.MonitorDatabase()
result = db.select('SELECT notifier_id, subject_text, body_text FROM notify_log '
'WHERE agent_id = 17 AND timestamp >= ? ',
args=[time.time() - 5])
notifications = []
for item in result:
notification = {'subject_text': item['subject_text'],
'body_text': item['body_text'],
'delay': BROWSER_NOTIFIERS.get(item['notifier_id'], 5)}
notifications.append(notification)
return {'notifications': notifications}

View file

@ -3126,8 +3126,7 @@ class WebInterface(object):
@cherrypy.tools.json_out() @cherrypy.tools.json_out()
@requireAuth(member_of("admin")) @requireAuth(member_of("admin"))
def get_browser_notifications(self, **kwargs): def get_browser_notifications(self, **kwargs):
browser = notifiers.BROWSER() result = notifiers.get_browser_notifications()
result = browser.get_notifications()
if result: if result:
notifications = result['notifications'] notifications = result['notifications']