From b30a91a96cc84321068df0f96afa99b5f6de8928 Mon Sep 17 00:00:00 2001 From: Simon THOBY Date: Sun, 16 Sep 2018 13:32:12 +0200 Subject: [PATCH] URL: Do not send "OLD!" messages whenever the link is quite fresh --- url/__init__.py | 23 ++++++++++++++--------- url/model.py | 2 ++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/url/__init__.py b/url/__init__.py index acab3d2..49773fa 100644 --- a/url/__init__.py +++ b/url/__init__.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import re +from datetime import timedelta, datetime import sqlalchemy.exc from pipobot.lib.known_users import KnownUser @@ -50,16 +51,20 @@ def check_repost(self, sender, urls): if not any(i in url for i in self.repost_ignore): res = self.bot.session.query(RepostUrl).filter(RepostUrl.url == url).first() if res: - send.append('OLD! ') - first = KnownUser.get_antihl(res.jid, self.bot) - first_date = 'le ' + res.date.strftime('%x') + ' à ' + res.date.strftime('%X') - first_date = first_date.decode("utf-8") - if res.count == 1: - send.append(u'Ce lien a déjà été posté %s par %s sur %s…' % (first_date, first, res.chan)) - else: - ret = u'Ce lien a déjà été posté %s fois depuis que %s l’a découvert, %s, sur %s…' - send.append(ret % (res.count, first, first_date, res.chan)) + # Do not send a message if the link was shared less than a few minutes ago + if (datetime.now() - res.last_date) > timedelta(minutes=5): + send.append('OLD! ') + first = KnownUser.get_antihl(res.jid, self.bot) + first_date = 'le ' + res.date.strftime('%x') + ' à ' + res.date.strftime('%X') + first_date = first_date.decode("utf-8") + if res.count == 1: + send.append(u'Ce lien a déjà été posté %s par %s sur %s…' % (first_date, first, first.chan)) + else: + ret = u'Ce lien a déjà été posté %s fois depuis que %s l’a découvert, %s, sur %s…' + send.append(ret % (res.count, first, first_date, res.chan)) res.count += 1 + # Update the time someone posted the link + res.last_date = datetime.now() else: u = RepostUrl(url, self.bot.occupants.pseudo_to_jid(sender), diff --git a/url/model.py b/url/model.py index e4a5f48..66883d8 100644 --- a/url/model.py +++ b/url/model.py @@ -10,6 +10,7 @@ class RepostUrl(Base): url = Column(String(250), primary_key=True) count = Column(Integer) date = Column(DateTime) + last_date = Column(DateTime) jid = Column(String(250)) chan = Column(String(250)) @@ -18,4 +19,5 @@ def __init__(self, url, jid, chan): self.jid = jid self.count = 1 self.date = datetime.datetime.now() + self.last_date = self.date self.chan = chan