diff --git a/messages.py b/messages.py index bda9490e29bafafe20f8737414bd79bd53d7ae0a..35866cf987ad2e01126bccbebbeb45764231c177 100644 --- a/messages.py +++ b/messages.py @@ -9,7 +9,7 @@ messages = { """Bonjour, je suis le bot de la Quadrature du Net, vous pouvez me demander de l'aide si besoin. (wantzel help)""", "help": - """Mes commandes sont : ~help ~rp(cpa) ~status ~kill ~stats et ~admin. + """Mes commandes sont : ~help ~rp(cpa) ~status ~flag ~kill ~stats et ~admin. Pour plus d'informations, voir ici: https://wiki.laquadrature.net/Wantzel Pour obtenir de l'aide sur une commande en particulier, il suffit de taper ~help """, @@ -25,6 +25,10 @@ messages = { """Cette commande sert à retrouver les informations concernant un article ajouté à la Revue de Presse (https://wiki.laquadrature.net/Revue_de_presse) L'utilisation se fait sous la forme: ~status """, + "help_flag": + """Cette commande sert à modifier les flags d'un article ajouté à la Revue de Presse (https://wiki.laquadrature.net/Revue_de_presse) + L'utilisation se fait sous la forme: ~flag . La liste des flags est accessible sur https://wiki.laquadrature.net/Wantzel""", + "help_stats": """Cette commande permet de fournir quelques statistiques sur la Revue de Presse (https://wiki.laquadrature.net/Revue_de_presse) Les statistiques sont calculées sur des notes supérieurs ou égales à 0, 3, et 4. Et sur les 1, 3, 7, et 15 derniers jours.""", @@ -131,5 +135,9 @@ messages = { "master_rp_taken_article": """Merci %s ! L'article est prêt pour la revue de presse !""", + "flag_update_done": + "%s a changé de flags", + "unknown_flag": + "Ce flag n'est pas supporté", } diff --git a/tests/test_commands.py b/tests/test_commands.py index 7effe4d252da42fcfa98640480faac0a1e6c5ddd..f327015ccfb349cd5251aa966a191a693dcf5577 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -167,6 +167,21 @@ class TestWantzel(unittest.TestCase): "test: note 1 / publié (https://laquadrature.net/node/%s)" % i ) + def testFlagCommandForAnUnknownArticle(self): + self.wantzel.on_privmsg("test!test.me", "#test_channel", "~flag http://test.me/article p") + self.wantzel.send_message.assert_called_with( + "#test_channel", + messages["status_unknown_article"] % "test" + ) + + def testFlagCommandForAKnownArticle(self): + self.wantzel.on_privmsg("test!test.me", "#test_channel", "~rp http://test.me/article") + self.wantzel.on_privmsg("test!test.me", "#test_channel", "~flag http://test.me/article a") + self.wantzel.send_message.assert_called_with( + "test", + "test: note 1 / archivé / non publié" + ) + def testStatsCommandWithNoArticle(self): self.wantzel.on_privmsg("test!test.me", "#test_channel", "~stats") self.wantzel.send_message.assert_called_with( diff --git a/wantzel.py b/wantzel.py index 304992645cff16a1de84b271de8ec9c1a36a2e86..22a87ea032e40b83e47203d3ca239988034ded0c 100644 --- a/wantzel.py +++ b/wantzel.py @@ -368,7 +368,7 @@ class Wantzel(object): if "wantzel" in msg and ("help" in msg or "aide" in msg): self.help(user, channel, msg) # Find known command - command = re.search("[!~](rp[acp]*|status|kill|help|stats|admin)", msg) + command = re.search("[!~](rp[acp]*|status|kill|help|stats|admin|flag)", msg) Utils.debug("Command: %s" % command) if command: Utils.debug("group(0): %s" % command.group(0)) @@ -383,6 +383,9 @@ class Wantzel(object): elif command == "help": Utils.debug("Calling self.help") self.help(user, channel, msg) + elif command == "flag": + Utils.debug("Calling self.flag") + self.flag(command, user, channel, msg) elif command == "kill": Utils.debug("Calling self.kill") self.kill(user, channel, msg) @@ -416,7 +419,7 @@ class Wantzel(object): """ Utils.debug("help command") # Searching for a command after help keyword - command = re.search("[!~]help (help|rp|status|stats|kill|admin)", msg) + command = re.search("[!~]help (help|rp|status|flag|stats|kill|admin)", msg) if command: command = command.group(1) self.send_message(user, messages["help_"+command]) @@ -609,6 +612,74 @@ class Wantzel(object): message += "non publié / " self.send_message(channel, message[:-3]) + def flag(self, command, user, channel, msg): + """ + Edit the article's flags + """ + Utils.debug("rp command : %s" % command) + Utils.debug("rp user : %s" % user) + Utils.debug("rp channel : %s" % channel) + Utils.debug("rp msg : %s" % msg) + url = get_url(msg) + Utils.debug("url: %s" % url) + if not url: + return + + # Looking for such an article in database + cursor = get_cursor() + # We need to be able to retrieve an url with "http" or "https" + if url.startswith("https"): + url2 = "http" + url[5:] + else: + url2 = "https" + url[4:] + cursor.execute(""" + SELECT id, cite + FROM presse + WHERE url = %s + OR url = %s""", + (url, url2) + ) + rows = cursor.fetchall() + if not rows: + self.send_message(channel, messages["status_unknown_article"] % user) + return + + flag = msg[-1] + flag_score = 0 + + # Matching each flag to its integer value + # LQdN is quoted + if flag == "c": + flag_score = 1 + # the article speak about LQdN + if flag == "p": + flag_score = 2 + # Archive this article + if flag == "a": + flag_score = 4 + + # Check if flag is known. m is 0, so not here. + if flag in ["c", "p", "a"]: + if flag_score & rows[0][1]: + Utils.debug("Removing the %s flag" % flag) + new_flag = rows[0][1] - flag_score + else: + Utils.debug("Adding the %s flag" % flag) + new_flag = rows[0][1] + flag_score + Utils.debug("New flag score: %s" % new_flag) + cursor.execute(""" + UPDATE presse + SET cite=%s + WHERE id=%s""", + (new_flag, rows[0][0]) + ) + self.send_message(channel, messages["flag_update_done"] % url) + # Send flags to user in a private message + self.status("status", user, user, msg) + else: + Utils.debug("Unknown flag") + self.send_message(channel, messages["unknown_flag"]) + def kill(self, user, channel, msg): """ Kill an article by setting its score to -100.