From 50bfd92105f37a6af6032da1831fed4f24b09fe0 Mon Sep 17 00:00:00 2001 From: terrtia Date: Tue, 13 Feb 2024 16:13:18 +0100 Subject: [PATCH] chg: [chat] add endpoints to download chat, subchannel and thread, + fix message translated by default --- bin/lib/chats_viewer.py | 39 +++++++++++++++++++++ bin/lib/objects/Messages.py | 2 +- bin/lib/objects/abstract_chat_object.py | 10 +++--- bin/lib/objects/abstract_object.py | 2 +- var/www/blueprints/chats_explorer.py | 45 +++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 6 deletions(-) diff --git a/bin/lib/chats_viewer.py b/bin/lib/chats_viewer.py index 797a9ed8..07cd806f 100755 --- a/bin/lib/chats_viewer.py +++ b/bin/lib/chats_viewer.py @@ -407,6 +407,45 @@ def api_get_user_account(user_id, instance_uuid, translation_target=None): meta = user_account.get_meta({'chats', 'icon', 'info', 'subchannels', 'threads', 'translation', 'username', 'username_meta'}, translation_target=translation_target) return meta, 200 +def api_download_chat(chat_id, subtype): + chat = Chats.Chat(chat_id, subtype) + if not chat.exists(): + return {"status": "error", "reason": "Unknown chat"}, 404 + meta = chat.get_meta({'created_at', 'info', 'nb_participants', 'subchannels', 'threads', 'username'}) # 'icon' 'translation' + if meta['username']: + meta['username'] = get_username_meta_from_global_id(meta['username']) + if meta['subchannels']: + meta['subchannels'] = get_subchannels_meta_from_global_id(meta['subchannels']) + else: + options = {'content', 'files-names', 'images', 'link', 'parent', 'parent_meta', 'reactions', 'thread', 'user-account'} + meta['messages'], _, _ = chat.get_messages(nb=-1, options=options) + return meta, 200 + +def api_download_subchannel(subchannel_id, subtype): + subchannel = ChatSubChannels.ChatSubChannel(subchannel_id, subtype) + if not subchannel.exists(): + return {"status": "error", "reason": "Unknown subchannel"}, 404 + meta = subchannel.get_meta( + {'chat', 'created_at', 'nb_messages', 'nb_participants', 'threads'}) + if meta['chat']: + meta['chat'] = get_chat_meta_from_global_id(meta['chat']) + if meta.get('threads'): + meta['threads'] = get_threads_metas(meta['threads']) + if meta.get('username'): + meta['username'] = get_username_meta_from_global_id(meta['username']) + options = {'content', 'files-names', 'images', 'link', 'parent', 'parent_meta', 'reactions', 'thread', 'user-account'} + meta['messages'], _, _ = subchannel.get_messages(nb=-1, options=options) + return meta, 200 + +def api_download_thread(thread_id, subtype): + thread = ChatThreads.ChatThread(thread_id, subtype) + if not thread.exists(): + return {"status": "error", "reason": "Unknown thread"}, 404 + meta = thread.get_meta({'chat', 'nb_messages', 'nb_participants'}) + options = {'content', 'files-names', 'images', 'link', 'parent', 'parent_meta', 'reactions', 'thread', 'user-account'} + meta['messages'], _, _ = thread.get_messages(nb=-1, options=options) + return meta, 200 + # # # # # # # # # # LATER # # # ChatCategory # diff --git a/bin/lib/objects/Messages.py b/bin/lib/objects/Messages.py index a88eb6da..e01143ec 100755 --- a/bin/lib/objects/Messages.py +++ b/bin/lib/objects/Messages.py @@ -236,7 +236,7 @@ class Message(AbstractObject): # return r_object.hget(f'meta:item::{self.id}', 'url') # options: set of optional meta fields - def get_meta(self, options=None, timestamp=None, translation_target='en'): + def get_meta(self, options=None, timestamp=None, translation_target=''): """ :type options: set :type timestamp: float diff --git a/bin/lib/objects/abstract_chat_object.py b/bin/lib/objects/abstract_chat_object.py index 1073008f..79e4361e 100755 --- a/bin/lib/objects/abstract_chat_object.py +++ b/bin/lib/objects/abstract_chat_object.py @@ -197,12 +197,14 @@ class AbstractChatObject(AbstractSubtypeObject, ABC): week_date = Date.get_current_week_day() return self.get_nb_message_by_week(week_date) - def get_message_meta(self, message, timestamp=None, translation_target='en'): # TODO handle file message + def get_message_meta(self, message, timestamp=None, translation_target='', options=None): # TODO handle file message message = Messages.Message(message[9:]) - meta = message.get_meta(options={'content', 'files-names', 'images', 'link', 'parent', 'parent_meta', 'reactions', 'thread', 'translation', 'user-account'}, timestamp=timestamp, translation_target=translation_target) + if not options: + options = {'content', 'files-names', 'images', 'link', 'parent', 'parent_meta', 'reactions', 'thread', 'translation', 'user-account'} + meta = message.get_meta(options=options, timestamp=timestamp, translation_target=translation_target) return meta - def get_messages(self, start=0, page=-1, nb=500, unread=False, translation_target='en'): # threads ???? # TODO ADD last/first message timestamp + return page + def get_messages(self, start=0, page=-1, nb=500, unread=False, options=None, translation_target='en'): # threads ???? # TODO ADD last/first message timestamp + return page # TODO return message meta tags = {} messages = {} @@ -224,7 +226,7 @@ class AbstractChatObject(AbstractSubtypeObject, ABC): if date_day != curr_date: messages[date_day] = [] curr_date = date_day - mess_dict = self.get_message_meta(message[0], timestamp=timestamp, translation_target=translation_target) + mess_dict = self.get_message_meta(message[0], timestamp=timestamp, translation_target=translation_target, options=options) messages[date_day].append(mess_dict) if mess_dict.get('tags'): diff --git a/bin/lib/objects/abstract_object.py b/bin/lib/objects/abstract_object.py index 64697b1e..1a794614 100755 --- a/bin/lib/objects/abstract_object.py +++ b/bin/lib/objects/abstract_object.py @@ -72,7 +72,7 @@ class AbstractObject(ABC): 'type': self.get_type(), 'subtype': self.get_subtype(r_str=True)} if tags: - dict_meta['tags'] = self.get_tags() + dict_meta['tags'] = self.get_tags(r_list=True) if link: dict_meta['link'] = self.get_link() return dict_meta diff --git a/var/www/blueprints/chats_explorer.py b/var/www/blueprints/chats_explorer.py index ef385b44..5b834128 100644 --- a/var/www/blueprints/chats_explorer.py +++ b/var/www/blueprints/chats_explorer.py @@ -158,6 +158,51 @@ def chats_explorer_chat_participants(): meta = meta[0] return render_template('chat_participants.html', meta=meta, bootstrap_label=bootstrap_label) + +@chats_explorer.route("/chats/explorer/chat/download", methods=['GET']) +@login_required +@login_read_only +def chats_explorer_chat_download(): + chat_id = request.args.get('id') + chat_subtype = request.args.get('uuid') + chat = chats_viewer.api_download_chat(chat_id, chat_subtype) + if chat[1] != 200: + if chat[1] == 404: + abort(404) + else: + return create_json_response(chat[0], chat[1]) + else: + return jsonify(chat[0]) + +@chats_explorer.route("/chats/explorer/subchannel/download", methods=['GET']) +@login_required +@login_read_only +def objects_subchannel_messages_download(): + subchannel_id = request.args.get('id') + instance_uuid = request.args.get('uuid') + subchannel = chats_viewer.api_download_subchannel(subchannel_id, instance_uuid) + if subchannel[1] != 200: + return create_json_response(subchannel[0], subchannel[1]) + else: + return jsonify(subchannel[0]) + + +@chats_explorer.route("/chats/explorer/thread/download", methods=['GET']) +@login_required +@login_read_only +def objects_thread_messages_download(): + thread_id = request.args.get('id') + instance_uuid = request.args.get('uuid') + thread = chats_viewer.api_download_thread(thread_id, instance_uuid) + if thread[1] != 200: + return create_json_response(thread[0], thread[1]) + else: + return jsonify(thread[0]) + + +#### #### + + @chats_explorer.route("/objects/message", methods=['GET']) @login_required @login_read_only