censor_bear 0.1.0 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b7bf815d7375b755966c23467029b2219e3a3c3645bc93d0796b9afb0720bdc
4
- data.tar.gz: 82a58299728acbeca22d1bf5ec0739ed2a07d4150e6c48639decc0a652f9480a
3
+ metadata.gz: '084b5a745f3e2eeef9a39a15bc2f87a9a0dadfe1b0cb41a30ac4bf131338c074'
4
+ data.tar.gz: c1351c23521fad8b976b36cb7d8d4eeaaf04688eb9c3463152ddd6edbd786019
5
5
  SHA512:
6
- metadata.gz: c403de59e6da357406191a1cbbeecba4dc0b1fcadd5fca74ffdd6ba32a3fca4f63fdd0c0b32d02db6956916e28bb998ca180080eaa6c0296235fbf731d58b7de
7
- data.tar.gz: 84e32c7fc6e3d029829fcbc6bf65da0901a816e7a411a37cb09beb75cf2e0fecd2de9a929c708038f40b9476e5d725becc449539f737a8d734f168e7d8063eaa
6
+ metadata.gz: 71533c0392c3e6af14ad00fed488dcbc41d1cfe8d6dff8ee8664735857ee158f78ae70ab644b073e9f5e60c48ba4a05ee1d35a61310bccf31368892d897f8714
7
+ data.tar.gz: f97b66983de762c530c79d2b541c756092c5fb1e2e6fd93178783cd26633121df88afe32cd28c4cc4851215c191901614ea255dc0375397cdceb42a321382157
@@ -46,7 +46,7 @@ module CensorBear
46
46
 
47
47
  def load_stop_words
48
48
  builder ||= stop_word_scope
49
- builder = builder.where("word ilike ?", "%#{params[:q]}%") if params[:q].present?
49
+ builder = builder.where("key ilike ?", "%#{params[:q]}%") if params[:q].present?
50
50
  builder = builder.order(id: :desc)
51
51
 
52
52
  @pagy, @stop_words = pagy(builder)
@@ -1,5 +1,9 @@
1
1
  module CensorBear
2
2
  module ApplicationHelper
3
3
  include Pagy::Frontend
4
+
5
+ def current_user
6
+ main_app.scope.request.env['warden'].user
7
+ end
4
8
  end
5
9
  end
@@ -17,6 +17,8 @@
17
17
  <% @logs.each do |log| %>
18
18
  <tr>
19
19
  <td><%= log.original_content %></td>
20
+ <td><%= log.scenario %></td>
21
+ <td><%= log.stage %></td>
20
22
  <td><%= log.action %></td>
21
23
  <td><%= log.filtered_content %></td>
22
24
  <td><%= log.mod_words %></td>
@@ -10,6 +10,16 @@
10
10
  <%= @log.action %>
11
11
  </p>
12
12
 
13
+ <p>
14
+ <strong>Scenario:</strong>
15
+ <%= @log.scenario %>
16
+ </p>
17
+
18
+ <p>
19
+ <strong>Stage:</strong>
20
+ <%= @log.stage %>
21
+ </p>
22
+
13
23
  <p>
14
24
  <strong>Filtered content:</strong>
15
25
  <%= @log.filtered_content %>
@@ -10,19 +10,19 @@
10
10
  %div= form.text_field :key, class: "border p-1 w-full rounded-md"
11
11
  %div
12
12
  %div{class: "text-gray-500 text-xs"}= form.label :ugc, "UGC内容处理方式"
13
- %div= form.select :ugc, options_for_select(CensorBear.action_options, params[:action_type]), class: "border p-1 w-full rounded-md"
13
+ %div= form.select :ugc, options_for_select(CensorBear.action_options, stop_word.ugc), class: "border p-1 w-full rounded-md"
14
14
  %div
15
15
  %div{class: "text-gray-500 text-xs"}= form.label :username, "用户名处理方式"
16
- %div= form.select :username, options_for_select(CensorBear.action_options, params[:action_type]), class: "border p-1 w-full rounded-md"
16
+ %div= form.select :username, options_for_select(CensorBear.action_options, stop_word.username), class: "border p-1 w-full rounded-md"
17
17
  %div
18
18
  %div{class: "text-gray-500 text-xs"}= form.label :nickname, "用户昵称处理方式"
19
- %div= form.select :nickname, options_for_select(CensorBear.action_options, params[:action_type]), class: "border p-1 w-full rounded-md"
19
+ %div= form.select :nickname, options_for_select(CensorBear.action_options, stop_word.nickname), class: "border p-1 w-full rounded-md"
20
20
  %div
21
21
  %div{class: "text-gray-500 text-xs"}= form.label :signature, "签名处理方式"
22
- %div= form.select :signature, options_for_select(CensorBear.action_options, params[:action_type]), class: "border p-1 w-full rounded-md"
22
+ %div= form.select :signature, options_for_select(CensorBear.action_options, stop_word.signature), class: "border p-1 w-full rounded-md"
23
23
  %div
