From 8d039903f630db0b89f2df44a94f4e5c938bd7bf Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Tue, 17 Oct 2023 11:43:07 -0400 Subject: Added marking comments as spam --- app/controllers/admin/comments_controller.rb | 23 ++++++++++++++++++++++ app/controllers/comments_controller.rb | 7 +++++-- app/views/admin/comments/pending.haml | 1 + config/routes.rb | 1 + .../20231017153558_add_more_comment_columns.rb | 9 +++++++++ db/schema.rb | 5 ++++- 6 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20231017153558_add_more_comment_columns.rb diff --git a/app/controllers/admin/comments_controller.rb b/app/controllers/admin/comments_controller.rb index 9958232..d3d948c 100644 --- a/app/controllers/admin/comments_controller.rb +++ b/app/controllers/admin/comments_controller.rb @@ -31,6 +31,29 @@ class Admin::CommentsController < Admin::AdminController redirect_to pending_admin_comments_url end + def mark_spam + @comment = Comment.find(params[:id]) + + akismet_params = { + type: "comment", + text: @comment.body, + created_at: @comment.created_at, + author: @comment.username, + author_email: @comment.email, + author_url: @comment.website, + post_url: url_for(@comment.blog), + post_modified_at: @comment.blog.updated_at, + referrer: @comment.referrer + } + + Akismet.spam @comment.request_ip, @comment.user_agent, akismet_params + + @comment.destroy! + + flash.notice = "Comment successfully marked as spam." + redirect_back pending_admin_comments_url + end + def destroy if Comment.destroy(params[:id]) flash.notice = "Comment successfully deleted." diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 2f3117f..9fa05e4 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -6,6 +6,9 @@ class CommentsController < ApplicationController raise ActiveRecord::RecordNotFound unless @blog.published @comment = @blog.comments.new(comment_params) + @comment.request_ip = request.ip + @comment.user_agent = request.user_agent + @comment.referrer = request.referrer unless @comment.valid? flash.alert = "Error posting comment." @@ -23,11 +26,11 @@ class CommentsController < ApplicationController author_url: @comment.website, post_url: url_for(@comment.blog), post_modified_at: @comment.blog.updated_at, - referrer: request.referrer, + referrer: @comment.referrer, env: request.env.slice(*akismet_vars) } - is_spam, is_blatant = Akismet.check(request.ip, request.user_agent, akismet_params) + is_spam, is_blatant = Akismet.check(@comment.ip, @comment.user_agent, akismet_params) if is_blatant # I am lying. diff --git a/app/views/admin/comments/pending.haml b/app/views/admin/comments/pending.haml index 18ae6c2..3303457 100644 --- a/app/views/admin/comments/pending.haml +++ b/app/views/admin/comments/pending.haml @@ -22,4 +22,5 @@ %ul.admin-actions %li= link_to "Accept", accept_admin_comment_url(comment), method: :post, data: { confirm: "Are you sure you want to accept this comment?" } %li= link_to "Reject", reject_admin_comment_url(comment), method: :post, data: { confirm: "Are you sure you want to reject this comment?" } + %li= link_to "Mark Spam", mark_spam_admin_comment_url(comment), method: :post, data: { confirm: "Are you sure you want to mark this comment as spam?" } = will_paginate @comments diff --git a/config/routes.rb b/config/routes.rb index 4b68dc3..416e939 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -24,6 +24,7 @@ Rails.application.routes.draw do member do post 'accept' post 'reject' + post 'mark_spam' end end end diff --git a/db/migrate/20231017153558_add_more_comment_columns.rb b/db/migrate/20231017153558_add_more_comment_columns.rb new file mode 100644 index 0000000..9ce3e07 --- /dev/null +++ b/db/migrate/20231017153558_add_more_comment_columns.rb @@ -0,0 +1,9 @@ +class AddMoreCommentColumns < ActiveRecord::Migration[7.0] + def change + change_table :comments do |t| + t.string :request_ip + t.string :user_agent + t.string :referrer + end + end +end diff --git a/db/schema.rb b/db/schema.rb index edbb24e..3a9e111 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_10_14_194459) do +ActiveRecord::Schema[7.0].define(version: 2023_10_17_153558) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -97,6 +97,9 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_14_194459) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "reply_to_id" + t.string "request_ip" + t.string "user_agent" + t.string "referrer" t.index ["blog_id"], name: "index_comments_on_blog_id" t.index ["reply_to_id"], name: "index_comments_on_reply_to_id" end -- cgit 1.4.1