diff options
| author | Bobby <[email protected]> | 2024-06-06 18:15:28 +0000 |
|---|---|---|
| committer | Bobby <[email protected]> | 2024-06-06 18:15:28 +0000 |
| commit | debfb4676328d587d31d44cc679c3d382fadd6ed (patch) | |
| tree | 604fe923a7f2e0971f6a35ec3f33d4328cde6e5e | |
| parent | 57d54e24ac6fe72ac8a37236e827914c0dd235ee (diff) | |
| download | thatcomputerscientist-debfb4676328d587d31d44cc679c3d382fadd6ed.tar.xz thatcomputerscientist-debfb4676328d587d31d44cc679c3d382fadd6ed.zip | |
Spam Comments Fix
| -rw-r--r-- | .github/workflows/main.yml | 2 | ||||
| -rw-r--r-- | blog/context_processors.py | 10 | ||||
| -rw-r--r-- | blog/views.py | 13 | ||||
| -rw-r--r-- | ignis/views.py | 6 | ||||
| -rw-r--r-- | templates/blog/post.html | 2 |
5 files changed, 22 insertions, 11 deletions
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9bc4b08a..064a6ffe 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,5 +15,3 @@ jobs: script: | grep -qxF 'GEMINI_API_KEY=${{ secrets.GEMINI_API_KEY }}' /home/ubuntu/.shifooenv || echo 'GEMINI_API_KEY=${{ secrets.GEMINI_API_KEY }}' >> /home/ubuntu/.shifooenv sh /home/ubuntu/deploy.sh - -
\ No newline at end of file diff --git a/blog/context_processors.py b/blog/context_processors.py index 99655dc2..ebb19577 100644 --- a/blog/context_processors.py +++ b/blog/context_processors.py @@ -28,7 +28,7 @@ akismet_api = akismet.Akismet( ) -def check_spam(comment): +def check_spam(comment, post): # spam = False # akismet_data = { # "comment_type": "comment", @@ -47,14 +47,18 @@ def check_spam(comment): else: genai.configure(api_key=gemini_api_key) - model = genai.GenerativeModel("gemini-1.5-flash") + model = genai.GenerativeModel("gemini-pro") - input_prompt = f"Comment Processing Checker. This is for a personal blog site. Output only Y or N for the included text. Y if the comment seems like spam. N if the comment seems safe. Do not access links. Just mark Y or N for the text. \n\nText: {comment}" + input_prompt = f"Comment Processing Checker. This is for a personal blog site. Output only Y or N for the included text. Y if the comment seems like spam, unrelated to post or random gibberish. N if the comment seems safe. Do not access links. Just mark Y or N for the text. Post Title: {post.title}. \n Post Body: {post.body}. \n Comment: {comment}. \n Judge if the comment belongs on the post or not. Random texts, links, and gibberish are considered spam. Trying to phish or promote shady links are also considered spam. Please mark Y if the comment seems like spam. N if the comment seems safe. Only output a single character. Y or N." response = model.generate_content(input_prompt) r_text = response.text + r_text = r_text.strip() + + print(r_text) + return r_text diff --git a/blog/views.py b/blog/views.py index f7fec1b4..ec4d9376 100644 --- a/blog/views.py +++ b/blog/views.py @@ -224,7 +224,8 @@ def comment(request, slug): if request.method == 'POST': if request.user.is_authenticated: try: - r_spam = check_spam(comment=request.POST.get('body')) + print(request.POST.get('comment')) + r_spam = check_spam(comment=request.POST.get('comment'), post=Post.objects.get(slug=slug)) if r_spam != 'N': messages.error(request, r_spam, extra_tags='spam') return redirect(reverse('blog:post', kwargs={'slug': slug}) + '#new-comment') @@ -259,7 +260,7 @@ def anon_comment(request, slug): anonymous_token, at = request.POST.get('anonymous-token'), request.POST.get('anonymous-token') new_anonymous_token = request.POST.get('new-anonymous-token') anonymous_comment = request.POST.get('anonymous-comment') - res_spam = check_spam(comment=anonymous_comment) + res_spam = check_spam(comment=anonymous_comment, post=Post.objects.get(slug=slug)) if res_spam != 'N': messages.error(request, res_spam, extra_tags='spam') return redirect(reverse('blog:post', kwargs={'slug': slug}) + '#new-comment') @@ -330,7 +331,9 @@ def edit_comment(request, slug): user_ip = request.META.get('REMOTE_ADDR') user_agent_string = request.META.get('HTTP_USER_AGENT', '') user_agent = parse(user_agent_string) - if check_spam(user_ip=user_ip, user_agent=user_agent, comment=request.POST.get('body'), author=request.user.username): + # if check_spam(user_ip=user_ip, user_agent=user_agent, comment=request.POST.get('body'), author=request.user.username + res_spam = check_spam(comment=request.POST.get('body'), post=comment.post) + if res_spam != 'N': messages.error(request, request.POST.get('body'), extra_tags='spam') return redirect(reverse('blog:post', kwargs={'slug': slug}) + '#comment-' + str(comment.id)) if comment.user == request.user: @@ -369,7 +372,9 @@ def anon_edit_comment(request, slug): user_ip = request.META.get('REMOTE_ADDR') user_agent_string = request.META.get('HTTP_USER_AGENT', '') user_agent = parse(user_agent_string) - if check_spam(user_ip=user_ip, user_agent=user_agent, comment=request.POST.get('body'), author=comment.anonymous_user.name): + res_spam = check_spam(comment=request.POST.get('body'), post=comment.post) + if res_spam != 'N': + # if check_spam(user_ip=user_ip, user_agent=user_agent, comment=request.POST.get('body'), author=comment.anonymous_user.name): messages.error(request, request.POST.get('body'), extra_tags='spam') return redirect(reverse('blog:post', kwargs={'slug': slug}) + '#comment-' + str(comment.id)) if comment.anonymous_user.token == anonymous_token: diff --git a/ignis/views.py b/ignis/views.py index d6bd7faf..942ab187 100644 --- a/ignis/views.py +++ b/ignis/views.py @@ -74,7 +74,11 @@ def post_image(request, size, post_id): @csrf_exempt def get_image(request, post_id, image_name): # get image from post_id - pi = PostImage.objects.filter(post=Post.objects.get(id=post_id), name=image_name) + try: + post = Post.objects.get(id=post_id) + except: + return HttpResponse('No post found!', status=404) + pi = PostImage.objects.filter(post=post, name=image_name) if not pi: return HttpResponse('No image found!', status=404) diff --git a/templates/blog/post.html b/templates/blog/post.html index 61fc094a..8da852a4 100644 --- a/templates/blog/post.html +++ b/templates/blog/post.html @@ -183,7 +183,7 @@ background: #ffffff0d; {% if messages %} {% for message in messages %} {% if 'spam' in message.tags %} - <p style="color: #ffb6b6;">Your comment was not allowed as it was marked as possible spam. If you think this is a mistake, please contact me at <a href="mailto:[email protected]">[email protected]</a>. ({{ message }})</p> + <p style="color: #ffb6b6;">Your comment was not allowed as it was marked as possible spam. If you think this is a mistake, please contact me at <a href="mailto:[email protected]">[email protected]</a>.</p> {% endif %} {% endfor %} {% endif %} |
