From 5fc9b1403fabbafc4cbc1659f12051d0aafb2e66 Mon Sep 17 00:00:00 2001 From: terrtia Date: Mon, 11 Dec 2023 00:46:15 +0100 Subject: [PATCH] chg: [chats] add pagination --- bin/lib/chats_viewer.py | 12 +-- bin/lib/objects/abstract_chat_object.py | 49 ++++++++--- var/www/blueprints/chats_explorer.py | 12 ++- .../chats_explorer/SubChannelMessages.html | 32 +++++-- .../chats_explorer/ThreadMessages.html | 20 +++++ .../chats_explorer/block_obj_time_search.html | 85 +++++++++++++++++++ .../chats_explorer/chat_participants.html | 2 +- .../templates/chats_explorer/chat_viewer.html | 20 +++++ .../templates/chats_explorer/pagination.html | 50 +++++++++++ 9 files changed, 256 insertions(+), 26 deletions(-) create mode 100644 var/www/templates/chats_explorer/block_obj_time_search.html create mode 100644 var/www/templates/chats_explorer/pagination.html diff --git a/bin/lib/chats_viewer.py b/bin/lib/chats_viewer.py index b083537a..e1809f47 100755 --- a/bin/lib/chats_viewer.py +++ b/bin/lib/chats_viewer.py @@ -331,7 +331,7 @@ 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): +def api_get_chat(chat_id, chat_instance_uuid, translation_target=None, nb=-1, page=-1): chat = Chats.Chat(chat_id, chat_instance_uuid) if not chat.exists(): return {"status": "error", "reason": "Unknown chat"}, 404 @@ -341,7 +341,7 @@ def api_get_chat(chat_id, chat_instance_uuid, translation_target=None): if meta['subchannels']: meta['subchannels'] = get_subchannels_meta_from_global_id(meta['subchannels']) else: - meta['messages'], meta['tags_messages'] = chat.get_messages(translation_target=translation_target) + 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_id, chat_instance_uuid): @@ -367,7 +367,7 @@ def api_get_chat_participants(chat_type, chat_subtype, chat_id): meta['participants'] = chat_participants return meta, 200 -def api_get_subchannel(chat_id, chat_instance_uuid, translation_target=None): +def api_get_subchannel(chat_id, chat_instance_uuid, translation_target=None, nb=-1, page=-1): subchannel = ChatSubChannels.ChatSubChannel(chat_id, chat_instance_uuid) if not subchannel.exists(): return {"status": "error", "reason": "Unknown subchannel"}, 404 @@ -378,17 +378,17 @@ def api_get_subchannel(chat_id, chat_instance_uuid, translation_target=None): meta['threads'] = get_threads_metas(meta['threads']) if meta.get('username'): meta['username'] = get_username_meta_from_global_id(meta['username']) - meta['messages'], meta['tags_messages'] = subchannel.get_messages(translation_target=translation_target) + meta['messages'], meta['pagination'], meta['tags_messages'] = subchannel.get_messages(translation_target=translation_target, nb=nb, page=page) return meta, 200 -def api_get_thread(thread_id, thread_instance_uuid, translation_target=None): +def api_get_thread(thread_id, thread_instance_uuid, translation_target=None, nb=-1, page=-1): thread = ChatThreads.ChatThread(thread_id, thread_instance_uuid) if not thread.exists(): return {"status": "error", "reason": "Unknown thread"}, 404 meta = thread.get_meta({'chat', 'nb_messages', 'nb_participants'}) # if meta['chat']: # meta['chat'] = get_chat_meta_from_global_id(meta['chat']) - meta['messages'], meta['tags_messages'] = thread.get_messages(translation_target=translation_target) + meta['messages'], meta['pagination'], meta['tags_messages'] = thread.get_messages(translation_target=translation_target, nb=nb, page=page) return meta, 200 def api_get_message(message_id): diff --git a/bin/lib/objects/abstract_chat_object.py b/bin/lib/objects/abstract_chat_object.py index 5311cb85..1073008f 100755 --- a/bin/lib/objects/abstract_chat_object.py +++ b/bin/lib/objects/abstract_chat_object.py @@ -130,18 +130,36 @@ class AbstractChatObject(AbstractSubtypeObject, ABC): def get_nb_messages(self): return r_object.zcard(f'messages:{self.type}:{self.subtype}:{self.id}') - def _get_messages(self, nb=-1, page=1): + def _get_messages(self, nb=-1, page=-1): if nb < 1: - return r_object.zrange(f'messages:{self.type}:{self.subtype}:{self.id}', 0, -1, withscores=True) + messages = r_object.zrange(f'messages:{self.type}:{self.subtype}:{self.id}', 0, -1, withscores=True) + nb_pages = 0 + page = 1 + total = len(messages) + nb_first = 1 + nb_last = total else: + total = r_object.zcard(f'messages:{self.type}:{self.subtype}:{self.id}') + nb_pages = total / nb + if not nb_pages.is_integer(): + nb_pages = int(nb_pages) + 1 + else: + nb_pages = int(nb_pages) + if page > nb_pages or page < 1: + page = nb_pages + if page > 1: - start = page - 1 + nb + start = (page - 1) * nb else: start = 0 - messages = r_object.zrevrange(f'messages:{self.type}:{self.subtype}:{self.id}', start, start+nb-1, withscores=True) - if messages: - messages = reversed(messages) - return messages + messages = r_object.zrange(f'messages:{self.type}:{self.subtype}:{self.id}', start, start+nb-1, withscores=True) + # if messages: + # messages = reversed(messages) + nb_first = start+1 + nb_last = start+nb + if nb_last > total: + nb_last = total + return messages, {'nb': nb, 'page': page, 'nb_pages': nb_pages, 'total': total, 'nb_first': nb_first, 'nb_last': nb_last} def get_timestamp_first_message(self): return r_object.zrange(f'messages:{self.type}:{self.subtype}:{self.id}', 0, 0, withscores=True) @@ -184,12 +202,23 @@ class AbstractChatObject(AbstractSubtypeObject, ABC): meta = message.get_meta(options={'content', 'files-names', 'images', 'link', 'parent', 'parent_meta', 'reactions', 'thread', 'translation', 'user-account'}, 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, translation_target='en'): # threads ???? # TODO ADD last/first message timestamp + return page # TODO return message meta tags = {} messages = {} curr_date = None - for message in self._get_messages(nb=2000, page=1): + try: + nb = int(nb) + except TypeError: + nb = 500 + if not page: + page = -1 + try: + page = int(page) + except TypeError: + page = 1 + mess, pagination = self._get_messages(nb=nb, page=page) + for message in mess: timestamp = message[1] date_day = datetime.fromtimestamp(timestamp).strftime('%Y/%m/%d') if date_day != curr_date: @@ -203,7 +232,7 @@ class AbstractChatObject(AbstractSubtypeObject, ABC): if tag not in tags: tags[tag] = 0 tags[tag] += 1 - return messages, tags + return messages, pagination, tags # TODO REWRITE ADD OR ADD MESSAGE ???? # add diff --git a/var/www/blueprints/chats_explorer.py b/var/www/blueprints/chats_explorer.py index 54ae3d25..081df950 100644 --- a/var/www/blueprints/chats_explorer.py +++ b/var/www/blueprints/chats_explorer.py @@ -82,7 +82,9 @@ def chats_explorer_chat(): chat_id = request.args.get('id') instance_uuid = request.args.get('uuid') target = request.args.get('target') - chat = chats_viewer.api_get_chat(chat_id, instance_uuid, translation_target=target) + nb_messages = request.args.get('nb') + page = request.args.get('page') + chat = chats_viewer.api_get_chat(chat_id, instance_uuid, translation_target=target, nb=nb_messages, page=page) if chat[1] != 200: return create_json_response(chat[0], chat[1]) else: @@ -109,7 +111,9 @@ def objects_subchannel_messages(): subchannel_id = request.args.get('id') instance_uuid = request.args.get('uuid') target = request.args.get('target') - subchannel = chats_viewer.api_get_subchannel(subchannel_id, instance_uuid, translation_target=target) + nb_messages = request.args.get('nb') + page = request.args.get('page') + subchannel = chats_viewer.api_get_subchannel(subchannel_id, instance_uuid, translation_target=target, nb=nb_messages, page=page) if subchannel[1] != 200: return create_json_response(subchannel[0], subchannel[1]) else: @@ -124,7 +128,9 @@ def objects_thread_messages(): thread_id = request.args.get('id') instance_uuid = request.args.get('uuid') target = request.args.get('target') - thread = chats_viewer.api_get_thread(thread_id, instance_uuid, translation_target=target) + nb_messages = request.args.get('nb') + page = request.args.get('page') + thread = chats_viewer.api_get_thread(thread_id, instance_uuid, translation_target=target, nb=nb_messages, page=page) if thread[1] != 200: return create_json_response(thread[0], thread[1]) else: diff --git a/var/www/templates/chats_explorer/SubChannelMessages.html b/var/www/templates/chats_explorer/SubChannelMessages.html index 7ca7da41..5720acf1 100644 --- a/var/www/templates/chats_explorer/SubChannelMessages.html +++ b/var/www/templates/chats_explorer/SubChannelMessages.html @@ -126,12 +126,12 @@ - {% with obj_type='chat', obj_id=subchannel['id'], obj_subtype=subchannel['subtype'] %} - {% include 'modals/investigations_register_obj.html' %} - {% endwith %} - +{# {% with obj_type='chat', obj_id=subchannel['id'], obj_subtype=subchannel['subtype'] %}#} +{# {% include 'modals/investigations_register_obj.html' %}#} +{# {% endwith %}#} +{# #} @@ -191,6 +191,18 @@ {% with translate_url=url_for('chats_explorer.objects_subchannel_messages', uuid=subchannel['subtype']), obj_id=subchannel['id'] %} {% include 'chats_explorer/block_translation.html' %} {% endwith %} + {% with obj_subtype=subchannel['subtype'], obj_id=subchannel['id'], url_endpoint=url_for("chats_explorer.objects_subchannel_messages"), nb=subchannel['pagination']['nb'] %} + {% set date_from=subchannel['first_seen'] %} + {% set date_to=subchannel['last_seen'] %} + {% include 'block_obj_time_search.html' %} + {% endwith %} + {% with endpoint_url=url_for('chats_explorer.objects_subchannel_messages', uuid=subchannel['subtype']), pagination=subchannel['pagination'] %} + {% set endpoint_url = endpoint_url + "&id=" + subchannel['id'] + "&nb=" + subchannel['pagination']['nb'] | string %} + {% if translation_target %} + {% set endpoint_url = endpoint_url + "&target=" + translation_target %} + {% endif %} + {% include 'chats_explorer/pagination.html' %} + {% endwith %}
@@ -216,6 +228,14 @@
+ {% with endpoint_url=url_for('chats_explorer.objects_subchannel_messages', uuid=subchannel['subtype']), pagination=subchannel['pagination'] %} + {% set endpoint_url = endpoint_url + "&id=" + subchannel['id'] + "&nb=" + subchannel['pagination']['nb'] | string %} + {% if translation_target %} + {% set endpoint_url = endpoint_url + "&target=" + translation_target %} + {% endif %} + {% include 'chats_explorer/pagination.html' %} + {% endwith %} + diff --git a/var/www/templates/chats_explorer/ThreadMessages.html b/var/www/templates/chats_explorer/ThreadMessages.html index 49d26ce9..ba5a02a5 100644 --- a/var/www/templates/chats_explorer/ThreadMessages.html +++ b/var/www/templates/chats_explorer/ThreadMessages.html @@ -141,6 +141,18 @@ {% with translate_url=url_for('chats_explorer.objects_thread_messages', uuid=meta['subtype']), obj_id=meta['id'] %} {% include 'chats_explorer/block_translation.html' %} {% endwith %} + {% with obj_subtype=meta['subtype'], obj_id=meta['id'], url_endpoint=url_for("chats_explorer.objects_thread_messages"), nb=meta['pagination']['nb'] %} + {% set date_from=meta['first_seen'] %} + {% set date_to=meta['last_seen'] %} + {% include 'block_obj_time_search.html' %} + {% endwith %} + {% with endpoint_url=url_for('chats_explorer.objects_thread_messages', uuid=meta['subtype']), pagination=meta['pagination'] %} + {% set endpoint_url = endpoint_url + "&id=" + meta['id'] + "&nb=" + meta['pagination']['nb'] | string %} + {% if translation_target %} + {% set endpoint_url = endpoint_url + "&target=" + translation_target %} + {% endif %} + {% include 'chats_explorer/pagination.html' %} + {% endwith %}
@@ -166,6 +178,14 @@
+ {% with endpoint_url=url_for('chats_explorer.objects_thread_messages', uuid=meta['subtype']), pagination=meta['pagination'] %} + {% set endpoint_url = endpoint_url + "&id=" + meta['id'] + "&nb=" + meta['pagination']['nb'] | string %} + {% if translation_target %} + {% set endpoint_url = endpoint_url + "&target=" + translation_target %} + {% endif %} + {% include 'chats_explorer/pagination.html' %} + {% endwith %} + diff --git a/var/www/templates/chats_explorer/block_obj_time_search.html b/var/www/templates/chats_explorer/block_obj_time_search.html new file mode 100644 index 00000000..515a8ea7 --- /dev/null +++ b/var/www/templates/chats_explorer/block_obj_time_search.html @@ -0,0 +1,85 @@ +
+
+
Filter by Time :
+ +{#
#} +{#
#} +{#
#} +{#
#} +{# #} +{#
#} +{#
#} +{#
#} +{# #} +{#
#} +{#
#} +{#
#} +{#
#} +{#
#} +{# #} +{#
#} +{#
#} +{#
#} +{# #} +{#
#} +{#
#} +{#
#} + +
+
+
Numbers by page
+ +
+
+ + + +
+
+ + + + + diff --git a/var/www/templates/chats_explorer/chat_participants.html b/var/www/templates/chats_explorer/chat_participants.html index 3b44f291..b8e367b0 100644 --- a/var/www/templates/chats_explorer/chat_participants.html +++ b/var/www/templates/chats_explorer/chat_participants.html @@ -97,7 +97,7 @@ info First Seen Last Seen - NB Messages + diff --git a/var/www/templates/chats_explorer/chat_viewer.html b/var/www/templates/chats_explorer/chat_viewer.html index 81f7e407..41cdd3cd 100644 --- a/var/www/templates/chats_explorer/chat_viewer.html +++ b/var/www/templates/chats_explorer/chat_viewer.html @@ -164,6 +164,18 @@ {% with translate_url=url_for('chats_explorer.chats_explorer_chat', uuid=chat['subtype']), obj_id=chat['id'] %} {% include 'chats_explorer/block_translation.html' %} {% endwith %} + {% with obj_subtype=chat['subtype'], obj_id=chat['id'], url_endpoint=url_for("chats_explorer.chats_explorer_chat"), nb=chat['pagination']['nb'] %} + {% set date_from=chat['first_seen'] %} + {% set date_to=chat['last_seen'] %} + {% include 'block_obj_time_search.html' %} + {% endwith %} + {% with endpoint_url=url_for('chats_explorer.chats_explorer_chat', uuid=chat['subtype']), pagination=chat['pagination'] %} + {% set endpoint_url = endpoint_url + "&id=" + chat['id'] + "&nb=" + chat['pagination']['nb'] | string %} + {% if translation_target %} + {% set endpoint_url = endpoint_url + "&target=" + translation_target %} + {% endif %} + {% include 'chats_explorer/pagination.html' %} + {% endwith %}
@@ -189,6 +201,14 @@
+ {% with endpoint_url=url_for('chats_explorer.chats_explorer_chat', uuid=chat['subtype']), pagination=chat['pagination'] %} + {% set endpoint_url = endpoint_url + "&id=" + chat['id'] + "&nb=" + chat['pagination']['nb'] | string %} + {% if translation_target %} + {% set endpoint_url = endpoint_url + "&target=" + translation_target %} + {% endif %} + {% include 'chats_explorer/pagination.html' %} + {% endwith %} + {% endif %} diff --git a/var/www/templates/chats_explorer/pagination.html b/var/www/templates/chats_explorer/pagination.html new file mode 100644 index 00000000..0bde498d --- /dev/null +++ b/var/www/templates/chats_explorer/pagination.html @@ -0,0 +1,50 @@ +
+
+ +
+ + {%if pagination['total'] %} +
+ + results:  + {{ pagination['nb_first'] }}-{{ pagination['nb_last'] }} + / + {{ pagination['total'] }} + +
+
+
+
+ {%endif%} +