Added reply notification for commenter

This commit is contained in:
Thomas Sileo 2013-12-26 19:19:15 +01:00
parent ab27ce5450
commit 08313c191c
8 changed files with 59 additions and 31 deletions

View File

@ -214,5 +214,11 @@ a {
}
}
}
> .notification-section {
display: none;
padding-bottom: 10px;
}
}
}

View File

@ -20,7 +20,7 @@ class Comments:
"""
fields = ['tid', 'id', 'parent', 'created', 'modified', 'mode', 'remote_addr',
'text', 'author', 'email', 'website', 'likes', 'dislikes', 'voters']
'text', 'author', 'email', 'website', 'likes', 'dislikes', 'voters', 'notification']
def __init__(self, db):
@ -30,7 +30,8 @@ class Comments:
' tid REFERENCES threads(id), id INTEGER PRIMARY KEY, parent INTEGER,',
' created FLOAT NOT NULL, modified FLOAT, mode INTEGER, remote_addr VARCHAR,',
' text VARCHAR, author VARCHAR, email VARCHAR, website VARCHAR,',
' likes INTEGER DEFAULT 0, dislikes INTEGER DEFAULT 0, voters BLOB NOT NULL);'])
' likes INTEGER DEFAULT 0, dislikes INTEGER DEFAULT 0, voters BLOB NOT NULL,',
' notification INTEGER);'])
def add(self, uri, c):
"""
@ -41,16 +42,16 @@ class Comments:
'INSERT INTO comments (',
' tid, parent,'
' created, modified, mode, remote_addr,',
' text, author, email, website, voters )',
' text, author, email, website, voters, notification)',
'SELECT',
' threads.id, ?,',
' ?, ?, ?, ?,',
' ?, ?, ?, ?, ?',
' ?, ?, ?, ?, ?, ?',
'FROM threads WHERE threads.uri = ?;'], (
c.get('parent'),
c.get('created') or time.time(), None, c["mode"], c['remote_addr'],
c['text'], c.get('author'), c.get('email'), c.get('website'), buffer(
Bloomfilter(iterable=[c['remote_addr']]).array),
Bloomfilter(iterable=[c['remote_addr']]).array), c.get('notification', 0),
uri)
)

View File

