Add onesignal_id to register device API

This commit is contained in:
JonnyWong16 2020-07-12 14:44:31 -07:00
parent 0272c35047
commit 36324d10dc
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
6 changed files with 38 additions and 21 deletions

5
API.md
View file

@ -2674,11 +2674,12 @@ Registers the Tautulli Android App for notifications.
```
Required parameters:
device_name (str): The device name of the Tautulli Android App
device_id (str): The OneSignal device id of the Tautulli Android App
device_id (str): The unique device identifier for the mobile device
device_name (str): The device name of the mobile device
Optional parameters:
friendly_name (str): A friendly name to identify the mobile device
onesignal_id (str): The OneSignal id for the mobile device
Returns:
None

View file

@ -33,7 +33,7 @@
<label for="friendly_name">OneSignal Device ID</label>
<div class="row">
<div class="col-md-8">
<input type="text" class="form-control" id="device_id" value="${device['device_id']}" size="30" readonly>
<input type="text" class="form-control" id="onesignal_id" value="${device['onesignal_id'] or ''}" size="30" readonly>
</div>
</div>
<p class="help-block">Your OneSignal device ID for notifications.</p>

View file

@ -743,7 +743,7 @@ def dbcheck():
c_db.execute(
'CREATE TABLE IF NOT EXISTS mobile_devices (id INTEGER PRIMARY KEY AUTOINCREMENT, '
'device_id TEXT NOT NULL UNIQUE, device_token TEXT, device_name TEXT, friendly_name TEXT, '
'last_seen INTEGER, official INTEGER DEFAULT 0)'
'onesignal_id TEXT, last_seen INTEGER, official INTEGER DEFAULT 0)'
)
# tvmaze_lookup table :: This table keeps record of the TVmaze lookups
@ -2012,6 +2012,15 @@ def dbcheck():
c_db.execute('UPDATE mobile_devices SET official = ? WHERE device_id = ?',
[mobile_app.validate_device_id(device_id), device_id])
# Upgrade mobile_devices table from earlier versions
try:
c_db.execute('SELECT onesignal_id FROM mobile_devices')
except sqlite3.OperationalError:
logger.debug("Altering database. Updating database table mobile_devices.")
c_db.execute(
'ALTER TABLE mobile_devices ADD COLUMN onesignal_id TEXT'
)
# Upgrade notifiers table from earlier versions
try:
c_db.execute('SELECT custom_conditions FROM notifiers')

View file

@ -391,16 +391,17 @@ class API2(object):
return data
def register_device(self, device_id='', device_name='', friendly_name='', **kwargs):
def register_device(self, device_id='', device_name='', friendly_name='', onesignal_id='', **kwargs):
""" Registers the Tautulli Android App for notifications.
```
Required parameters:
device_name (str): The device name of the Tautulli Android App
device_id (str): The OneSignal device id of the Tautulli Android App
device_id (str): The unique device identifier for the mobile device
device_name (str): The device name of the mobile device
Optional parameters:
friendly_name (str): A friendly name to identify the mobile device
onesignal_id (str): The OneSignal id for the mobile device
Returns:
None
@ -419,7 +420,8 @@ class API2(object):
result = mobile_app.add_mobile_device(device_id=device_id,
device_name=device_name,
device_token=self._api_apikey,
friendly_name=friendly_name)
friendly_name=friendly_name,
onesignal_id=onesignal_id)
if result:
self._api_msg = 'Device registration successful.'

View file

@ -87,13 +87,14 @@ def get_mobile_device_by_token(device_token=None):
return get_mobile_devices(device_token=device_token)
def add_mobile_device(device_id=None, device_name=None, device_token=None, friendly_name=None):
def add_mobile_device(device_id=None, device_name=None, device_token=None, friendly_name=None, onesignal_id=None):
db = database.MonitorDatabase()
keys = {'device_id': device_id}
values = {'device_name': device_name,
'device_token': device_token,
'official': validate_device_id(device_id=device_id)}
'onesignal_id': onesignal_id,
'official': validate_onesignal_id(onesignal_id=onesignal_id)}
if friendly_name:
values['friendly_name'] = friendly_name
@ -172,11 +173,14 @@ def set_last_seen(device_token=None):
return
def validate_device_id(device_id):
def validate_onesignal_id(onesignal_id):
if onesignal_id is None:
return False
headers = {'Content-Type': 'application/json'}
payload = {'app_id': _ONESIGNAL_APP_ID}
r = requests.get('https://onesignal.com/api/v1/players/{}'.format(device_id), headers=headers, json=payload)
r = requests.get('https://onesignal.com/api/v1/players/{}'.format(onesignal_id), headers=headers, json=payload)
return r.status_code == 200

