censor_bear 0.1.13 → 0.1.14

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: a3bf3dd6d88b6f7ece7434deabf56aa6322c305536e44d5ead4bae5c1aa94111
4
- data.tar.gz: 9484d90389880990a519229d851394f7a484455972316f90f3d9891dda32851d
3
+ metadata.gz: e8f5e80bfe3be4dc22e9c8fc14932f6c830a87efc0ee15d5588bd258f0311e0e
4
+ data.tar.gz: bc47827fb4f09f2cd875d20db1c4593168917709b0174d0780237c503c156e5e
5
5
  SHA512:
6
- metadata.gz: e283297831488e216fe57089166d37084725ef93a387ea615d6897298d95616b181880af06c19e1ce3caf536ac29bdaedf1a25408f7392719390da40d721be8d
7
- data.tar.gz: 11568a082cc47a024aecb4bd7a2b1f09e785b7690e88bba1bb6acb5445b7eb31ac4f4834f4a63c5429fe8a2c1c40ae6e6c72c21efd130903d3cdb4df81a25d16
6
+ metadata.gz: fc58e6353da8c5697b92f2c4857a7dcaa948a6e03c367d14aed8960b015c0809cc33094628efc1983f07865f270ae646bbf05c942dda7a58b425db54f3434f00
7
+ data.tar.gz: d080b530b575ece289451d58079a40e3a85ad0f7721e8707cac299d141ea066047f5b36c13a9a5a7a133edf0e70707d28b85ea622ea4fe20f5309cc5be132462
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,31 @@
1
+ require_dependency 'censor_bear/application_controller'
2
+
3
+ module CensorBear
4
+ class HomeController < ApplicationController
5
+ def index; end
6
+
7
+ def debugger; end
8
+
9
+ def debug
10
+ content = params[:content]
11
+ type = params[:type]
12
+
13
+ case params[:stage]
14
+ when 'aliyun_check'
15
+ @r = AliyunGreen::Text.scan(content)
16
+ when 'check_text'
17
+ @r = CensorBear.check_text(content, type.to_sym)
18
+ when 'check_search'
19
+ # TODO: 搜索前置检查
20
+ @r = {}
21
+ when 'check_image'
22
+ # TODO: 检查图片
23
+ @r = {}
24
+ when 'qq_regex'
25
+ @r = content.match(Censor::QQ_REG)
26
+ when 'wx_regex'
27
+ @r = content.match(Censor::WX_REG)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -55,6 +55,7 @@ module CensorBear
55
55
  def load_logs
56
56
  builder ||= log_scope
57
57
  builder = builder.where("original_content ilike ?", "%#{params[:q]}%") if params[:q].present?
58
+ builder = builder.where(stage: params[:stage]) if params[:stage].present?
58
59
  builder = builder.order(id: :desc)
59
60
 
60
61
  @pagy, @logs = pagy(builder)
@@ -0,0 +1,4 @@
1
+ module CensorBear
2
+ module HomeHelper
3
+ end
4
+ end
@@ -1,5 +1,23 @@
1
1
  module CensorBear
2
2
  class Log < ApplicationRecord
3
3
  belongs_to :user, class_name: CensorBear.config.user_class, optional: true
4
+
5
+ def suggestion
6
+ return nil if response.blank?
7
+
8
+ response['data']&.first['results']&.first['suggestion']
9
+ end
10
+
11
+ def rate
12
+ return nil if response.blank?
13
+
14
+ response['data']&.first['results']&.first['rate']
15
+ end
16
+
17
+ def label
18
+ return nil if response.blank?
19
+
20
+ response['data']&.first['results']&.first['label']
21
+ end
4
22
  end
5
23
  end
@@ -1,7 +1,7 @@
1
1
  module CensorBear
2
2
  class StopWord < ApplicationRecord
3
- FIELDS = %w(ugc username nickname signature dialog)
4
- ACTIONS_MAP = %w(ignore mod banned replace)
3
+ FIELDS = %w(ugc username nickname signature dialog).freeze
4
+ ACTIONS_MAP = %w(ignore mod banned replace).freeze
5
5
  belongs_to :user, class_name: CensorBear.config.user_class
6
6
  end
7
7
  end
