From 21642fe9d42048eb8cfcb136cbfdbcf84d6e91be Mon Sep 17 00:00:00 2001 From: terrtia Date: Mon, 13 May 2024 10:51:46 +0200 Subject: [PATCH] chg: [user message] show user messages by chat --- bin/lib/chats_viewer.py | 55 +++++++- bin/lib/objects/UsersAccount.py | 1 + var/www/blueprints/chats_explorer.py | 20 +++ .../chats_explorer/basic_card_chat.html | 2 +- .../chats_explorer/user_account.html | 2 +- .../chats_explorer/user_chat_messages.html | 133 ++++++++++++++++++ 6 files changed, 207 insertions(+), 6 deletions(-) create mode 100644 var/www/templates/chats_explorer/user_chat_messages.html diff --git a/bin/lib/chats_viewer.py b/bin/lib/chats_viewer.py index 8e47ef8b..33fb729a 100755 --- a/bin/lib/chats_viewer.py +++ b/bin/lib/chats_viewer.py @@ -328,6 +328,31 @@ def get_username_meta_from_global_id(username_global_id): username = Usernames.Username(username_id, instance_uuid) return username.get_meta(options={'icon'}) +############################################################################### +# TODO Pagination +def list_messages_to_dict(l_messages_id, translation_target=None): + options = {'content', 'files-names', 'images', 'language', 'link', 'parent', 'parent_meta', 'reactions', 'thread', 'translation', 'user-account'} + meta = {} + curr_date = None + for mess_id in l_messages_id: + message = Messages.Message(mess_id[1:]) + timestamp = message.get_timestamp() + date_day = message.get_date() + date_day = f'{date_day[0:4]}/{date_day[4:6]}/{date_day[6:8]}' + if date_day != curr_date: + meta[date_day] = [] + curr_date = date_day + meta_mess = message.get_meta(options=options, timestamp=timestamp, translation_target=translation_target) + meta[date_day].append(meta_mess) + + # if mess_dict.get('tags'): + # for tag in mess_dict['tags']: + # if tag not in tags: + # tags[tag] = 0 + # tags[tag] += 1 + # return messages, pagination, tags + return meta + # TODO Filter ## Instance type ## Chats IDS @@ -354,7 +379,7 @@ def get_messages_iterator(filters={}): # threads for threads in chat.get_threads(): thread = ChatThreads.ChatThread(threads['id'], instance_uuid) - _, _ = thread._get_messages(nb=-1) + messages, _ = thread._get_messages(nb=-1) for mess in messages: message_id, _, message_id = mess[0].split(':', ) yield Messages.Message(message_id) @@ -404,6 +429,15 @@ def get_user_account_chats_meta(user_id, chats, subchannels): meta.append(chat_meta) return meta +def get_user_account_chat_message(user_id, subtype, chat_id): # TODO subchannel + threads ... + meta = {} + chat = Chats.Chat(chat_id, subtype) + chat_meta = chat.get_meta(options={'icon', 'info', 'nb_participants', 'tags_safe', 'username'}) + if chat_meta['username']: + chat_meta['username'] = get_username_meta_from_global_id(chat_meta['username']) + + meta['messages'] = list_messages_to_dict(chat.get_user_messages(user_id), translation_target=None) + return meta def get_user_account_nb_all_week_messages(user_id, chats, subchannels): week = {} @@ -487,12 +521,12 @@ def api_get_chat_service_instance(chat_instance_uuid): return {"status": "error", "reason": "Unknown uuid"}, 404 return chat_instance.get_meta({'chats'}), 200 -def api_get_chat(chat_id, chat_instance_uuid, translation_target=None, nb=-1, page=-1): +def api_get_chat(chat_id, chat_instance_uuid, translation_target=None, nb=-1, page=-1, messages=True): chat = Chats.Chat(chat_id, chat_instance_uuid) if not chat.exists(): return {"status": "error", "reason": "Unknown chat"}, 404 # print(chat.get_obj_language_stats()) - meta = chat.get_meta({'created_at', 'icon', 'info', 'nb_participants', 'subchannels', 'threads', 'translation', 'username'}, translation_target=translation_target) + meta = chat.get_meta({'created_at', 'icon', 'info', 'nb_participants', 'subchannels', 'tags_safe', 'threads', 'translation', 'username'}, translation_target=translation_target) if meta['username']: meta['username'] = get_username_meta_from_global_id(meta['username']) if meta['subchannels']: @@ -500,7 +534,8 @@ def api_get_chat(chat_id, chat_instance_uuid, translation_target=None, nb=-1, pa else: if translation_target not in Language.get_translation_languages(): translation_target = None - meta['messages'], meta['pagination'], meta['tags_messages'] = chat.get_messages(translation_target=translation_target, nb=nb, page=page) + if messages: + meta['messages'], meta['pagination'], meta['tags_messages'] = chat.get_messages(translation_target=translation_target, nb=nb, page=page) return meta, 200 def api_get_nb_message_by_week(chat_type, chat_instance_uuid, chat_id): @@ -602,6 +637,18 @@ def api_get_user_account(user_id, instance_uuid, translation_target=None): meta['chats'] = get_user_account_chats_meta(user_id, meta['chats'], meta['subchannels']) return meta, 200 +def api_get_user_account_chat_messages(user_id, instance_uuid, chat_id, translation_target=None): + user_account = UsersAccount.UserAccount(user_id, instance_uuid) + if not user_account.exists(): + return {"status": "error", "reason": "Unknown user-account"}, 404 + meta = get_user_account_chat_message(user_id, instance_uuid, chat_id) + meta['user-account'] = user_account.get_meta({'icon', 'info', 'translation', 'username', 'username_meta'}, translation_target=translation_target) + resp = api_get_chat(chat_id, instance_uuid, translation_target=translation_target, messages=False) + if resp[1] != 200: + return resp + meta['chat'] = resp[0] + return meta, 200 + def api_get_user_account_nb_all_week_messages(user_id, instance_uuid): user_account = UsersAccount.UserAccount(user_id, instance_uuid) if not user_account.exists(): diff --git a/bin/lib/objects/UsersAccount.py b/bin/lib/objects/UsersAccount.py index f9b09b48..f16a487f 100755 --- a/bin/lib/objects/UsersAccount.py +++ b/bin/lib/objects/UsersAccount.py @@ -157,6 +157,7 @@ class UserAccount(AbstractSubtypeObject): meta['usernames'] = self.get_usernames() if 'icon' in options: meta['icon'] = self.get_icon() + meta['svg_icon'] = meta['icon'] if 'info' in options: meta['info'] = self.get_info() if 'translation' in options and translation_target: diff --git a/var/www/blueprints/chats_explorer.py b/var/www/blueprints/chats_explorer.py index 6d8d2091..5a1f9b8a 100644 --- a/var/www/blueprints/chats_explorer.py +++ b/var/www/blueprints/chats_explorer.py @@ -297,6 +297,26 @@ def objects_user_account(): ail_tags=Tag.get_modal_add_tags(user_account['id'], user_account['type'], user_account['subtype']), translation_languages=languages, translation_target=target) +@chats_explorer.route("/objects/user-account/chat", methods=['GET']) +@login_required +@login_read_only +def objects_user_account_chat(): + instance_uuid = request.args.get('subtype') + user_id = request.args.get('id') + chat_id = request.args.get('chat_id') + target = request.args.get('target') + if target == "Don't Translate": + target = None + meta = chats_viewer.api_get_user_account_chat_messages(user_id, instance_uuid, chat_id, translation_target=target) + if meta[1] != 200: + return create_json_response(meta[0], meta[1]) + else: + meta = meta[0] + languages = Language.get_translation_languages() + return render_template('chats_explorer/user_chat_messages.html', meta=meta, bootstrap_label=bootstrap_label, + ail_tags=Tag.get_modal_add_tags(meta['user-account']['id'], meta['user-account']['type'], meta['user-account']['subtype']), + translation_languages=languages, translation_target=target) + @chats_explorer.route("objects/user-account/messages/stats/week/all", methods=['GET']) @login_required @login_read_only diff --git a/var/www/templates/chats_explorer/basic_card_chat.html b/var/www/templates/chats_explorer/basic_card_chat.html index d747ff8e..4190788e 100644 --- a/var/www/templates/chats_explorer/basic_card_chat.html +++ b/var/www/templates/chats_explorer/basic_card_chat.html @@ -85,7 +85,7 @@ - {{ meta["nb_messages"] }}   + {{ meta["nb_messages"] }}   {% endif %} diff --git a/var/www/templates/chats_explorer/user_account.html b/var/www/templates/chats_explorer/user_account.html index 188779b2..32a61718 100644 --- a/var/www/templates/chats_explorer/user_account.html +++ b/var/www/templates/chats_explorer/user_account.html @@ -45,7 +45,7 @@

