Spaces:
Paused
Paused
CBV01 commited on
Commit ·
7d230dc
1
Parent(s): 09b6531
fix(backend): fix entity resolution types and input entity types in join_single_target
Browse files- backend/main.py +29 -12
backend/main.py
CHANGED
|
@@ -1165,8 +1165,16 @@ async def join_single_target(req: SingleJoinRequest, user_id: str = Depends(get_
|
|
| 1165 |
source_phone = req.source_phone.strip()
|
| 1166 |
try:
|
| 1167 |
client_source = await POOL.get_client(user_id, source_phone)
|
| 1168 |
-
|
| 1169 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1170 |
# If the entity actually has a public username, join by username
|
| 1171 |
uname = getattr(source_entity, 'username', None)
|
| 1172 |
if uname:
|
|
@@ -1183,22 +1191,24 @@ async def join_single_target(req: SingleJoinRequest, user_id: str = Depends(get_
|
|
| 1183 |
if is_chan:
|
| 1184 |
from telethon.tl.functions.channels import GetFullChannelRequest
|
| 1185 |
from telethon.tl.functions.messages import ExportChatInviteRequest
|
| 1186 |
-
|
|
|
|
| 1187 |
if hasattr(full.full_chat, 'exported_invite') and full.full_chat.exported_invite:
|
| 1188 |
invite_link = getattr(full.full_chat.exported_invite, 'link', None)
|
| 1189 |
if not invite_link:
|
| 1190 |
try:
|
| 1191 |
-
exp = await client_source(ExportChatInviteRequest(
|
| 1192 |
invite_link = getattr(exp, 'link', None)
|
| 1193 |
except Exception: pass
|
| 1194 |
else:
|
| 1195 |
from telethon.tl.functions.messages import GetFullChatRequest, ExportChatInviteRequest
|
| 1196 |
-
|
|
|
|
| 1197 |
if hasattr(full.full_chat, 'exported_invite') and full.full_chat.exported_invite:
|
| 1198 |
invite_link = getattr(full.full_chat.exported_invite, 'link', None)
|
| 1199 |
if not invite_link:
|
| 1200 |
try:
|
| 1201 |
-
exp = await client_source(ExportChatInviteRequest(
|
| 1202 |
invite_link = getattr(exp, 'link', None)
|
| 1203 |
except Exception: pass
|
| 1204 |
except Exception as exp_err:
|
|
@@ -1210,26 +1220,33 @@ async def join_single_target(req: SingleJoinRequest, user_id: str = Depends(get_
|
|
| 1210 |
|
| 1211 |
# Try direct invitation via source account
|
| 1212 |
try:
|
| 1213 |
-
user_b = None
|
| 1214 |
try:
|
| 1215 |
-
user_b = await client_source.
|
| 1216 |
-
except Exception:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1217 |
|
| 1218 |
if user_b:
|
| 1219 |
is_chan = getattr(source_entity, 'broadcast', False) or getattr(source_entity, 'megagroup', False)
|
| 1220 |
if is_chan:
|
| 1221 |
from telethon.tl.functions.channels import InviteToChannelRequest
|
| 1222 |
-
await client_source(
|
|
|
|
| 1223 |
else:
|
| 1224 |
from telethon.tl.functions.messages import AddChatUserRequest
|
| 1225 |
-
|
|
|
|
| 1226 |
|
| 1227 |
# Record persistence in database
|
| 1228 |
try:
|
| 1229 |
conn = get_db_connection()
|
| 1230 |
conn.execute(
|
| 1231 |
"INSERT OR IGNORE INTO account_joins (user_id, phone_number, group_id) VALUES (?, ?, ?)",
|
| 1232 |
-
(user_id, phone_number,
|
| 1233 |
)
|
| 1234 |
if hasattr(conn, "commit"): conn.commit()
|
| 1235 |
if hasattr(conn, "close"): conn.close()
|
|
|
|
| 1165 |
source_phone = req.source_phone.strip()
|
| 1166 |
try:
|
| 1167 |
client_source = await POOL.get_client(user_id, source_phone)
|
| 1168 |
+
res_obj = await resolve_tg_entity(client_source, target_str)
|
| 1169 |
+
if isinstance(res_obj, tuple) and res_obj[0] == "invite":
|
| 1170 |
+
try:
|
| 1171 |
+
await smart_join(client, target_str, user_id, phone_number)
|
| 1172 |
+
return {"status": "success", "message": "Successfully joined via invite"}
|
| 1173 |
+
except Exception as inv_join_err:
|
| 1174 |
+
log_debug(f"INVITE_JOIN_ERR: {inv_join_err}")
|
| 1175 |
+
|
| 1176 |
+
source_entity: Any = res_obj
|
| 1177 |
+
if source_entity and not isinstance(source_entity, tuple):
|
| 1178 |
# If the entity actually has a public username, join by username
|
| 1179 |
uname = getattr(source_entity, 'username', None)
|
| 1180 |
if uname:
|
|
|
|
| 1191 |
if is_chan:
|
| 1192 |
from telethon.tl.functions.channels import GetFullChannelRequest
|
| 1193 |
from telethon.tl.functions.messages import ExportChatInviteRequest
|
| 1194 |
+
source_input = await client_source.get_input_entity(source_entity)
|
| 1195 |
+
full = await client_source(GetFullChannelRequest(source_input)) # type: ignore
|
| 1196 |
if hasattr(full.full_chat, 'exported_invite') and full.full_chat.exported_invite:
|
| 1197 |
invite_link = getattr(full.full_chat.exported_invite, 'link', None)
|
| 1198 |
if not invite_link:
|
| 1199 |
try:
|
| 1200 |
+
exp = await client_source(ExportChatInviteRequest(source_input)) # type: ignore
|
| 1201 |
invite_link = getattr(exp, 'link', None)
|
| 1202 |
except Exception: pass
|
| 1203 |
else:
|
| 1204 |
from telethon.tl.functions.messages import GetFullChatRequest, ExportChatInviteRequest
|
| 1205 |
+
source_id = int(getattr(source_entity, 'id', 0))
|
| 1206 |
+
full = await client_source(GetFullChatRequest(source_id)) # type: ignore
|
| 1207 |
if hasattr(full.full_chat, 'exported_invite') and full.full_chat.exported_invite:
|
| 1208 |
invite_link = getattr(full.full_chat.exported_invite, 'link', None)
|
| 1209 |
if not invite_link:
|
| 1210 |
try:
|
| 1211 |
+
exp = await client_source(ExportChatInviteRequest(source_id)) # type: ignore
|
| 1212 |
invite_link = getattr(exp, 'link', None)
|
| 1213 |
except Exception: pass
|
| 1214 |
except Exception as exp_err:
|
|
|
|
| 1220 |
|
| 1221 |
# Try direct invitation via source account
|
| 1222 |
try:
|
| 1223 |
+
user_b: Any = None
|
| 1224 |
try:
|
| 1225 |
+
user_b = await client_source.get_input_entity(phone_number)
|
| 1226 |
+
except Exception:
|
| 1227 |
+
try:
|
| 1228 |
+
ub_ent = await client_source.get_entity(phone_number)
|
| 1229 |
+
if ub_ent:
|
| 1230 |
+
user_b = await client_source.get_input_entity(ub_ent)
|
| 1231 |
+
except Exception: pass
|
| 1232 |
|
| 1233 |
if user_b:
|
| 1234 |
is_chan = getattr(source_entity, 'broadcast', False) or getattr(source_entity, 'megagroup', False)
|
| 1235 |
if is_chan:
|
| 1236 |
from telethon.tl.functions.channels import InviteToChannelRequest
|
| 1237 |
+
source_input = await client_source.get_input_entity(source_entity)
|
| 1238 |
+
await client_source(InviteToChannelRequest(channel=source_input, users=[user_b])) # type: ignore
|
| 1239 |
else:
|
| 1240 |
from telethon.tl.functions.messages import AddChatUserRequest
|
| 1241 |
+
source_id = int(getattr(source_entity, 'id', 0))
|
| 1242 |
+
await client_source(AddChatUserRequest(chat_id=source_id, user_id=user_b, fwd_limit=100)) # type: ignore
|
| 1243 |
|
| 1244 |
# Record persistence in database
|
| 1245 |
try:
|
| 1246 |
conn = get_db_connection()
|
| 1247 |
conn.execute(
|
| 1248 |
"INSERT OR IGNORE INTO account_joins (user_id, phone_number, group_id) VALUES (?, ?, ?)",
|
| 1249 |
+
(user_id, phone_number, target_str)
|
| 1250 |
)
|
| 1251 |
if hasattr(conn, "commit"): conn.commit()
|
| 1252 |
if hasattr(conn, "close"): conn.close()
|