forked from excid3/simple_discussion
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: user can now report the forum post as spam
- Loading branch information
Showing
13 changed files
with
120 additions
and
27 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
app/assets/javascripts/simple_discussion/controllers/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
18 changes: 18 additions & 0 deletions
18
app/assets/javascripts/simple_discussion/controllers/report_spam_controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}/`) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
44 changes: 20 additions & 24 deletions
44
app/views/simple_discussion/forum_posts/_forum_post.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/views/simple_discussion/forum_posts/_report_spam_modal_form.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<div class="modal fade" id="reportSpamModal" tabindex="-1" role="dialog" aria-labelledby="reportSpamModalLabel" aria-hidden="true"> | ||
<div class="modal-dialog modal-dialog-centered" role="document"> | ||
<div class="modal-content"> | ||
<div class="modal-header"> | ||
<h5 class="modal-title" id="reportSpamModalLabel">Report Post as Spam</h5> | ||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"> | ||
<span aria-hidden="true">×</span> | ||
</button> | ||
</div> | ||
<div class="modal-body"> | ||
<%= form_with id:"reportSpamForm", url: simple_discussion.forum_thread_path(forum_thread), method: :post, local: true do |f| %> | ||
<% if @spam_report && @spam_report.errors.any? %> | ||
<div class="alert alert-danger" role="alert"> | ||
<% @spam_report.errors.full_messages.each do |message| %> | ||
<li><%= message %></li> | ||
<% end %> | ||
</div> | ||
<% end %> | ||
|
||
<div class="form-group"> | ||
<%= f.collection_radio_buttons :reason, SpamReport.reasons.keys, :to_s, :humanize, include_hidden: false do |b| %> | ||
<div class="form-check"> | ||
<%= b.radio_button(class: "form-check-input") %> | ||
<%= b.label(class: "form-check-label") %> | ||
</div> | ||
<% end %> | ||
</div> | ||
<div class="form-group"> | ||
<%= f.label :details, "Reason in detail (if others)" %> | ||
<%= f.text_area :details, class: "form-control", rows: 2 %> | ||
</div> | ||
<%= f.submit "Report", class: "btn btn-danger" %> | ||
<% end %> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
member do | ||
put :solved | ||
put :unsolved | ||
post :report_spam | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |