aboutsummaryrefslogtreecommitdiff
path: root/jobs/comments.py
blob: 5100cff07f17090fef38e0ffbeb198af9127cd6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from celery import shared_task
from django.db import transaction
from blog.models import Comment
from internal.weblog_utilities import check_comment_spam
import logging

logger = logging.getLogger(__name__)


def _process_single_comment(comment):
    spam_result = check_comment_spam(comment=comment.body, post=comment.post)
    
    if spam_result == "N":
        comment.approve_comment()
        return "approved"
    else:
        comment.delete()
        return "deleted as spam"


@shared_task(bind=True, max_retries=3)
def check_comment_spam_async(self, comment_id):
    try:
        with transaction.atomic():
            try:
                comment = Comment.objects.select_for_update().get(id=comment_id)
            except Comment.DoesNotExist:
                return f"Comment {comment_id} not found"

            if comment.spam_status != "pending":
                return f"Comment {comment_id} already processed"

            result = _process_single_comment(comment)
            return f"Comment {comment_id} processed: {result}"

    except Exception as exc:
        logger.error(f"Error processing comment {comment_id}: {type(exc).__name__}: {str(exc)}")
        if self.request.retries < self.max_retries:
            raise self.retry(countdown=60 * (2**self.request.retries))
        else:
            try:
                comment = Comment.objects.get(id=comment_id)
                if comment.spam_status == "pending":
                    comment.approve_comment()
            except:
                pass
            return f"Comment {comment_id} auto-approved after failed spam check"


@shared_task
def process_pending_and_spam_comments():
    processed_count = 0
    deleted_count = 0

    manually_spam_comments = Comment.objects.filter(spam_status="spam")
    for comment in manually_spam_comments:
        comment.delete()
        deleted_count += 1

    pending_comments = Comment.objects.filter(spam_status="pending")
    for comment in pending_comments:
        try:
            result = _process_single_comment(comment)
            if result == "deleted as spam":
                deleted_count += 1
            processed_count += 1
        except Exception as e:
            logger.error(f"Error processing comment {comment.id}: {e}")
            comment.approve_comment()
            processed_count += 1

    return f"Processed {processed_count} pending comments, deleted {deleted_count} spam comments"