Purge old announcements.
authorCMDR furrycat <elite@furrycat.net>
Wed, 12 Oct 2016 18:04:06 +0000 (19:04 +0100)
committerCMDR furrycat <elite@furrycat.net>
Wed, 12 Oct 2016 18:04:06 +0000 (19:04 +0100)
bot.py

diff --git a/bot.py b/bot.py
index 9c93a87..c8d1960 100755 (executable)
--- a/bot.py
+++ b/bot.py
@@ -1362,6 +1362,34 @@ def do_announcements():
     # Convert to list because we will be sharing the cursor.
     for announcement in list(db.get_announcements(client)):
       yield from announce(announcement)
+
+    # Delete old announcements.
+    now = int(time.time())
+    for announcement in db.get_all_announcements(client):
+      if announcement['asap'] == 'true':
+        # Don't delete announcements that were requested ASAP.
+        continue
+      if not announcement['last_spoke']:
+        # Don't delete announcements that haven't played yet.
+        continue
+      if now - announcement['last_spoke'] < max(announcement['interval'], 86400):
+        # Don't delete announcements that aren't old.
+        continue
+      if announcement['interval']:
+        if not announcement['end_date']:
+          # Don't delete ongoing announcements.
+          continue
+        if announcement['end_date'] > now:
+          # Don't delete announcements that still have time to run.
+          continue
+      if dryrun:
+        log.info("Would delete old announcement {}".format(announcement['id']))
+      else:
+        if db.delete_announcement(client, announcement['id']):
+          log.info('Deleted old announcement {}'.format(announcement['id']))
+        else:
+          log.info('Failed to delete old announcement {}'.format(announcement['id']))
+
     yield from asyncio.sleep(waittime)
 
 @asyncio.coroutine