From ba26daa5e01b05a12f4ce551c31d9c3d9fdd1054 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Mon, 17 Jun 2024 15:10:54 +0530 Subject: [PATCH] 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 | 15 +++++++++++++++ app/models/forum_thread.rb | 17 +++++++++++++++++ .../forum_posts/_form.html.erb | 12 ++++-------- .../forum_threads/_form.html.erb | 6 +----- simple_discussion.gemspec | 1 + 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index 267d5e5..a361d18 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -2,12 +2,27 @@ class ForumPost < ApplicationRecord belongs_to :forum_thread, counter_cache: true, touch: true belongs_to :user + validate :clean_body validates :user_id, :body, presence: true scope :sorted, -> { order(:created_at) } after_update :solve_forum_thread, if: :solved? + def clean_body + filters = [:profanity, :sex, :violence, :hate] + detected_words = Set.new + + filters.each do |matchlist| + filter = LanguageFilter::Filter.new(matchlist: matchlist) + detected_words.merge(filter.matched(body)) if filter.match?(body) + end + + if detected_words.any? + errors.add(:body, "contains inappropriate language: #{detected_words.to_a.join(', ')}") + end + end + def solve_forum_thread forum_thread.update(solved: true) end diff --git a/app/models/forum_thread.rb b/app/models/forum_thread.rb index ec0d044..dd98024 100644 --- a/app/models/forum_thread.rb +++ b/app/models/forum_thread.rb @@ -1,6 +1,7 @@ class ForumThread < ApplicationRecord extend FriendlyId friendly_id :title, use: :slugged + validate :clean_title belongs_to :forum_category belongs_to :user @@ -22,6 +23,22 @@ class ForumThread < ApplicationRecord scope :unpinned, -> { where.not(pinned: true) } scope :unsolved, -> { where.not(solved: true) } + def clean_title + puts "title: #{title}" + filters = [:profanity, :sex, :violence, :hate] + + detected_words = Set.new + + filters.each do |matchlist| + filter = LanguageFilter::Filter.new(matchlist: matchlist) + detected_words.merge(filter.matched(title)) if filter.match?(title) + end + + if detected_words.any? + errors.add(:title, "contains inappropriate language: #{detected_words.to_a.join(', ')}") + end + end + def subscribed_users (users + optin_subscribers).uniq - optout_subscribers end diff --git a/app/views/simple_discussion/forum_posts/_form.html.erb b/app/views/simple_discussion/forum_posts/_form.html.erb index eb99b33..1a99524 100644 --- a/app/views/simple_discussion/forum_posts/_form.html.erb +++ b/app/views/simple_discussion/forum_posts/_form.html.erb @@ -4,14 +4,10 @@ html: { data: { behavior: "comment-form" } } do |f| %> <% if @forum_post.errors.any? %> -
-

<%= pluralize(@forum_post.errors.count, "error") %> <%= t("forum_thread_error_explanation") %>

- - + <% end %> diff --git a/app/views/simple_discussion/forum_threads/_form.html.erb b/app/views/simple_discussion/forum_threads/_form.html.erb index 2582d56..64f2d18 100644 --- a/app/views/simple_discussion/forum_threads/_form.html.erb +++ b/app/views/simple_discussion/forum_threads/_form.html.erb @@ -3,14 +3,10 @@ html: { data: {behavior: "comment-form"} } do |f| %> <% if @forum_thread.errors.any? %> -
-

<%= pluralize(@forum_thread.errors.count, "error") %> <%= t("forum_thread_error_explanation") %>

- -
    +
<% end %> diff --git a/simple_discussion.gemspec b/simple_discussion.gemspec index c243509..d7da08a 100644 --- a/simple_discussion.gemspec +++ b/simple_discussion.gemspec @@ -26,5 +26,6 @@ Gem::Specification.new do |spec| spec.add_dependency "friendly_id", ">= 5.2.0" 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