add telegram post creation feature but experimental
This commit is contained in:
@@ -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]:
|
||||
|
||||
Reference in New Issue
Block a user