* fix(diff): add type hints to whitelisted methods * fix(global_search): add type hints to whitelisted methods * fix(custom_html_block): add type hints to whitelisted methods * fix(deleted_document): add type hints to whitelisted methods * fix(log_settings): add type hints to whitelisted methods * fix(role): add type hints to whitelisted methods * fix(user_type): add type hints to whitelisted methods * fix(rq_job): add type hints to whitelisted methods * fix(link_preview): add type hints to whitelisted methods * fix(email_account): add type hints to whitelisted methods * fix(web_form): add type hints to whitelisted methods * fix(web_page_view): add type hints to whitelisted methods * fix(csvutils): add type hints to whitelisted methods * fix(file_manager): add type hints to whitelisted methods * fix(email_body): add type hints to whitelisted methods * fix(email_queue): add type hints to whitelisted methods * fix(email_template): add type hints to whitelisted methods * fix(notification): add type hints to whitelisted methods * fix(email_group): add type hints to whitelisted methods * fix(inbox): add type hints to whitelisted methods * fix(recorder): add type hints to whitelisted methods * fix(sms_settings): add type hints to whitelisted methods * fix: tighten type hints * fix(data_import): add type hints to whitelisted methods * fix(user_permission): add type hints to whitelisted methods * fix(gantt): add type hints to whitelisted methods * fix(like): add type hints to whitelisted methods * fix(search): add type hints to whitelisted methods * fix(onboarding_step): add type hints to whitelisted methods * fix(system_console): add type hints to whitelisted methods * fix(workspace_sidebar): add type hints to whitelisted methods * fix(todo): add type hints to whitelisted methods * fix: correct type hints * fix(print_format): add type hints to whitelisted methods * fix(client): add type hints to whitelisted methods
92 lines
2.3 KiB
Python
92 lines
2.3 KiB
Python
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
|
# License: MIT. See LICENSE
|
|
|
|
"""Allow adding of likes to documents"""
|
|
|
|
import json
|
|
|
|
import frappe
|
|
from frappe import _
|
|
from frappe.database.schema import add_column
|
|
from frappe.desk.form.document_follow import follow_document
|
|
from frappe.utils import get_link_to_form
|
|
|
|
|
|
@frappe.whitelist()
|
|
def toggle_like(doctype: str, name: str, add: str | bool = False):
|
|
"""Adds / removes the current user in the `__liked_by` property of the given document.
|
|
If column does not exist, will add it in the database.
|
|
|
|
The `_liked_by` property is always set from this function and is ignored if set via
|
|
Document API
|
|
|
|
:param doctype: DocType of the document to like
|
|
:param name: Name of the document to like
|
|
:param add: `Yes` if like is to be added. If not `Yes` the like will be removed."""
|
|
|
|
_toggle_like(doctype, name, add)
|
|
|
|
|
|
def _toggle_like(doctype, name, add, user=None):
|
|
"""Same as toggle_like but hides param `user` from API"""
|
|
|
|
if not user:
|
|
user = frappe.session.user
|
|
|
|
try:
|
|
liked_by = frappe.db.get_value(doctype, name, "_liked_by")
|
|
|
|
if liked_by:
|
|
liked_by = json.loads(liked_by)
|
|
else:
|
|
liked_by = []
|
|
|
|
if add == "Yes":
|
|
if user not in liked_by:
|
|
liked_by.append(user)
|
|
add_comment(doctype, name)
|
|
if frappe.get_cached_value("User", user, "follow_liked_documents"):
|
|
follow_document(doctype, name, user)
|
|
else:
|
|
if user in liked_by:
|
|
liked_by.remove(user)
|
|
remove_like(doctype, name)
|
|
|
|
if frappe.get_meta(doctype).issingle:
|
|
frappe.db.set_single_value(doctype, "_liked_by", json.dumps(liked_by), update_modified=False)
|
|
else:
|
|
frappe.db.set_value(doctype, name, "_liked_by", json.dumps(liked_by), update_modified=False)
|
|
|
|
except frappe.db.ProgrammingError as e:
|
|
if frappe.db.is_missing_column(e):
|
|
add_column(doctype, "_liked_by", "Text")
|
|
_toggle_like(doctype, name, add, user)
|
|
else:
|
|
raise
|
|
|
|
|
|
def remove_like(doctype, name):
|
|
"""Remove previous Like"""
|
|
# remove Comment
|
|
frappe.delete_doc(
|
|
"Comment",
|
|
[
|
|
c.name
|
|
for c in frappe.get_all(
|
|
"Comment",
|
|
filters={
|
|
"comment_type": "Like",
|
|
"reference_doctype": doctype,
|
|
"reference_name": name,
|
|
"owner": frappe.session.user,
|
|
},
|
|
)
|
|
],
|
|
ignore_permissions=True,
|
|
force=True,
|
|
)
|
|
|
|
|
|
def add_comment(doctype, name):
|
|
doc = frappe.get_lazy_doc(doctype, name)
|
|
doc.add_comment("Like", _("Liked"))
|