From 9431f834119a60f5e5118b95997d8598bdfd2924 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Tue, 25 Jun 2024 16:22:39 +0530 Subject: [PATCH] feat: moderator can review and delete reported posts --- .../application_controller.rb | 6 ++ .../forum_posts_controller.rb | 18 +++++- .../forum_threads_controller.rb | 6 ++ app/models/forum_post.rb | 2 +- app/models/spam_report.rb | 2 +- app/views/layouts/simple_discussion.html.erb | 8 +++ .../forum_posts/_forum_post.html.erb | 2 +- .../forum_posts/_spam_report.html.erb | 57 +++++++++++++++++++ .../forum_threads/spam_reports.html.erb | 16 ++++++ config/routes.rb | 1 + 10 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 app/views/simple_discussion/forum_posts/_spam_report.html.erb create mode 100644 app/views/simple_discussion/forum_threads/spam_reports.html.erb diff --git a/app/controllers/simple_discussion/application_controller.rb b/app/controllers/simple_discussion/application_controller.rb index 0d041f7..7ed17df 100644 --- a/app/controllers/simple_discussion/application_controller.rb +++ b/app/controllers/simple_discussion/application_controller.rb @@ -29,6 +29,12 @@ def require_mod_or_author_for_thread! end end + def require_mod! + unless is_moderator? + redirect_to_root + end + end + private def redirect_to_root diff --git a/app/controllers/simple_discussion/forum_posts_controller.rb b/app/controllers/simple_discussion/forum_posts_controller.rb index 06662ed..a797a26 100644 --- a/app/controllers/simple_discussion/forum_posts_controller.rb +++ b/app/controllers/simple_discussion/forum_posts_controller.rb @@ -29,8 +29,22 @@ def update end def destroy - @forum_post.destroy! - redirect_to simple_discussion.forum_thread_path(@forum_thread) + # if @forum_post is first post of forum_thread then we need to destroy forum_thread + if @forum_thread.forum_posts.first == @forum_post + @forum_thread.destroy! + if params[:from] == "moderators_page" + redirect_to simple_discussion.spam_reports_forum_threads_path + else + redirect_to simple_discussion.root_path + end + else + @forum_post.destroy! + if params[:from] == "moderators_page" + redirect_to simple_discussion.spam_reports_forum_threads_path + else + redirect_to simple_discussion.forum_thread_path(@forum_thread) + end + end end def solved diff --git a/app/controllers/simple_discussion/forum_threads_controller.rb b/app/controllers/simple_discussion/forum_threads_controller.rb index cdc1fce..ce1f0dc 100644 --- a/app/controllers/simple_discussion/forum_threads_controller.rb +++ b/app/controllers/simple_discussion/forum_threads_controller.rb @@ -2,6 +2,7 @@ class SimpleDiscussion::ForumThreadsController < SimpleDiscussion::ApplicationCo before_action :authenticate_user!, only: [:mine, :participating, :new, :create] before_action :set_forum_thread, only: [:show, :edit, :update, :destroy] before_action :require_mod_or_author_for_thread!, only: [:edit, :update, :destroy] + before_action :require_mod!, only: [:spam_reports] def index @forum_threads = ForumThread.pinned_first.sorted.includes(:user, :forum_category).paginate(page: page_number) @@ -27,6 +28,11 @@ def participating render action: :index end + def spam_reports + @spam_reports = SpamReport.includes(:forum_post).paginate(page: page_number) + render action: :spam_reports + end + def show @forum_post = ForumPost.new @forum_post.user = current_user diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index 8be9045..46efe03 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -1,7 +1,7 @@ class ForumPost < ApplicationRecord belongs_to :forum_thread, counter_cache: true, touch: true belongs_to :user - has_many :spam_posts, dependent: :destroy + has_many :spam_reports, 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 index 3347a49..cf9bbd5 100644 --- a/app/models/spam_report.rb +++ b/app/models/spam_report.rb @@ -11,5 +11,5 @@ class SpamReport < ApplicationRecord irrelevant_content: 2, misleading_content: 3, others: 4 -} + } end diff --git a/app/views/layouts/simple_discussion.html.erb b/app/views/layouts/simple_discussion.html.erb index 886791d..f66a978 100644 --- a/app/views/layouts/simple_discussion.html.erb +++ b/app/views/layouts/simple_discussion.html.erb @@ -41,6 +41,14 @@ <%= t('.unanswered') %> <% end %> + <% if is_moderator? %> + <%= forum_link_to simple_discussion.spam_reports_forum_threads_path do %> + + <% end %> + <% end %> <% if @forum_thread.present? && @forum_thread.persisted? %>
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 647e025..ec1e2fc 100644 --- a/app/views/simple_discussion/forum_posts/_forum_post.html.erb +++ b/app/views/simple_discussion/forum_posts/_forum_post.html.erb @@ -10,7 +10,6 @@ <%= icon("fas","ellipsis-v") %>
<% end %> diff --git a/app/views/simple_discussion/forum_posts/_spam_report.html.erb b/app/views/simple_discussion/forum_posts/_spam_report.html.erb new file mode 100644 index 0000000..1768f5a --- /dev/null +++ b/app/views/simple_discussion/forum_posts/_spam_report.html.erb @@ -0,0 +1,57 @@ +<%= content_tag :tr, id: dom_id(spam_report), class: "forum-post" do %> + +
+
+
+
avatar of user
+
+

+ <%= spam_report.forum_post.user.name %> <%= forum_user_badge(spam_report.forum_post.user) %> +

+

<%= t('on') %>  <%= spam_report.forum_post.created_at.strftime("%b %d, %Y") %>

+
+
+
+ +
+ <%= formatted_content spam_report.forum_post.body %> +
+
+ + + +
+ <% if spam_report.reason == "others" %> + <%= spam_report.details %> + <% else %> + <%= spam_report.reason.humanize %> + <% end %> +
+ + + +
+ <%= link_to spam_report.user.name, user_path(spam_report.user), class: "btn btn-outline-primary", title: t('user_profile') %> +
+ + +
+ <%= link_to simple_discussion.forum_thread_path(spam_report.forum_post.forum_thread, anchor: "forum_post_#{spam_report.forum_post.id}"), + class: "btn btn-dark", + data: { toggle: "tooltip", placement: "left" } do %> + + <% end %> +
+ + +
+ <%= link_to simple_discussion.forum_thread_forum_post_path(spam_report.forum_post.forum_thread, spam_report.forum_post, from: "moderators_page"), + method: :delete, + data: { toggle: "tooltip", placement: "left", confirm: "Are you sure you want to delete this post?" }, + title: t('delete_this_post'), + class: "btn btn-danger" do %> + + <% end %> +
+ +<% end %> diff --git a/app/views/simple_discussion/forum_threads/spam_reports.html.erb b/app/views/simple_discussion/forum_threads/spam_reports.html.erb new file mode 100644 index 0000000..e5ffbdd --- /dev/null +++ b/app/views/simple_discussion/forum_threads/spam_reports.html.erb @@ -0,0 +1,16 @@ + + + + + + + + + + + + <%= render partial: "simple_discussion/forum_posts/spam_report", collection: @spam_reports%> + +
Forum PostReasonReported byView in ThreadDelete
+ + <%= will_paginate @spam_reports, url_builder: simple_discussion, renderer: SimpleDiscussion::BootstrapLinkRenderer %> diff --git a/config/routes.rb b/config/routes.rb index fcf44bf..1410ad5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,6 +6,7 @@ get :unanswered get :mine get :participating + get :spam_reports get "category/:id", to: "forum_categories#index", as: :forum_category end