From 734e9086729eec1dce76139fec259f59749a5e85 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Mon, 17 Jun 2024 15:10:54 +0530 Subject: [PATCH 01/33] added the profanity, hate, violance, sex filter using `language_filter` gem - these filters are applied on forum_thread's title, forum_posts's body - error messages are now improves with proper altert message styling using bootstrap's alter class --- app/models/forum_post.rb | 1 + app/models/forum_thread.rb | 1 + simple_discussion.gemspec | 1 + 3 files changed, 3 insertions(+) diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index dfd9202..4a086e7 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -3,6 +3,7 @@ class ForumPost < ApplicationRecord belongs_to :forum_thread, counter_cache: true, touch: true belongs_to :user + validate :clean_body validates :user_id, :body, presence: true validate :clean_body, if: -> { SimpleDiscussion.profanity_filter } diff --git a/app/models/forum_thread.rb b/app/models/forum_thread.rb index 18ad3c7..3e4377d 100644 --- a/app/models/forum_thread.rb +++ b/app/models/forum_thread.rb @@ -2,6 +2,7 @@ class ForumThread < ApplicationRecord extend FriendlyId friendly_id :title, use: :slugged + validate :clean_title belongs_to :forum_category belongs_to :user diff --git a/simple_discussion.gemspec b/simple_discussion.gemspec index d7da08a..3bcc7c4 100644 --- a/simple_discussion.gemspec +++ b/simple_discussion.gemspec @@ -27,5 +27,6 @@ Gem::Specification.new do |spec| spec.add_dependency "rails", ">= 4.2" spec.add_dependency "will_paginate", ">= 3.1.0" spec.add_dependency "language_filter", ">= 0.3.01" + spec.metadata["rubygems_mfa_required"] = "true" end From 08b32d0eace7450f4c9aac5081cc834e916ea060 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Mon, 24 Jun 2024 00:03:01 +0530 Subject: [PATCH 02/33] fix: moderator can delete thread --- .../simple_discussion/application_controller.rb | 2 +- .../simple_discussion/forum_threads_controller.rb | 9 +++++++-- app/models/forum_thread.rb | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/controllers/simple_discussion/application_controller.rb b/app/controllers/simple_discussion/application_controller.rb index 11c3841..e5d408f 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.respond_to?(:moderator) && current_user.moderator? + current_user.moderator? end helper_method :is_moderator? diff --git a/app/controllers/simple_discussion/forum_threads_controller.rb b/app/controllers/simple_discussion/forum_threads_controller.rb index 6edc92d..570597a 100644 --- a/app/controllers/simple_discussion/forum_threads_controller.rb +++ b/app/controllers/simple_discussion/forum_threads_controller.rb @@ -1,7 +1,7 @@ class SimpleDiscussion::ForumThreadsController < SimpleDiscussion::ApplicationController before_action :authenticate_user!, only: [:mine, :participating, :new, :create] - before_action :set_forum_thread, only: [:show, :edit, :update] - before_action :require_mod_or_author_for_thread!, only: [:edit, :update] + before_action :set_forum_thread, only: [:show, :edit, :update, :destroy] + before_action :require_mod_or_author_for_thread!, only: [:edit, :update, :destroy] def index @forum_threads = ForumThread.pinned_first.sorted.includes(:user, :forum_category).paginate(page: page_number) @@ -60,6 +60,11 @@ def update end end + def destroy + @forum_thread.destroy! + redirect_to simple_discussion.forum_threads_path + end + private def set_forum_thread diff --git a/app/models/forum_thread.rb b/app/models/forum_thread.rb index 3e4377d..607710b 100644 --- a/app/models/forum_thread.rb +++ b/app/models/forum_thread.rb @@ -6,7 +6,7 @@ class ForumThread < ApplicationRecord belongs_to :forum_category belongs_to :user - has_many :forum_posts + has_many :forum_posts, dependent: :destroy has_many :forum_subscriptions has_many :optin_subscribers, -> { where(forum_subscriptions: {subscription_type: :optin}) }, through: :forum_subscriptions, source: :user has_many :optout_subscribers, -> { where(forum_subscriptions: {subscription_type: :optout}) }, through: :forum_subscriptions, source: :user From 1d002a5166652144c4d6b6d2a3b4f1fbbaf24310 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Mon, 24 Jun 2024 00:04:24 +0530 Subject: [PATCH 03/33] added js for dropdown, and for markdown editor in future --- Gemfile | 8 ++++---- .../simple_discussion/application.js | 1 + .../controllers/application.js | 9 +++++++++ .../controllers/dropdown_controller.js | 20 +++++++++++++++++++ .../simple_discussion/controllers/index.js | 5 +++++ app/views/layouts/simple_discussion.html.erb | 4 ++++ lib/simple_discussion/engine.rb | 19 ++++++++++++++++++ package.json | 15 ++++++++++++++ 8 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 app/assets/javascripts/simple_discussion/application.js create mode 100644 app/assets/javascripts/simple_discussion/controllers/application.js create mode 100644 app/assets/javascripts/simple_discussion/controllers/dropdown_controller.js create mode 100644 app/assets/javascripts/simple_discussion/controllers/index.js create mode 100644 package.json diff --git a/Gemfile b/Gemfile index ae8dea2..e550bbe 100644 --- a/Gemfile +++ b/Gemfile @@ -8,12 +8,12 @@ gemspec # used for dummy rails app integration gem "devise" gem "puma" -gem "sprockets-rails" - -# testing against sqlite3 db -gem "sqlite3", "~> 1.7" # testing gem "appraisal" gem "standardrb" gem "font-awesome-sass", "~> 5.13.1" +gem "sqlite3" + +gem "sprockets-rails" +gem "jsbundling-rails", "~> 1.1" diff --git a/app/assets/javascripts/simple_discussion/application.js b/app/assets/javascripts/simple_discussion/application.js new file mode 100644 index 0000000..6ec9251 --- /dev/null +++ b/app/assets/javascripts/simple_discussion/application.js @@ -0,0 +1 @@ +import "./controllers"; diff --git a/app/assets/javascripts/simple_discussion/controllers/application.js b/app/assets/javascripts/simple_discussion/controllers/application.js new file mode 100644 index 0000000..f408ed0 --- /dev/null +++ b/app/assets/javascripts/simple_discussion/controllers/application.js @@ -0,0 +1,9 @@ +import { Application } from "@hotwired/stimulus" + +const application = Application.start() + +application.warnings = true +application.debug = false +window.Stimulus = application + +export { application } diff --git a/app/assets/javascripts/simple_discussion/controllers/dropdown_controller.js b/app/assets/javascripts/simple_discussion/controllers/dropdown_controller.js new file mode 100644 index 0000000..c828ddb --- /dev/null +++ b/app/assets/javascripts/simple_discussion/controllers/dropdown_controller.js @@ -0,0 +1,20 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + static targets = ["dropdownButton", "dropdownMenu"] + + connect() { + this.dropdownButtonTarget.addEventListener("click", this.toggleDropdown.bind(this)) + // if click somewhere else in the document, close the dropdown + window.addEventListener("click", (event) => { + if (!this.dropdownButtonTarget.contains(event.target)) { + this.dropdownMenuTarget.classList.remove("show") + } + }) + } + + // note that we are using bootstrap + toggleDropdown() { + this.dropdownMenuTarget.classList.toggle("show") + } +} diff --git a/app/assets/javascripts/simple_discussion/controllers/index.js b/app/assets/javascripts/simple_discussion/controllers/index.js new file mode 100644 index 0000000..19e8b72 --- /dev/null +++ b/app/assets/javascripts/simple_discussion/controllers/index.js @@ -0,0 +1,5 @@ +import { application } from "./application" + +import DropdownController from "./dropdown_controller" + +application.register("dropdown", DropdownController); diff --git a/app/views/layouts/simple_discussion.html.erb b/app/views/layouts/simple_discussion.html.erb index 80f1e73..886791d 100644 --- a/app/views/layouts/simple_discussion.html.erb +++ b/app/views/layouts/simple_discussion.html.erb @@ -67,3 +67,7 @@ <% parent_layout("application") %> + +<% SimpleDiscussion::Engine.javascripts.each do |js_path| %> + <%= javascript_include_tag js_path %> +<% end %> diff --git a/lib/simple_discussion/engine.rb b/lib/simple_discussion/engine.rb index 5181d2c..54229cf 100644 --- a/lib/simple_discussion/engine.rb +++ b/lib/simple_discussion/engine.rb @@ -6,5 +6,24 @@ class Engine < ::Rails::Engine config.after_initialize do SimpleDiscussion::Engine.routes.default_url_options = ActionMailer::Base.default_url_options end + + # javascripts assets precompiled for dropdown and markdown text editor + @@javascripts = [] + + initializer "simple_discussion.assets.precompile" do |app| + app.config.assets.precompile += [ + "simple_discussion/application.js", + ] + end + + def self.add_javascript(script) + @@javascripts << script + end + + def self.javascripts + @@javascripts + end + + add_javascript "simple_discussion/application.js" end end diff --git a/package.json b/package.json new file mode 100644 index 0000000..688eee6 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "simple_discussion", + "version": "1.0.0", + "description": "javascript asset precompiling for this gem", + "author": "CircuitVerse", + "license": "MIT", + "homepage": "https://github.com/circuitverse/simple_discussion#readme", + "scripts": { + "build": "esbuild app/assets/javascripts/simple_discussion/application.js --bundle --sourcemap --outdir=app/assets/builds/simple_discussion --public-path=/assets/simple_discussion" + }, + "dependencies": { + "@hotwired/stimulus": "^3.2.2", + "esbuild": "^0.20.2" + } +} From 4d48d4d5353c4f66e73907338c932149fa571d39 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Mon, 24 Jun 2024 00:10:23 +0530 Subject: [PATCH 04/33] ui: dropdown for delete, edit, mark as solution actions --- .gitignore | 1 + .../forum_posts/_forum_post.html.erb | 69 +++++++++---------- .../forum_posts/edit.html.erb | 4 +- .../forum_threads/show.html.erb | 24 +++++++ 4 files changed, 60 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index ccca47c..ebfa704 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ .log .sqlite3 /test/dummy/tmp +/node_modules 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 88e4809..c9f0f5f 100644 --- a/app/views/simple_discussion/forum_posts/_forum_post.html.erb +++ b/app/views/simple_discussion/forum_posts/_forum_post.html.erb @@ -1,19 +1,40 @@ <%# We don't currently cache the forum posts because they have permissions to deal with %> -<%= content_tag :div, id: dom_id(forum_post), class: "forum-post" do %> -
- +<%= content_tag :div, id: forum_post.solved ? 'solution' : dom_id(forum_post), class: "forum-post" do %> +
<% if is_moderator_or_owner?(forum_post) %> -
- <%= link_to icon("fas","edit"), simple_discussion.edit_forum_thread_forum_post_path(@forum_thread, forum_post), - class: "text-muted", - data: { toggle: "tooltip", placement: "left" }, - title: t('edit_this_post') %>   - <%= link_to icon("fas","trash"), simple_discussion.forum_thread_forum_post_path(@forum_thread, forum_post), - class: "text-muted", - method: :delete, - data: { toggle: "tooltip", placement: "left", confirm: "Are you sure you want to delete this post?" }, - title: t('delete_this_post') %> +
+ <% if forum_post.solved? %> + <%= t('solution') %> + <% end %> +
<% end %>
@@ -30,26 +51,4 @@
<%= formatted_content forum_post.body %>
- - <% if @forum_thread.solved? && forum_post.solved? %> - - - <% elsif is_moderator_or_owner?(@forum_thread) %> - - <% end %> - <% end %> diff --git a/app/views/simple_discussion/forum_posts/edit.html.erb b/app/views/simple_discussion/forum_posts/edit.html.erb index b7ea600..f529b3f 100644 --- a/app/views/simple_discussion/forum_posts/edit.html.erb +++ b/app/views/simple_discussion/forum_posts/edit.html.erb @@ -25,7 +25,5 @@
-
- <%= render "form" %> -
+ <%= render "form" %> <% end %> diff --git a/app/views/simple_discussion/forum_threads/show.html.erb b/app/views/simple_discussion/forum_threads/show.html.erb index eb73340..874cc91 100644 --- a/app/views/simple_discussion/forum_threads/show.html.erb +++ b/app/views/simple_discussion/forum_threads/show.html.erb @@ -1,9 +1,33 @@