24
24
  %div{class: "text-gray-500 text-xs"}= form.label :dialog, "私信处理方式"
25
- %div= form.select :dialog, options_for_select(CensorBear.action_options, params[:action_type]), class: "border p-1 w-full rounded-md"
25
+ %div= form.select :dialog, options_for_select(CensorBear.action_options, stop_word.dialog), class: "border p-1 w-full rounded-md"
26
26
  %div
27
27
  %div{class: "text-gray-500 text-xs"}= form.label :replacement, "过滤词替换"
28
28
  %div= form.text_field :replacement, class: "border p-1 w-full rounded-md"
@@ -12,5 +12,7 @@ class CreateCensorBearStopWords < ActiveRecord::Migration[6.1]
12
12
 
13
13
  t.timestamps
14
14
  end
15
+
16
+ add_index :censor_bear_stop_words, :key, unique: true
15
17
  end
16
18
  end
@@ -2,7 +2,9 @@ class CreateCensorBearLogs < ActiveRecord::Migration[6.1]
2
2
  def change
3
3
  create_table :censor_bear_logs do |t|
4
4
  t.text :original_content
5
+ t.string :scenario
5
6
  t.string :action
7
+ t.string :stage
6
8
  t.text :filtered_content
7
9
  t.string :mod_words, array: true, default: []
8
10
 
@@ -10,6 +10,9 @@ module CensorBear
10
10
  end
11
11
 
12
12
  class Censor
13
+ QQ_REG = /(?:[加茄qQ企鹅号码\s]{1,}|[群号]{1,}|[叩叩]{1,}|[抠抠]{1,}|[扣扣]{1,})(?:[\u4e00-\u9eff]*)(?:[:,:]?)([\d\s]{6,})/
14
+ WX_REG = /(?:[加+微++➕薇?vV威卫星♥❤姓xX信]{2,}|weixin|weix)(?:[,❤️.\s]?)(?:[\u4e00-\u9eff]?)(?:[:,:]?)([\w\s]{6,})/
15
+
13
16
  def initialize(content, type = 'ugc')
14
17
  @is_mod = false
15
18
  @mod_words = []
@@ -21,9 +24,19 @@ module CensorBear
21
24
  return Result.new(@content) if @content.blank?
22
25
  return Result.new(@content) unless CensorBear::StopWord::FIELDS.include?(@type)
23
26
 
27
+ if @content.match(QQ_REG)
28
+ CensorBear.info(@content, @type, 'BANNED', 'qq_regex')
29
+ raise NotPassedException
30
+ end
31
+
32
+ if @content.match(WX_REG)
33
+ CensorBear.info(@content, @type, 'BANNED', 'wx_regex')
34
+ raise NotPassedException
35
+ end
36
+
24
37
  local_check
25
38
 
26
- # 用户相关类型直接封禁,速错
39
+ # 用户相关类型直接禁止创建
27
40
  raise NotPassedException if @is_mod && %w[username signature dialog nickname].include?(@type)
28
41
 
29
42
  Result.new(@content, @is_mod, @mod_words.uniq)
@@ -40,14 +53,26 @@ module CensorBear
40
53
  case action
41
54
  when 'REPLACE'
42
55
  @content = @content.gsub(finder, word.replacement)
43
- CensorBear::Log.create(original_content: original_content, action: "#{@type}_#{action}", filtered_content: @content, mod_words: [word.key])
56
+ CensorBear.info(
57
+ original_content, @type, action, 'local_check',
58
+ filtered_content: @content,
59
+ mod_words: [word.key]
60
+ )
44
61
  when 'MOD'
45
62
  @is_mod = true
46
- CensorBear::Log.create(original_content: original_content, action: "#{@type}_#{action}", filtered_content: nil, mod_words: [word.key])
63
+ CensorBear.info(
64
+ original_content, @type, action, 'local_check',
65
+ filtered_content: nil,
66
+ mod_words: [word.key]
67
+ )
47
68
  when 'BANNED'
48
69
  @is_mod = true
49
70
  # 禁止的直接抛出异常
50
- CensorBear::Log.create(original_content: original_content, action: "#{@type}_#{action}", filtered_content: nil, mod_words: [word.key])
71
+ CensorBear.info(
72
+ original_content, @type, action, 'local_check',
73
+ filtered_content: nil,
74
+ mod_words: [word.key]
75
+ )
51
76
  raise NotPassedException
52
77
  end
53
78
  end
@@ -8,5 +8,10 @@ module CensorBear
8
8
 
9
9
  # user name method, default: "name"
10
10
  attr_accessor :user_name_method
11
+
12
+ # import task file locaation
13
+ attr_accessor :import_file_path
14
+ attr_accessor :format_from_file_path
15
+ attr_accessor :format_dest_file_path
11
16
  end
