ACL, channel_charing
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2025-09-06 10:57:10 +09:00
parent c6104455d8
commit 5c81aae29c
10 changed files with 657 additions and 275 deletions

View File

@@ -1,8 +1,12 @@
# handlers/add_channel.py
from telegram import Update
from telegram.ext import ContextTypes, ConversationHandler, CommandHandler, MessageHandler, filters
from telegram.ext import (
ContextTypes, ConversationHandler, CommandHandler, MessageHandler, filters
)
from sqlalchemy import select
from db import AsyncSessionLocal
from models import Channel
from models import Channel, Admin
INPUT_NAME, INPUT_LINK = range(2)
@@ -14,40 +18,60 @@ async def add_channel_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
return INPUT_NAME
async def input_channel_name(update: Update, context: ContextTypes.DEFAULT_TYPE):
if context.user_data is None:
context.user_data = {}
text = update.message.text.strip() if update.message and update.message.text else ''
context.user_data['channel_name'] = text
if update.message:
await update.message.reply_text('Теперь отправьте ссылку на канал (должна начинаться с @):')
if not update.message:
return ConversationHandler.END
name = (update.message.text or "").strip()
if not name:
await update.message.reply_text("Имя не может быть пустым. Введите имя канала:")
return INPUT_NAME
context.user_data["channel_name"] = name
await update.message.reply_text('Отправьте ссылку на канал (формат "@username" или "-100..."):')
return INPUT_LINK
async def input_channel_link(update: Update, context: ContextTypes.DEFAULT_TYPE):
if context.user_data is None:
context.user_data = {}
link = update.message.text.strip() if update.message and update.message.text else ''
if not link.startswith('@'):
if update.message:
await update.message.reply_text('Ошибка: ссылка на канал должна начинаться с @. Попробуйте снова.')
return INPUT_LINK
context.user_data['channel_link'] = link
return await save_channel(update, context)
async def _get_or_create_admin(session, tg_id: int) -> Admin:
res = await session.execute(select(Admin).where(Admin.tg_id == tg_id))
admin = res.scalar_one_or_none()
if not admin:
admin = Admin(tg_id=tg_id)
session.add(admin)
await session.flush()
return admin
async def save_channel(update: Update, context: ContextTypes.DEFAULT_TYPE):
if context.user_data is None:
context.user_data = {}
name = context.user_data.get('channel_name')
link = context.user_data.get('channel_link')
if not name or not link:
if update.message:
await update.message.reply_text('Ошибка: не указано название или ссылка.')
async def input_channel_link(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not update.message:
return ConversationHandler.END
link = (update.message.text or "").strip()
if not (link.startswith("@") or link.startswith("-100")):
await update.message.reply_text('Неверный формат. Укажите "@username" или "-100...".')
return INPUT_LINK
name = (context.user_data or {}).get("channel_name", "").strip()
if not name:
await update.message.reply_text("Не найдено имя. Начните заново: /add_channel")
return ConversationHandler.END
user = update.effective_user
if not user:
await update.message.reply_text("Не удалось определить администратора.")
return ConversationHandler.END
async with AsyncSessionLocal() as session:
channel = Channel(name=name, link=link)
session.add(channel)
await session.commit()
if update.message:
await update.message.reply_text(f'Канал "{name}" добавлен.')
admin = await _get_or_create_admin(session, user.id)
# если канал уже есть — обновим имя и владельца
existing_q = await session.execute(select(Channel).where(Channel.link == link))
existing = existing_q.scalar_one_or_none()
if existing:
existing.name = name
existing.admin_id = admin.id
await session.commit()
await update.message.reply_text(f'Канал "{name}" уже был — обновил владельца и имя.')
else:
channel = Channel(name=name, link=link, admin_id=admin.id)
session.add(channel)
await session.commit()
await update.message.reply_text(f'Канал "{name}" добавлен и привязан к вашему админ-аккаунту.')
return ConversationHandler.END
add_channel_conv = ConversationHandler(