censor_bear 0.1.3 → 0.1.7

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: 0c0acc1f6d43f1e10ddbc406e7dc4aa1bfb37ad8c821d048282695d5c9d0ade9
4
- data.tar.gz: b6228ea570225303bc1c8147f8006b1efa362f19aeb95673f72ed6414d879d22
3
+ metadata.gz: 58ec10c9991ef9cd50eee20651e4e4433b09b4c455cfc1089e67da4eb835875a
4
+ data.tar.gz: a60d24c01590c3c89ff14f376db5ec67b82b494e73aa77bd1cef94df497f05c2
5
5
  SHA512:
6
- metadata.gz: c09a551d136e8bc286399cb40e66b17103d8124861b8620650b3e63a221829562ef3e17262fa82f9b14e336b9c6ed79f0f3de05b9ec9935eb9910688b5d668d0
7
- data.tar.gz: 21dc98c9d40fa65381f623d4f37b2dbe465590d2dd41136d18c8743d82c5f6cbaa4de3b4cd351434b3e13917f21fc0064c634b1a9712f4cb44194fad8f2967f1
6
+ metadata.gz: 9e1e3ad9f9afdae97ad89fce16ed0f4faa252595c1219bc9270020c0f049e1a59360b2bcf37964f282c08494b71f1764c9e55f36c47fda95eb431a810fb5c768
7
+ data.tar.gz: 8f2d44931bbed7280c83548e11a6251dd65ed4f9e6fa84b93e560a81a571ebe966dad3cd5919797c636f4d606cd8c44050a7fdfdbfaae25f9a6811e606b9905f
@@ -6,7 +6,7 @@ module CensorBear
6
6
 
7
7
  # GET /logs
8
8
  def index
9
- @logs = Log.all
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)
@@ -1,4 +1,5 @@
1
1
  module CensorBear
2
2
  class Log < ApplicationRecord
3
+ belongs_to :user, class_name: CensorBear.config.user_class, optional: true
3
4
  end
4
5
  end
@@ -0,0 +1,32 @@
1
+ %div{class: "flex flex-col"}
2
+ %div{class: "border rounded-md mb-2 p-2 flex flex-col space-y-1"}
3
+ %div{class: "flex justify-between"}
4
+ %div{class: ""}
5
+ - unless log.scenario.blank?
6
+ %span{class: "text-sm bg-blue-100 text-blue-600 py-1 px-2 rounded-lg"}= log.scenario.upcase
7
+ - unless log.stage.blank?
8
+ %span{class: "text-sm bg-pink-100 text-pink-600 py-1 px-2 rounded-lg"}= log.stage.upcase
9
+ - unless log.action.blank?
10
+ %span{class: "text-sm bg-yellow-100 text-yellow-600 py-1 px-2 rounded-lg"}= log.action.upcase
11
+ - unless log.user_id.blank?
12
+ %span{class: "text-sm bg-gray-100 text-gray-600 py-1 px-2 rounded-lg font-bold"}="##{log.user_id}"
13
+ - unless log.ip.blank?
14
+ %span{class: "text-sm bg-gray-100 text-gray-600 py-1 px-2 rounded-lg"}=log.ip
15
+ %span{class: "text-sm bg-gray-100 text-gray-600 py-1 px-2 rounded-lg"}= log.created_at
16
+ %div{class: "flex space-x-2 items-center"}
17
+ %span
18
+ = link_to '详情', log
19
+ %span
20
+ = button_to '删除', log, method: :delete, data: { confirm: 'Are you sure?', turbo: false }, class: "text-red-600 rounded-md px-2 py-0.5"
21
+ %div{class: "flex justify-between items-center"}
22
+ %div{class: "flex flex-col"}
23
+ %div
24
+ %div= log.original_content
25
+ - if log.filtered_content
26
+ %div{class: "border-t pb-2"}
27
+ %div= log.filtered_content
28
+ %div{class: "text-yellow-500 font-meidum text-xl"}
29
+ - log.mod_words.each do |word|
30
+ %span [
31
+ %span= link_to word, stop_words_path(q: word)
32
+ %span ]
@@ -0,0 +1,18 @@
1
+ %div{class: "flex flex-col space-y-2 justify-center"}
2
+ %div{class: "flex justify-between"}
3
+ %div
4
+ = form_with(url: logs_path, method: :get, class: "flex items-center") do |f|
5
+ %div{class: "space-x-0.5"}
6
+ = f.text_field :q, value: params[:q], placeholder: "关键词", class: "border rounded-md m py-0.5 px-1"
7
+ = f.submit "检索", class: "rounded-md px-2 py-1 text-sm bg-black text-white cursor-pointer"
8
+ = link_to "重置", logs_path, class: "text-sm"
9
+
10
+ %div="共 #{@pagy.count} 条"
11
+ %div
12
+ - if @logs.blank?
13
+ %div{class: "flex justify-center text-gray-500 p-8 border rounded-md"} 空空如也
14
+ - else
15
+ - @logs.each do |log|
16
+ = render log
17
+ %div{class: "flex justify-center"}
18
+ = raw pagy_nav(@pagy)
@@ -24,7 +24,7 @@
24
24
  %div{class: "text-center"}=I18n.t("censor_log.action.#{stop_word.dialog}")