@@ -0,0 +1,8 @@
1
+ = turbo_stream.update(:result) do
2
+ %h3{class: "text-lg"} 检测结果
3
+ %div{class: "bg-yellow-100 text-yellow-700 p-4 rounded-lg"}
4
+ %pre{class: "whitespace-pre-wrap"}
5
+ - if @r.is_a?(MatchData)
6
+ = @r.pretty_inspect
7
+ - else
8
+ = JSON.pretty_generate(@r.is_a?(CensorBear::Result) ? JSON.parse(@r.to_json) : @r)
@@ -0,0 +1,9 @@
1
+ %div{class: "flex flex-col space-y-2 rounded-lg border p-4"}
2
+ %h2{class: "text-xl font-medium text-center"} 在线调试
3
+ %div{class: "flex justify-center"}
4
+ = form_with(url: debug_path, method: :post, class: "flex flex-col items-center w-full space-y-4") do |f|
5
+ = f.select :stage, options_for_select([%w[check_text check_text], %w[check_search check_search], %w[check_search check_search], %w[qq_regex qq_regex], %w[wx_regex wx_regex]], params[:stage]), {}, class: "border rounded-md m py-0.5 px-1"
6
+ = f.select :type, options_for_select(CensorBear::StopWord::FIELDS.map{|f| [f.upcase, f]}, params[:type]), {}, class: "border rounded-md m py-0.5 px-1"
7
+ = f.text_area :content, value: params[:content], placeholder: "输入正文", rows: 6, class: "bg-gray-100 rounded-lg p-4 w-full"
8
+ = f.submit "检测", class: "rounded-lg px-10 py-1 bg-black text-white cursor-pointer"
9
+ #result
@@ -0,0 +1,2 @@
1
+ <h1>Home#index</h1>
2
+ <p>Find me in app/views/censor_bear/home/index.html.erb</p>
@@ -9,6 +9,12 @@
9
9
  %span{class: "text-sm bg-pink-100 text-pink-600 py-1 px-2 rounded-lg"}=t("censor_log.stage.#{log.stage}")
10
10
  - unless log.action.blank?
11
11
  %span{class: "text-sm bg-yellow-100 text-yellow-600 py-1 px-2 rounded-lg"}=t("censor_log.action.#{log.action.downcase}")
12
+ - unless log.suggestion.blank?
13
+ %span{class: "text-sm bg-green-100 text-green-600 py-1 px-2 rounded-lg"}=log.suggestion.upcase
14
+ - unless log.rate.blank?
15
+ %span{class: "text-sm bg-green-100 text-green-600 py-1 px-2 rounded-lg"}=log.rate
16
+ - unless log.label.blank?
17
+ %span{class: "text-sm bg-green-100 text-green-600 py-1 px-2 rounded-lg"}=log.label.upcase
12
18
  - unless log.user_id.blank?
13
19
  %span{class: "text-sm bg-gray-100 text-gray-600 py-1 px-2 rounded-lg font-bold"}="##{log.user_id}"
14
20
  - unless log.ip.blank?
@@ -19,7 +25,7 @@
19
25
  %span{class: "text-sm bg-gray-100 text-gray-600 py-1 px-2 rounded-lg"}= log.created_at
20
26
  %div{class: "flex space-x-2 items-center"}
21
27
  %span
22
- = link_to '详情', log
28
+ = link_to '详情', log, data: { turbo: false }
23
29
  %span
24
30
  = button_to '删除', log, method: :delete, data: { confirm: 'Are you sure?' }, class: "text-red-600 rounded-md px-2 py-0.5"
25
31
  %div{class: "flex justify-between items-center"}
@@ -4,6 +4,7 @@
4
4
  = form_with(url: logs_path, method: :get, class: "flex items-center") do |f|
5
5
  %div{class: "space-x-0.5"}
6
6
  = f.text_field :q, value: params[:q], placeholder: "关键词", class: "border rounded-md m py-0.5 px-1"
7
+ = f.select :stage, options_for_select([%w[aliyun_check aliyun_check], %w[aliyun_check aliyun_check], %w[qq_regex qq_regex], %w[wx_regex wx_regex], %w[local_check local_check]], params[:stage]), {include_blank: "-- 按Stage筛选 --"}, class: "border rounded-md m py-0.5 px-1"
7
8
  = f.submit "检索", class: "rounded-md px-2 py-1 text-sm bg-black text-white cursor-pointer"
8
9
  = link_to "重置", logs_path, class: "text-sm"
9
10
 
