censor_bear 0.1.2 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/censor_bear/logs_controller.rb +15 -1
- data/app/controllers/censor_bear/stop_words_controller.rb +1 -1
- data/app/helpers/censor_bear/application_helper.rb +4 -0
- data/app/views/censor_bear/logs/index.html.erb +8 -1
- data/app/views/censor_bear/logs/show.html.erb +10 -0
- data/app/views/censor_bear/stop_words/_form.html.haml +5 -5
- data/db/migrate/20211129093508_create_censor_bear_logs.rb +2 -0
- data/lib/censor_bear/censor.rb +34 -17
- data/lib/censor_bear/version.rb +1 -1
- data/lib/censor_bear.rb +22 -5
- data/lib/tasks/censor_bear_tasks.rake +2 -2
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3808c2d3d47946f4109915ed8dbc7958693e4f90c2f33bfa588752848dcb287d
|
4
|
+
data.tar.gz: 273b95b59c959c83fe077d9bd2a81df28fa823472ddb5e620683ed97e4e8787f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7003eaf638b80b5a3d5d0e1303c45841cfbf4dba9c3dab24e38c1b03f5d30b20d9f5ce5447cf924a3656f82b0797066fa1b844325e99d930809b0d06cb471cd5
|
7
|
+
data.tar.gz: 29c9cb99c1df3c8039a88e7b363c012064d94366419f845920cc0978a6652cc66c3ffd1f28b15138b5c2e8843b7ce33edd92d125b1ed29cc3e814152198620f1
|
@@ -6,7 +6,7 @@ module CensorBear
|
|
6
6
|
|
7
7
|
# GET /logs
|
8
8
|
def index
|
9
|
-
|
9
|
+
load_logs
|
10
10
|
end
|
11
11
|
|
12
12
|
# GET /logs/1
|
@@ -48,12 +48,26 @@ module CensorBear
|
|
48
48
|
redirect_to logs_url, notice: 'Log was successfully destroyed.'
|
49
49
|
end
|
50
50
|
|
51
|
+
def load_logs
|
52
|
+
builder ||= log_scope
|
53
|
+
builder = builder.where("original_content ilike ?", "%#{params[:q]}%") if params[:q].present?
|
54
|
+
builder = builder.order(id: :desc)
|
55
|
+
|
56
|
+
@pagy, @logs = pagy(builder)
|
57
|
+
end
|
58
|
+
|
59
|
+
|
51
60
|
private
|
61
|
+
|
52
62
|
# Use callbacks to share common setup or constraints between actions.
|
53
63
|
def set_log
|
54
64
|
@log = Log.find(params[:id])
|
55
65
|
end
|
56
66
|
|
67
|
+
def log_scope
|
68
|
+
Log
|
69
|
+
end
|
70
|
+
|
57
71
|
# Only allow a list of trusted parameters through.
|
58
72
|
def log_params
|
59
73
|
params.require(:log).permit(:original_content, :action, :filtered_content, :mod_words)
|
@@ -46,7 +46,7 @@ module CensorBear
|
|
46
46
|
|
47
47
|
def load_stop_words
|
48
48
|
builder ||= stop_word_scope
|
49
|
-
builder = builder.where("
|
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)
|
@@ -17,17 +17,24 @@
|
|
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>
|
23
25
|
<td><%= link_to 'Show', log %></td>
|
24
26
|
<td><%= link_to 'Edit', edit_log_path(log) %></td>
|
25
|
-
<td><%=
|
27
|
+
<td><%= button_to 'Destroy', log, method: :delete, data: { confirm: 'Are you sure?', turbo: false } %></td>
|
26
28
|
</tr>
|
27
29
|
<% end %>
|
28
30
|
</tbody>
|
29
31
|
</table>
|
30
32
|
|
33
|
+
|
34
|
+
<br>
|
35
|
+
|
36
|
+
<%= raw pagy_nav(@pagy) %>
|
37
|
+
|
31
38
|
<br>
|
32
39
|
|
33
40
|
<%= link_to 'New Log', new_log_path %>
|
@@ -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,
|
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,
|
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,
|
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,
|
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,
|
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
|
|
data/lib/censor_bear/censor.rb
CHANGED
@@ -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
|
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
|
-
@
|
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
|
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
|
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
|
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
|
data/lib/censor_bear/version.rb
CHANGED
data/lib/censor_bear.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require 'censor_bear/version'
|
2
|
+
require 'censor_bear/engine'
|
3
|
+
require 'censor_bear/censor'
|
4
|
+
require 'censor_bear/configuration'
|
5
5
|
|
6
|
-
require
|
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
|
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:
|
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.
|
4
|
+
version: 0.1.6
|
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-
|
11
|
+
date: 2021-12-05 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.0.
|
19
|
+
version: 6.0.4
|
20
20
|
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 6.0.
|
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.0.
|
29
|
+
version: 6.0.4
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: 6.0.
|
32
|
+
version: 6.0.4.1
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: hamlit
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|