censor_bear 0.1.1 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0cdef69a9d04136c6116ed8245122bda98d27849a91e313487282be5d0640c4d
4
- data.tar.gz: 5088b2e4f868403f242b62344af06edbe47daf4e90bbbaa264848e7c982e574d
3
+ metadata.gz: afa762c57b39c4646ab5d236763e02f0f7cdb8f32a0e9b38058ccf342382e526
4
+ data.tar.gz: e82d0275c7be7525037a91451d852d54549686f3fed0ca4d73a142a64f2e7b34
5
5
  SHA512:
6
- metadata.gz: 3b9138dc7b8af468e6fb59960f1bdc6801927c986758c90d709f49ce408b4a6127bf520e5480198c667717f24a2bfc9cd657938e385a5440c3c811862f47f682
7
- data.tar.gz: 80966dea001f69dd7db9a1a64d6c687a67062a39fd01b2809a3a1c5d9b028b2066526e69594730805f76c975c49255da8974e6063e2446e7136aa2cf8bd61d89
6
+ metadata.gz: e7e2ca96a5d99afcfdbac547d67bfa7e4dbe21e321e2a260db2006cbf1943577cb8b9306150a01d308e23c25f6d51399af7f4cbbfb592be50d3a4a0d4d8fc91f
7
+ data.tar.gz: 95fe89608248a273f2fa5e2560afe8ec9510555bee25afe542057c93f04799cc841b34d23be8846f8fcc1eeccfd7846a70b2887d926062f0f1a4f5fded287def
@@ -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"
@@ -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
 
@@ -25,34 +25,39 @@ module CensorBear
25
25
  return Result.new(@content) unless CensorBear::StopWord::FIELDS.include?(@type)
26
26
 
27
27
  if @content.match(QQ_REG)
28
- CensorBear::Log.create(
29
- original_content: @content,
30
- action: "#{@type}_regex_qq",
31
- filtered_content: nil,
32
- mod_words: @mod_words
33
- )
28
+ CensorBear.info(@content, @type, 'BANNED', 'qq_regex')
34
29
  raise NotPassedException
35
30
  end
36
31
 
37
32
  if @content.match(WX_REG)
38
- @is_mod = true
39
- CensorBear::Log.create(
40
- original_content: @content,
41
- action: "#{@type}_regex_wx",
42
- filtered_content: nil,
43
- mod_words: @mod_words
44
- )
33
+ CensorBear.info(@content, @type, 'BANNED', 'wx_regex')
45
34
  raise NotPassedException
46
35
  end
47
36
 
48
37
  local_check
49
38
 
50
- # 用户相关类型直接封禁,速错
39
+ # 用户相关类型直接禁止创建
51
40
  raise NotPassedException if @is_mod && %w[username signature dialog nickname].include?(@type)
52
41
 
53
42
  Result.new(@content, @is_mod, @mod_words.uniq)
54
43
  end
55
44
 
45
+ # default type = :ugc
46
+ def check_search
47
+ result = false
48
+ stop_words = CensorBear::StopWord.where("#{@type} != 'IGNORE'")
49
+ stop_words.each do |word|
50
+ finder = Regexp.new(Regexp.escape(word.key))
51
+ action = word.send(@type.to_sym).upcase
52
+ if finder.match(@content)
53
+ result = true
54
+ CensorBear.info(@content, @type, action, 'check_search', mod_words: [word.key])
55
+ break
56
+ end
57
+ end
58
+ result
59
+ end
60
+
56
61
  def local_check
57
62
  original_content = @content.dup
58
63
  stop_words = CensorBear::StopWord.where("#{@type} != 'IGNORE'")
@@ -64,14 +69,26 @@ module CensorBear
64
69
  case action
65
70
  when 'REPLACE'
66
71
  @content = @content.gsub(finder, word.replacement)
67
- CensorBear::Log.create(original_content: original_content, action: "#{@type}_#{action}", filtered_content: @content, mod_words: [word.key])
72
+ CensorBear.info(
73
+ original_content, @type, action, 'local_check',
74
+ filtered_content: @content,
75
+ mod_words: [word.key]
76
+ )
68
77
  when 'MOD'
69
78
  @is_mod = true
70
- CensorBear::Log.create(original_content: original_content, action: "#{@type}_#{action}", filtered_content: nil, mod_words: [word.key])
79
+ CensorBear.info(
80
+ original_content, @type, action, 'local_check',
81
+ filtered_content: nil,
82
+ mod_words: [word.key]
83
+ )
71
84
  when 'BANNED'
72
85
  @is_mod = true
73
86
  # 禁止的直接抛出异常
74
- CensorBear::Log.create(original_content: original_content, action: "#{@type}_#{action}", filtered_content: nil, mod_words: [word.key])
87
+ CensorBear.info(
88
+ original_content, @type, action, 'local_check',
89
+ filtered_content: nil,
90
+ mod_words: [word.key]
91
+ )
75
92
  raise NotPassedException
76
93
  end
77
94
  end
@@ -1,3 +1,3 @@
1
1
  module CensorBear
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.5'.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
@@ -31,6 +31,23 @@ module CensorBear
31
31
  Censor.new(content, type).check_text
32
32
  end
33
33
 
34
+ def check_search(content)
35
+ Censor.new(content, "ugc").check_search
36
+ end
37
+
38
+ def info(content, scenario, action, stage, options = {})
39
+ filtered_content = options[:filtered_content] || nil
40
+ mod_words = options[:mod_words] || []
41
+
42
+ CensorBear::Log.create(
43
+ original_content: content,
44
+ scenario: scenario,
45
+ action: action,
46
+ stage: stage,
47
+ filtered_content: filtered_content,
48
+ mod_words: mod_words
49
+ )
50
+ end
34
51
 
35
52
  def action_options
36
53
  I18n.t('censor_log.action').map { |k, v| [v, k.to_s] }
@@ -4,7 +4,7 @@ namespace :censor_bear do
4
4
  from_file = File.read(CensorBear.config.format_from_file_path)
5
5
  File.open(CensorBear.config.format_dest_file_path, 'w') do |f|
6
6
  from_file.each_line do |word|
7
- line = "#{word.strip!}={BANNED}|{BANNED}|{BANNED}|{BANNED}\r\n"
7
+ line = "#{word.strip}={BANNED}|{BANNED}|{BANNED}|{BANNED}\r\n"
8
8
  f.write(line)
9
9
  f.flush
10
10
  end
@@ -35,7 +35,7 @@ namespace :censor_bear do
35
35
  dialog: dialog,
36
36
  nickname: nickname,
37
37
  replacement: replacement,
38
- user_id: User.last&.id,
38
+ user_id: CensorBear.config.user_class.constantize.last&.id,
39
39
  created_at: Time.now,
40
40
  updated_at: Time.now
41
41
  }
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.1
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - 42up
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-01 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