From 263e9fd6157da369777c7a697c67507520483e6c Mon Sep 17 00:00:00 2001 From: dece Date: Mon, 12 Oct 2020 19:19:50 +0200 Subject: [PATCH] notes: limit number of notes for an user --- config.json.example | 4 +++- edmond/plugins/notes.py | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/config.json.example b/config.json.example index 546b3c6..6b7abe5 100644 --- a/config.json.example +++ b/config.json.example @@ -42,7 +42,9 @@ "commands": ["note down"], "content_regex": "for (?P\\S+) (?P.+)", "confirmation": "Ok.", - "deliver_format": "{dest}, {sender} tells you: {message}" + "deliver_format": "{dest}, {sender} tells you: {message}", + "limit": 5, + "too_many_notes": "There are too many notes for this user." }, "random": { "commands": ["choose"], diff --git a/edmond/plugins/notes.py b/edmond/plugins/notes.py index 21ce8e0..7b33522 100644 --- a/edmond/plugins/notes.py +++ b/edmond/plugins/notes.py @@ -5,7 +5,10 @@ from edmond.plugin import Plugin class NotesPlugin(Plugin): - REQUIRED_CONFIGS = ["commands", "content_regex", "deliver_format"] + REQUIRED_CONFIGS = [ + "commands", "content_regex", "confirmation", "deliver_format", "limit", + "too_many_notes" + ] def __init__(self, bot): super().__init__(bot) @@ -48,6 +51,12 @@ class NotesPlugin(Plugin): if any(k not in groups for k in ("target", "note")): return False target = groups["target"] + + # Check we did not reach our note limits. + if self.count_user_notes(target) >= self.config["limit"]: + self.bot.say(event.target, self.config["too_many_notes"]) + return True + message = groups["note"] self.bot.log_d(f"Noting for {target}: {message}") note = { @@ -57,3 +66,8 @@ class NotesPlugin(Plugin): } self.append_storage_list_value("notes", note) self.bot.say(event.target, self.config["confirmation"]) + + def count_user_notes(self, nick): + """Count the number of undelivered notes this user has.""" + notes = self.get_storage_value("notes", []) + return sum(note["dest"] == nick for note in notes)