From ae097e5139dd866993550edd128e49b3c9e9027c Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Thu, 7 Feb 2019 16:12:19 +0530 Subject: [PATCH] fix: tests, check for spam comments --- frappe/core/doctype/comment/comment.json | 4 ++-- frappe/core/doctype/communication/email.py | 4 ++-- .../templates/includes/comments/comments.html | 7 +++++- .../templates/includes/comments/comments.py | 22 ++++++++++++++----- frappe/utils/background_jobs.py | 2 +- frappe/website/utils.py | 2 +- 6 files changed, 28 insertions(+), 13 deletions(-) diff --git a/frappe/core/doctype/comment/comment.json b/frappe/core/doctype/comment/comment.json index 2c79d8fb00..fe3a9a346a 100644 --- a/frappe/core/doctype/comment/comment.json +++ b/frappe/core/doctype/comment/comment.json @@ -22,7 +22,7 @@ "columns": 0, "default": "Comment", "fieldname": "comment_type", - "fieldtype": "Data", + "fieldtype": "Select", "hidden": 0, "ignore_user_permissions": 0, "ignore_xss_filter": 0, @@ -476,7 +476,7 @@ "issingle": 0, "istable": 0, "max_attachments": 0, - "modified": "2019-02-07 15:26:21.867083", + "modified": "2019-02-07 16:06:05.455876", "modified_by": "Administrator", "module": "Core", "name": "Comment", diff --git a/frappe/core/doctype/communication/email.py b/frappe/core/doctype/communication/email.py index fbcd30a3f2..92b2b29f27 100755 --- a/frappe/core/doctype/communication/email.py +++ b/frappe/core/doctype/communication/email.py @@ -494,9 +494,9 @@ def sendmail(communication_name, print_html=None, print_format=None, attachments communication._notify(print_html=print_html, print_format=print_format, attachments=attachments, recipients=recipients, cc=cc, bcc=bcc) - except frappe.db.InternalError: + except frappe.db.InternalError as e: # deadlock, try again - if frappe.db.is_deadlocked(): + if frappe.db.is_deadlocked(e): frappe.db.rollback() time.sleep(1) continue diff --git a/frappe/templates/includes/comments/comments.html b/frappe/templates/includes/comments/comments.html index 92d41d7701..3b18d8c2cb 100644 --- a/frappe/templates/includes/comments/comments.html +++ b/frappe/templates/includes/comments/comments.html @@ -112,7 +112,12 @@ if(r._server_messages) frappe.msgprint(r._server_messages); } else { - frappe.msgprint('{{ _("Thank you for your comment. It will be published after approval") }}'); + if (r.message) { + $(r.message).appendTo("#comment-list"); + } else { + // probably spam + frappe.msgprint('{{ _("Thank you for your comment. It will be published after approval") }}'); + } $(".no-comment, .add-comment").toggle(false); $("#comment-form").toggle(); } diff --git a/frappe/templates/includes/comments/comments.py b/frappe/templates/includes/comments/comments.py index 3ae972bb63..04de6f5a15 100644 --- a/frappe/templates/includes/comments/comments.py +++ b/frappe/templates/includes/comments/comments.py @@ -17,24 +17,34 @@ def add_comment(comment, comment_email, comment_by, reference_doctype, reference comment_email = comment_email, comment_by = comment_by) + blacklist = ['http://', 'https://', '@gmail.com'] + + if not any([b in comment.content for b in blacklist]): + # probably not spam! + comment.db_set('published', 1) + # since comments are embedded in the page, clear the web cache clear_cache(route) content = (doc.content + "

{2}

".format(frappe.utils.get_request_site_address(), doc.name, - route, _("Open Comment Form"))) + route, _("View Comment"))) # notify creator frappe.sendmail( - recipients = doc.owner, - subject = _('Please Approve New Comment on {0}: {1}').format(doc.doctype, doc.name), + recipients = frappe.get_doc('User', doc.owner, 'email') or doc.owner, + subject = _('New Comment on {0}: {1}').format(doc.doctype, doc.name), message = content, reference_doctype=doc.doctype, reference_name=doc.name ) - # revert with template - template = frappe.get_template("templates/includes/comments/comment.html") + if comment.published: + # revert with template if all clear (no backlinks) + template = frappe.get_template("templates/includes/comments/comment.html") - return template.render({"comment": comment.as_dict()}) + return template.render({"comment": comment.as_dict()}) + + else: + return '' diff --git a/frappe/utils/background_jobs.py b/frappe/utils/background_jobs.py index f1194edaf4..5587704c27 100755 --- a/frappe/utils/background_jobs.py +++ b/frappe/utils/background_jobs.py @@ -105,7 +105,7 @@ def execute_job(site, method, event, job_name, kwargs, user=None, is_async=True, if (retry < 5 and (isinstance(e, frappe.RetryBackgroundJobError) or - (frappe.db.is_deadlocked() or frappe.db.is_timedout()))): + (frappe.db.is_deadlocked(e) or frappe.db.is_timedout(e)))): # retry the job if # 1213 = deadlock # 1205 = lock wait timeout diff --git a/frappe/website/utils.py b/frappe/website/utils.py index 8e6f16c2e7..be8bb2a4f8 100644 --- a/frappe/website/utils.py +++ b/frappe/website/utils.py @@ -39,7 +39,7 @@ def get_comment_list(doctype, name): filters = dict( reference_doctype = doctype, reference_name = name, - comment_type = comment, + comment_type = 'Comment', published = 1 ), order_by = 'creation asc')