@@ -0,0 +1,29 @@
1
+ %div{class: "flex flex-col space-y-2"}
2
+ %div
3
+ %div{class: "font-bold"} 源内容:
4
+ %div= @log.original_content
5
+ %div
6
+ %div{class: "font-bold"} 过滤后内容:
7
+ %div= @log.filtered_content
8
+ %div
9
+ %div{class: "font-bold"} 动作:
10
+ %div= @log.action
11
+ %div
12
+ %div{class: "font-bold"} 场景:
13
+ %div= @log.scenario
14
+ %div
15
+ %div{class: "font-bold"} 检测阶段:
16
+ %div= @log.stage
17
+ %div
18
+ %div{class: "font-bold"} 命中敏感词:
19
+ %div= @log.mod_words
20
+ %div
21
+ %div{class: "font-bold"} 云检测返回标签:
22
+ %div= @log.labels
23
+ %div
24
+ %div{class: "font-bold"} 云检测返回标签:
25
+ %div= @log.labels
26
+ %div
27
+ %div{class: "font-bold"} 云检测返回:
28
+ %div{class: "p-1 bg-gray-100 rounded-lg"}
29
+ %pre= JSON.pretty_generate(@log.response)
@@ -12,6 +12,7 @@
12
12
  %li= link_to "审查(#{CensorBear::ModLog.pending.count})", mod_logs_path
13
13
  %li= link_to "日志", logs_path
14
14
  %li= link_to "敏感词", stop_words_path
15
+ %li= link_to "调试", debugger_path
15
16
  %li= link_to "控制台", main_app.send(CensorBear.config.main_app_root_path_method.to_sym)
16
17
  %li= current_user.email
17
18
  %div
data/config/routes.rb CHANGED
@@ -8,5 +8,9 @@ CensorBear::Engine.routes.draw do
8
8
  resources :logs
9
9
  resources :stop_words
10
10
 
11
- root to: "stop_words#index"
11
+ get '/welcome', to: 'home#index'
12
+ get '/debugger', to: 'home#debugger'
13
+ post '/debug', to: 'home#debug'
14
+
15
+ root to: 'mod_logs#index'
12
16
  end
@@ -0,0 +1,5 @@
1
+ class AddResponseToCensorBearLogs < ActiveRecord::Migration[6.0]
2
+ def change
3
+ add_column :censor_bear_logs, :response, :json
4
+ end
5
+ end
@@ -117,30 +117,42 @@ module CensorBear
117
117
  @labels = concat_labels(r['details'])
118
118
  if action == 'block' && rate >= 70 && %w[politics terrorism].include?(r['label'])
119
119
  CensorBear.info(
120
- d['content'], @type, action, 'aliyun_check',
120
+ d['content'], @type, 'banned', 'aliyun_check',
121
121
  filtered_content: d['filteredContent'],
122
122
  mod_words: @mod_words,
123
123
  labels: @labels,
124
- ip: @ip, user_id: @user_id
124
+ ip: @ip, user_id: @user_id,
125
+ response: response
125
126
  )
126
127
  raise NotPassedException
127
- elsif action == 'block'
128
+ elsif action == 'block' && rate >= 90
128
129
  @is_mod = true
129
130
  CensorBear.info(
130
- d['content'], @type, action, 'aliyun_check',
131
+ d['content'], @type, 'mod', 'aliyun_check',
131
132
  filtered_content: d['filteredContent'],
132
133
  mod_words: @mod_words,
133
134
  labels: @labels,
134
- ip: @ip, user_id: @user_id
135
+ ip: @ip, user_id: @user_id,
136
+ response: response
135
137
  )
136
138
  elsif action == 'review'
137
139
  @is_mod = true
138
140
  CensorBear.info(
139
- d['content'], @type, action, 'aliyun_check',
141
+ d['content'], @type, 'mod', 'aliyun_check',
140
142
  filtered_content: d['filteredContent'],
141
143
  mod_words: @mod_words,
142
144
  labels: @labels,
143
- ip: @ip, user_id: @user_id
145
+ ip: @ip, user_id: @user_id,
146
+ response: response
147
+ )
148
+ else
149
+ CensorBear.info(
150
+ d['content'], @type, 'ignore', 'aliyun_check',
151
+ filtered_content: d['filteredContent'],
152
+ mod_words: @mod_words,
153
+ labels: @labels,
154
+ ip: @ip, user_id: @user_id,
155
+ response: response
144
156
  )
145
157
  end
146
158
  end
@@ -149,6 +161,7 @@ module CensorBear
149
161
 
150
162
  def concat_labels(data)
151
163
  return [] if data.blank?
164
+
152
165
  labels = []
153
166
  data.map do |d|
154
167
  next if d['label'].blank?
@@ -160,6 +173,7 @@ module CensorBear
160
173
 
161
174
  def concat_words(data)
162
175
  return [] if data.blank?
176
+
163
177
  words = []
164
178
  data.each do |d|
165
179
  next if d['contexts'].blank?
@@ -1,3 +1,3 @@
1
1
  module CensorBear
