understanding commited on
Commit
edfaa7a
·
verified ·
1 Parent(s): 91f044c

Create user.py

Browse files
Files changed (1) hide show
  1. handlers/user.py +87 -0
handlers/user.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # filename: handlers/user.py
2
+
3
+ import logging
4
+ from telethon import events, Button
5
+ from core.bot import bot, BATCH_JOBS, ACTIVE_USER_TASKS, USER_TURN_ORDER
6
+ import templates
7
+ from database import manager as db_manager
8
+ from datetime import datetime
9
+ import config
10
+ from utils.helpers import format_bytes
11
+
12
+ logger = logging.getLogger(__name__)
13
+
14
+ @bot.on(events.NewMessage(pattern='/myaccount'))
15
+ async def myaccount_handler(event):
16
+ await process_myaccount_query(event)
17
+
18
+ @bot.on(events.CallbackQuery(data=b'my_account'))
19
+ async def myaccount_button_handler(event):
20
+ await process_myaccount_query(event)
21
+
22
+ async def process_myaccount_query(event):
23
+ """Core logic for handling myaccount requests from commands or buttons."""
24
+ user_id = event.sender_id
25
+ user = await db_manager.get_user(user_id)
26
+
27
+ if not user:
28
+ # This case is unlikely if they used /start, but good to have.
29
+ sender = await event.get_sender()
30
+ user = await db_manager.add_or_update_user(sender.id, sender.username, sender.first_name)
31
+
32
+ if user.get('is_premium') and user.get('premium_expiry_date') > datetime.utcnow():
33
+ response = templates.BotResponses.MY_ACCOUNT_PREMIUM.format(
34
+ user_id=user['user_id'],
35
+ expiry_date=user['premium_expiry_date'].strftime('%Y-%m-%d %H:%M IST')
36
+ )
37
+ else:
38
+ response = templates.BotResponses.MY_ACCOUNT_FREE.format(
39
+ user_id=user['user_id'],
40
+ free_limit=format_bytes(config.FREE_USER_FILE_SIZE_LIMIT_BYTES),
41
+ owner_id=config.OWNER_ID
42
+ )
43
+
44
+ # For callback queries, we edit the message. For commands, we reply.
45
+ if isinstance(event, events.CallbackQuery.Event):
46
+ await event.edit(response, buttons=[Button.inline("⬅️ Back", data="start_menu")])
47
+ else:
48
+ await event.reply(response)
49
+
50
+
51
+ @bot.on(events.NewMessage(pattern=r'/cancel_(\w+)'))
52
+ async def cancel_handler(event):
53
+ batch_id = event.pattern_match.group(1)
54
+ user_id = event.sender_id
55
+
56
+ job = BATCH_JOBS.get(batch_id)
57
+
58
+ if not job:
59
+ await event.reply("This job ID is invalid or has already been completed.")
60
+ return
61
+
62
+ if job['user_id'] != user_id:
63
+ await event.reply("You can only cancel your own jobs.")
64
+ return
65
+
66
+ # --- Cancellation Logic ---
67
+ # 1. Clear pending tasks for this user from the scheduler's view
68
+ if user_id in ACTIVE_USER_TASKS:
69
+ ACTIVE_USER_TASKS[user_id] = [task for task in ACTIVE_USER_TASKS[user_id] if task['batch_id'] != batch_id]
70
+ if not ACTIVE_USER_TASKS[user_id]:
71
+ del ACTIVE_USER_TASKS[user_id]
72
+ # Try to remove the user from the turn order deque
73
+ try:
74
+ USER_TURN_ORDER.remove(user_id)
75
+ except ValueError:
76
+ pass # User might not be in the deque anymore
77
+
78
+ # 2. Delete the job from the main tracking dictionary
79
+ del BATCH_JOBS[batch_id]
80
+
81
+ # 3. Inform the user
82
+ await bot.edit_message(
83
+ job['chat_id'],
84
+ job['status_message_id'],
85
+ templates.BotResponses.BATCH_CANCELLED.format(batch_id=batch_id)
86
+ )
87
+ logger.info(f"User {user_id} cancelled Batch #{batch_id}")