Complete publishing process by email

This commit is contained in:
Yax 2015-05-16 20:55:55 +02:00
parent 8c1366ffaa
commit 2f48340547

View file

@ -35,8 +35,8 @@ class Processor(Thread):
msg = queue.get() msg = queue.get()
if msg['request'] == 'new_comment': if msg['request'] == 'new_comment':
new_comment(msg['data']) new_comment(msg['data'])
#elif msg['type'] == 'reply_comment_email': elif msg['request'] == 'new_mail':
# reply_comment_email(req['From'], req['Subject'], req['Body']) reply_comment_email(msg['data'])
#elif req['type'] == 'unsubscribe': #elif req['type'] == 'unsubscribe':
# unsubscribe_reader(req['email'], req['article']) # unsubscribe_reader(req['email'], req['article'])
else: else:
@ -87,7 +87,7 @@ def new_comment(data):
# send email # send email
# TODO subject should embed a key # TODO subject should embed a key
subject = '%s: [%d]' % (site.name, comment.id) subject = '%s: [%s:%d]' % (site.name, token, comment.id)
mail(site.admin_email, subject, email_body) mail(site.admin_email, subject, email_body)
# TODO support subscription # TODO support subscription
@ -98,50 +98,57 @@ def new_comment(data):
logger.debug("new comment processed ") logger.debug("new comment processed ")
def reply_comment_email(from_email, subject, message): def reply_comment_email(data):
try:
m = re.search('\[(\d+)\-(\w+)\]', subject)
branch_name = m.group(1)
article = m.group(2)
message = decode_best_effort(message) email_address = data['from']
subject = data['subject']
message = ''
for part in data['parts']:
if part['content-type'] == 'text/plain':
message = part['content']
break
# safe logic: no answer or unknown answer is a go for publishing m = re.search('\[(\w+)\:(\d+)\]', subject)
if message[:2].upper() == 'NO': token = m.group(1)
logger.info('discard comment: %s' % branch_name) comment_id = int(m.group(2))
email_body = get_template('drop_comment').render(original=message)
mail(pecosys.get_config('post', 'from_email'),
pecosys.get_config('post', 'to_email'),
'Re: ' + subject, email_body)
else:
if pecosys.get_config("git", "disabled"):
logger.debug("GIT usage disabled (debug mode)")
else:
git.merge(branch_name)
if pecosys.get_config("git", "remote"):
git.push()
logger.info('commit comment: %s' % branch_name)
# send approval confirmation email to admin # retrieve site and comment rows
email_body = get_template('approve_comment').render(original=message) comment = Comment.select().where(Comment.id == comment_id).get()
mail(pecosys.get_config('post', 'from_email'), if comment.site.token != token:
pecosys.get_config('post', 'to_email'), logger.warn('ignore corrupted email')
'Re: ' + subject, email_body) return
# notify reader once comment is published # TODO validate chardet decoding is no more needed
reader_email, article_url = get_email_metadata(message) #message = decode_best_effort(message)
if reader_email: if not message:
notify_reader(reader_email, article_url) logger.warn('ignore empty email')
return
# notify subscribers every time a new comment is published # safe logic: no answer or unknown answer is a go for publishing
notify_subscribers(article) if message[:2].upper() == 'NO':
logger.info('discard comment: %d' % comment_id)
comment.delete_instance()
email_body = get_template('drop_comment').render(original=message)
mail(email_address, 'Re: ' + subject, email_body)
else:
# update Comment row
comment.published = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
comment.save()
if pecosys.get_config("git", "disabled"): logger.info('commit comment: %d' % comment_id)
logger.debug("GIT usage disabled (debug mode)")
else: # send approval confirmation email to admin
git.branch("-D", branch_name) email_body = get_template('approve_comment').render(original=message)
except: mail(email_address, 'Re: ' + subject, email_body)
logger.exception("new email failure")
# TODO manage subscriptions
# notify reader once comment is published
#reader_email, article_url = get_email_metadata(message)
#if reader_email:
# notify_reader(reader_email, article_url)
# notify subscribers every time a new comment is published
#notify_subscribers(article)
def get_email_metadata(message): def get_email_metadata(message):