Skip to content

Commit

Permalink
chg: [chat] add endpoints to download chat, subchannel and thread, + …
Browse files Browse the repository at this point in the history
…fix message translated by default
  • Loading branch information
Terrtia committed Feb 13, 2024
1 parent a9323e0 commit 50bfd92
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 6 deletions.
39 changes: 39 additions & 0 deletions bin/lib/chats_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
Expand Down
2 changes: 1 addition & 1 deletion bin/lib/objects/Messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def get_misp_object(self): # TODO
# 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
Expand Down
10 changes: 6 additions & 4 deletions bin/lib/objects/abstract_chat_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,14 @@ def get_nb_message_this_week(self):
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 = {}
Expand All @@ -224,7 +226,7 @@ def get_messages(self, start=0, page=-1, nb=500, unread=False, translation_targe
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'):
Expand Down
2 changes: 1 addition & 1 deletion bin/lib/objects/abstract_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def get_default_meta(self, tags=False, link=False):
'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
Expand Down
45 changes: 45 additions & 0 deletions var/www/blueprints/chats_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 50bfd92

Please sign in to comment.