User Chats:

{% for meta_chats in meta['chats'] %}
- {% with meta=meta_chats %} + {% with meta=meta_chats,main_obj_id=meta['id'] %} {% include 'chats_explorer/basic_card_chat.html' %} {% endwith %}
diff --git a/var/www/templates/chats_explorer/user_chat_messages.html b/var/www/templates/chats_explorer/user_chat_messages.html new file mode 100644 index 00000000..d79931bc --- /dev/null +++ b/var/www/templates/chats_explorer/user_chat_messages.html @@ -0,0 +1,133 @@ + + + + + User Account - AIL + + + + + + + + + + + + + + + + + + + + + {% include 'nav_bar.html' %} + +
+
+ + {% include 'sidebars/sidebar_objects.html' %} + +
+ +

User:

+ {% with meta=meta['user-account'] %} + {% include 'chats_explorer/card_user_account.html' %} + {% endwith %} + +

Chat:

+ {% with meta=meta['chat'] %} + {% include 'chats_explorer/basic_card_chat.html' %} + {% endwith %} + +
+ {% with translate_url=url_for('chats_explorer.objects_user_account', subtype=meta['subtype']), obj_id=meta['id'] %} + {% include 'chats_explorer/block_translation.html' %} + {% endwith %} + {% include 'objects/image/block_blur_img_slider.html' %} +
+ + +
+
+ + {% for date in meta['messages'] %} + +
+

+ {{ date }} +

+
+ + {% for mess in meta['messages'][date] %} + + {% with message=mess %} + {% include 'chats_explorer/block_message.html' %} + {% endwith %} + + {% endfor %} +
+ {% endfor %} + +
+
+ + + +{# {% if meta['chats'] %}#} +{#

User All Messages:

#} +{#
#} +{##} +{#

User Chats:

#} +{# {% for meta_chats in meta['chats'] %}#} +{#
#} +{# {% with meta=meta_chats %}#} +{# {% include 'chats_explorer/basic_card_chat.html' %}#} +{# {% endwith %}#} +{#
#} +{# {% endfor %}#} +{# {% endif %}#} + + +
+ +
+
+ + + + + + + + + +