From 171fcfab7271cd1e89ed3ce4cf12b461ad33eba7 Mon Sep 17 00:00:00 2001 From: Pelle Nilsson Date: Wed, 18 Jul 2018 21:24:21 +0200 Subject: [PATCH] Postpone notifications to users until comment has been approved by moderator --- isso/db/threads.py | 3 +++ isso/ext/notifications.py | 18 +++++++++++++----- isso/views/comments.py | 5 ++++- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/isso/db/threads.py b/isso/db/threads.py index 4f0b476..060f074 100644 --- a/isso/db/threads.py +++ b/isso/db/threads.py @@ -25,6 +25,9 @@ class Threads(object): def __getitem__(self, uri): return Thread(*self.db.execute("SELECT * FROM threads WHERE uri=?", (uri, )).fetchone()) + def get(self, id): + return Thread(*self.db.execute("SELECT * FROM threads WHERE id=?", (id, )).fetchone()) + def new(self, uri, title): self.db.execute( "INSERT INTO threads (uri, title) VALUES (?, ?)", (uri, title)) diff --git a/isso/ext/notifications.py b/isso/ext/notifications.py index 78516bb..dc521dc 100644 --- a/isso/ext/notifications.py +++ b/isso/ext/notifications.py @@ -97,7 +97,8 @@ class SMTP(object): uwsgi.spooler = spooler def __iter__(self): - yield "comments.new:after-save", self.notify + yield "comments.new:after-save", self.notify_new + yield "comments.activate", self.notify_activated def format(self, thread, comment, parent_comment, recipient=None, admin=False): @@ -141,7 +142,17 @@ class SMTP(object): rv.seek(0) return rv.read() - def notify(self, thread, comment): + def notify_new(self, thread, comment): + body = self.format(thread, comment, None, admin=True) + self.sendmail(thread["title"], body, thread, comment) + + if comment["mode"] == 1: + self.notify_users(thread, comment) + + def notify_activated(self, thread, comment): + self.notify_users(thread, comment) + + def notify_users(self, thread, comment): if "parent" in comment and comment["parent"] is not None: # Notify interested authors that a new comment is posted notified = [] @@ -157,9 +168,6 @@ class SMTP(object): self.sendmail(subject, body, thread, comment, to=email) notified.append(email) - body = self.format(thread, comment, None, admin=True) - self.sendmail(thread["title"], body, thread, comment) - def sendmail(self, subject, body, thread, comment, to=None): if uwsgi: uwsgi.spool({b"subject": subject.encode("utf-8"), diff --git a/isso/views/comments.py b/isso/views/comments.py index 15b16e8..3899fa1 100644 --- a/isso/views/comments.py +++ b/isso/views/comments.py @@ -623,9 +623,12 @@ class API(object): return Response(modal, 200, content_type="text/html") if action == "activate": + if item['mode'] == 1: + return Response("Already activated", 200) with self.isso.lock: self.comments.activate(id) - self.signal("comments.activate", id) + thread = self.threads.get(item['tid']) + self.signal("comments.activate", thread, item) return Response("Yo", 200) elif action == "edit": data = request.get_json()