From 71f80b26183c6cf8e51321ed2d9a5a7d1efba84c Mon Sep 17 00:00:00 2001 From: ennucore Date: Tue, 21 Jun 2022 22:11:01 +0300 Subject: [PATCH] Getting communities for a user, started writing meeting requests --- README.md | 2 -- community.py | 9 ++++++++- templates.txt | 15 +++++++++++++-- user.py | 13 +++++++++++++ views.py | 15 +++++++++++++++ 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 07a91b4..8b1d1c9 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,6 @@ Random meetings for chats, kind of like RandomCoffee. 4. The bot sends the matches and links to their Telegram accounts TODO: -- Auto-scheduling: run it once a day and send meeting info - A button to request a meeting -- Add limitations to the number of meetings - Add commands like info - In templates replace "this week" with "soon" diff --git a/community.py b/community.py index 959baf7..1414c97 100644 --- a/community.py +++ b/community.py @@ -70,6 +70,12 @@ class Community: Thread(target=upd_data).start() return data + def can_user_request_a_meeting(self, user_id: int) -> bool: + # Check if there isn't a meeting in archived_meetings with its scheduling date today or yesterday + return not any( + r in [date.today(), date.today() - timedelta(days=1)] and user_id in [l, m] + for l, m, r in self.scheduled_meetings + self.archived_meetings) + def add_member(self, user_id: int, bot: Bot) -> bool: chat_member_info: ChatMember = bot.get_chat_member(self.chat_id, user_id) if chat_member_info.is_member or chat_member_info.status in ['creator', 'administrator', 'admin', 'member']: @@ -81,7 +87,8 @@ class Community: def users_to_poll(self) -> typing.List[int]: return [ user_id for user_id in self.members - if user_id not in self.polled or self.polled[user_id] <= date.today() - timedelta(days=7) + if (user_id not in self.polled or self.polled[user_id] <= date.today() - timedelta(days=7)) + and self.can_user_request_a_meeting(user_id) ] def add_answer(self, user_id: typing.Union[int, bool], answer: bool, bot: Bot): diff --git a/templates.txt b/templates.txt index bf70b0d..b18bc9d 100644 --- a/templates.txt +++ b/templates.txt @@ -45,12 +45,23 @@ Do you want to have any meetings this week{% if community.name %} in the communi ||<| meeting_info |>|| //| en |// -You have a meeting this week with {{ print_user_link(person) }} from {{ community.name }}. +You have a meeting with {{ print_user_link(person) }} from {{ community.name }}. You should meet in person or online. It is recommended to contact your partner in advance //| ru |// -На этой неделе у Вас встреча с {{ print_user_link(person) }} из {{ community.name }}. +У Вас встреча с {{ print_user_link(person) }} из {{ community.name }}. Вам предлагается встретиться лично за чашкой кофе чая. Рекомендуем написать человеку заранее +||<| request_meeting |>|| +//| en |// +What community do you want to have a meeting in? +{% for id, name in comm_list %} +>>| {{ name }} :-: request_meeting_{{ id }} |<< +{% endfor %} +//| ru |// +В каком сообществе вы хотите встретиться? +{% for id, name in comm_list %} +>>| {{ name }} :-: request_meeting_{{ id }} |<< +{% endfor %} ||<| canceled |>|| //| en |// diff --git a/user.py b/user.py index 5bde030..d0ca572 100644 --- a/user.py +++ b/user.py @@ -3,6 +3,10 @@ from dataclasses import dataclass, field, asdict import typing import time +if typing.TYPE_CHECKING: + from bot import Bot + from community import Community + @dataclass class User: @@ -38,3 +42,12 @@ class User: def is_admin(self) -> bool: return self.user_id in [218952152] + + def get_communities(self, bot: Bot) -> typing.Iterable[Community]: + """ + Returns a list of community ids for which the user is a member + + Perform a MongoDB query to get the list of communities for which the user is a member + """ + for community in bot.db.communities.find({'members': self.user_id}): + yield Community.from_dict(community) diff --git a/views.py b/views.py index ee3fc48..712c1b5 100644 --- a/views.py +++ b/views.py @@ -23,6 +23,21 @@ def views(bot: Bot): def handle_help(msg, _user, _args): bot.reply_with_template(msg, 'help') + @bot.handle_commands(['/request', '📝 Request a meeting', '📝 Запросить встречу']) + def request_meeting(msg, user, _args): + comm_ids_and_names = [(community.chat_id, community.name) for community in user.communities] + bot.reply_with_template(msg, 'request_meeting', comm_list=comm_ids_and_names) + + @bot.handle_callback('request_meeting') + def request_meeting_callback(msg, user, args): + community = Community.by_id(int(args), bot) + if community.can_user_request_a_meeting(user.user_id): + # todo: request the meeting and write the template + bot.reply_with_template(msg, 'request_meeting_success', community=community) + else: + bot.reply_with_template(msg, 'request_meeting_failure', community=community) + bot.answer_callback_query(msg.id) + @bot.handle_commands(['/send_all']) def send_spam(msg, user, _args): if not user.is_admin():