Skip to content

Commit

Permalink
Merge branch 'master' into markdown-syntax-extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Waishnav authored Aug 21, 2024
2 parents 6881aa5 + 470e204 commit 562b11e
Show file tree
Hide file tree
Showing 14 changed files with 210 additions and 42 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ By default, SimpleDiscussion will attempt to send email and slack notifications
SimpleDiscussion.setup do |config|
config.send_email_notifications = false # Default: true
config.send_slack_notifications = false # Default: true

config.markdown_circuit_embed = false # Default: false
config.markdown_user_tagging = false # Default: false
config.markdown_video_embed = false # Default false
end
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def unsolved
redirect_to simple_discussion.forum_thread_path(@forum_thread, anchor: ActionView::RecordIdentifier.dom_id(@forum_post))
end

def report_spam
def report_post
@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])

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 @@ -3,7 +3,7 @@ class SpamReport < ApplicationRecord
belongs_to :user

validates :forum_post_id, :user_id, :reason, presence: true
validates :details, presence: true, if: -> { reason == "other" }
validates :details, presence: true, if: -> { reason == "others" }

enum reason: {
sexual_content: 0,
Expand Down
19 changes: 9 additions & 10 deletions app/views/layouts/simple_discussion.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@

<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.min.js"></script>

<script type="module">
import { Application, Controller } from "https://unpkg.com/@hotwired/stimulus/dist/stimulus.js"
window.Stimulus = Application.start()
Expand All @@ -103,19 +103,19 @@
}
})


