CBV01 commited on
Commit
b4fbd6e
·
1 Parent(s): 8163b32

Sync server time to UTC, add pagination for groups, and expand scraping limits

Browse files
Files changed (2) hide show
  1. backend/main.py +17 -17
  2. frontend/src/app/campaigns/page.tsx +57 -24
backend/main.py CHANGED
@@ -7,7 +7,7 @@ from pydantic import BaseModel # type: ignore
7
  from fastapi.middleware.cors import CORSMiddleware # type: ignore
8
  from database import get_db_connection # type: ignore
9
  import uuid
10
- from datetime import datetime, timedelta
11
  import collections
12
  import asyncio
13
  import random
@@ -98,12 +98,13 @@ async def global_exception_handler(request: Request, exc: Exception):
98
 
99
  @app.get("/health")
100
  async def health():
101
- now = datetime.now()
 
102
  return {
103
  "status": "healthy",
104
- "server_time": now.isoformat(),
105
- "poller_format": now.strftime('%Y-%m-%dT%H:%M'),
106
- "timezone": str(datetime.now().astimezone().tzinfo)
107
  }
108
 
109
  # Temporary in-memory store for login sessions
@@ -353,7 +354,7 @@ async def get_accounts(user_id: str = Depends(get_current_user_id)):
353
  return {"accounts": accounts}
354
 
355
  @app.get("/api/telegram/scrape")
356
- async def scrape_keyword(query: str, limit: int = 200, phone_number: Optional[str] = None, user_id: str = Depends(get_current_user_id)):
357
  conn = get_db_connection()
358
  if phone_number:
359
  row = conn.execute("SELECT session_string, api_id, api_hash FROM accounts WHERE phone_number = ? AND user_id = ?", (phone_number, user_id)).fetchone()
