diff --git a/raven/api/search.py b/raven/api/search.py index d43225016..25b95794e 100644 --- a/raven/api/search.py +++ b/raven/api/search.py @@ -20,7 +20,7 @@ def get_search_result(filter_type, doctype, search_text=None, from_user=None, in } query = frappe.qb.from_(doctype).select( - doctype.name, doctype.file, doctype.owner, doctype.creation, doctype.message_type, doctype.channel_id, doctype.text).join(channel, JoinType.left).on(doctype.channel_id == channel.name).join(channel_member, JoinType.left).on( + doctype.name, doctype.file, doctype.owner, doctype.creation, doctype.message_type, doctype.channel_id, doctype.text, doctype.content).join(channel, JoinType.left).on(doctype.channel_id == channel.name).join(channel_member, JoinType.left).on( channel_member.channel_id == doctype.channel_id).where((channel.type != 'Private') | (channel_member.user_id == frappe.session.user)) if filter_type == 'File': @@ -40,7 +40,7 @@ def get_search_result(filter_type, doctype, search_text=None, from_user=None, in query = query.where(doctype.file.like( "/private/files/%" + search_text + "%")) elif filter_type == 'Message': - query = query.where(doctype.text.like("%" + search_text + "%")) + query = query.where(doctype.content.like("%" + search_text + "%")) elif filter_type == 'Channel': query = query.where( doctype.channel_name.like("%" + search_text + "%")) diff --git a/raven/patches.txt b/raven/patches.txt index b617b5528..e8b60c652 100644 --- a/raven/patches.txt +++ b/raven/patches.txt @@ -3,3 +3,4 @@ [post_model_sync] raven.patches.v1_2.create_raven_users raven.patches.v1_3.create_raven_message_indexes #23 +raven.patches.v1_3.update_all_messages_to_include_message_content #1 \ No newline at end of file diff --git a/raven/patches/v1_3/update_all_messages_to_include_message_content.py b/raven/patches/v1_3/update_all_messages_to_include_message_content.py new file mode 100644 index 000000000..30c69b3ff --- /dev/null +++ b/raven/patches/v1_3/update_all_messages_to_include_message_content.py @@ -0,0 +1,26 @@ +import frappe +from frappe.utils import strip_html_tags + + +def execute(): + update_old_messages_to_include_message_content() + + +def update_old_messages_to_include_message_content(): + ''' + Update all old messages to include message content + Message content is required for search + It is basically the message's text content but without any html tags + (this is done to improve search results) + This is a one-time operation, not required for new messages + ''' + messages = frappe.db.get_all('Raven Message', fields=[ + 'name', 'text', 'message_type']) + for message in messages: + if message.text: + cleaned_text = strip_html_tags(message.text).replace( + '\ufeff', '').replace(' ', ' ') + content = cleaned_text + frappe.db.set_value( + 'Raven Message', message.name, 'content', content) + frappe.db.commit() diff --git a/raven/raven_messaging/doctype/raven_message/raven_message.json b/raven/raven_messaging/doctype/raven_message/raven_message.json index 7eee68fda..ff7d32024 100644 --- a/raven/raven_messaging/doctype/raven_message/raven_message.json +++ b/raven/raven_messaging/doctype/raven_message/raven_message.json @@ -22,7 +22,8 @@ "is_reply", "linked_message", "link_doctype", - "link_document" + "link_document", + "content" ], "fields": [ { @@ -109,11 +110,17 @@ "fieldtype": "Dynamic Link", "label": "Link Document", "options": "link_doctype" + }, + { + "fieldname": "content", + "fieldtype": "Long Text", + "label": "Content", + "read_only": 1 } ], "index_web_pages_for_search": 1, "links": [], - "modified": "2023-12-22 18:25:02.237368", + "modified": "2024-01-19 14:56:44.534433", "modified_by": "Administrator", "module": "Raven Messaging", "name": "Raven Message", diff --git a/raven/raven_messaging/doctype/raven_message/raven_message.py b/raven/raven_messaging/doctype/raven_message/raven_message.py index 6b43a05f6..09acfea0b 100644 --- a/raven/raven_messaging/doctype/raven_message/raven_message.py +++ b/raven/raven_messaging/doctype/raven_message/raven_message.py @@ -3,9 +3,11 @@ import frappe from frappe import _ from frappe.model.document import Document +from frappe.utils import strip_html_tags import json from raven.api.raven_message import track_visit + class RavenMessage(Document): # begin: auto-generated types # This code is auto-generated. Do not modify anything in this block. @@ -16,6 +18,7 @@ class RavenMessage(Document): from frappe.types import DF channel_id: DF.Link + content: DF.LongText | None file: DF.Attach | None file_thumbnail: DF.Attach | None image_height: DF.Data | None @@ -91,3 +94,12 @@ def on_trash(self): def before_save(self): if frappe.db.get_value('Raven Channel', self.channel_id, 'type') != 'Private' or frappe.db.exists("Raven Channel Member", {"channel_id": self.channel_id, "user_id": frappe.session.user}): track_visit(self.channel_id) + + +def on_doctype_update(): + ''' + Add indexes to Raven Message table + ''' + # Index the selector (channel or message type) first for faster queries (less rows to sort in the next step) + frappe.db.add_index("Raven Message", ["channel_id", "creation"]) + frappe.db.add_index("Raven Message", ["message_type", "creation"])