Skip to content

Commit

Permalink
Add recently liked (#87)
Browse files Browse the repository at this point in the history
* add recently_liked

* run tests in CI too

* fix null

* playing with db config

* allow admin replies even if message was deleted

* fix reply message_id not found
  • Loading branch information
ohld authored Sep 30, 2024
1 parent 4e0307a commit 0214747
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 21 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
DATABASE_URL=postgresql+asyncpg://app:app@app_db:5432/app
DATABASE_ASYNC_URL=postgresql+asyncpg://app:app@app_db:5432/app
REDIS_URL=redis://:myStrongPassword@redis:6379

SITE_DOMAIN=127.0.0.1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
container: python:3.10.7
services:
postgres:
image: postgres:14.1
image: postgres:16.1
env:
POSTGRES_PASSWORD: app
POSTGRES_USER: app
Expand Down
5 changes: 5 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class Config(BaseSettings):
DATABASE_URL: PostgresDsn
DATABASE_ASYNC_URL: PostgresDsn
DATABASE_POOL_SIZE: int = 16
DATABASE_POOL_TTL: int = 60 * 20 # 20 minutes
DATABASE_POOL_PRE_PING: bool = True

REDIS_URL: RedisDsn

SITE_DOMAIN: str = "myapp.com"
Expand Down
9 changes: 4 additions & 5 deletions src/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@
MEME_RAW_VK_MEME_SOURCE_POST_UNIQUE_CONSTRAINT,
)

DATABASE_URL = str(settings.DATABASE_URL)
DATABASE_URL = str(settings.DATABASE_ASYNC_URL)
engine = create_async_engine(
DATABASE_URL,
pool_pre_ping=True,
pool_size=10, # You can adjust this value based on your needs
max_overflow=20,
pool_recycle=1800, # Recycle connections after 1800 seconds (30 minutes)
pool_timeout=30, # Timeout for acquiring a connection from the pool
pool_size=settings.DATABASE_POOL_SIZE,
pool_recycle=settings.DATABASE_POOL_TTL,
pool_pre_ping=settings.DATABASE_POOL_PRE_PING,
)

metadata = MetaData(naming_convention=DB_NAMING_CONVENTION)
Expand Down
19 changes: 14 additions & 5 deletions src/tgbot/handlers/chat/feedback.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import random

from telegram import Update
from telegram.error import BadRequest
from telegram.ext import ContextTypes

from src.tgbot.constants import TELEGRAM_FEEDBACK_CHAT_ID
Expand Down Expand Up @@ -41,8 +42,16 @@ async def handle_feedback_reply(update: Update, context: ContextTypes.DEFAULT_TY
header, _ = update.message.reply_to_message.text.split("\n", 1)
user_id, message_id = header.split(":")

await context.bot.send_message(
chat_id=user_id,
text=reply_text,
reply_to_message_id=int(message_id),
)
try:
await context.bot.send_message(
chat_id=user_id,
text=reply_text,
reply_to_message_id=int(message_id),
)
except BadRequest:
# message was deleted ??
# trying again without reply_message_id
await context.bot.send_message(
chat_id=user_id,
text=reply_text,
)
26 changes: 19 additions & 7 deletions src/tgbot/handlers/upload/moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from telegram import Bot, InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.constants import ParseMode
from telegram.error import BadRequest
from telegram.ext import ContextTypes

from src.config import settings
Expand Down Expand Up @@ -246,18 +247,29 @@ async def handle_uploaded_meme_review_button(
external_id=str(meme["id"]),
)

await context.bot.send_message(
chat_id=meme_upload["user_id"],
reply_to_message_id=meme_upload["message_id"],
text="""
text = """
🎉🎉🎉
Your <b>meme has been approved</b> and soon bot will send it to other users!
See realtime stats of your uploaded memes: /uploads
""",
parse_mode=ParseMode.HTML,
)
"""

try:
await context.bot.send_message(
chat_id=meme_upload["user_id"],
reply_to_message_id=meme_upload["message_id"],
text=text,
parse_mode=ParseMode.HTML,
)
except BadRequest:
# messsage was deleted ??
# trying again withount reply_message_id
await context.bot.send_message(
chat_id=meme_upload["user_id"],
text=text,
parse_mode=ParseMode.HTML,
)

else:
await update_meme_by_upload_id(upload_id, status=MemeStatus.REJECTED)
Expand Down
6 changes: 3 additions & 3 deletions src/tgbot/handlers/upload/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ async def get_uploaded_memes_of_user_id(user_id: int) -> list[dict[str, Any]]:
M.status,
M.telegram_file_id,
M.type,
MS.nmemes_sent,
MS.nlikes,
MS.ndislikes
COALESCE(MS.nmemes_sent, 0) nmemes_sent,
COALESCE(MS.nlikes, 0) nlikes,
COALESCE(MS.ndislikes, 0) ndislikes
FROM meme M
LEFT JOIN meme_source S
ON M.meme_source_id = S.id
Expand Down

0 comments on commit 0214747

Please sign in to comment.