From fa01c1c7697f78dcfdd2b15d3877ac7a2c392af2 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Tue, 25 Jun 2024 12:07:37 +0530 Subject: [PATCH] feat: user can now report the forum post as spam --- .../simple_discussion/controllers/index.js | 2 + .../controllers/report_spam_controller.js | 18 ++++++++ .../application_controller.rb | 2 +- .../forum_posts_controller.rb | 11 +++++ app/models/forum_post.rb | 1 + app/models/spam_report.rb | 15 +++++++ .../forum_posts/_forum_post.html.erb | 44 +++++++++---------- .../_report_spam_modal_form.html.erb | 37 ++++++++++++++++ .../forum_threads/_form.html.erb | 1 - .../forum_threads/show.html.erb | 1 + bin/rails | 2 +- config/routes.rb | 1 + .../20240624124709_create_spam_reports.rb | 12 +++++ 13 files changed, 120 insertions(+), 27 deletions(-) create mode 100644 app/assets/javascripts/simple_discussion/controllers/report_spam_controller.js create mode 100644 app/models/spam_report.rb create mode 100644 app/views/simple_discussion/forum_posts/_report_spam_modal_form.html.erb create mode 100644 db/migrate/20240624124709_create_spam_reports.rb diff --git a/app/assets/javascripts/simple_discussion/controllers/index.js b/app/assets/javascripts/simple_discussion/controllers/index.js index 19e8b72..e293ae3 100644 --- a/app/assets/javascripts/simple_discussion/controllers/index.js +++ b/app/assets/javascripts/simple_discussion/controllers/index.js @@ -1,5 +1,7 @@ import { application } from "./application" import DropdownController from "./dropdown_controller" +import ReportSpamController from "./report_spam_controller"; application.register("dropdown", DropdownController); +application.register("report-spam", ReportSpamController); diff --git a/app/assets/javascripts/simple_discussion/controllers/report_spam_controller.js b/app/assets/javascripts/simple_discussion/controllers/report_spam_controller.js new file mode 100644 index 0000000..1250f7f --- /dev/null +++ b/app/assets/javascripts/simple_discussion/controllers/report_spam_controller.js @@ -0,0 +1,18 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + static targets = ["reportSpamButton"] + + connect() { + const reportSpamForm = document.getElementById("reportSpamForm") + const postId = this.element.dataset.postId + this.reportSpamButtonTarget.addEventListener("click", () => { + const formActionArray = reportSpamForm.action.split("/") + if (formActionArray[formActionArray.length - 2] === "threads") { + reportSpamForm.action += `/posts/${postId}/report_spam` + } else { + reportSpamForm.action = reportSpamForm.action.replace(/\/\d+\//, `/${postId}/`) + } + }) + } +} diff --git a/app/controllers/simple_discussion/application_controller.rb b/app/controllers/simple_discussion/application_controller.rb index e5d408f..0d041f7 100644 --- a/app/controllers/simple_discussion/application_controller.rb +++ b/app/controllers/simple_discussion/application_controller.rb @@ -13,7 +13,7 @@ def is_moderator_or_owner?(object) helper_method :is_moderator_or_owner? def is_moderator? - current_user.moderator? + current_user&.moderator? end helper_method :is_moderator? diff --git a/app/controllers/simple_discussion/forum_posts_controller.rb b/app/controllers/simple_discussion/forum_posts_controller.rb index 4303e97..06662ed 100644 --- a/app/controllers/simple_discussion/forum_posts_controller.rb +++ b/app/controllers/simple_discussion/forum_posts_controller.rb @@ -52,6 +52,17 @@ def unsolved redirect_to simple_discussion.forum_thread_path(@forum_thread, anchor: ActionView::RecordIdentifier.dom_id(@forum_post)) end + def report_spam + @forum_post = @forum_thread.forum_posts.find(params[:id]) + @spam_report = SpamReport.new(forum_post: @forum_post, user: current_user, reason: params[:reason], details: params[:details]) + + if @spam_report.save + redirect_to simple_discussion.forum_thread_path(@forum_thread, anchor: ActionView::RecordIdentifier.dom_id(@forum_post)) + else + render template: "simple_discussion/forum_threads/show" + end + end + private def set_forum_thread diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index a361d18..8be9045 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -1,6 +1,7 @@ class ForumPost < ApplicationRecord belongs_to :forum_thread, counter_cache: true, touch: true belongs_to :user + has_many :spam_posts, dependent: :destroy validate :clean_body validates :user_id, :body, presence: true diff --git a/app/models/spam_report.rb b/app/models/spam_report.rb new file mode 100644 index 0000000..3347a49 --- /dev/null +++ b/app/models/spam_report.rb @@ -0,0 +1,15 @@ +class SpamReport < ApplicationRecord + belongs_to :forum_post + belongs_to :user + + validates :forum_post_id, :user_id, :reason, presence: true + validates :details, presence: true, if: -> { reason == 'other' } + + enum reason: { + sexual_content: 0, + violent_content: 1, + irrelevant_content: 2, + misleading_content: 3, + others: 4 +} +end diff --git a/app/views/simple_discussion/forum_posts/_forum_post.html.erb b/app/views/simple_discussion/forum_posts/_forum_post.html.erb index c9f0f5f..647e025 100644 --- a/app/views/simple_discussion/forum_posts/_forum_post.html.erb +++ b/app/views/simple_discussion/forum_posts/_forum_post.html.erb @@ -1,42 +1,38 @@ -<%# We don't currently cache the forum posts because they have permissions to deal with %> - <%= content_tag :div, id: forum_post.solved ? 'solution' : dom_id(forum_post), class: "forum-post" do %>
- <% if is_moderator_or_owner?(forum_post) %> -
- <% if forum_post.solved? %> - <%= t('solution') %> - <% end %> +
+ <% if forum_post.solved? %> + <%= t('solution') %> + <% end %> + <% if user_signed_in? %> -
- <% end %> + <% end %> +
avatar of user
diff --git a/app/views/simple_discussion/forum_posts/_report_spam_modal_form.html.erb b/app/views/simple_discussion/forum_posts/_report_spam_modal_form.html.erb new file mode 100644 index 0000000..2b64a44 --- /dev/null +++ b/app/views/simple_discussion/forum_posts/_report_spam_modal_form.html.erb @@ -0,0 +1,37 @@ + diff --git a/app/views/simple_discussion/forum_threads/_form.html.erb b/app/views/simple_discussion/forum_threads/_form.html.erb index 64f2d18..49e37e8 100644 --- a/app/views/simple_discussion/forum_threads/_form.html.erb +++ b/app/views/simple_discussion/forum_threads/_form.html.erb @@ -36,5 +36,4 @@ <%= f.button "Update Thread", class: "btn forum-primary-btn", data: {disable_with: " #{t('saving')}"} %> <% end %>
- <% end %> diff --git a/app/views/simple_discussion/forum_threads/show.html.erb b/app/views/simple_discussion/forum_threads/show.html.erb index 874cc91..84ec509 100644 --- a/app/views/simple_discussion/forum_threads/show.html.erb +++ b/app/views/simple_discussion/forum_threads/show.html.erb @@ -39,5 +39,6 @@ <%= render partial: "simple_discussion/forum_posts/forum_post", collection: @forum_thread.forum_posts.includes(:user).sorted %> <%= render partial: "simple_discussion/forum_posts/form" if user_signed_in? %> +<%= render partial: "simple_discussion/forum_posts/report_spam_modal_form", locals: { forum_thread: @forum_thread } if user_signed_in? %> <%= render partial: "login_bar" if !user_signed_in? %> diff --git a/bin/rails b/bin/rails index c789bdc..1514c33 100755 --- a/bin/rails +++ b/bin/rails @@ -3,7 +3,7 @@ # installed from the root of your application. ENGINE_ROOT = File.expand_path('..', __dir__) -ENGINE_PATH = File.expand_path('../lib/pay/engine', __dir__) +ENGINE_PATH = File.expand_path('../lib/simple_discussion/engine', __dir__) APP_PATH = File.expand_path('../test/dummy/config/application', __dir__) # Set up gems listed in the Gemfile. diff --git a/config/routes.rb b/config/routes.rb index b490da3..fcf44bf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,6 +13,7 @@ member do put :solved put :unsolved + post :report_spam end end diff --git a/db/migrate/20240624124709_create_spam_reports.rb b/db/migrate/20240624124709_create_spam_reports.rb new file mode 100644 index 0000000..a744dfc --- /dev/null +++ b/db/migrate/20240624124709_create_spam_reports.rb @@ -0,0 +1,12 @@ +class CreateSpamReports < ActiveRecord::Migration[7.0] + def change + create_table :spam_reports do |t| + t.references :forum_post, null: false, foreign_key: true + t.references :user, null: false, foreign_key: true # The user who reported the post + t.integer :reason, null: false # Enum for reason + t.text :details # optional column if the reason is 'other' + + t.timestamps + end + end +end