Use mentions in feeds.
authorCMDR furrycat <elite@furrycat.net>
Mon, 5 Dec 2016 18:03:25 +0000 (18:03 +0000)
committerCMDR furrycat <elite@furrycat.net>
Mon, 5 Dec 2016 18:03:25 +0000 (18:03 +0000)
plugin/feeds/feeds.py

index 2d1d429..92e4956 100644 (file)
@@ -26,7 +26,7 @@ class Feeds(DBConnection):
 
   def create_tables(self):
     self.open_db()
-    cursor = self.query('create table if not exists feeds (id char(36) not null, bot_id varchar(32) not null, server_id varchar(32) not null, channel_id varchar(32) not null, description varchar(64), url varchar(128) not null, date boolean not null default true, summary boolean not null default false, show_link boolean not null default true, link_json varchar(256), permissions boolean not null default true, interval int, enabled boolean not null default true, last_spoke datetime)')
+    cursor = self.query('create table if not exists feeds (id char(36) not null, bot_id varchar(32) not null, server_id varchar(32) not null, channel_id varchar(32) not null, description varchar(64), mention varchar(32), url varchar(128) not null, date boolean not null default true, summary boolean not null default false, show_link boolean not null default true, link_json varchar(256), permissions boolean not null default true, interval int, enabled boolean not null default true, last_spoke datetime)')
     cursor = self.query('create unique index if not exists feed_id on feeds (id)')
     cursor = self.query('create index if not exists feed_bot_id on feeds (bot_id)')
     self.dbh.commit()
@@ -36,7 +36,7 @@ class Feeds(DBConnection):
 
   def get_feeds(self, client):
     now = self.now()
-    cursor = self.query("select id, server_id, channel_id, url, date, summary, show_link, link_json, permissions, interval, enabled, last_spoke from feeds where bot_id=? and interval is null or (last_spoke is null or last_spoke < ? - interval)", [client.user.id, now])
+    cursor = self.query("select id, server_id, channel_id, mention, url, date, summary, show_link, link_json, permissions, interval, enabled, last_spoke from feeds where bot_id=? and interval is null or (last_spoke is null or last_spoke < ? - interval)", [client.user.id, now])
     for row in cursor.fetchall():
       yield dict(row)
     self.close_db()
@@ -59,6 +59,7 @@ class Feeds(DBConnection):
       'channel_id': 'in',
       'description': 'title',
       'interval': 'every',
+      'mention': 'tell',
       'show_link': 'link'
     }
     if k in d:
@@ -132,9 +133,6 @@ class Feeds(DBConnection):
         if last_spoke < now:
           update['last_spoke'] = last_spoke
           log.info('Forcing last_spoke for feed {} to {} in line with schedule'.format(feed['id'], bot.iso8601(update['last_spoke'])))
-        dedupe = False
-      else:
-        dedupe = True
 
       url = feed['url']
       d = feedparser.parse(url)
@@ -144,7 +142,7 @@ class Feeds(DBConnection):
         else:
           order = now
           now += 0.001
-        all_entries.append({ 'order': order, 'dedupe': dedupe, 'date': date, 'summary': summary, 'show_link': show_link, 'url': feed['url'], 'link_json': feed['link_json'], 'entry': entry })
+        all_entries.append({ 'mention': feed['mention'], 'order': order, 'date': date, 'summary': summary, 'show_link': show_link, 'url': feed['url'], 'link_json': feed['link_json'], 'entry': entry })
     if not len(all_entries):
       log.debug('No messages to post to {}'.format(channel.name))
     else:
@@ -181,14 +179,20 @@ class Feeds(DBConnection):
           log.warning('No link for entry {}'.format(entry['title']))
           continue
         formatted += '**{}**{}'.format(entry['title'], '\n{}'.format(link) if feed['show_link'] else '')
+        if feed['mention']:
+          if feed['mention'] in ['everyone', 'here']:
+            formatted += '\n@{}'.format(feed['mention'])
+          elif feed['mention'][0] == '#':
+            formatted += '\n<{}>'.format(feed['mention'])
+          else:
+            formatted += '\n<@{}>'.format(feed['mention'])
         if feed['summary']:
           formatted += '\n{}'.format(''.join(bs4.BeautifulSoup(entry['summary'], 'html.parser').findAll(text = True)))
         formatted = formatted.strip()
         digest = bot.digest(formatted)
         if digest in digests:
-          if feed['dedupe']:
-            log.debug("Already posted RSS with digest {}".format(digest))
-            continue
+          log.debug("Already posted RSS with digest {}".format(digest))
+          continue
         try:
           if bot.get('dryrun'):
             log.info('Dryrun: Not posting to {}: {}'.format(channel, formatted))
@@ -343,6 +347,12 @@ class Feeds(DBConnection):
     text = 'feed create'
     text += ' title "{}"'.format(feed['description'])
     text += ' url {}'.format(feed['url'])
+    if feed['mention']:
+      if message.channel.is_private and feed['mention'][0] == '&':
+        # Can't mention a role in a private channel.
+        text += ' tell @{}'.format(feed['mention'])
+      else:
+        text += ' tell <@{}>'.format(feed['mention'])
     text += ' in <#{}>'.format(feed['channel_id'])
     if feed['interval']:
       text += ' every {}'.format(bot.unparse_seconds(int(feed['interval'])))
@@ -406,6 +416,17 @@ class Feeds(DBConnection):
         parsed['interval'] = 0
         i -= 1
         ok = True
+      elif arg == 'tell':
+        if editing and param == 'none':
+          parsed['mention'] = None
+          ok = True
+        elif param:
+          mention = bot.parse_mention(param, message.author)
+          if mention:
+            parsed['mention'] = mention
+            ok = True
+          else:
+            break
       elif arg == 'in':
         k = 'channel_id'
         if param:
@@ -610,6 +631,9 @@ class Feeds(DBConnection):
         '```',
         'Equivalent to `interval 0` and means no reposting of a feed.',
         '',
+        '```tell MENTION```',
+        'Mention `@user`, `@role` or `#channel` in the post.',
+        '',
         '```date false```',
         "Use this if the dates returned by the RSS URL aren't valid.  For instance the official GalNet feed always returns the date its cache was updated NOT the date the story was posted.",
         '',