Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring Notification preferences #1655

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions changedetectionio/update_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,39 +66,40 @@ def queue_notification_for_watch(self, n_object, watch):
self.notification_q.put(n_object)

# Prefer - Individual watch settings > Tag settings > Global settings (in that order)
def _check_cascading_vars(self, var_name, watch):
def _resolve_notification_var(self, var_name, watch):

from changedetectionio.notification import (
default_notification_format_for_watch,
default_notification_body,
default_notification_title,
)


# Would be better if this was some kind of Object where Watch can reference the parent datastore etc
v = watch.get(var_name)
if v and not watch.get('notification_muted'):
return v

tags = self.datastore.get_all_tags_for_watch(uuid=watch.get('uuid'))
if tags:
for tag_uuid, tag in tags.items():
v = tag.get(var_name)
if v and not tag.get('notification_muted'):
return v

if self.datastore.data['settings']['application'].get(var_name):
return self.datastore.data['settings']['application'].get(var_name)

# Otherwise could be defaults
if var_name == 'notification_format':
return default_notification_format_for_watch
if var_name == 'notification_body':
return default_notification_body
if var_name == 'notification_title':
return default_notification_title

return None
# Cascades to the first setting (ie, closest to the individual watch settings) that's defined
return next(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return next( hmm I'll have to rtfm about next

(
resolved_var
for resolved_var in [

# Would be better if this was some kind of Object where Watch can reference the parent datastore etc
# Resolve individual settings first if exists;
watch.get(var_name),

# .. then first tag that defines the setting;
*[tag.get(var_name) for tag_uuid, tag in tags.items()],

# .. otherwise, from global settings;
self.datastore.data['settings']['application'].get(var_name)
] if resolved_var

# .. and finally, if not defined anywhere, use static defaults -- or just None
), None
) or {
'notification_format': default_notification_format_for_watch,
'notification_body': default_notification_body,
'notification_title': default_notification_title
}.get(var_name)

def send_content_changed_notification(self, watch_uuid):

Expand All @@ -119,14 +120,14 @@ def send_content_changed_notification(self, watch_uuid):
# Should be a better parent getter in the model object

# Prefer - Individual watch settings > Tag settings > Global settings (in that order)
n_object['notification_urls'] = self._check_cascading_vars('notification_urls', watch)
n_object['notification_title'] = self._check_cascading_vars('notification_title', watch)
n_object['notification_body'] = self._check_cascading_vars('notification_body', watch)
n_object['notification_format'] = self._check_cascading_vars('notification_format', watch)
n_object['notification_urls'] = self._resolve_notification_var('notification_urls', watch)
n_object['notification_title'] = self._resolve_notification_var('notification_title', watch)
n_object['notification_body'] = self._resolve_notification_var('notification_body', watch)
n_object['notification_format'] = self._resolve_notification_var('notification_format', watch)

# (Individual watch) Only prepare to notify if the rules above matched
queued = False
if n_object and n_object.get('notification_urls'):
if n_object and n_object.get('notification_urls') and not self._resolve_notification_var('notification_muted', watch):
queued = True
self.queue_notification_for_watch(n_object, watch)

Expand Down