Stimulus.register("report-spam", class extends Controller {
static targets = ["reportSpamButton"]
Stimulus.register("report-post", class extends Controller {
static targets = ["reportPostButton"]

connect() {
const reportSpamForm = document.getElementById("reportSpamForm")
const reportPostForm = document.getElementById("reportPostForm")
const postId = this.element.dataset.postId
this.reportSpamButtonTarget.addEventListener("click", () => {
const formActionArray = reportSpamForm.action.split("/")
this.reportPostButtonTarget.addEventListener("click", () => {
const formActionArray = reportPostForm.action.split("/")
console.log(formActionArray)
if (formActionArray[formActionArray.length - 2] === "threads") {
reportSpamForm.action += `/posts/${postId}/report_spam`
reportPostForm.action += `/posts/${postId}/report_post`
} else {
reportSpamForm.action = reportSpamForm.action.replace(/\/\d+\//, `/${postId}/`)
reportPostForm.action = reportPostForm.action.replace(/\/\d+\//, `/${postId}/`)
}
})
}
Expand Down Expand Up @@ -366,4 +366,3 @@
}
})
</script>

20 changes: 11 additions & 9 deletions app/views/simple_discussion/forum_posts/_forum_post.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
<%= content_tag :div, id: forum_post.solved ? 'solution' : dom_id(forum_post), class: "forum-post" do %>
<div class="forum-post-header">
<div class="float-right d-flex align-items-center">
<% if forum_post.solved? %>
<% unless forum_post == @forum_thread.forum_posts.first %>
<% if is_moderator_or_owner?(@forum_thread) %>
<% if @forum_thread.solved? && forum_post.solved? %>
<%= link_to t('unmark_as_solution'), simple_discussion.unsolved_forum_thread_forum_post_path(@forum_thread, forum_post), method: :put, class: "badge badge-info p-2 mr-2" %>
<% else %>
<%= link_to t('mark_as_solution'), simple_discussion.solved_forum_thread_forum_post_path(@forum_thread, forum_post), method: :put, class: "badge badge-info p-2 mr-2" %>
<% end %>
<% end %>
<% end %>
<% if forum_post.solved? && forum_post != @forum_thread.forum_posts.first && !is_moderator_or_owner?(@forum_thread) %>
<span class="badge badge-success p-2 mr-2"><%= t('solution') %></span>
<% end %>
<% if user_signed_in? %>
Expand All @@ -21,14 +30,7 @@
data: { toggle: "tooltip", placement: "left", confirm: "Are you sure you want to delete this post?" },
title: t('delete_this_post') %>
<% end %>
<% if is_moderator_or_owner?(@forum_thread) %>
<% if @forum_thread.solved? && forum_post.solved? %>
<%= link_to t('unmark_as_solution'), simple_discussion.unsolved_forum_thread_forum_post_path(@forum_thread, forum_post), class: "dropdown-item", method: :put %>
<% else %>
<%= 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_post'), "#", class: "dropdown-item", data: { controller: "report-spam", report_spam_target: "reportSpamButton", toggle: "modal", target: "#reportSpamModal", post_id: forum_post.id } %>
<%= link_to t('report_post'), "#", class: "dropdown-item", data: { controller: "report-post", report_post_target: "reportPostButton", toggle: "modal", target: "#reportPostModal", post_id: forum_post.id } %>
</div>
</div>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<div class="modal fade" id="reportPostModal" 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">&times;</span>
</button>
</div>
<div class="modal-body">
<%= form_with id:"reportPostForm", 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 toggle-reason", required: true) %>
<%= b.label(class: "form-check-label") %>
</div>
<% end %>
</div>
<div class="form-group" id="details-group" style="display: none;">
<%= f.label :details, "Reason in detail (if others)" %>
<%= f.text_area :details, class: "form-control", rows: 2, id: "details-input" %>
<div class="invalid-feedback">Please provide details for 'Other' reason.</div>
</div>
<%= f.submit "Report", class: "btn btn-danger" %>
<% end %>
</div>
</div>
</div>
</div>

<script>
$(document).ready(function() {
const $detailsGroup = $('#details-group');
const $detailsInput = $('#details-input');
const $form = $('#reportPostForm');

$('.toggle-reason').change(function() {
if ($(this).val() === 'others') {
$detailsGroup.show();
$detailsInput.attr('required', true);
} else {
$detailsGroup.hide();
$detailsInput.removeAttr('required');
}
});

$form.on('submit', function(e) {
if ($('input[name="reason"]:checked').val() === 'others' && $detailsInput.val().trim() === '') {
e.preventDefault();
$detailsInput.addClass('is-invalid');
} else {
$detailsInput.removeClass('is-invalid');
}
});

$detailsInput.on('input', function() {
$(this).removeClass('is-invalid');
});
});
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<td>
<div class="d-flex flex-row justify-content-between align-items-center p-2">
<% if spam_report.reason == "others" %>
<%= spam_report.details %>
Other:<%= spam_report.details %>
<% else %>
<%= spam_report.reason.humanize %>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/simple_discussion/forum_threads/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +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: "simple_discussion/forum_posts/report_post_modal_form", locals: { forum_thread: @forum_thread } if user_signed_in? %>

<%= render partial: "login_bar" if !user_signed_in? %>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
member do
put :solved
put :unsolved
post :report_spam
post :report_post
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ class User < ApplicationRecord
:recoverable, :rememberable, :validatable

def moderator?
true
moderator
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddModeratorToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :moderator, :boolean, default: false
end
end
48 changes: 35 additions & 13 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,39 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_07_28_092034) do
ActiveRecord::Schema.define(version: 2024_08_13_072347) do
create_table "forum_categories", force: :cascade do |t|
t.string "name", null: false
t.string "slug", null: false
t.string "color", default: "000000"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
end

create_table "forum_leaderboards", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "points", default: 0, null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.index ["points"], name: "index_forum_leaderboards_on_points"
t.index ["user_id"], name: "index_forum_leaderboards_on_user_id", unique: true
end

create_table "forum_posts", force: :cascade do |t|
t.integer "forum_thread_id"
t.integer "user_id"
t.text "body"
t.boolean "solved", default: false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
end

create_table "forum_subscriptions", force: :cascade do |t|
t.integer "forum_thread_id"
t.integer "user_id"
t.string "subscription_type"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
end

create_table "forum_threads", force: :cascade do |t|
Expand All @@ -44,8 +53,19 @@
t.integer "forum_posts_count", default: 0
t.boolean "pinned", default: false
t.boolean "solved", default: false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
end

create_table "spam_reports", force: :cascade do |t|
t.integer "forum_post_id", null: false
t.integer "user_id", null: false
t.integer "reason", null: false
t.text "details"
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.index ["forum_post_id"], name: "index_spam_reports_on_forum_post_id"
t.index ["user_id"], name: "index_spam_reports_on_user_id"
end

create_table "spam_reports", force: :cascade do |t|
Expand All @@ -63,15 +83,17 @@
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "reset_password_sent_at", precision: nil
t.datetime "remember_created_at", precision: nil
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.string "name"
t.boolean "moderator", default: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

add_foreign_key "forum_leaderboards", "users"
add_foreign_key "forum_posts", "forum_threads"
add_foreign_key "forum_posts", "users"
add_foreign_key "forum_subscriptions", "forum_threads"
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ one:
two:
name: "Second User"
email: "second@example.com"

moderator:
name: "Moderator"
email: "moderator@moderator.com"
moderator: true
Loading

0 comments on commit 562b11e

Please sign in to comment.