2
- VERSION = '0.1.13'.freeze
2
+ VERSION = '0.1.14'.freeze
3
3
  end
data/lib/censor_bear.rb CHANGED
@@ -47,6 +47,7 @@ module CensorBear
47
47
  labels = options[:labels] || []
48
48
  ip = options[:ip] || nil
49
49
  user_id = options[:user_id] || nil
50
+ response = options[:response] || {}
50
51
 
51
52
  CensorBear::Log.create(
52
53
  original_content: content,
@@ -58,6 +59,7 @@ module CensorBear
58
59
  labels: labels,
59
60
  ip: ip,
60
61
  user_id: user_id,
62
+ response: response
61
63
  )
62
64
  end
63
65
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: censor_bear
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - 42up
@@ -112,16 +112,19 @@ files:
112
112
  - Rakefile
113
113
  - app/assets/config/censor_bear_manifest.js
114
114
  - app/assets/stylesheets/censor_bear/application.css
115
+ - app/assets/stylesheets/censor_bear/home.css
115
116
  - app/assets/stylesheets/censor_bear/logs.css
116
117
  - app/assets/stylesheets/censor_bear/mod_logs.css
117
118
  - app/assets/stylesheets/censor_bear/stop_words.css
118
119
  - app/assets/stylesheets/censor_bear/tailwind.min.css
119
120
  - app/assets/stylesheets/scaffold.css
120
121
  - app/controllers/censor_bear/application_controller.rb
122
+ - app/controllers/censor_bear/home_controller.rb
121
123
  - app/controllers/censor_bear/logs_controller.rb
122
124
  - app/controllers/censor_bear/mod_logs_controller.rb
123
125
  - app/controllers/censor_bear/stop_words_controller.rb
124
126
  - app/helpers/censor_bear/application_helper.rb
127
+ - app/helpers/censor_bear/home_helper.rb
125
128
  - app/helpers/censor_bear/logs_helper.rb
126
129
  - app/helpers/censor_bear/mod_logs_helper.rb
127
130
  - app/helpers/censor_bear/stop_words_helper.rb
@@ -131,13 +134,16 @@ files:
131
134
  - app/models/censor_bear/log.rb
132
135
  - app/models/censor_bear/mod_log.rb
133
136
  - app/models/censor_bear/stop_word.rb
137
+ - app/views/censor_bear/home/debug.turbo_stream.haml
138
+ - app/views/censor_bear/home/debugger.html.haml
139
+ - app/views/censor_bear/home/index.html.erb
134
140
  - app/views/censor_bear/logs/_form.html.erb
135
141
  - app/views/censor_bear/logs/_log.html.haml
136
142
  - app/views/censor_bear/logs/destroy.turbo_stream.haml
137
143
  - app/views/censor_bear/logs/edit.html.erb
138
144
  - app/views/censor_bear/logs/index.html.haml
139
145
  - app/views/censor_bear/logs/new.html.erb
140
- - app/views/censor_bear/logs/show.html.erb
146
+ - app/views/censor_bear/logs/show.html.haml
141
147
  - app/views/censor_bear/mod_logs/_form.html.erb
142
148
  - app/views/censor_bear/mod_logs/_mod_log.html.haml
143
149
  - app/views/censor_bear/mod_logs/destroy.turbo_stream.haml
@@ -162,6 +168,7 @@ files:
162
168
  - db/migrate/20211129110218_create_censor_bear_mod_logs.rb
163
169
  - db/migrate/20211206092626_add_ip_to_censor_bear_logs.rb
164
170
  - db/migrate/20211208041114_add_labels_to_censor_bear_logs.rb
171
+ - db/migrate/20211209093050_add_response_to_censor_bear_logs.rb
165
172
  - lib/censor_bear.rb
166
173
  - lib/censor_bear/censor.rb
167
174
  - lib/censor_bear/configuration.rb
@@ -1,34 +0,0 @@
1
- <p id="notice"><%= notice %></p>
2
-
3
- <p>
4
- <strong>Orginal content:</strong>
5
- <%= @log.original_content %>
6
- </p>
7
-
8
- <p>
9
- <strong>Action:</strong>
10
- <%= @log.action %>
11
- </p>
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
-
23
- <p>
24
- <strong>Filtered content:</strong>
25
- <%= @log.filtered_content %>
26
- </p>
27
-
28
- <p>
29
- <strong>Mod words:</strong>
30
- <%= @log.mod_words %>
31
- </p>
32
-
33
- <%= link_to 'Edit', edit_log_path(@log) %> |
34
- <%= link_to 'Back', logs_path %>