25
25
  %div{class: "text-gray-500 text-xs text-center"} 私信处理
26
26
  %div{class: "flex flex-col justify-center border rounded-md p-1"}
27
- %div{class: "text-center"}=stop_word.replacement
27
+ %div{class: "text-center"}=stop_word.replacement || "**"
28
28
  %div{class: "text-gray-500 text-xs text-center"} 过滤词替换
29
29
  %div{class: "flex justify-end space-x-2 text-sm text-blue-600"}
30
30
  %span
@@ -0,0 +1,6 @@
1
+ class AddIpToCensorBearLogs < ActiveRecord::Migration[6.1]
2
+ def change
3
+ add_column :censor_bear_logs, :ip, :string
4
+ add_column :censor_bear_logs, :user_id, :bigint
5
+ end
6
+ end
@@ -13,11 +13,14 @@ module CensorBear
13
13
  QQ_REG = /(?:[加茄qQ企鹅号码\s]{1,}|[群号]{1,}|[叩叩]{1,}|[抠抠]{1,}|[扣扣]{1,})(?:[\u4e00-\u9eff]*)(?:[:,:]?)([\d\s]{6,})/
14
14
  WX_REG = /(?:[加+微++➕薇?vV威卫星♥❤姓xX信]{2,}|weixin|weix)(?:[,❤️.\s]?)(?:[\u4e00-\u9eff]?)(?:[:,:]?)([\w\s]{6,})/
15
15
 
16
- def initialize(content, type = 'ugc')
16
+ def initialize(content, type = 'ugc', options = {})
17
17
  @is_mod = false
18
18
  @mod_words = []
19
19
  @content = content
20
20
  @type = type.to_s
21
+ @options = options
22
+ @ip = options[:ip] || nil
23
+ @user_id = options[:user_id] || nil
21
24
  end
22
25
 
23
26
  def check_text
@@ -25,12 +28,12 @@ module CensorBear
25
28
  return Result.new(@content) unless CensorBear::StopWord::FIELDS.include?(@type)
26
29
 
27
30
  if @content.match(QQ_REG)
28
- CensorBear.info(@content, @type, 'BANNED', 'qq_regex')
31
+ CensorBear.info(@content, @type, 'BANNED', 'qq_regex', ip: @ip, user_id: @user_id)
29
32
  raise NotPassedException
30
33
  end
31
34
 
32
35
  if @content.match(WX_REG)
33
- CensorBear.info(@content, @type, 'BANNED', 'wx_regex')
36
+ CensorBear.info(@content, @type, 'BANNED', 'wx_regex', ip: @ip, user_id: @user_id)
34
37
  raise NotPassedException
35
38
  end
36
39
 
@@ -42,6 +45,22 @@ module CensorBear
42
45
  Result.new(@content, @is_mod, @mod_words.uniq)
43
46
  end
44
47
 
48
+ # default type = :ugc
49
+ def check_search(user = nil, ip = nil)
50
+ result = false
51
+ stop_words = CensorBear::StopWord.where("#{@type} != 'IGNORE'")
52
+ stop_words.each do |word|
53
+ finder = Regexp.new(Regexp.escape(word.key))
54
+ action = word.send(@type.to_sym).upcase
55
+ if finder.match(@content)
56
+ result = true
57
+ CensorBear.info(@content, @type, action, 'check_search', mod_words: [word.key], ip: @ip, user_id: @user_id)
58
+ break
59
+ end
60
+ end
61
+ result
62
+ end
63
+
45
64
  def local_check
46
65
  original_content = @content.dup
47
66
  stop_words = CensorBear::StopWord.where("#{@type} != 'IGNORE'")
@@ -52,18 +71,21 @@ module CensorBear
52
71
  @mod_words.push(word.key)
53
72
  case action
54
73
  when 'REPLACE'
55
- @content = @content.gsub(finder, word.replacement)
74
+ replacement = word.replacement.blank? ? "**" : word.replacement
75
+ @content = @content.gsub(finder, replacement)
56
76
  CensorBear.info(
57
77
  original_content, @type, action, 'local_check',
58
78
  filtered_content: @content,
59
- mod_words: [word.key]
79
+ mod_words: [word.key],
80
+ ip: @ip, user_id: @user_id
60
81
  )
61
82
  when 'MOD'
