Commit 22c5210d authored by Martin Matějek's avatar Martin Matějek

WIP; Preparation to message validity for dissmiss builtin

parent 9ba9a083
......@@ -21,9 +21,9 @@ class Api:
config.get('settings', 'persistent_dir'),
)
def delete_old_messages(self):
"""Delete all old messages in storage"""
self.notifications.delete_old_messages()
def delete_invalid_messages(self):
"""Delete all invalid messages in storage"""
self.notifications.delete_invalid_messages()
def get_actions(self, plug_name):
"""Get actions of specified plugin"""
......@@ -31,12 +31,12 @@ class Api:
def get_notifications(self):
"""Return all notifications"""
self.delete_old_messages()
self.delete_invalid_messages()
return self.notifications.get_all()
def get_notification(self, msgid, media_type, lang):
"""Show notification of one specific by id"""
self.delete_old_messages()
self.delete_invalid_messages()
return self.notifications.get_notification(msgid, media_type, lang)
def get_templates(self):
......
......@@ -10,9 +10,9 @@ from .notificationskeleton import NotificationSkeleton
class Notification:
ATTRS = ['notif_id', 'timestamp', 'skeleton', 'persistent', 'timeout', 'severity', 'data', 'fallback']
ATTRS = ['notif_id', 'timestamp', 'skeleton', 'persistent', 'timeout', 'severity', 'data', 'fallback', 'valid']
def __init__(self, notif_id, timestamp, skeleton, data, persistent, timeout, severity, fallback=None):
def __init__(self, notif_id, timestamp, skeleton, data, persistent, timeout, severity, fallback=None, valid=True):
self.notif_id = notif_id
self.timestamp = timestamp
......@@ -23,6 +23,8 @@ class Notification:
self.timeout = timeout
self.severity = severity
self.valid = valid
self.data = data
if not self.fallback:
......@@ -55,8 +57,11 @@ class Notification:
# TODO: proper logging per exception
logger.warning("Failed to deserialize json file: %s", e)
def valid(self, timestamp=None):
"""If notification is still valid"""
def is_valid(self, timestamp=None):
"""If notification is still valid based on multiple conditions"""
if not self.valid:
return False
if self.timeout:
if not timestamp:
timestamp = int(datetime.utcnow().timestamp())
......
......@@ -74,13 +74,13 @@ class NotificationStorage:
# for n in self.notifications:
# self.render_one(n)
def delete_old_messages(self):
def delete_invalid_messages(self):
"""Delete messages based on their timeout"""
to_delete = []
now = datetime.utcnow()
for n in self.notifications.values():
if not n.valid(now):
if not n.is_valid(now):
to_delete.append(n)
for n in to_delete:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment