From 5c0b80405cacbf0d2321ed9fe3ab76ba8825b331 Mon Sep 17 00:00:00 2001 From: terrtia Date: Tue, 9 Jul 2024 17:15:27 +0200 Subject: [PATCH] chg: [chats explorer] filter Chats that don't contain messages --- bin/importer/feeders/abstract_chats_feeder.py | 3 ++ bin/lib/chats_viewer.py | 28 ++++++++++++++++++- bin/lib/objects/Chats.py | 6 ++++ bin/lib/objects/abstract_chat_object.py | 3 ++ update/v5.7/Update.py | 4 ++- 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/bin/importer/feeders/abstract_chats_feeder.py b/bin/importer/feeders/abstract_chats_feeder.py index 3b71a72e..9d48c5d0 100755 --- a/bin/importer/feeders/abstract_chats_feeder.py +++ b/bin/importer/feeders/abstract_chats_feeder.py @@ -229,12 +229,15 @@ class AbstractChatFeeder(DefaultFeeder, ABC): if meta.get('subchannel'): subchannel, thread = self.process_subchannel(obj, date, timestamp, reply_id=reply_id) chat.add_children(obj_global_id=subchannel.get_global_id()) + if obj.type == 'message': + chat.add_chat_with_messages() else: if obj.type == 'message': if self.get_thread_id(): thread = self.process_thread(obj, chat, date, timestamp, reply_id=reply_id) else: chat.add_message(obj.get_global_id(), self.get_message_id(), timestamp, reply_id=reply_id) + chat.add_chat_with_messages() chats_obj = [chat] if subchannel: diff --git a/bin/lib/chats_viewer.py b/bin/lib/chats_viewer.py index 0654d9e9..45a72fe3 100755 --- a/bin/lib/chats_viewer.py +++ b/bin/lib/chats_viewer.py @@ -180,6 +180,11 @@ class ChatServiceInstance: meta['chats'] = [] for chat_id in self.get_chats(): meta['chats'].append(Chats.Chat(chat_id, self.uuid).get_meta({'created_at', 'icon', 'nb_subchannels', 'nb_messages'})) + if 'chats_with_messages': + meta['chats'] = [] + for chat_id in self.get_chats_with_messages(): + meta['chats'].append( + Chats.Chat(chat_id, self.uuid).get_meta({'created_at', 'icon', 'nb_subchannels', 'nb_messages'})) return meta def get_nb_chats(self): @@ -188,6 +193,9 @@ class ChatServiceInstance: def get_chats(self): return Chats.Chats().get_ids_by_subtype(self.uuid) + def get_chats_with_messages(self): + return Chats.Chats().get_ids_with_messages_by_subtype(self.uuid) + def get_chat_service_instances(): return r_obj.smembers(f'chatSerIns:all') @@ -583,6 +591,23 @@ def fix_correlations_subchannel_message(): _, _, message_id = mess[0].split(':', ) subchannel.add_correlation('message', '', message_id) +def fix_chats_with_messages(): + for instance_uuid in get_chat_service_instances(): + for chat_id in ChatServiceInstance(instance_uuid).get_chats(): + chat = Chats.Chat(chat_id, instance_uuid) + + messages = chat.get_nb_messages() + if messages > 0: + chat.add_chat_with_messages() + continue + + for subchannel_gid in chat.get_subchannels(): + _, _, subchannel_id = subchannel_gid.split(':', 2) + subchannel = ChatSubChannels.ChatSubChannel(subchannel_id, instance_uuid) + if subchannel.get_nb_messages() > 0: + chat.add_chat_with_messages() + break + #### API #### def get_chat_user_account_label(chat_gid): @@ -636,7 +661,8 @@ def api_get_chat_service_instance(chat_instance_uuid): chat_instance = ChatServiceInstance(chat_instance_uuid) if not chat_instance.exists(): return {"status": "error", "reason": "Unknown uuid"}, 404 - return chat_instance.get_meta({'chats'}), 200 + # return chat_instance.get_meta({'chats'}), 200 + return chat_instance.get_meta({'chats_with_messages'}), 200 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) diff --git a/bin/lib/objects/Chats.py b/bin/lib/objects/Chats.py index 941228ce..f1ffa21f 100755 --- a/bin/lib/objects/Chats.py +++ b/bin/lib/objects/Chats.py @@ -203,6 +203,12 @@ class Chats(AbstractChatObjects): def __init__(self): super().__init__('chat') + def get_ids_with_messages_by_subtype(self, subtype): + return r_object.smembers(f'{self.type}_w_mess:{subtype}') + + # def get_ids_with_messages_iter(self, subtype): + # return sscan_iter(r_object, f'{self.type}_w_mess:{subtype}') + # TODO factorize def get_all_subtypes(): return ail_core.get_object_all_subtypes('chat') diff --git a/bin/lib/objects/abstract_chat_object.py b/bin/lib/objects/abstract_chat_object.py index 76641857..b5562fb2 100755 --- a/bin/lib/objects/abstract_chat_object.py +++ b/bin/lib/objects/abstract_chat_object.py @@ -284,6 +284,9 @@ class AbstractChatObject(AbstractSubtypeObject, ABC): objs_global_id.append(obj_global_id) return objs_global_id + def add_chat_with_messages(self): + r_object.sadd(f'{self.type}_w_mess:{self.subtype}', self.id) + def add_message(self, obj_global_id, message_id, timestamp, reply_id=None): r_object.hset(f'messages:ids:{self.type}:{self.subtype}:{self.id}', message_id, obj_global_id) r_object.zadd(f'messages:{self.type}:{self.subtype}:{self.id}', {obj_global_id: float(timestamp)}) diff --git a/update/v5.7/Update.py b/update/v5.7/Update.py index 06f720fb..54574188 100755 --- a/update/v5.7/Update.py +++ b/update/v5.7/Update.py @@ -13,7 +13,7 @@ sys.path.append(os.environ['AIL_HOME']) from update.bin.ail_updater import AIL_Updater from lib import ail_users from lib.ConfigLoader import ConfigLoader - +from lib import chats_viewer class Updater(AIL_Updater): """default Updater.""" @@ -33,6 +33,8 @@ if __name__ == '__main__': r_serv_db.hset(f'ail:user:metadata:{user_id}', 'created_at', date) r_serv_db.hset(f'ail:user:metadata:{user_id}', 'last_edit', date) + chats_viewer.fix_chats_with_messages() + updater = Updater('v5.7') updater.run_update()