Spaces:
Paused
Paused
CBV01 commited on
Commit ·
e169f89
1
Parent(s): d0e4595
Integrated latest user improvements: added dialog timeout, megagroup detection, and robust frontend error handling.
Browse files- backend/main.py +29 -14
- frontend/src/app/campaigns/page.tsx +14 -5
backend/main.py
CHANGED
|
@@ -897,20 +897,35 @@ async def get_dialogs(req: FetchDialogsRequest, user_id: str = Depends(get_curre
|
|
| 897 |
|
| 898 |
dialogs_list = []
|
| 899 |
try:
|
| 900 |
-
# use a reasonable limit,
|
| 901 |
-
|
| 902 |
-
|
| 903 |
-
|
| 904 |
-
|
| 905 |
-
|
| 906 |
-
|
| 907 |
-
|
| 908 |
-
|
| 909 |
-
|
| 910 |
-
|
| 911 |
-
|
| 912 |
-
|
| 913 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 914 |
except Exception as dialog_err:
|
| 915 |
log_debug(f"DIALOGS_ITER_ERROR for {phone}: {str(dialog_err)}")
|
| 916 |
pass
|
|
|
|
| 897 |
|
| 898 |
dialogs_list = []
|
| 899 |
try:
|
| 900 |
+
# use a reasonable limit, and include archived
|
| 901 |
+
# Wrap in a timeout to prevent hanging on large accounts
|
| 902 |
+
async with asyncio.timeout(30):
|
| 903 |
+
async for dialog in client.iter_dialogs(limit=2000, archived=True, ignore_migrated=False):
|
| 904 |
+
if dialog.is_group or dialog.is_channel:
|
| 905 |
+
did = str(dialog.id)
|
| 906 |
+
title = getattr(dialog, 'name', '') or getattr(dialog, 'title', '') or did
|
| 907 |
+
|
| 908 |
+
# Better categorization
|
| 909 |
+
is_group = bool(dialog.is_group)
|
| 910 |
+
is_channel = bool(dialog.is_channel)
|
| 911 |
+
|
| 912 |
+
# In Telethon, supergroups have is_channel=True but are effectively groups
|
| 913 |
+
# We can check if it's a megagroup if it's a channel
|
| 914 |
+
is_supergroup = False
|
| 915 |
+
if is_channel and hasattr(dialog.entity, 'megagroup'):
|
| 916 |
+
is_supergroup = bool(dialog.entity.megagroup)
|
| 917 |
+
|
| 918 |
+
dialogs_list.append({
|
| 919 |
+
"id": did,
|
| 920 |
+
"name": title,
|
| 921 |
+
"title": title,
|
| 922 |
+
"is_group": is_group or is_supergroup,
|
| 923 |
+
"is_channel": is_channel and not is_supergroup
|
| 924 |
+
})
|
| 925 |
+
|
| 926 |
+
log_debug(f"DIALOGS: Successfully scanned {len(dialogs_list)} entities for {phone}.")
|
| 927 |
+
except asyncio.TimeoutError:
|
| 928 |
+
log_debug(f"DIALOGS_TIMEOUT for {phone}: Returning partial list of {len(dialogs_list)}")
|
| 929 |
except Exception as dialog_err:
|
| 930 |
log_debug(f"DIALOGS_ITER_ERROR for {phone}: {str(dialog_err)}")
|
| 931 |
pass
|
frontend/src/app/campaigns/page.tsx
CHANGED
|
@@ -91,20 +91,29 @@ export default function CampaignsPage() {
|
|
| 91 |
setFetchingDialogs(true);
|
| 92 |
setDialogs([]);
|
| 93 |
setCurrentPage(1); // Reset to first page
|
|
|
|
|
|
|
| 94 |
try {
|
| 95 |
const res = await apiFetch('/api/telegram/dialogs', {
|
| 96 |
method: 'POST',
|
| 97 |
body: JSON.stringify({ phone_number: phone })
|
| 98 |
});
|
|
|
|
| 99 |
const text = await res.text();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
if (res.ok) {
|
| 101 |
-
const data = JSON.parse(text);
|
| 102 |
setDialogs(data.dialogs || []);
|
| 103 |
if (data.error && (data.dialogs || []).length === 0) {
|
| 104 |
setCreationError(`Account Error: ${data.error}`);
|
| 105 |
-
} else {
|
| 106 |
-
setCreationError("");
|
| 107 |
}
|
|
|
|
|
|
|
| 108 |
}
|
| 109 |
} catch (err: any) {
|
| 110 |
console.error("Failed to fetch dialogs", err);
|
|
@@ -362,8 +371,8 @@ export default function CampaignsPage() {
|
|
| 362 |
const filteredDialogs = dialogs.filter((d: any) => {
|
| 363 |
const matchesSearch = d.title?.toLowerCase().includes(searchTerm.toLowerCase()) || d.id?.toString().includes(searchTerm);
|
| 364 |
if (!matchesSearch) return false;
|
| 365 |
-
if (filterType === 'groups') return d.is_group;
|
| 366 |
-
if (filterType === 'channels') return d.is_channel && !d.is_group;
|
| 367 |
return true;
|
| 368 |
});
|
| 369 |
|
|
|
|
| 91 |
setFetchingDialogs(true);
|
| 92 |
setDialogs([]);
|
| 93 |
setCurrentPage(1); // Reset to first page
|
| 94 |
+
setCreationError(""); // Clear previous errors
|
| 95 |
+
|
| 96 |
try {
|
| 97 |
const res = await apiFetch('/api/telegram/dialogs', {
|
| 98 |
method: 'POST',
|
| 99 |
body: JSON.stringify({ phone_number: phone })
|
| 100 |
});
|
| 101 |
+
|
| 102 |
const text = await res.text();
|
| 103 |
+
let data: any = {};
|
| 104 |
+
try {
|
| 105 |
+
data = JSON.parse(text);
|
| 106 |
+
} catch (e) {
|
| 107 |
+
console.error("Failed to parse JSON", text);
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
if (res.ok) {
|
|
|
|
| 111 |
setDialogs(data.dialogs || []);
|
| 112 |
if (data.error && (data.dialogs || []).length === 0) {
|
| 113 |
setCreationError(`Account Error: ${data.error}`);
|
|
|
|
|
|
|
| 114 |
}
|
| 115 |
+
} else {
|
| 116 |
+
setCreationError(data.detail || data.error || `Server returned ${res.status}: ${res.statusText}`);
|
| 117 |
}
|
| 118 |
} catch (err: any) {
|
| 119 |
console.error("Failed to fetch dialogs", err);
|
|
|
|
| 371 |
const filteredDialogs = dialogs.filter((d: any) => {
|
| 372 |
const matchesSearch = d.title?.toLowerCase().includes(searchTerm.toLowerCase()) || d.id?.toString().includes(searchTerm);
|
| 373 |
if (!matchesSearch) return false;
|
| 374 |
+
if (filterType === 'groups') return d.is_group || (d.is_channel && d.name?.toLowerCase().includes('group'));
|
| 375 |
+
if (filterType === 'channels') return d.is_channel && !d.is_group && !d.name?.toLowerCase().includes('group');
|
| 376 |
return true;
|
| 377 |
});
|
| 378 |
|