From de2140eab3e5c6401b70f62f6140e91058379ce9 Mon Sep 17 00:00:00 2001 From: darker Date: Mon, 29 Apr 2024 14:33:18 +0200 Subject: [PATCH] f/check (WIP) people around + chore(doc) updates (#9) * feat(checks): implem in wip * feat: small update on DOC * release ready for the alpha * fix: test --- README.md | 10 ++++++---- pyproject.toml | 2 +- tchaka/commands.py | 18 ++++++++++++++---- tchaka/config.py | 2 +- tchaka/core.py | 15 ++++++++++----- tchaka/utils.py | 11 +++++------ tests/test_core.py | 5 +++-- 7 files changed, 40 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 3a84de0..19b8766 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ ## TCHAKA -This bot is a simple telegram bot that makes you in contacts with people -'around you anonymously'. +This bot makes you in contact with people 'around' you anonymously based on +your localisation. + +No DATA saved + all IN MEMORY... ## HOW GET IT RUN @@ -13,7 +15,7 @@ $ cp .env.example .env $ docker build -t tchaka:latest -f ./Dockerfile . $ docker run -ti tchaka -INFO:__main__:tchaka v0.0.1 started successfully... +INFO:__main__:tchaka started successfully... INFO:telegram.ext.Application:Application started ``` @@ -29,7 +31,7 @@ $ make install $ make run # to start the bot... python -m tchaka.main -INFO:__main__:tchaka v0.0.1 started successfully... +INFO:__main__:tchaka started successfully... INFO:telegram.ext.Application:Application started ``` diff --git a/pyproject.toml b/pyproject.toml index 81f8b76..327569d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "tchaka" -version = "0.0.3" +version = "0.1.0-alpha" description = "Euphemeral chat based on the position" authors = ["sanix-darker "] license = "Apache" diff --git a/tchaka/commands.py b/tchaka/commands.py index e586326..ac176d9 100644 --- a/tchaka/commands.py +++ b/tchaka/commands.py @@ -66,17 +66,23 @@ async def start_callback(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None async def check_callback(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None: """ + TODO: not working yet, will be fixed in future version CheckCallBack to check how many people are in the area """ _, message = await get_user_and_message(update) + await append_chat_ids_messages(message.chat_id, message.message_id) + if not (given_user_name := _CHAT_IDS.get(message.chat_id)): given_user_name = "New User" + await message.reply_text( + text="Send your localisation to be put in a group first please" + ) + return - await append_chat_ids_messages(message.chat_id, message.message_id) - await message.reply_text(text="---") + await message.reply_text(text="There is ---") _LOGGER.info(f"/start :: {given_user_name=}") @@ -173,7 +179,11 @@ async def location_callback(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> N user_new_name = await build_user_hash(user.full_name) await append_chat_ids_messages(message.chat_id, message.message_id) - _USERS, _GROUPS = await populate_new_user_to_appropriate_group( + ( + _USERS, + _GROUPS, + count_user_same_group, + ) = await populate_new_user_to_appropriate_group( user_new_name=user_new_name, current_chat_id=message.chat_id, latitude=location.latitude, @@ -195,7 +205,7 @@ async def location_callback(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> N text=html_format_text( build_welcome_location_message_for_current_user( user_new_name, - _USERS, + count_user_same_group, user.language_code or "en", ) ) diff --git a/tchaka/config.py b/tchaka/config.py index e20e516..05bafe2 100644 --- a/tchaka/config.py +++ b/tchaka/config.py @@ -10,7 +10,7 @@ "fr": { "WELCOME_MESSAGE": """Bienvenue sur Tchaka! Commencez par envoyer votre localisation (aucun soucis, c est anonyme et rien -ne se sauvegardes.):""", +ne se sauvegardes)""", "HELP_MESSAGE": """/start - Pour demarrer. /help - Comment cela fonctionne. /stop - Pour stoper le bot et cleaner toutes vos infos. diff --git a/tchaka/core.py b/tchaka/core.py index e3e04bc..3b502a9 100644 --- a/tchaka/core.py +++ b/tchaka/core.py @@ -39,13 +39,15 @@ def haversine_distance(lat1: float, lon1: float, lat2: float, lon2: float) -> fl async def group_coordinates( coordinates: list[tuple[float, float]], distance_threshold: int = 100, -) -> dict[str, list]: + user_coords: tuple[float, float] | None = None, +) -> tuple[dict[str, list], int]: """ Group coordinates based on their proximity within a certain distance threshold. Returns a dictionary with group IDs as keys and lists of coordinates as values. """ + users_in_same_group = 0 groups: dict[str, list] = {} for coord in coordinates: group_found = False @@ -59,11 +61,13 @@ async def group_coordinates( ): groups[group_id].append(coord) group_found = True + if user_coords and user_coords in groups[group_id]: + users_in_same_group = len(groups[group_id]) break if not group_found: groups[f"___G-{len(groups)+1}"] = [coord] - return groups + return groups, users_in_same_group async def dispatch_msg_in_group( @@ -177,19 +181,20 @@ async def populate_new_user_to_appropriate_group( longitude: float, user_list: dict[str, Any], group_list: dict[str, Any], -) -> tuple[dict[str, Any], dict[str, Any]]: +) -> tuple[dict[str, Any], dict[str, Any], int]: """ This method set the user infos and put him in a group """ user_list[user_new_name] = [current_chat_id, (latitude, longitude)] - group_list = await group_coordinates( + group_list, user_same_group = await group_coordinates( coordinates=[user_info[1] for user_info in user_list.values()], distance_threshold=100, + user_coords=(latitude, longitude), ) - return user_list, group_list + return user_list, group_list, user_same_group async def clean_all_msg( diff --git a/tchaka/utils.py b/tchaka/utils.py index e50d8c0..25628ad 100644 --- a/tchaka/utils.py +++ b/tchaka/utils.py @@ -1,5 +1,4 @@ from functools import lru_cache -from typing import Any from telegram import Message, Update, User import logging import html @@ -57,7 +56,7 @@ async def build_user_hash(fullname: str) -> str: def build_welcome_location_message_for_current_user( user_new_name: str, - users_list: dict[str, Any], + users_list_count: int, lang: str, ) -> str: """ @@ -68,11 +67,11 @@ def build_welcome_location_message_for_current_user( if lang == "fr": suggest_to_connect = ( ( - f"Il y a ({len(users_list)-1}) personnes dans la meme zone que " + f"Il y a ({users_list_count}) personnes dans la meme zone que " "vous. ils sont avertis.\n" "N'hesitez pas a dire bonjour.\n" ) - if len(users_list) > 1 + if users_list_count > 1 else ("0 utilisateurs ici pour le moment.\n") ) final_msg = ( @@ -85,11 +84,11 @@ def build_welcome_location_message_for_current_user( else: suggest_to_connect = ( ( - f"There is ({len(users_list)-1}) people in the same area than " + f"There is ({users_list_count}) people in the same area than " "you. They just get notified.\n" "Feel free to say hi.\n" ) - if len(users_list) > 1 + if users_list_count > 1 else ("0 users here for now.\n") ) final_msg = ( diff --git a/tests/test_core.py b/tests/test_core.py index 569f9cb..cd4d973 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -35,7 +35,7 @@ async def test_group_coordinates(): (2.8205, 4.0055), (52.5200, 13.4060), ] - grouped_coordinates = await group_coordinates( + grouped_coordinates, count_u = await group_coordinates( coordinates, distance_threshold=100, ) @@ -113,7 +113,7 @@ async def test_populate_new_user_to_appropriate_group( mocker: MockType, ) -> None: mocker.patch( - "tchaka.core.group_coordinates", return_value={"group1": [(50.0, 60.0)]} + "tchaka.core.group_coordinates", return_value=({"group1": [(50.0, 60.0)]}, 1) ) new_user_name = "user1" @@ -124,6 +124,7 @@ async def test_populate_new_user_to_appropriate_group( ( updated_user_list, updated_group_list, + count_u, ) = await populate_new_user_to_appropriate_group( new_user_name, current_chat_id, latitude, longitude, {}, {} )