View file

@ -929,7 +929,7 @@ class ANDROIDAPP(Notifier):
#logger.debug("Salt (base64): {}".format(base64.b64encode(salt)))
payload = {'app_id': mobile_app._ONESIGNAL_APP_ID,
'include_player_ids': [self.config['device_id']],
'include_player_ids': [device['onesignal_id']],
'contents': {'en': 'Tautulli Notification'},
'data': {'encrypted': True,
'cipher_text': base64.b64encode(encrypted_data),
@ -942,7 +942,7 @@ class ANDROIDAPP(Notifier):
"Install the library to encrypt the notifications.")
payload = {'app_id': mobile_app._ONESIGNAL_APP_ID,
'include_player_ids': [self.config['device_id']],
'include_player_ids': [device['onesignal_id']],
'contents': {'en': 'Tautulli Notification'},
'data': {'encrypted': False,
'plain_text': plaintext_data}
@ -958,7 +958,8 @@ class ANDROIDAPP(Notifier):
db = database.MonitorDatabase()
try:
query = 'SELECT * FROM mobile_devices WHERE official = 1'
query = 'SELECT * FROM mobile_devices WHERE official = 1 ' \
'AND onesignal_id IS NOT NULL AND onesignal_id != ""'
result = db.select(query=query)
except Exception as e:
logger.warn("Tautulli Notifiers :: Unable to retrieve Android app devices list: %s." % e)
@ -983,9 +984,9 @@ class ANDROIDAPP(Notifier):
'The content of your notifications will be sent unencrypted!</strong><br>'
'Please install the library to encrypt the notification contents. '
'Instructions can be found in the '
'<a href="https://github.com/%s/%s-Wiki/wiki/'
'Frequently-Asked-Questions#notifications-pycryptodome'
'" target="_blank">FAQ</a>.' % (plexpy.CONFIG.GIT_USER, plexpy.CONFIG.GIT_REPO),
'<a href="' + helpers.anon_url(
'https://github.com/%s/%s-Wiki/wiki/Frequently-Asked-Questions#notifications-pycryptodome'
% (plexpy.CONFIG.GIT_USER, plexpy.CONFIG.GIT_REPO)) + '" target="_blank">FAQ</a>.' ,
'input_type': 'help'
})
else:
@ -998,7 +999,7 @@ class ANDROIDAPP(Notifier):
config_option[-1]['description'] += '<br><br>Notifications are sent using the ' \
'<a href="' + helpers.anon_url('https://onesignal.com') + '" target="_blank">' \
'OneSignal</a> API. Some user data is collected and cannot be encrypted. ' \
'OneSignal</a>. Some user data is collected and cannot be encrypted. ' \
'Please read the <a href="' + helpers.anon_url(
'https://onesignal.com/privacy_policy') + '" target="_blank">' \
'OneSignal Privacy Policy</a> for more details.'
@ -1008,7 +1009,7 @@ class ANDROIDAPP(Notifier):
if not devices:
config_option.append({
'label': 'Device',
'description': 'No devices registered. '
'description': 'No mobile devices registered with OneSignal. '
'<a data-tab-destination="android_app" data-toggle="tab" data-dismiss="modal">'
'Get the Android App</a> and register a device.',
'input_type': 'help'
@ -1018,7 +1019,7 @@ class ANDROIDAPP(Notifier):
'label': 'Device',
'value': self.config['device_id'],
'name': 'androidapp_device_id',
'description': 'Set your Android app device or '
'description': 'Set your mobile device or '
'<a data-tab-destination="android_app" data-toggle="tab" data-dismiss="modal">'
'register a new device</a> with Tautulli.',
'input_type': 'select',