12
17
  end
@@ -1,3 +1,3 @@
1
1
  module CensorBear
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.4'.freeze
3
3
  end
data/lib/censor_bear.rb CHANGED
@@ -1,9 +1,9 @@
1
- require "censor_bear/version"
2
- require "censor_bear/engine"
3
- require "censor_bear/censor"
4
- require "censor_bear/configuration"
1
+ require 'censor_bear/version'
2
+ require 'censor_bear/engine'
3
+ require 'censor_bear/censor'
4
+ require 'censor_bear/configuration'
5
5
 
6
- require "hamlit"
6
+ require 'hamlit'
7
7
  require 'pagy'
8
8
 
9
9
  module CensorBear
@@ -17,6 +17,9 @@ module CensorBear
17
17
  @config.user_class = 'User'
18
18
  @config.current_user_method = 'current_user'
19
19
  @config.user_name_method = 'name'
20
+ @config.import_file_path = "#{Rails.root}/tmp/dest_sensitives.txt"
21
+ @config.format_from_file_path = "#{Rails.root}/tmp/from_sensitives.txt"
22
+ @config.format_dest_file_path = "#{Rails.root}/tmp/dest_sensitives.txt"
20
23
  @config
21
24
  end
22
25
 
@@ -28,6 +31,19 @@ module CensorBear
28
31
  Censor.new(content, type).check_text
29
32
  end
30
33
 
34
+ def info(content, scenario, action, stage, options = {})
35
+ filtered_content = options['filtered_content'] || nil
36
+ mod_words = options['mod_words'] || []
37
+
38
+ CensorBear::Log.create(
39
+ original_content: content,
40
+ scenario: scenario,
41
+ action: action,
42
+ stage: stage,
43
+ filtered_content: filtered_content,
44
+ mod_words: mod_words
45
+ )
46
+ end
31
47
 
32
48
  def action_options
33
49
  I18n.t('censor_log.action').map { |k, v| [v, k.to_s] }
@@ -1,4 +1,58 @@
1
- # desc "Explaining what the task does"
2
- # task :censor_bear do
3
- # # Task goes here
4
- # end
1
+ namespace :censor_bear do
2
+ desc 'Format words'
3
+ task format: :environment do
4
+ from_file = File.read(CensorBear.config.format_from_file_path)
5
+ File.open(CensorBear.config.format_dest_file_path, 'w') do |f|
6
+ from_file.each_line do |word|
7
+ line = "#{word.strip}={BANNED}|{BANNED}|{BANNED}|{BANNED}\r\n"
8
+ f.write(line)
9
+ f.flush
10
+ end
11
+ end
12
+ end
13
+
14
+ desc 'Import words'
15
+ task import: :environment do
16
+ is_overwrite = true
17
+ file = File.read(CensorBear.config.import_file_path)
18
+ # lines = doc.split(/\n/)
19
+ file.each_line do |line|
20
+ line.strip!
21
+ key, rule_str = line.split('=')
22
+ rules = rule_str.split('|')
23
+ ugc = parse(rules[0]).blank? ? 'replace' : parse(rules[0])
24
+ username = parse(rules[1]) || 'ignore'
25
+ signature = parse(rules[2]) || 'ignore'
26
+ dialog = parse(rules[3]) || 'ignore'
27
+ nickname = parse(rules[1]) || 'ignore'
28
+ replacement = ugc == 'replace' ? rules[0] : '**'
29
+
30
+ payload = {
31
+ key: key,
32
+ ugc: ugc,
33
+ username: username,
34
+ signature: signature,
35
+ dialog: dialog,
36
+ nickname: nickname,
37
+ replacement: replacement,
38
+ user_id: CensorBear.config.user_class.constantize.last&.id,
39
+ created_at: Time.now,
40
+ updated_at: Time.now
41
+ }
42
+
43
+ word = CensorBear::StopWord.find_by(key: key)
44
+ if word
45
+ word.update_columns(payload) if is_overwrite
46
+ else
47
+ CensorBear::StopWord.create(payload)
48
+ end
49
+ end
50
+ end
51
+
52
+ def parse(rule)
53
+ return nil if rule.blank?
54
+
55
+ r = rule.match(/^{(\w+)}$/)
56
+ r.blank? ? nil : r[1].downcase
57
+ end
58
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: censor_bear
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - 42up
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-30 00:00:00.000000000 Z
11
+ date: 2021-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,20 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 6.1.4
19
+ version: 6.0.4
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 6.1.4.1
22
+ version: 6.0.4.1
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - "~>"
28
28
  - !ruby/object:Gem::Version
29
- version: 6.1.4
29
+ version: 6.0.4
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 6.1.4.1
32
+ version: 6.0.4.1
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: hamlit
35
35
  requirement: !ruby/object:Gem::Requirement