33from aiogram import F , types
44from aiogram .filters .state import StateFilter
55from aiogram .fsm .context import FSMContext
6+ from sqlalchemy import select
67from sqlalchemy .ext .asyncio import AsyncSession
78
9+ from app .business .dating_service import send_user_like_alert
810from app .business .profile_service import complaint_to_profile , send_profile_with_dist
911from app .constans import EFFECTS_DICTIONARY
1012from app .handlers .common .start import start_command
1416from app .states .default import LikeResponse
1517from app .text import message_text as mt
1618from database .models import UserModel
19+ from database .models .match import MatchModel , MatchStatus
1720from database .services import Match , Profile , User
1821from loader import bot
1922
2023
24+ async def _send_mutual_like_notifications (session : AsyncSession , user : UserModel ) -> None :
25+ """
26+ Отправляет уведомления о взаимных лайках для матчей со статусом Accepted,
27+ которые исходят от пользователя
28+ """
29+ effect_id = EFFECTS_DICTIONARY ["🎉" ]
30+
31+ # Найти все матчи со статусом Accepted, где пользователь является отправителем
32+ result = await session .execute (
33+ select (MatchModel )
34+ .where (MatchModel .sender_id == user .id )
35+ .where (MatchModel .status == MatchStatus .Accepted )
36+ .where (MatchModel .is_active == True )
37+ )
38+ accepted_matches = result .scalars ().all ()
39+ for match in accepted_matches :
40+ try :
41+ # Получаем профиль получателя
42+ receiver = await User .get_with_profile (session , match .receiver_id )
43+ await send_profile_with_dist (session = session , user = user , profile = receiver .profile )
44+ if receiver and receiver .profile :
45+ # Генерируем ссылку и текст
46+ link = generate_user_link (id = receiver .id , username = receiver .username )
47+ text = mt .LIKE_ACCEPT (user .language ).format (
48+ link , html .escape (receiver .profile .name )
49+ )
50+
51+ # Отправляем уведомление
52+ await bot .send_message (
53+ chat_id = user .id , text = text , message_effect_id = effect_id , parse_mode = "HTML"
54+ )
55+
56+ # Деактивируем матч, чтобы не отправлять уведомление повторно
57+ await Match .update (session = session , id = match .id , is_active = False )
58+
59+ except Exception as e :
60+ pass
61+
62+
2163@dating_router .message (StateFilter (None ), F .text == "📭" )
2264async def match_archive (
2365 message : types .Message , state : FSMContext , user : UserModel , session : AsyncSession
@@ -30,18 +72,21 @@ async def match_archive(
3072 username = message .from_user .username ,
3173 ) # needs to be redone
3274
75+ # Проверяем и отправляем уведомления о взаимных лайках
76+ await _send_mutual_like_notifications (session , user )
77+
3378 if liker_ids := await Match .get_user_matchs (session , message .from_user .id ):
34- text = mt .ARCHIVE_SEARCH .format (len (liker_ids ))
79+ text = mt .ARCHIVE_SEARCH ( user . language ) .format (len (liker_ids ))
3580 await message .answer (text = text , reply_markup = match_kb )
3681
3782 await state .update_data (ids = liker_ids )
3883 profile = await Profile .get (session , liker_ids [0 ])
3984 match_data = await Match .get (session , user .id , profile .id )
4085 await send_profile_with_dist (user = user , profile = profile , session = session )
4186 if match_data and match_data .message :
42- await message .answer (mt .MESSAGE_TO_YOU .format (match_data .message ))
87+ await message .answer (mt .MESSAGE_TO_YOU ( user . language ) .format (match_data .message ))
4388 else :
44- await message .answer (mt .LIKE_ARCHIVE )
89+ await message .answer (mt .LIKE_ARCHIVE ( user . language ) )
4590 await start_command (message = message , user = user , state = state )
4691
4792
@@ -56,19 +101,31 @@ async def _match_atchive_callback(
56101 id = user .id ,
57102 username = callback .from_user .username ,
58103 ) # needs to be redone
59- await callback .message .answer (text = mt .SEARCH , reply_markup = match_kb )
104+ await callback .message .answer (text = mt .SEARCH ( user . language ) , reply_markup = match_kb )
60105 await callback .answer ()
61106
107+ # Проверяем и отправляем уведомления о взаимных лайках
108+ await _send_mutual_like_notifications (session , user )
109+
62110 if liker_ids := await Match .get_user_matchs (session , callback .from_user .id ):
63111 await state .update_data (ids = liker_ids )
64112 profile = await Profile .get (session , liker_ids [0 ])
65113 match_data = await Match .get (session , user .id , profile .id )
66114 await send_profile_with_dist (user = user , profile = profile , session = session )
67115 if match_data and match_data .message :
68- await callback .message .answer (mt .MESSAGE_TO_YOU .format (match_data .message ))
116+ await callback .message .answer (
117+ mt .MESSAGE_TO_YOU (user .language ).format (match_data .message )
118+ )
69119 else :
70- await callback .message .answer (mt .LIKE_ARCHIVE )
71- await start_command (callback .message , user = user , state = state )
120+ # from app.keyboards.default.base import menu_kb
121+
122+ await callback .message .answer (mt .LIKE_ARCHIVE (user .language ))
123+ # await state.clear()
124+ # await callback.message.answer(
125+ # mt.MENU,
126+ # reply_markup=menu_kb,
127+ # )
128+ await start_command (message = callback .message , user = user , state = state )
72129
73130
74131@dating_router .message (
@@ -82,27 +139,17 @@ async def _match_response(
82139 ids = data .get ("ids" )
83140
84141 another_user = await User .get_with_profile (session , ids [0 ])
142+ match_data = await Match .get (session , user .id , another_user .id )
85143
86144 if message .text == "❤️" :
87- effect_id = EFFECTS_DICTIONARY ["🎉" ]
88145 """Отправка пользователю который ответил на лайк"""
89- link = generate_user_link (id = another_user .id , username = another_user .username )
90- text = mt .LIKE_ACCEPT (another_user .language ).format (
91- link , html .escape (another_user .profile .name )
92- )
93- try :
94- await bot .send_message (chat_id = user .id , text = text , message_effect_id = effect_id )
95- except :
96- ...
97- """Отправка пользователю которому ответили на лайк"""
98- link = generate_user_link (id = user .id , username = user .username )
99- text = mt .LIKE_ACCEPT_ALERT (user .language ).format (link , html .escape (user .profile .name ))
100- try :
101- await bot .send_message (chat_id = another_user .id , text = text , message_effect_id = effect_id )
102- except :
103- ...
146+ await like_accept (session = session , user = user , another_user = another_user , match = match_data )
104147 elif message .text == "👎" :
105148 pass
149+ await Match .update (
150+ session = session , id = match_data .id , status = MatchStatus .Rejected , is_active = False
151+ )
152+
106153 elif message .text == "💢" :
107154 await message .answer (mt .COMPLAINT , reply_markup = compleint_kb ())
108155 return
@@ -115,8 +162,8 @@ async def _match_response(
115162 reason = message .text ,
116163 )
117164 elif message .text == "↩️" :
118- await message .answer (mt .SEARCH , reply_markup = match_kb )
119- await Match .delete (session , user .id , another_user .id )
165+ await message .answer (mt .SEARCH ( user . language ) , reply_markup = match_kb )
166+ # await Match.delete(session, user.id, another_user.id)
120167
121168 ids .pop (0 )
122169 await state .update_data (ids = ids )
@@ -125,9 +172,9 @@ async def _match_response(
125172 match_data = await Match .get (session , user .id , profile .id )
126173 await send_profile_with_dist (user = user , profile = profile , session = session )
127174 if match_data and match_data .message :
128- await message .answer (mt .MESSAGE_TO_YOU .format (match_data .message ))
175+ await message .answer (mt .MESSAGE_TO_YOU ( user . language ) .format (match_data .message ))
129176 else :
130- await message .answer (mt .EMPTY_PROFILE_SEARCH )
177+ await message .answer (mt .EMPTY_PROFILE_SEARCH ( user . language ) )
131178 await start_command (message = message , user = user , state = state )
132179
133180
@@ -140,3 +187,39 @@ def generate_user_link(id: int, username: str = None) -> str:
140187 if username :
141188 return f"https://t.me/{ username } "
142189 return f"tg://user?id={ id } "
190+
191+
192+ async def like_accept (
193+ session : AsyncSession , user : UserModel , another_user : UserModel , match : MatchModel
194+ ):
195+ effect_id = EFFECTS_DICTIONARY ["🎉" ]
196+ if match .status == MatchStatus .Accepted :
197+ # Если изначальный отправитель получил взимный лайк и зашел в inbox
198+ sender = user
199+ reciver = another_user
200+ await Match .update (session = session , id = match .id , is_active = False )
201+
202+ link = generate_user_link (id = reciver .id , username = reciver .username )
203+ text = mt .LIKE_ACCEPT (sender .language ).format (link , html .escape (reciver .profile .name ))
204+ try :
205+ await bot .send_message (
206+ chat_id = sender .id , text = text , message_effect_id = effect_id , parse_mode = "HTML"
207+ )
208+ except :
209+ ...
210+
211+ else :
212+ # Если изначальный отправитель не этот же человек
213+ sender = another_user
214+ reciver = user
215+ await Match .update (session = session , id = match .id , status = MatchStatus .Accepted )
216+ await send_user_like_alert (session , sender )
217+
218+ link = generate_user_link (id = sender .id , username = sender .username )
219+ text = mt .LIKE_ACCEPT (reciver .language ).format (link , html .escape (sender .profile .name ))
220+ try :
221+ await bot .send_message (
222+ chat_id = reciver .id , text = text , message_effect_id = effect_id , parse_mode = "HTML"
223+ )
224+ except :
225+ ...
0 commit comments