@@ -367,11 +368,14 @@ async def scrape_keyword(query: str, limit: int = 200, phone_number: Optional[st
367
 
368
  groups: List[Dict[str, Any]] = []
369
  try:
370
- # Telethon Search limit is typically capped at around 100-200 by Telegram itself for Global Search
 
371
  search_result = await client(SearchRequest(q=query, limit=limit))
372
 
373
  conn2 = get_db_connection()
374
  for chat in search_result.chats:
 
 
375
  is_private = getattr(chat, 'username', None) is None
376
 
377
  # Simple country detection based on language/keywords or just "Global"
@@ -789,17 +793,13 @@ async def task_poller():
789
  while True:
790
  try:
791
  conn = get_db_connection()
792
- # Normalize comparison: Browser sends YYYY-MM-DDTHH:MM.
793
- # now_str should match or be slightly ahead.
794
- now_dt = datetime.now()
795
  now_str = now_dt.strftime('%Y-%m-%dT%H:%M')
796
 
797
- # Check for ANY pending tasks to see if the table is even working
798
- # (Just for logging)
799
  all_pending = conn.execute("SELECT COUNT(*) FROM tasks WHERE status = 'pending'").fetchone()
800
  count = all_pending[0] if all_pending else 0
801
- if count > 0:
802
- print(f"BKG_POLLER: Found {count} total pending tasks in DB.")
803
 
804
  # Only pick pending tasks that are due now or in the past
805
  tasks = conn.execute(
@@ -808,12 +808,12 @@ async def task_poller():
808
  ).fetchall()
809
 
810
  if len(tasks) > 0:
811
- print(f"BKG_POLLER: Found {len(tasks)} tasks READY to send (due at or before {now_str})")
812
  elif count > 0:
813
- # We have pending tasks, but none are due yet
814
  next_task = conn.execute("SELECT scheduled_time FROM tasks WHERE status = 'pending' ORDER BY scheduled_time ASC LIMIT 1").fetchone()
815
  if next_task:
816
- print(f"BKG_POLLER: No tasks ready. Next one due at: {next_task[0]} (Current server time: {now_str})")
817
 
818
  if hasattr(conn, "close"): conn.close()
819
 
 
7
  from fastapi.middleware.cors import CORSMiddleware # type: ignore
8
  from database import get_db_connection # type: ignore
9
  import uuid
10
+ from datetime import datetime, timedelta, timezone
11
  import collections
12
  import asyncio
13
  import random
 
98
 
99
  @app.get("/health")
100
  async def health():
101
+ # Use UTC for absolute consistency across platforms
102
+ now_utc = datetime.now(timezone.utc)
103
  return {
104
  "status": "healthy",
105
+ "server_time_utc": now_utc.isoformat(),
106
+ "poller_format_utc": now_utc.strftime('%Y-%m-%dT%H:%M'),
107
+ "note": "All campaigns are processed in UTC time to match browser standards."
108
  }
109
 
110
  # Temporary in-memory store for login sessions
 
354
  return {"accounts": accounts}
355
 
356
  @app.get("/api/telegram/scrape")
357
+ async def scrape_keyword(query: str, limit: int = 500, phone_number: Optional[str] = None, user_id: str = Depends(get_current_user_id)):
358
  conn = get_db_connection()
359
  if phone_number:
360
  row = conn.execute("SELECT session_string, api_id, api_hash FROM accounts WHERE phone_number = ? AND user_id = ?", (phone_number, user_id)).fetchone()
 
368
 
369
  groups: List[Dict[str, Any]] = []
370
  try:
371
+ # Requesting a very high limit. Telegram usually caps Global Search at ~200-300 results,
372
+ # but Telethon will try to paginate if possible or we can at least ensure we take everything we get.
373
  search_result = await client(SearchRequest(q=query, limit=limit))
374
 
375
  conn2 = get_db_connection()
376
  for chat in search_result.chats:
377
+ # Skip deleted or restricted chats
378
+ if not chat or getattr(chat, 'min', False): continue
379
  is_private = getattr(chat, 'username', None) is None
380
 
381
  # Simple country detection based on language/keywords or just "Global"
 
793
  while True:
794
  try:
795
  conn = get_db_connection()
796
+ # USE UTC FOR EVERYTHING. This syncs backend (server) with frontend (browser).
797
+ now_dt = datetime.now(timezone.utc)
 
798
  now_str = now_dt.strftime('%Y-%m-%dT%H:%M')
799
 
800
+ # Check for ANY pending tasks
 
801
  all_pending = conn.execute("SELECT COUNT(*) FROM tasks WHERE status = 'pending'").fetchone()
802
  count = all_pending[0] if all_pending else 0
 
 
803
 
804
  # Only pick pending tasks that are due now or in the past
805
  tasks = conn.execute(
 
808
  ).fetchall()
809
 
810
  if len(tasks) > 0:
811
+ print(f"BKG_POLLER: Found {len(tasks)} tasks READY to send (Trigger: UTC {now_str})")
812
  elif count > 0:
813
+ # Log the next one coming up to help user verify sync
814
  next_task = conn.execute("SELECT scheduled_time FROM tasks WHERE status = 'pending' ORDER BY scheduled_time ASC LIMIT 1").fetchone()
815
  if next_task:
816
+ print(f"BKG_POLLER: No tasks ready. Next up in DB: {next_task[0]} (Current Server UTC: {now_str})")
817
 
818
  if hasattr(conn, "close"): conn.close()
819
 
frontend/src/app/campaigns/page.tsx CHANGED
@@ -23,7 +23,8 @@ export default function CampaignsPage() {
23
  const [loading, setLoading] = useState(false);
24
  const [success, setSuccess] = useState(false);
25
 
26
- const [loadingTasks, setLoadingTasks] = useState(true);
 
27
 
28
  const fetchCampaigns = async () => {
29
  try {
@@ -44,6 +45,7 @@ export default function CampaignsPage() {
44
  if (!phone) return;
45
  setFetchingDialogs(true);
46
  setDialogs([]);
 
47
  try {
48
  const res = await apiFetch('/api/telegram/dialogs', {
49
  method: 'POST',
@@ -94,12 +96,18 @@ export default function CampaignsPage() {
94
  alert("Please select at least one group/channel.");
95
  return;
96
  }
 
 
 
 
 
97
  setLoading(true);
98
  try {
99
  const res = await apiFetch('/api/telegram/campaigns', {
100
  method: 'POST',
101
  body: JSON.stringify({
102
  ...campaignData,
 
103
  groups: selectedGroups
104
  })
105
  });
@@ -318,37 +326,62 @@ export default function CampaignsPage() {
318
  />
319
  </div>
320
 
321
- <div className="flex-1 bg-[#1c2231] border border-white/[0.05] rounded-2xl overflow-hidden flex flex-col min-h-[320px]">
322
  {fetchingDialogs ? (
323
  <div className="flex-1 flex flex-col items-center justify-center gap-3">
324
  <Loader2 className="animate-spin text-[#7c7fff]/40" size={24} />
325
  <p className="text-[10px] font-bold text-white/20 uppercase tracking-widest">Fetching Groups...</p>
326
  </div>
327
  ) : dialogs.length > 0 ? (
328
- <div className="flex-1 overflow-y-auto p-4 space-y-2 custom-scrollbar">
329
- {dialogs
330
- .filter(d =>
331
- d.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
332
- (d.username && d.username.toLowerCase().includes(searchTerm.toLowerCase()))
333
- )
334
- .map((d) => (
335
- <div
336
- key={d.id}
337
- onClick={() => toggleGroup(d.id)}
338
- className={`p-3 rounded-xl border cursor-pointer transition-all flex items-center justify-between group/item ${selectedGroups.includes(d.id) ? 'bg-[#7c7fff]/10 border-[#7c7fff]/30' : 'bg-white/[0.02] border-white/[0.05] hover:bg-white/[0.04]'}`}
339
- >
340
- <div className="flex items-center gap-3 overflow-hidden">
341
- <div className={`w-8 h-8 rounded-lg flex items-center justify-center shrink-0 ${d.is_channel ? 'bg-indigo-500/10 text-indigo-400' : 'bg-blue-500/10 text-blue-400'}`}>
342
- {d.is_channel ? <Send size={14} /> : <Users size={14} />}
 
 
 
 
 
 
 
343
  </div>
344
- <span className="text-xs font-bold text-white/80 truncate">{d.name}</span>
345
- </div>
346
- <div className={`w-4 h-4 rounded-full border-2 transition-all flex items-center justify-center ${selectedGroups.includes(d.id) ? 'bg-[#7c7fff] border-[#7c7fff]' : 'border-white/10'}`}>
347
- {selectedGroups.includes(d.id) && <CheckCircle2 size={10} className="text-white" />}
348
  </div>
349
- </div>
350
- ))}
351
- </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
352
  ) : (
353
  <div className="flex-1 flex flex-col items-center justify-center text-center p-10">
354
  <p className="text-xs text-white/20 font-bold uppercase tracking-widest">Select an account to load targets</p>
 
23
  const [loading, setLoading] = useState(false);
24
  const [success, setSuccess] = useState(false);
25
 
26
+ const [currentPage, setCurrentPage] = useState(1);
27
+ const pageSize = 50;
28
 
29
  const fetchCampaigns = async () => {
30
  try {
 
45
  if (!phone) return;
46
  setFetchingDialogs(true);
47
  setDialogs([]);
48
+ setCurrentPage(1); // Reset to first page
49
  try {
50
  const res = await apiFetch('/api/telegram/dialogs', {
51
  method: 'POST',
 
96
  alert("Please select at least one group/channel.");
97
  return;
98
  }
99
+
100
+ // SYNC TIME: Convert local time picking to UTC for the backend poller
101
+ // Date.toISOString() gives YYYY-MM-DDTHH:mm:ss.sssZ (always in UTC)
102
+ const utcScheduleTime = new Date(campaignData.schedule_time).toISOString().slice(0, 16);
103
+
104
  setLoading(true);
105
  try {
106
  const res = await apiFetch('/api/telegram/campaigns', {
107
  method: 'POST',
108
  body: JSON.stringify({
109
  ...campaignData,
110
+ schedule_time: utcScheduleTime,
111
  groups: selectedGroups
112
  })
113
  });
 
326
  />
327
  </div>
328
 
329
+ <div className="flex-1 bg-[#1c2231] border border-white/[0.05] rounded-2xl overflow-hidden flex flex-col min-h-[400px]">
330
  {fetchingDialogs ? (
331
  <div className="flex-1 flex flex-col items-center justify-center gap-3">
332
  <Loader2 className="animate-spin text-[#7c7fff]/40" size={24} />
333
  <p className="text-[10px] font-bold text-white/20 uppercase tracking-widest">Fetching Groups...</p>
334
  </div>
335
  ) : dialogs.length > 0 ? (
336
+ <>
337
+ <div className="flex-1 overflow-y-auto p-4 space-y-2 custom-scrollbar">
338
+ {dialogs
339
+ .filter(d =>
340
+ d.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
341
+ (d.username && d.username.toLowerCase().includes(searchTerm.toLowerCase()))
342
+ )
343
+ .slice((currentPage - 1) * pageSize, currentPage * pageSize)
344
+ .map((d) => (
345
+ <div
346
+ key={d.id}
347
+ onClick={() => toggleGroup(d.id)}
348
+ className={`p-3 rounded-xl border cursor-pointer transition-all flex items-center justify-between group/item ${selectedGroups.includes(d.id) ? 'bg-[#7c7fff]/10 border-[#7c7fff]/30' : 'bg-white/[0.02] border-white/[0.05] hover:bg-white/[0.04]'}`}
349
+ >
350
+ <div className="flex items-center gap-3 overflow-hidden">
351
+ <div className={`w-8 h-8 rounded-lg flex items-center justify-center shrink-0 ${d.is_channel ? 'bg-indigo-500/10 text-indigo-400' : 'bg-blue-500/10 text-blue-400'}`}>
352
+ {d.is_channel ? <Send size={14} /> : <Users size={14} />}
353
+ </div>
354
+ <span className="text-xs font-bold text-white/80 truncate">{d.name}</span>
355
+ </div>
356
+ <div className={`w-4 h-4 rounded-full border-2 transition-all flex items-center justify-center ${selectedGroups.includes(d.id) ? 'bg-[#7c7fff] border-[#7c7fff]' : 'border-white/10'}`}>
357
+ {selectedGroups.includes(d.id) && <CheckCircle2 size={10} className="text-white" />}
358
  </div>
 
 
 
 
359
  </div>
360
+ ))}
361
+ </div>
362
+ {/* Pagination Controls */}
363
+ <div className="p-4 border-t border-white/[0.05] flex items-center justify-between bg-white/[0.01]">
364
+ <button
365
+ type="button"
366
+ disabled={currentPage === 1}
367
+ onClick={() => setCurrentPage(p => Math.max(1, p - 1))}
368
+ className="text-[10px] font-black uppercase tracking-widest text-white/40 hover:text-[#7c7fff] disabled:opacity-20 transition-colors"
369
+ >
370
+ Prev
371
+ </button>
372
+ <span className="text-[10px] font-bold text-white/20 uppercase tracking-widest">
373
+ Page {currentPage} of {Math.ceil(dialogs.filter(d => d.name.toLowerCase().includes(searchTerm.toLowerCase()) || (d.username && d.username.toLowerCase().includes(searchTerm.toLowerCase()))).length / pageSize)}
374
+ </span>
375
+ <button
376
+ type="button"
377
+ disabled={currentPage >= Math.ceil(dialogs.filter(d => d.name.toLowerCase().includes(searchTerm.toLowerCase()) || (d.username && d.username.toLowerCase().includes(searchTerm.toLowerCase()))).length / pageSize)}
378
+ onClick={() => setCurrentPage(p => p + 1)}
379
+ className="text-[10px] font-black uppercase tracking-widest text-white/40 hover:text-[#7c7fff] disabled:opacity-20 transition-colors"
380
+ >
381
+ Next
382
+ </button>
383
+ </div>
384
+ </>
385
  ) : (
386
  <div className="flex-1 flex flex-col items-center justify-center text-center p-10">
387
  <p className="text-xs text-white/20 font-bold uppercase tracking-widest">Select an account to load targets</p>