Complete publishing process by email
This commit is contained in:
parent
8c1366ffaa
commit
2f48340547
1 changed files with 48 additions and 41 deletions
|
@ -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
|
||||||
|
|
||||||
|
m = re.search('\[(\w+)\:(\d+)\]', subject)
|
||||||
|
token = m.group(1)
|
||||||
|
comment_id = int(m.group(2))
|
||||||
|
|
||||||
|
# retrieve site and comment rows
|
||||||
|
comment = Comment.select().where(Comment.id == comment_id).get()
|
||||||
|
if comment.site.token != token:
|
||||||
|
logger.warn('ignore corrupted email')
|
||||||
|
return
|
||||||
|
|
||||||
|
# TODO validate chardet decoding is no more needed
|
||||||
|
#message = decode_best_effort(message)
|
||||||
|
if not message:
|
||||||
|
logger.warn('ignore empty email')
|
||||||
|
return
|
||||||
|
|
||||||
# safe logic: no answer or unknown answer is a go for publishing
|
# safe logic: no answer or unknown answer is a go for publishing
|
||||||
if message[:2].upper() == 'NO':
|
if message[:2].upper() == 'NO':
|
||||||
logger.info('discard comment: %s' % branch_name)
|
logger.info('discard comment: %d' % comment_id)
|
||||||
|
comment.delete_instance()
|
||||||
email_body = get_template('drop_comment').render(original=message)
|
email_body = get_template('drop_comment').render(original=message)
|
||||||
mail(pecosys.get_config('post', 'from_email'),
|
mail(email_address, 'Re: ' + subject, email_body)
|
||||||
pecosys.get_config('post', 'to_email'),
|
|
||||||
'Re: ' + subject, email_body)
|
|
||||||
else:
|
else:
|
||||||
if pecosys.get_config("git", "disabled"):
|
# update Comment row
|
||||||
logger.debug("GIT usage disabled (debug mode)")
|
comment.published = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
else:
|
comment.save()
|
||||||
git.merge(branch_name)
|
|
||||||
if pecosys.get_config("git", "remote"):
|
logger.info('commit comment: %d' % comment_id)
|
||||||
git.push()
|
|
||||||
logger.info('commit comment: %s' % branch_name)
|
|
||||||
|
|
||||||
# send approval confirmation email to admin
|
# send approval confirmation email to admin
|
||||||
email_body = get_template('approve_comment').render(original=message)
|
email_body = get_template('approve_comment').render(original=message)
|
||||||
mail(pecosys.get_config('post', 'from_email'),
|
mail(email_address, 'Re: ' + subject, email_body)
|
||||||
pecosys.get_config('post', 'to_email'),
|
|
||||||
'Re: ' + subject, email_body)
|
|
||||||
|
|
||||||
|
# TODO manage subscriptions
|
||||||
# notify reader once comment is published
|
# notify reader once comment is published
|
||||||
reader_email, article_url = get_email_metadata(message)
|
#reader_email, article_url = get_email_metadata(message)
|
||||||
if reader_email:
|
#if reader_email:
|
||||||
notify_reader(reader_email, article_url)
|
# notify_reader(reader_email, article_url)
|
||||||
|
|
||||||
# notify subscribers every time a new comment is published
|
# notify subscribers every time a new comment is published
|
||||||
notify_subscribers(article)
|
#notify_subscribers(article)
|
||||||
|
|
||||||
if pecosys.get_config("git", "disabled"):
|
|
||||||
logger.debug("GIT usage disabled (debug mode)")
|
|
||||||
else:
|
|
||||||
git.branch("-D", branch_name)
|
|
||||||
except:
|
|
||||||
logger.exception("new email failure")
|
|
||||||
|
|
||||||
|
|
||||||
def get_email_metadata(message):
|
def get_email_metadata(message):
|
||||||
|
|
Loading…
Add table
Reference in a new issue