feat: batch notifications
This commit is contained in:
@@ -103,7 +103,7 @@ doc_events = {
|
||||
]
|
||||
},
|
||||
"Discussion Reply": {"after_insert": "lms.lms.utils.handle_notifications"},
|
||||
"Notification Log": {"after_insert": "lms.lms.utils.publish_notifications"},
|
||||
"Notification Log": {"on_change": "lms.lms.utils.publish_notifications"},
|
||||
}
|
||||
|
||||
# Scheduled Tasks
|
||||
|
||||
@@ -398,3 +398,20 @@ def get_all_users():
|
||||
)
|
||||
|
||||
return {user.name: user for user in users}
|
||||
|
||||
|
||||
@frappe.whitelist()
|
||||
def mark_as_read(name):
|
||||
doc = frappe.get_doc("Notification Log", name)
|
||||
doc.read = 1
|
||||
doc.save(ignore_permissions=True)
|
||||
|
||||
|
||||
@frappe.whitelist()
|
||||
def mark_all_as_read():
|
||||
notifications = frappe.get_all(
|
||||
"Notification Log", {"for_user": frappe.session.user, "read": 0}, pluck="name"
|
||||
)
|
||||
|
||||
for notification in notifications:
|
||||
mark_as_read(notification)
|
||||
|
||||
@@ -645,33 +645,40 @@ def handle_notifications(doc, method):
|
||||
|
||||
|
||||
def create_notification_log(doc, topic):
|
||||
course = frappe.db.get_value("Course Lesson", topic.reference_docname, "course")
|
||||
course_title = frappe.db.get_value("LMS Course", course, "title")
|
||||
instructors = frappe.db.get_all(
|
||||
"Course Instructor", {"parent": course}, pluck="instructor"
|
||||
)
|
||||
users = []
|
||||
if topic.reference_doctype == "LMS Course":
|
||||
course = frappe.db.get_value("Course Lesson", topic.reference_docname, "course")
|
||||
course_title = frappe.db.get_value("LMS Course", course, "title")
|
||||
instructors = frappe.db.get_all(
|
||||
"Course Instructor", {"parent": course}, pluck="instructor"
|
||||
)
|
||||
users.append(topic.owner)
|
||||
users += instructors
|
||||
subject = _("New reply on the topic {0} in course {1}").format(
|
||||
topic.title, course_title
|
||||
)
|
||||
link = get_lesson_url(course, get_lesson_index(topic.reference_docname))
|
||||
|
||||
else:
|
||||
batch_title = frappe.db.get_value("LMS Batch", topic.reference_docname, "title")
|
||||
subject = _("New comment in batch {0}").format(batch_title)
|
||||
link = f"/batches/{topic.reference_docname}"
|
||||
moderators = frappe.get_all("Has Role", {"role": "Moderator"}, pluck="parent")
|
||||
users += moderators
|
||||
|
||||
notification = frappe._dict(
|
||||
{
|
||||
"subject": _("New reply on the topic {0} in course {1}").format(
|
||||
topic.title, course_title
|
||||
),
|
||||
"subject": subject,
|
||||
"email_content": doc.reply,
|
||||
"document_type": topic.reference_doctype,
|
||||
"document_name": topic.reference_docname,
|
||||
"for_user": topic.owner,
|
||||
"from_user": doc.owner,
|
||||
"type": "Alert",
|
||||
"link": get_lesson_url(course, get_lesson_index(topic.reference_docname)),
|
||||
"link": link,
|
||||
}
|
||||
)
|
||||
|
||||
users = []
|
||||
if doc.owner != topic.owner:
|
||||
users.append(topic.owner)
|
||||
|
||||
if doc.owner not in instructors:
|
||||
users += instructors
|
||||
make_notification_logs(notification, users)
|
||||
|
||||
|
||||
@@ -681,21 +688,31 @@ def notify_mentions_on_portal(doc, topic):
|
||||
return
|
||||
|
||||
from_user_name = get_fullname(doc.owner)
|
||||
course = frappe.db.get_value("Course Lesson", topic.reference_docname, "course")
|
||||
|
||||
if topic.reference_doctype == "LMS Course":
|
||||
course = frappe.db.get_value("Course Lesson", topic.reference_docname, "course")
|
||||
subject = _("{0} mentioned you in a comment in {1}").format(
|
||||
from_user_name, topic.title
|
||||
)
|
||||
link = get_lesson_url(course, get_lesson_index(topic.reference_docname))
|
||||
else:
|
||||
batch_title = frappe.db.get_value("LMS Batch", topic.reference_docname, "title")
|
||||
subject = _("{0} mentioned you in a comment in {1}").format(
|
||||
from_user_name, batch_title
|
||||
)
|
||||
link = f"/batches/{topic.reference_docname}"
|
||||
|
||||
for user in mentions:
|
||||
notification = frappe._dict(
|
||||
{
|
||||
"subject": _("{0} mentioned you in a comment in {1}").format(
|
||||
from_user_name, topic.title
|
||||
),
|
||||
"subject": subject,
|
||||
"email_content": doc.reply,
|
||||
"document_type": topic.reference_doctype,
|
||||
"document_name": topic.reference_docname,
|
||||
"for_user": user,
|
||||
"from_user": doc.owner,
|
||||
"type": "Alert",
|
||||
"link": get_lesson_url(course, get_lesson_index(topic.reference_docname)),
|
||||
"link": link,
|
||||
}
|
||||
)
|
||||
make_notification_logs(notification, user)
|
||||
|
||||
Reference in New Issue
Block a user