add telegram post creation feature but experimental

This commit is contained in:
2026-02-19 20:02:49 +01:00
parent 5ba5cae98f
commit 2d3c559fcb
7 changed files with 631 additions and 1 deletions

View File

@@ -817,6 +817,63 @@ class DatabaseClient:
await cache.invalidate_linkedin_account(user_id)
logger.info(f"Deleted LinkedIn account: {account_id}")
# ==================== TELEGRAM ACCOUNTS ====================
async def get_telegram_account(self, user_id: UUID) -> Optional['TelegramAccount']:
"""Get Telegram account for user."""
from src.database.models import TelegramAccount
result = await asyncio.to_thread(
lambda: self.client.table("telegram_accounts").select("*")
.eq("user_id", str(user_id)).eq("is_active", True).execute()
)
if result.data:
return TelegramAccount(**result.data[0])
return None
async def get_telegram_account_by_chat_id(self, chat_id: str) -> Optional['TelegramAccount']:
"""Get Telegram account by chat_id."""
from src.database.models import TelegramAccount
result = await asyncio.to_thread(
lambda: self.client.table("telegram_accounts").select("*")
.eq("telegram_chat_id", chat_id).eq("is_active", True).execute()
)
if result.data:
return TelegramAccount(**result.data[0])
return None
async def save_telegram_account(self, account: 'TelegramAccount') -> 'TelegramAccount':
"""Create or update a Telegram account connection."""
from src.database.models import TelegramAccount
data = account.model_dump(exclude={'id', 'created_at', 'updated_at'}, exclude_none=True)
data['user_id'] = str(data['user_id'])
existing = await asyncio.to_thread(
lambda: self.client.table("telegram_accounts").select("id")
.eq("user_id", str(account.user_id)).execute()
)
if existing.data:
result = await asyncio.to_thread(
lambda: self.client.table("telegram_accounts").update(data)
.eq("user_id", str(account.user_id)).execute()
)
else:
result = await asyncio.to_thread(
lambda: self.client.table("telegram_accounts").insert(data).execute()
)
logger.info(f"Saved Telegram account for user: {account.user_id}")
return TelegramAccount(**result.data[0])
async def delete_telegram_account(self, user_id: UUID) -> bool:
"""Delete Telegram account connection for user."""
await asyncio.to_thread(
lambda: self.client.table("telegram_accounts").delete()
.eq("user_id", str(user_id)).execute()
)
logger.info(f"Deleted Telegram account for user: {user_id}")
return True
# ==================== USERS ====================
async def get_user(self, user_id: UUID) -> Optional[User]:

View File

@@ -103,6 +103,22 @@ class LinkedInAccount(DBModel):
last_error_at: Optional[datetime] = None
class TelegramAccount(DBModel):
"""Telegram account connection for bot access."""
id: Optional[UUID] = None
user_id: UUID
created_at: Optional[datetime] = None
updated_at: Optional[datetime] = None
telegram_user_id: str
telegram_username: Optional[str] = None
telegram_first_name: Optional[str] = None
telegram_chat_id: str
is_active: bool = True
last_used_at: Optional[datetime] = None
last_error: Optional[str] = None
last_error_at: Optional[datetime] = None
class User(DBModel):
"""User model - combines auth.users data with profile data.