@ -58,10 +58,10 @@ class SMTP(object):
def __enter__(self):
klass = (smtplib.SMTP_SSL if self.conf.get('security') == 'ssl' else smtplib.SMTP)
klass = smtplib.SMTP
self.client = klass(host=self.conf.get('host'), port=self.conf.getint('port'))
if self.conf.get('security') == 'starttls':
self.client.starttls();
#if self.conf.get('security') == 'starttls':
# self.client.starttls();
if self.conf.get('username') and self.conf.get('password'):
self.client.login(self.conf.get('username'),
@ -75,7 +75,7 @@ class SMTP(object):
def __iter__(self):
yield "comments.new:after-save", self.notify
def format(self, thread, comment):
def format(self, thread, comment, admin=False):
rv = io.StringIO()
@ -88,39 +88,50 @@ class SMTP(object):
rv.write(comment["text"] + "\n")
rv.write("\n")
if comment["website"]:
rv.write("User's URL: %s\n" % comment["website"])
if admin:
if comment["website"]:
rv.write("User's URL: %s\n" % comment["website"])
rv.write("IP address: %s\n" % comment["remote_addr"])
rv.write("Link to comment: %s\n" % (local("origin") + thread["uri"] + "#isso-%i" % comment["id"]))
rv.write("\n")
rv.write("IP address: %s\n" % comment["remote_addr"])
rv.write("Link to comment: %s\n" % (local("origin") + thread["uri"] + "#isso-%i" % comment["id"]))
rv.write("\n")
uri = local("host") + "/id/%i" % comment["id"]
key = self.isso.sign(comment["id"])
uri = local("host") + "/id/%i" % comment["id"]
key = self.isso.sign(comment["id"])
rv.write("---\n")
rv.write("Delete comment: %s\n" % (uri + "/delete/" + key))
rv.write("---\n")
rv.write("Delete comment: %s\n" % (uri + "/delete/" + key))
if comment["mode"] == 2:
rv.write("Activate comment: %s\n" % (uri + "/activate/" + key))
if comment["mode"] == 2:
rv.write("Activate comment: %s\n" % (uri + "/activate/" + key))
rv.seek(0)
return rv.read()
def notify(self, thread, comment):
if "parent" in comment:
comment_parent = self.isso.db.comments.get(comment["parent"])
# Notify the author that a new comment is posted if requested
if "email" in comment_parent and comment_parent["notification"]:
body = self.format(thread, comment, admin=False)
subject = "Re: New comment posted on %s" % thread["title"]
self.sendmail(subject, body, thread, comment, to=comment_parent["email"])
body = self.format(thread, comment)
body = self.format(thread, comment, admin=True)
self.sendmail(thread["title"], body, thread, comment)
def sendmail(self, subject, body, thread, comment, to=None):
if uwsgi:
uwsgi.spool({b"subject": thread["title"].encode("utf-8"),
b"body": body.encode("utf-8")})
uwsgi.spool({b"subject": subject.encode("utf-8"),
b"body": body.encode("utf-8"),
b"to": to})
else:
start_new_thread(self._retry, (thread["title"], body))
start_new_thread(self._retry, (subject, body, to))
def _sendmail(self, subject, body):
def _sendmail(self, subject, body, to=None):
from_addr = self.conf.get("from")
to_addr = self.conf.get("to")
to_addr = to or self.conf.get("to")
msg = MIMEText(body, 'plain', 'utf-8')
msg['From'] = "Ich schrei sonst! <%s>" % from_addr
@ -131,10 +142,10 @@ class SMTP(object):
with self as con:
con.sendmail(from_addr, to_addr, msg.as_string())
def _retry(self, subject, body):
def _retry(self, subject, body, to):
for x in range(5):
try:
self._sendmail(subject, body)
self._sendmail(subject, body, to)
except smtplib.SMTPConnectError:
time.sleep(60)
else:

View File

@ -3,6 +3,7 @@ define({
"postbox-author": "Name (optional)",
"postbox-email": "E-mail (optional)",
"postbox-submit": "Submit",
"postbox-notification": "Subscribe to email notification of replies",
"num-comments": "One Comment\n{{ n }} Comments",
"no-comments": "No Comments Yet",

View File

@ -3,6 +3,7 @@ define({
"postbox-author": "Nom (optionel)",
"postbox-email": "Courriel (optionel)",
"postbox-submit": "Soumettre",
"postbox-notification": "S'abonner aux notifications de réponses",
"num-comments": "Un commentaire\n{{ n }} commentaires",
"no-comments": "Aucun commentaire pour l'instant",

View File

@ -34,6 +34,7 @@ define(["app/text/html", "app/dom", "app/utils", "app/config", "app/api", "app/m
.then(function(rv) {
$(".avatar svg", el).replace(lib.identicons.generate(rv, 4, 48));
});
$(".notification-section").style.display = "block";
}, 200);
}, false);
@ -63,7 +64,8 @@ define(["app/text/html", "app/dom", "app/utils", "app/config", "app/api", "app/m
author: $("[name=author]", el).value || null,
email: $("[name=email]", el).value || null,
text: $("textarea", el).value,
parent: parent || null
parent: parent || null,
notification: $("[name=notification]", el).checked ? 1 : 0,
}).then(function(comment) {
$("[name=author]", el).value = "";
$("[name=email]", el).value = "";

View File

@ -6,6 +6,7 @@
<div class="textarea-wrapper">
<textarea name="text" rows="2" placeholder="{{ i18n-postbox-text }}"></textarea>
</div>
<section class="auth-section">
<p class="input-wrapper">
<input type="text" name="author" placeholder="{{ i18n-postbox-author }}"/>
@ -17,5 +18,8 @@
<input type="submit" value="{{ i18n-postbox-submit }}"/>
</p>
</section>
<section class="notification-section">
<label><input type="checkbox" name="notification"/> {{ i18n-postbox-notification }}</label>
</section>
</div>
</div>

View File

@ -52,10 +52,10 @@ def xhr(func):
class API(object):
FIELDS = set(['id', 'parent', 'text', 'author', 'website', 'email',
'mode', 'created', 'modified', 'likes', 'dislikes', 'hash'])
'mode', 'created', 'modified', 'likes', 'dislikes', 'hash', 'notification'])
# comment fields, that can be submitted
ACCEPT = set(['text', 'author', 'website', 'email', 'parent'])
ACCEPT = set(['text', 'author', 'website', 'email', 'parent', 'notification'])
VIEWS = [
('fetch', ('GET', '/')),
@ -123,6 +123,7 @@ class API(object):
valid, reason = API.verify(data)
if not valid:
print valid, "VALID"
return BadRequest(reason)
for field in ("author", "email"):
@ -134,6 +135,7 @@ class API(object):
with self.isso.lock:
if uri not in self.threads:
print "URI", uri, local('origin')
with http.curl('GET', local("origin"), uri) as resp:
if resp and resp.status == 200:
uri, title = parse.thread(resp.read(), id=uri)