diff --git a/etc/crontab b/etc/crontab index 907804305..a86d3060e 100644 --- a/etc/crontab +++ b/etc/crontab @@ -1,6 +1,7 @@ 0 1 * * * root cd /app && python3 manage.py delete_users >/proc/1/fd/1 2>/proc/1/fd/2 0 3 * * * root cd /app && python3 manage.py cleanup_old_oauth_tokens >/proc/1/fd/1 2>/proc/1/fd/2 0 8 * * 2,3,4,5 root cd /app && python3 manage.py send_daily_digest --production true >/proc/1/fd/1 2>/proc/1/fd/2 +30 9,12,15,18,22 * * 1-5 root cd /app && python3 manage.py send_best_comments >/proc/1/fd/1 2>/proc/1/fd/2 0 11 * * 1 root cd /app && python3 manage.py send_weekly_digest --production true >/proc/1/fd/1 2>/proc/1/fd/2 0 5 * * 7 root cd /app && python3 manage.py rebuild_search_index >/proc/1/fd/1 2>/proc/1/fd/2 0 8 * * * root cd /app && python3 manage.py replay_stuck_reviews >/proc/1/fd/1 2>/proc/1/fd/2 diff --git a/notifications/management/commands/send_best_comments.py b/notifications/management/commands/send_best_comments.py new file mode 100644 index 000000000..00557d529 --- /dev/null +++ b/notifications/management/commands/send_best_comments.py @@ -0,0 +1,52 @@ +import logging +from datetime import datetime, timedelta + +from django.conf import settings +from django.core.management import BaseCommand +from django.template.loader import render_to_string + +from comments.models import Comment +from notifications.telegram.common import send_telegram_message, Chat + +log = logging.getLogger(__name__) + +TELEGRAM_CHANNEL_ID = -1001814814883 +TIME_INTERVAL = timedelta(days=3) +LIMIT = 20 +MIN_UPVOTES = 25 + + +class Command(BaseCommand): + help = "Send best comments to the channel" + + def handle(self, *args, **options): + best_comments = Comment.visible_objects().filter( + created_at__gte=datetime.utcnow() - TIME_INTERVAL, + post__is_approved_by_moderator=True, + upvotes__gte=MIN_UPVOTES, + ).order_by("-upvotes")[:LIMIT] + + for comment in best_comments: + if not comment.metadata or not comment.metadata.get("in_best_comments"): + self.stdout.write(f"Comment {comment.id} +{comment.upvotes}") + comment.metadata = comment.metadata or {} + comment.metadata["in_best_comments"] = True + comment.save() + + message = render_to_string("messages/best_comments.html", { + "comment": comment, + "settings": settings, + }) + + try: + send_telegram_message( + chat=Chat(id=TELEGRAM_CHANNEL_ID), + text=message, + disable_preview=False, + ) + except Exception as ex: + self.stdout.write(f"Error sending the message: {ex}") + + break + + self.stdout.write("Done 🥙") diff --git a/notifications/telegram/templates/messages/best_comments.html b/notifications/telegram/templates/messages/best_comments.html new file mode 100644 index 000000000..edf48e18f --- /dev/null +++ b/notifications/telegram/templates/messages/best_comments.html @@ -0,0 +1,5 @@ +{% load text_filters %}{% load posts %}Re: {% if comment.post.emoji %}{{ comment.post.emoji }} {% endif %}{% if comment.post.prefix %}{{ comment.post.prefix }} {% endif %}{{ comment.post.title }} (+{{ comment.upvotes }}) + +{{ comment.author.slug }}: {% render_plain comment 3000 %} + +Посмотреть ➜