From e5e64c2ea9dcf3077daa79cf249d88de2b2e067d Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Mon, 29 Jan 2024 20:05:54 +0530 Subject: [PATCH] fix: create notification when someone mentions in comment --- crm/api/comment.py | 43 +++++++++++++++++++ .../crm_notification/crm_notification.json | 4 +- crm/hooks.py | 3 ++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 crm/api/comment.py diff --git a/crm/api/comment.py b/crm/api/comment.py new file mode 100644 index 00000000..6797da98 --- /dev/null +++ b/crm/api/comment.py @@ -0,0 +1,43 @@ +import frappe +from bs4 import BeautifulSoup + +def on_update(self, method): + notify_mentions(self) + + +def notify_mentions(doc): + """ + Extract mentions from `content`, and notify. + `content` must have `HTML` content. + """ + content = getattr(doc, "content", None) + if not content: + return + mentions = extract_mentions(content) + for mention in mentions: + values = frappe._dict( + doctype="CRM Notification", + from_user=doc.owner, + to_user=mention.email, + type="Mention", + message=doc.content, + comment=doc.name, + ) + # Why mention oneself? + # if values.from_user == values.to_user: + # continue + if frappe.db.exists("CRM Notification", values): + return + frappe.get_doc(values).insert() + + +def extract_mentions(html): + if not html: + return [] + soup = BeautifulSoup(html, "html.parser") + mentions = [] + for d in soup.find_all("span", attrs={"data-type": "mention"}): + mentions.append( + frappe._dict(full_name=d.get("data-label"), email=d.get("data-id")) + ) + return mentions \ No newline at end of file diff --git a/crm/fcrm/doctype/crm_notification/crm_notification.json b/crm/fcrm/doctype/crm_notification/crm_notification.json index 9ddb2cff..112d52f9 100644 --- a/crm/fcrm/doctype/crm_notification/crm_notification.json +++ b/crm/fcrm/doctype/crm_notification/crm_notification.json @@ -60,13 +60,13 @@ }, { "fieldname": "message", - "fieldtype": "Text Editor", + "fieldtype": "HTML Editor", "label": "Message" } ], "index_web_pages_for_search": 1, "links": [], - "modified": "2024-01-29 19:36:19.466742", + "modified": "2024-01-29 20:03:40.102839", "modified_by": "Administrator", "module": "FCRM", "name": "CRM Notification", diff --git a/crm/hooks.py b/crm/hooks.py index c60beac0..b25039c3 100644 --- a/crm/hooks.py +++ b/crm/hooks.py @@ -133,6 +133,9 @@ doc_events = { "ToDo": { "after_insert": ["crm.api.todo.after_insert"], }, + "Comment": { + "on_update": ["crm.api.comment.on_update"], + } } # Scheduled Tasks