Commit a06d2a39 authored by Martin Matějek's avatar Martin Matějek

Message timeout

parent 4ed9bd6d
......@@ -30,6 +30,7 @@ def create_argparser():
parser_action.add_argument("message", help="Notification message")
parser_action.add_argument("-t", "--template", help="Notification type / template", default='simple')
parser_action.add_argument("--persistent", help="Persistent notification (default: false)", action="store_true")
parser_action.add_argument("--timeout", help="Timeout in minutes after which message disappear", type=int)
parser_list = subparsers.add_parser("list", help="List various things")
parser_list.add_argument("target", help="List multiple things o your choice", choices=["all", "plugins", "templates"], nargs="?", default="all")
......@@ -83,6 +84,9 @@ def process_args(parser, args):
'message': args.message,
'persistent': args.persistent,
}
if args.timeout:
opts['timeout'] = args.timeout * 60
api.create(**opts)
elif args.command == 'list':
......
......@@ -17,6 +17,17 @@ class Api:
config.get('settings', 'persistent_dir'),
)
def delete_old_messages_before(func_to_decorate):
"""Decorator for delete_messages"""
# TODO: Rename
def wrapper(self, *args, **kwargs):
self.notifications.delete_messages()
return func_to_decorate(self, *args, **kwargs)
return wrapper
def list_plugins(self):
"""List of plugin names to client code"""
return self.plugins.get_all().keys()
......@@ -25,10 +36,12 @@ class Api:
"""Get actions of specified plugin"""
return self.plugins.get_plugin(plug_name).get_actions()
@delete_old_messages_before
def get_notifications(self):
"""Return all notifications"""
return self.notifications.get_all()
@delete_old_messages_before
def get_notification(self, msgid, media_type, lang):
"""Show notification of one specific by id"""
return self.notifications.get_notification(msgid, media_type, lang)
......
......@@ -10,7 +10,7 @@ from .notificationskeleton import NotificationSkeleton
class Notification:
def __init__(self, notif_id, timestamp, skeleton, fallback=None, persistent=False, **data):
def __init__(self, notif_id, timestamp, skeleton, fallback=None, persistent=False, timeout=None, **data):
self.notif_id = notif_id
self.timestamp = timestamp
......@@ -18,6 +18,7 @@ class Notification:
self.data = data
self.persistent = persistent
self.timeout = timeout
self.fallback = fallback
# TODO: parse opts into metadata
......@@ -55,10 +56,16 @@ class Notification:
def valid(self, timestamp=None):
"""If notification is still valid"""
if not timestamp:
ts = Notification.generate_timestamp()
if self.timeout:
if not timestamp:
timestamp = Notification.generate_timestamp()
# TODO: compare self.timestamp an ts
creat_time = dt.fromtimestamp(self.timestamp)
delta = timestamp - creat_time
return delta.total_seconds() < self.timeout
return True
def render(self, media_type, lang):
"""Return rendered template as given media type and in given language"""
......@@ -88,6 +95,7 @@ class Notification:
'notif_id': self.notif_id,
'timestamp': self.timestamp,
'persistent': self.persistent,
'timeout': self.timeout,
'message': self.content,
'skeleton': self.skeleton.serialize(),
'fallback': self.fallback,
......@@ -118,6 +126,7 @@ class Notification:
out += "\tskeleton: {}\n".format(self.skeleton)
out += "\ttimestamp: {}\n".format(self.timestamp)
out += "\tpersistent: {}\n".format(self.persistent)
out += "\ttimeout: {}\n".format(self.timeout)
out += "\tmessage: {}\n".format(self.content)
out += "}\n"
......
import os
import subprocess
from datetime import datetime as dt
from .logger import logger
from .notification import Notification
......@@ -71,3 +74,30 @@ class NotificationStorage:
# """Render all notifications"""
# for n in self.notifications:
# self.render_one(n)
def delete_messages(self):
"""Delete messages based on their timeout"""
to_delete = []
now = dt.utcnow()
for n in self.notifications.values():
if not n.valid(now):
to_delete.append(n)
for n in to_delete:
self.dismiss(n.notif_id)
logger.debug("Deleting notification '{}' due to timeout".format(n.notif_id))
def dismiss(self, msgid):
"""Dismiss specific notification"""
n = self.notifications[msgid]
del self.notifications[msgid]
if n.persistent:
storage_dir = self.storage_dirs['persistent']
else:
storage_dir = self.storage_dirs['volatile']
filename = os.path.join(storage_dir, "{}.json".format(msgid))
# TODO: delete atomically via mv
subprocess.call(["rm", filename])
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