<%= icon "fas", "thumb-tack", class: "text-muted" if @forum_thread.pinned? %> <%= @forum_thread.title %>

+
+ <% if @forum_thread.solved? %> + <%= link_to t('jump_to_solution'), simple_discussion.forum_thread_path(@forum_thread, anchor: "solution"), + class: "btn btn-outline-success h-25 mr-2", + title: t('jump_to_solution') + %> + <% end %> + <% if is_moderator_or_owner?(@forum_thread) %> + + <% end %> +
+
<%= t("created_by") %> <%= @forum_thread.user.name %> • <%= time_ago_in_words(@forum_thread.created_at) %> <%= t("ago") %> From 24b22b1ea429b27e5458030efaa30d076f6b43ae Mon Sep 17 00:00:00 2001 From: Waishnav Date: Tue, 25 Jun 2024 12:07:37 +0530 Subject: [PATCH 05/33] 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 + config/routes.rb | 1 + .../20240624124709_create_spam_reports.rb | 12 +++++ 12 files changed, 119 insertions(+), 26 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 23e59e9..afe51c8 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 4a086e7..863ed65 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -2,6 +2,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/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 From 155f8d41ebec724782c47fa1e1b9562794801bbd Mon Sep 17 00:00:00 2001 From: Waishnav Date: Tue, 25 Jun 2024 16:22:39 +0530 Subject: [PATCH 06/33] 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 afe51c8..6ed761d 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 570597a..048879c 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 863ed65..a240906 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -2,7 +2,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 From 0391c977e2dce1bac6f41cafa54a4579d0fc6f04 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Thu, 25 Jul 2024 15:54:31 +0530 Subject: [PATCH 07/33] fix standardrb formatting issue --- app/models/spam_report.rb | 2 +- lib/simple_discussion/engine.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/spam_report.rb b/app/models/spam_report.rb index cf9bbd5..58b8acb 100644 --- a/app/models/spam_report.rb +++ b/app/models/spam_report.rb @@ -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 == "other" } enum reason: { sexual_content: 0, diff --git a/lib/simple_discussion/engine.rb b/lib/simple_discussion/engine.rb index 54229cf..e32ac37 100644 --- a/lib/simple_discussion/engine.rb +++ b/lib/simple_discussion/engine.rb @@ -12,7 +12,7 @@ class Engine < ::Rails::Engine initializer "simple_discussion.assets.precompile" do |app| app.config.assets.precompile += [ - "simple_discussion/application.js", + "simple_discussion/application.js" ] end From 187c67f2d4390ce73532b99527a8d3cde60d0311 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Sat, 27 Jul 2024 15:57:56 +0530 Subject: [PATCH 08/33] fix: renaming report_spam button to report_post --- app/views/simple_discussion/forum_posts/_forum_post.html.erb | 2 +- config/locales/en.yml | 3 +++ config/locales/es.yml | 3 +++ config/locales/fr.yml | 3 +++ 4 files changed, 10 insertions(+), 1 deletion(-) 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 ec1e2fc..fe17edf 100644 --- a/app/views/simple_discussion/forum_posts/_forum_post.html.erb +++ b/app/views/simple_discussion/forum_posts/_forum_post.html.erb @@ -28,7 +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 } %> + <%= 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 } %>
<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index de519dd..82e5b61 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -57,3 +57,6 @@ en: login: "Log in" commented: "commented:" inappropriate_language_error_message: "contains inappropriate language: %{words}" + unmark_as_solution: "Démarquer comme solution" + mark_as_solution: "Marquer comme solution" + report_post: "Report Post" diff --git a/config/locales/es.yml b/config/locales/es.yml index 851831c..804b5e7 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -56,3 +56,6 @@ es: login: "Iniciar sesión" commented: "comentó:" inappropriate_language_error_message: "contiene lenguaje inapropiado: %{words}" + unmark_as_solution: "Desmarcar como solución" + mark_as_solution: "Marcar como solución" + report_post: "Reportar publicación" diff --git a/config/locales/fr.yml b/config/locales/fr.yml index c137054..31b362c 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -58,3 +58,6 @@ fr: login: "Se connecter" commented: "a commenté:" inappropriate_language_error_message: "contient un langage inapproprié : %{words}" + unmark_as_solution: "Démarquer comme solution" + mark_as_solution: "Marquer comme solution" + report_post: "Signaler le post" From 59f4315de4dbad9a98c5a23bbd79eaf8bcac8a74 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Sun, 28 Jul 2024 22:09:57 +0530 Subject: [PATCH 09/33] fix: remove js bundeling --- .gitignore | 1 - Gemfile | 2 - app/views/layouts/simple_discussion.html.erb | 44 ++++++++++++++++++-- config/locales/en.yml | 4 +- lib/simple_discussion/engine.rb | 19 --------- package.json | 15 ------- 6 files changed, 43 insertions(+), 42 deletions(-) delete mode 100644 package.json diff --git a/.gitignore b/.gitignore index ebfa704..ccca47c 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,3 @@ .log .sqlite3 /test/dummy/tmp -/node_modules diff --git a/Gemfile b/Gemfile index e550bbe..5797428 100644 --- a/Gemfile +++ b/Gemfile @@ -14,6 +14,4 @@ gem "appraisal" gem "standardrb" gem "font-awesome-sass", "~> 5.13.1" gem "sqlite3" - gem "sprockets-rails" -gem "jsbundling-rails", "~> 1.1" diff --git a/app/views/layouts/simple_discussion.html.erb b/app/views/layouts/simple_discussion.html.erb index f66a978..b5ca4e9 100644 --- a/app/views/layouts/simple_discussion.html.erb +++ b/app/views/layouts/simple_discussion.html.erb @@ -76,6 +76,44 @@ <% parent_layout("application") %> -<% SimpleDiscussion::Engine.javascripts.each do |js_path| %> - <%= javascript_include_tag js_path %> -<% end %> + diff --git a/config/locales/en.yml b/config/locales/en.yml index 82e5b61..a6943e4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -57,6 +57,6 @@ en: login: "Log in" commented: "commented:" inappropriate_language_error_message: "contains inappropriate language: %{words}" - unmark_as_solution: "Démarquer comme solution" - mark_as_solution: "Marquer comme solution" + unmark_as_solution: "Unmark as Solution" + mark_as_solution: "Mark as Solution" report_post: "Report Post" diff --git a/lib/simple_discussion/engine.rb b/lib/simple_discussion/engine.rb index e32ac37..5181d2c 100644 --- a/lib/simple_discussion/engine.rb +++ b/lib/simple_discussion/engine.rb @@ -6,24 +6,5 @@ class Engine < ::Rails::Engine config.after_initialize do SimpleDiscussion::Engine.routes.default_url_options = ActionMailer::Base.default_url_options end - - # javascripts assets precompiled for dropdown and markdown text editor - @@javascripts = [] - - initializer "simple_discussion.assets.precompile" do |app| - app.config.assets.precompile += [ - "simple_discussion/application.js" - ] - end - - def self.add_javascript(script) - @@javascripts << script - end - - def self.javascripts - @@javascripts - end - - add_javascript "simple_discussion/application.js" end end diff --git a/package.json b/package.json deleted file mode 100644 index 688eee6..0000000 --- a/package.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "name": "simple_discussion", - "version": "1.0.0", - "description": "javascript asset precompiling for this gem", - "author": "CircuitVerse", - "license": "MIT", - "homepage": "https://github.com/circuitverse/simple_discussion#readme", - "scripts": { - "build": "esbuild app/assets/javascripts/simple_discussion/application.js --bundle --sourcemap --outdir=app/assets/builds/simple_discussion --public-path=/assets/simple_discussion" - }, - "dependencies": { - "@hotwired/stimulus": "^3.2.2", - "esbuild": "^0.20.2" - } -} From b926dd48cf21c21820ddb0a0af0c5cc5bd7cf308 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Sun, 28 Jul 2024 22:14:42 +0530 Subject: [PATCH 10/33] fix: ci fail due to old schema in test dummy --- test/dummy/db/schema.rb | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index 6eba6dd..8a46d30 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_03_31_160746) do +ActiveRecord::Schema[7.0].define(version: 2024_07_28_092034) do create_table "forum_categories", force: :cascade do |t| t.string "name", null: false t.string "slug", null: false @@ -48,14 +48,25 @@ t.datetime "updated_at" 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", null: false + t.datetime "updated_at", 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 "users", force: :cascade do |t| 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", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "name" t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true @@ -67,4 +78,6 @@ add_foreign_key "forum_subscriptions", "users" add_foreign_key "forum_threads", "forum_categories" add_foreign_key "forum_threads", "users" + add_foreign_key "spam_reports", "forum_posts" + add_foreign_key "spam_reports", "users" end From cb339c5ee3ff9af95f89cce8501c1ff1286f63f2 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Sun, 28 Jul 2024 22:17:55 +0530 Subject: [PATCH 11/33] fix: ci fail due to migration version --- test/dummy/db/schema.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index 8a46d30..24d545e 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_07_28_092034) do +ActiveRecord::Schema.define(version: 2024_07_28_092034) do create_table "forum_categories", force: :cascade do |t| t.string "name", null: false t.string "slug", null: false From ecf53bb8f4f5f8c5b860299b4729bbacf07f0037 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Sun, 28 Jul 2024 22:22:26 +0530 Subject: [PATCH 12/33] fix: ci fail due to moderator? undefined --- test/dummy/app/models/user.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/dummy/app/models/user.rb b/test/dummy/app/models/user.rb index 5286d1f..0735bb1 100644 --- a/test/dummy/app/models/user.rb +++ b/test/dummy/app/models/user.rb @@ -5,4 +5,8 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable + + def moderator? + true + end end From a8ed0cb96562a07677633fa601df16f686de5a29 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Sun, 28 Jul 2024 22:32:25 +0530 Subject: [PATCH 13/33] removing stimulus controller --- .../simple_discussion/application.js | 1 - .../controllers/application.js | 9 --------- .../controllers/dropdown_controller.js | 20 ------------------- .../simple_discussion/controllers/index.js | 7 ------- .../controllers/report_spam_controller.js | 18 ----------------- 5 files changed, 55 deletions(-) delete mode 100644 app/assets/javascripts/simple_discussion/application.js delete mode 100644 app/assets/javascripts/simple_discussion/controllers/application.js delete mode 100644 app/assets/javascripts/simple_discussion/controllers/dropdown_controller.js delete mode 100644 app/assets/javascripts/simple_discussion/controllers/index.js delete mode 100644 app/assets/javascripts/simple_discussion/controllers/report_spam_controller.js diff --git a/app/assets/javascripts/simple_discussion/application.js b/app/assets/javascripts/simple_discussion/application.js deleted file mode 100644 index 6ec9251..0000000 --- a/app/assets/javascripts/simple_discussion/application.js +++ /dev/null @@ -1 +0,0 @@ -import "./controllers"; diff --git a/app/assets/javascripts/simple_discussion/controllers/application.js b/app/assets/javascripts/simple_discussion/controllers/application.js deleted file mode 100644 index f408ed0..0000000 --- a/app/assets/javascripts/simple_discussion/controllers/application.js +++ /dev/null @@ -1,9 +0,0 @@ -import { Application } from "@hotwired/stimulus" - -const application = Application.start() - -application.warnings = true -application.debug = false -window.Stimulus = application - -export { application } diff --git a/app/assets/javascripts/simple_discussion/controllers/dropdown_controller.js b/app/assets/javascripts/simple_discussion/controllers/dropdown_controller.js deleted file mode 100644 index c828ddb..0000000 --- a/app/assets/javascripts/simple_discussion/controllers/dropdown_controller.js +++ /dev/null @@ -1,20 +0,0 @@ -import { Controller } from "@hotwired/stimulus" - -export default class extends Controller { - static targets = ["dropdownButton", "dropdownMenu"] - - connect() { - this.dropdownButtonTarget.addEventListener("click", this.toggleDropdown.bind(this)) - // if click somewhere else in the document, close the dropdown - window.addEventListener("click", (event) => { - if (!this.dropdownButtonTarget.contains(event.target)) { - this.dropdownMenuTarget.classList.remove("show") - } - }) - } - - // note that we are using bootstrap - toggleDropdown() { - this.dropdownMenuTarget.classList.toggle("show") - } -} diff --git a/app/assets/javascripts/simple_discussion/controllers/index.js b/app/assets/javascripts/simple_discussion/controllers/index.js deleted file mode 100644 index e293ae3..0000000 --- a/app/assets/javascripts/simple_discussion/controllers/index.js +++ /dev/null @@ -1,7 +0,0 @@ -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 deleted file mode 100644 index 1250f7f..0000000 --- a/app/assets/javascripts/simple_discussion/controllers/report_spam_controller.js +++ /dev/null @@ -1,18 +0,0 @@ -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}/`) - } - }) - } -} From 1a85df000c56c765c3210475c8b79ef2ccf63aad Mon Sep 17 00:00:00 2001 From: Waishnav Date: Mon, 17 Jun 2024 15:10:54 +0530 Subject: [PATCH 14/33] added the profanity, hate, violance, sex filter using `language_filter` gem - these filters are applied on forum_thread's title, forum_posts's body - error messages are now improves with proper altert message styling using bootstrap's alter class --- app/models/forum_post.rb | 1 - app/models/forum_thread.rb | 1 - 2 files changed, 2 deletions(-) diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index a240906..80ccced 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -4,7 +4,6 @@ class ForumPost < ApplicationRecord belongs_to :user has_many :spam_reports, dependent: :destroy - validate :clean_body validates :user_id, :body, presence: true validate :clean_body, if: -> { SimpleDiscussion.profanity_filter } diff --git a/app/models/forum_thread.rb b/app/models/forum_thread.rb index 607710b..1bc79e7 100644 --- a/app/models/forum_thread.rb +++ b/app/models/forum_thread.rb @@ -2,7 +2,6 @@ class ForumThread < ApplicationRecord extend FriendlyId friendly_id :title, use: :slugged - validate :clean_title belongs_to :forum_category belongs_to :user From d32eb928a37725a75f09963a586d11e02c728b56 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Mon, 24 Jun 2024 00:04:24 +0530 Subject: [PATCH 15/33] added js for dropdown, and for markdown editor in future --- .../simple_discussion/application.js | 1 + .../controllers/application.js | 9 +++++++++ .../controllers/dropdown_controller.js | 20 +++++++++++++++++++ .../simple_discussion/controllers/index.js | 5 +++++ lib/simple_discussion/engine.rb | 19 ++++++++++++++++++ package.json | 15 ++++++++++++++ 6 files changed, 69 insertions(+) create mode 100644 app/assets/javascripts/simple_discussion/application.js create mode 100644 app/assets/javascripts/simple_discussion/controllers/application.js create mode 100644 app/assets/javascripts/simple_discussion/controllers/dropdown_controller.js create mode 100644 app/assets/javascripts/simple_discussion/controllers/index.js create mode 100644 package.json diff --git a/app/assets/javascripts/simple_discussion/application.js b/app/assets/javascripts/simple_discussion/application.js new file mode 100644 index 0000000..6ec9251 --- /dev/null +++ b/app/assets/javascripts/simple_discussion/application.js @@ -0,0 +1 @@ +import "./controllers"; diff --git a/app/assets/javascripts/simple_discussion/controllers/application.js b/app/assets/javascripts/simple_discussion/controllers/application.js new file mode 100644 index 0000000..f408ed0 --- /dev/null +++ b/app/assets/javascripts/simple_discussion/controllers/application.js @@ -0,0 +1,9 @@ +import { Application } from "@hotwired/stimulus" + +const application = Application.start() + +application.warnings = true +application.debug = false +window.Stimulus = application + +export { application } diff --git a/app/assets/javascripts/simple_discussion/controllers/dropdown_controller.js b/app/assets/javascripts/simple_discussion/controllers/dropdown_controller.js new file mode 100644 index 0000000..c828ddb --- /dev/null +++ b/app/assets/javascripts/simple_discussion/controllers/dropdown_controller.js @@ -0,0 +1,20 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + static targets = ["dropdownButton", "dropdownMenu"] + + connect() { + this.dropdownButtonTarget.addEventListener("click", this.toggleDropdown.bind(this)) + // if click somewhere else in the document, close the dropdown + window.addEventListener("click", (event) => { + if (!this.dropdownButtonTarget.contains(event.target)) { + this.dropdownMenuTarget.classList.remove("show") + } + }) + } + + // note that we are using bootstrap + toggleDropdown() { + this.dropdownMenuTarget.classList.toggle("show") + } +} diff --git a/app/assets/javascripts/simple_discussion/controllers/index.js b/app/assets/javascripts/simple_discussion/controllers/index.js new file mode 100644 index 0000000..19e8b72 --- /dev/null +++ b/app/assets/javascripts/simple_discussion/controllers/index.js @@ -0,0 +1,5 @@ +import { application } from "./application" + +import DropdownController from "./dropdown_controller" + +application.register("dropdown", DropdownController); diff --git a/lib/simple_discussion/engine.rb b/lib/simple_discussion/engine.rb index 5181d2c..54229cf 100644 --- a/lib/simple_discussion/engine.rb +++ b/lib/simple_discussion/engine.rb @@ -6,5 +6,24 @@ class Engine < ::Rails::Engine config.after_initialize do SimpleDiscussion::Engine.routes.default_url_options = ActionMailer::Base.default_url_options end + + # javascripts assets precompiled for dropdown and markdown text editor + @@javascripts = [] + + initializer "simple_discussion.assets.precompile" do |app| + app.config.assets.precompile += [ + "simple_discussion/application.js", + ] + end + + def self.add_javascript(script) + @@javascripts << script + end + + def self.javascripts + @@javascripts + end + + add_javascript "simple_discussion/application.js" end end diff --git a/package.json b/package.json new file mode 100644 index 0000000..688eee6 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "simple_discussion", + "version": "1.0.0", + "description": "javascript asset precompiling for this gem", + "author": "CircuitVerse", + "license": "MIT", + "homepage": "https://github.com/circuitverse/simple_discussion#readme", + "scripts": { + "build": "esbuild app/assets/javascripts/simple_discussion/application.js --bundle --sourcemap --outdir=app/assets/builds/simple_discussion --public-path=/assets/simple_discussion" + }, + "dependencies": { + "@hotwired/stimulus": "^3.2.2", + "esbuild": "^0.20.2" + } +} From b27d1e944f0e7801da00607f94c99d570a566463 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Tue, 25 Jun 2024 12:07:37 +0530 Subject: [PATCH 16/33] feat: user can now report the forum post as spam --- .../simple_discussion/controllers/index.js | 2 ++ .../controllers/report_spam_controller.js | 18 ++++++++++++++++++ .../forum_posts/_forum_post.html.erb | 1 + 3 files changed, 21 insertions(+) create mode 100644 app/assets/javascripts/simple_discussion/controllers/report_spam_controller.js 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/views/simple_discussion/forum_posts/_forum_post.html.erb b/app/views/simple_discussion/forum_posts/_forum_post.html.erb index fe17edf..8db1088 100644 --- a/app/views/simple_discussion/forum_posts/_forum_post.html.erb +++ b/app/views/simple_discussion/forum_posts/_forum_post.html.erb @@ -10,6 +10,7 @@ <%= icon("fas","ellipsis-v") %> -
diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index 341a78c..0000000 --- a/package-lock.json +++ /dev/null @@ -1,118 +0,0 @@ -{ - "name": "simple_discussion", - "version": "1.0.0", - "lockfileVersion": 3, - "requires": true, - "packages": { - "": { - "name": "simple_discussion", - "version": "1.0.0", - "license": "MIT", - "dependencies": { - "@hotwired/stimulus": "^3.2.2", - "bootstrap": "^5.3.3", - "esbuild": "^0.20.2", - "marked": "^13.0.2" - } - }, - "node_modules/@esbuild/linux-x64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.20.2.tgz", - "integrity": "sha512-1MdwI6OOTsfQfek8sLwgyjOXAu+wKhLEoaOLTjbijk6E2WONYpH9ZU2mNtR+lZ2B4uwr+usqGuVfFT9tMtGvGw==", - "cpu": [ - "x64" - ], - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@hotwired/stimulus": { - "version": "3.2.2", - "resolved": "https://registry.npmjs.org/@hotwired/stimulus/-/stimulus-3.2.2.tgz", - "integrity": "sha512-eGeIqNOQpXoPAIP7tC1+1Yc1yl1xnwYqg+3mzqxyrbE5pg5YFBZcA6YoTiByJB6DKAEsiWtl6tjTJS4IYtbB7A==", - "license": "MIT" - }, - "node_modules/@popperjs/core": { - "version": "2.11.8", - "resolved": "https://registry.npmjs.org/@popperjs/core/-/core-2.11.8.tgz", - "integrity": "sha512-P1st0aksCrn9sGZhp8GMYwBnQsbvAWsZAX44oXNNvLHGqAOcoVxmjZiohstwQ7SqKnbR47akdNi+uleWD8+g6A==", - "peer": true, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/popperjs" - } - }, - "node_modules/bootstrap": { - "version": "5.3.3", - "resolved": "https://registry.npmjs.org/bootstrap/-/bootstrap-5.3.3.tgz", - "integrity": "sha512-8HLCdWgyoMguSO9o+aH+iuZ+aht+mzW0u3HIMzVu7Srrpv7EBBxTnrFlSCskwdY1+EOFQSm7uMJhNQHkdPcmjg==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/twbs" - }, - { - "type": "opencollective", - "url": "https://opencollective.com/bootstrap" - } - ], - "peerDependencies": { - "@popperjs/core": "^2.11.8" - } - }, - "node_modules/esbuild": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.20.2.tgz", - "integrity": "sha512-WdOOppmUNU+IbZ0PaDiTst80zjnrOkyJNHoKupIcVyU8Lvla3Ugx94VzkQ32Ijqd7UhHJy75gNWDMUekcrSJ6g==", - "hasInstallScript": true, - "license": "MIT", - "bin": { - "esbuild": "bin/esbuild" - }, - "engines": { - "node": ">=12" - }, - "optionalDependencies": { - "@esbuild/aix-ppc64": "0.20.2", - "@esbuild/android-arm": "0.20.2", - "@esbuild/android-arm64": "0.20.2", - "@esbuild/android-x64": "0.20.2", - "@esbuild/darwin-arm64": "0.20.2", - "@esbuild/darwin-x64": "0.20.2", - "@esbuild/freebsd-arm64": "0.20.2", - "@esbuild/freebsd-x64": "0.20.2", - "@esbuild/linux-arm": "0.20.2", - "@esbuild/linux-arm64": "0.20.2", - "@esbuild/linux-ia32": "0.20.2", - "@esbuild/linux-loong64": "0.20.2", - "@esbuild/linux-mips64el": "0.20.2", - "@esbuild/linux-ppc64": "0.20.2", - "@esbuild/linux-riscv64": "0.20.2", - "@esbuild/linux-s390x": "0.20.2", - "@esbuild/linux-x64": "0.20.2", - "@esbuild/netbsd-x64": "0.20.2", - "@esbuild/openbsd-x64": "0.20.2", - "@esbuild/sunos-x64": "0.20.2", - "@esbuild/win32-arm64": "0.20.2", - "@esbuild/win32-ia32": "0.20.2", - "@esbuild/win32-x64": "0.20.2" - } - }, - "node_modules/marked": { - "version": "13.0.2", - "resolved": "https://registry.npmjs.org/marked/-/marked-13.0.2.tgz", - "integrity": "sha512-J6CPjP8pS5sgrRqxVRvkCIkZ6MFdRIjDkwUwgJ9nL2fbmM6qGQeB2C16hi8Cc9BOzj6xXzy0jyi0iPIfnMHYzA==", - "bin": { - "marked": "bin/marked.js" - }, - "engines": { - "node": ">= 18" - } - } - } -} diff --git a/package.json b/package.json deleted file mode 100644 index 4340c5f..0000000 --- a/package.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "name": "simple_discussion", - "version": "1.0.0", - "description": "javascript asset precompiling for this gem", - "author": "CircuitVerse", - "license": "MIT", - "homepage": "https://github.com/circuitverse/simple_discussion#readme", - "scripts": { - "build": "esbuild app/assets/javascripts/simple_discussion/application.js --bundle --sourcemap --outdir=app/assets/builds/simple_discussion --public-path=/assets/simple_discussion" - }, - "dependencies": { - "@hotwired/stimulus": "^3.2.2", - "bootstrap": "^5.3.3", - "esbuild": "^0.20.2", - "marked": "^13.0.2" - } -} diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index af35396..9b5ad67 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -68,17 +68,6 @@ t.index ["user_id"], name: "index_spam_reports_on_user_id" 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", null: false - t.datetime "updated_at", 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 "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false From 0e7d081e7667c9fb98d19f30b391b2458aa3f772 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Thu, 22 Aug 2024 00:00:31 +0530 Subject: [PATCH 33/33] fix: unecessary changes --- gemfiles/rails_6_1.gemfile | 4 ++-- gemfiles/rails_7.gemfile | 4 ++-- gemfiles/rails_7_1.gemfile | 4 ++-- gemfiles/rails_main.gemfile | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/gemfiles/rails_6_1.gemfile b/gemfiles/rails_6_1.gemfile index 3d03e3e..4045170 100644 --- a/gemfiles/rails_6_1.gemfile +++ b/gemfiles/rails_6_1.gemfile @@ -4,11 +4,11 @@ source "https://rubygems.org" gem "devise" gem "puma" +gem "sprockets-rails" +gem "sqlite3", "~> 1.7.2" gem "appraisal" gem "standardrb" gem "font-awesome-sass", "~> 5.13.1" -gem "sqlite3", "~> 1.7.2" -gem "sprockets-rails" gem "rails", "~> 6.1.0" gemspec path: "../" diff --git a/gemfiles/rails_7.gemfile b/gemfiles/rails_7.gemfile index 3294262..0ddc27e 100644 --- a/gemfiles/rails_7.gemfile +++ b/gemfiles/rails_7.gemfile @@ -4,11 +4,11 @@ source "https://rubygems.org" gem "devise" gem "puma" +gem "sprockets-rails" +gem "sqlite3", "~> 1.7.2" gem "appraisal" gem "standardrb" gem "font-awesome-sass", "~> 5.13.1" -gem "sqlite3", "~> 1.7.2" -gem "sprockets-rails" gem "rails", "~> 7.0.0" gemspec path: "../" diff --git a/gemfiles/rails_7_1.gemfile b/gemfiles/rails_7_1.gemfile index fd50374..c4c702b 100644 --- a/gemfiles/rails_7_1.gemfile +++ b/gemfiles/rails_7_1.gemfile @@ -4,11 +4,11 @@ source "https://rubygems.org" gem "devise" gem "puma" +gem "sprockets-rails" +gem "sqlite3", "~> 1.7.2" gem "appraisal" gem "standardrb" gem "font-awesome-sass", "~> 5.13.1" -gem "sqlite3", "~> 1.7.2" -gem "sprockets-rails" gem "rails", "~> 7.1.0" gemspec path: "../" diff --git a/gemfiles/rails_main.gemfile b/gemfiles/rails_main.gemfile index f943de1..1cdd81c 100644 --- a/gemfiles/rails_main.gemfile +++ b/gemfiles/rails_main.gemfile @@ -4,11 +4,11 @@ source "https://rubygems.org" gem "devise" gem "puma" +gem "sprockets-rails" +gem "sqlite3", "~> 2.0" gem "appraisal" gem "standardrb" gem "font-awesome-sass", "~> 5.13.1" -gem "sqlite3", "~> 2.0" -gem "sprockets-rails" gem "rails", branch: "main", git: "https://github.com/rails/rails" gemspec path: "../"