Skip to content

Commit

Permalink
feat: moderator can review and delete reported posts
Browse files Browse the repository at this point in the history
  • Loading branch information
Waishnav committed Jun 25, 2024
1 parent fa01c1c commit 9431f83
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 5 deletions.
6 changes: 6 additions & 0 deletions app/controllers/simple_discussion/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions app/controllers/simple_discussion/forum_posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/simple_discussion/forum_threads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/forum_post.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/spam_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ class SpamReport < ApplicationRecord
irrelevant_content: 2,
misleading_content: 3,
others: 4
}
}
end
8 changes: 8 additions & 0 deletions app/views/layouts/simple_discussion.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
<%= t('.unanswered') %>
</div>
<% end %>
<% if is_moderator? %>
<%= forum_link_to simple_discussion.spam_reports_forum_threads_path do %>
<div class="btn-link">
<%= icon "fa-fw fa", "exclamation-triangle" %>
<%= t('.spam_posts') %>
</div>
<% end %>
<% end %>
</div>
<% if @forum_thread.present? && @forum_thread.persisted? %>
<div class="mt-3">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<%= icon("fas","ellipsis-v") %>
</button>
<div class="dropdown-menu dropdown-menu-right" data-dropdown-target="dropdownMenu">
<%= link_to t('report_spam'), "#", class: "dropdown-item", data: { controller: "report-spam", report_spam_target: "reportSpamButton", toggle: "modal", target: "#reportSpamModal", post_id: forum_post.id } %>
<% if is_moderator_or_owner?(forum_post) %>
<%= link_to t('edit_post'), simple_discussion.edit_forum_thread_forum_post_path(@forum_thread, forum_post),
class: "dropdown-item",
Expand All @@ -29,6 +28,7 @@
<%= link_to t('mark_as_solution'), simple_discussion.solved_forum_thread_forum_post_path(@forum_thread, forum_post), method: :put, class: "dropdown-item" %>
<% end %>
<% end %>
<%= link_to t('report_spam'), "#", class: "dropdown-item", data: { controller: "report-spam", report_spam_target: "reportSpamButton", toggle: "modal", target: "#reportSpamModal", post_id: forum_post.id } %>
</div>
</div>
<% end %>
Expand Down
57 changes: 57 additions & 0 deletions app/views/simple_discussion/forum_posts/_spam_report.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<%= content_tag :tr, id: dom_id(spam_report), class: "forum-post" do %>
<td>
<div class="border rounded-lg">
<div class="forum-post-header">
<div class="user-details d-flex flex-row">
<div class="user-avatar mr-2" ><img src="<%= gravatar_url_for(spam_report.forum_post.user.email, size: 30) %>" alt="avatar of user" ></div>
<div class="details d-flex flex-column justify-content-between">
<p class="title">
<%= spam_report.forum_post.user.name %> <%= forum_user_badge(spam_report.forum_post.user) %>
</p>
<p class="subtitle"><%= t('on') %> &nbsp;<%= spam_report.forum_post.created_at.strftime("%b %d, %Y") %></p>
</div>
</div>
</div>

<div class="card-body p-3">
<%= formatted_content spam_report.forum_post.body %>
</div>
</div>
</td>

<td>
<div class="d-flex flex-row justify-content-between align-items-center p-2">
<% if spam_report.reason == "others" %>
<%= spam_report.details %>
<% else %>
<%= spam_report.reason.humanize %>
<% end %>
</div>
</td>

<td>
<div class="d-flex flex-row justify-content-between align-items-center p-2">
<%= link_to spam_report.user.name, user_path(spam_report.user), class: "btn btn-outline-primary", title: t('user_profile') %>
</div>
</td>
<td>
<div class="d-flex flex-row justify-content-between align-items-center p-2">
<%= 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 %>
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-square-arrow-out-up-right"><path d="M21 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h6"/><path d="m21 3-9 9"/><path d="M15 3h6v6"/></svg>
<% end %>
</div>
</td>
<td>
<div class="d-flex flex-row justify-content-between align-items-center p-2">
<%= 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 %>
<i class="fas fa-trash-alt"></i>
<% end %>
</div>
</td>
<% end %>
16 changes: 16 additions & 0 deletions app/views/simple_discussion/forum_threads/spam_reports.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<table class="table">
<thead>
<tr>
<th class="col-md-6" scope="col">Forum Post</th>
<th scope="col">Reason</th>
<th scope="col">Reported by</th>
<th scope="col">View in Thread</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
<%= render partial: "simple_discussion/forum_posts/spam_report", collection: @spam_reports%>
</tbody>
</table>

<%= will_paginate @spam_reports, url_builder: simple_discussion, renderer: SimpleDiscussion::BootstrapLinkRenderer %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
get :unanswered
get :mine
get :participating
get :spam_reports
get "category/:id", to: "forum_categories#index", as: :forum_category
end

Expand Down

0 comments on commit 9431f83

Please sign in to comment.