62
83
  @is_mod = true
63
84
  CensorBear.info(
64
85
  original_content, @type, action, 'local_check',
65
86
  filtered_content: nil,
66
- mod_words: [word.key]
87
+ mod_words: [word.key],
88
+ ip: @ip, user_id: @user_id
67
89
  )
68
90
  when 'BANNED'
69
91
  @is_mod = true
@@ -71,7 +93,8 @@ module CensorBear
71
93
  CensorBear.info(
72
94
  original_content, @type, action, 'local_check',
73
95
  filtered_content: nil,
74
- mod_words: [word.key]
96
+ mod_words: [word.key],
97
+ ip: @ip, user_id: @user_id
75
98
  )
76
99
  raise NotPassedException
77
100
  end
@@ -1,3 +1,3 @@
1
1
  module CensorBear
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.7'.freeze
3
3
  end
data/lib/censor_bear.rb CHANGED
@@ -27,13 +27,19 @@ module CensorBear
27
27
  yield(config)
28
28
  end
29
29
 
30
- def check_text(content, type = 'ugc')
31
- Censor.new(content, type).check_text
30
+ def check_text(content, type = 'ugc', options = {})
31
+ Censor.new(content, type, options).check_text
32
+ end
33
+
34
+ def check_search(content, options = {})
35
+ Censor.new(content, "ugc", options).check_search
32
36
  end
33
37
 
34
38
  def info(content, scenario, action, stage, options = {})
35
- filtered_content = options['filtered_content'] || nil
36
- mod_words = options['mod_words'] || []
39
+ filtered_content = options[:filtered_content] || nil
40
+ mod_words = options[:mod_words] || []
41
+ ip = options[:ip] || nil
42
+ user_id = options[:user_id] || nil
37
43
 
38
44
  CensorBear::Log.create(
39
45
  original_content: content,
@@ -41,7 +47,9 @@ module CensorBear
41
47
  action: action,
42
48
  stage: stage,
43
49
  filtered_content: filtered_content,
44
- mod_words: mod_words
50
+ mod_words: mod_words,
51
+ ip: ip,
52
+ user_id: user_id,
45
53
  )
46
54
  end
47
55
 
@@ -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.3
4
+ version: 0.1.7
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-02 00:00:00.000000000 Z
11
+ date: 2021-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -118,8 +118,9 @@ files:
118
118
  - app/models/censor_bear/mod_log.rb
119
119
  - app/models/censor_bear/stop_word.rb
120
120
  - app/views/censor_bear/logs/_form.html.erb
121
+ - app/views/censor_bear/logs/_log.html.haml
121
122
  - app/views/censor_bear/logs/edit.html.erb
122
- - app/views/censor_bear/logs/index.html.erb
123
+ - app/views/censor_bear/logs/index.html.haml
123
124
  - app/views/censor_bear/logs/new.html.erb
124
125
  - app/views/censor_bear/logs/show.html.erb
125
126
  - app/views/censor_bear/mod_logs/_form.html.erb
@@ -142,6 +143,7 @@ files:
142
143
  - db/migrate/20211126133758_create_censor_bear_stop_words.rb
143
144
  - db/migrate/20211129093508_create_censor_bear_logs.rb
144
145
  - db/migrate/20211129110218_create_censor_bear_mod_logs.rb
146
+ - db/migrate/20211206092626_add_ip_to_censor_bear_logs.rb
145
147
  - lib/censor_bear.rb
146
148
  - lib/censor_bear/censor.rb
147
149
  - lib/censor_bear/configuration.rb
@@ -1,37 +0,0 @@
1
- <p id="notice"><%= notice %></p>
2
-
3
- <h1>Logs</h1>
4
-
5
- <table>
6
- <thead>
7
- <tr>
8
- <th>Orginal content</th>
9
- <th>Action</th>
10
- <th>Filtered content</th>
11
- <th>Mod words</th>
12
- <th colspan="3"></th>
13
- </tr>
14
- </thead>
15
-
16
- <tbody>
17
- <% @logs.each do |log| %>
18
- <tr>
19
- <td><%= log.original_content %></td>
20
- <td><%= log.scenario %></td>
21
- <td><%= log.stage %></td>
22
- <td><%= log.action %></td>
23
- <td><%= log.filtered_content %></td>
24
- <td><%= log.mod_words %></td>
25
- <td><%= link_to 'Show', log %></td>
26
- <td><%= link_to 'Edit', edit_log_path(log) %></td>
27
- <td><%= link_to 'Destroy', log, method: :delete, data: { confirm: 'Are you sure?' } %></td>
28
- </tr>
29
- <% end %>
30
- </tbody>
31
- </table>
32
-
33
- <br>
34
-
35
- <%= link_to 'New Log', new_log_path %>
36
-
37
-