fix: create notification when someone mentions in comment
This commit is contained in:
parent
d95aa9ee07
commit
e5e64c2ea9
43
crm/api/comment.py
Normal file
43
crm/api/comment.py
Normal file
@ -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
|
||||
@ -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",
|
||||
|
||||
@ -133,6 +133,9 @@ doc_events = {
|
||||
"ToDo": {
|
||||
"after_insert": ["crm.api.todo.after_insert"],
|
||||
},
|
||||
"Comment": {
|
||||
"on_update": ["crm.api.comment.on_update"],
|
||||
}
|
||||
}
|
||||
|
||||
# Scheduled Tasks
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user