chg: [chat] add endpoints to download chat, subchannel and thread, + fix message translated by default

dev
terrtia 2024-02-13 16:13:18 +01:00
parent a9323e076e
commit 50bfd92105
No known key found for this signature in database
GPG Key ID: 1E1B1F50D84613D0
5 changed files with 92 additions and 6 deletions

View File

@ -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 #

View File

@ -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

View File

@ -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'):

View File

@ -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

View File

@ -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