Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
e1784f89
authored
Feb 21, 2025
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: Optimize sensitive word detection and text processing
parent
78f9a30c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
8 deletions
+26
-8
service/sensitive.go
+26
-8
No files found.
service/sensitive.go
View file @
e1784f89
...
...
@@ -9,18 +9,26 @@ import (
)
func
CheckSensitiveMessages
(
messages
[]
dto
.
Message
)
([]
string
,
error
)
{
if
len
(
messages
)
==
0
{
return
nil
,
nil
}
for
_
,
message
:=
range
messages
{
arrayContent
:=
message
.
ParseContent
()
for
_
,
m
:=
range
arrayContent
{
if
m
.
Type
==
"image_url"
{
// TODO: check image url
}
else
{
continue
}
// 检查 text 是否为空
if
m
.
Text
==
""
{
continue
}
if
ok
,
words
:=
SensitiveWordContains
(
m
.
Text
);
ok
{
return
words
,
errors
.
New
(
"sensitive words detected"
)
}
}
}
}
return
nil
,
nil
}
...
...
@@ -36,11 +44,11 @@ func CheckSensitiveInput(input any) ([]string, error) {
case
string
:
return
CheckSensitiveText
(
v
)
case
[]
string
:
text
:=
""
var
builder
strings
.
Builder
for
_
,
s
:=
range
v
{
text
+=
s
builder
.
WriteString
(
s
)
}
return
CheckSensitiveText
(
text
)
return
CheckSensitiveText
(
builder
.
String
()
)
}
return
CheckSensitiveText
(
fmt
.
Sprintf
(
"%v"
,
input
))
}
...
...
@@ -50,6 +58,9 @@ func SensitiveWordContains(text string) (bool, []string) {
if
len
(
setting
.
SensitiveWords
)
==
0
{
return
false
,
nil
}
if
len
(
text
)
==
0
{
return
false
,
nil
}
checkText
:=
strings
.
ToLower
(
text
)
return
AcSearch
(
checkText
,
setting
.
SensitiveWords
,
true
)
}
...
...
@@ -63,14 +74,21 @@ func SensitiveWordReplace(text string, returnImmediately bool) (bool, []string,
m
:=
InitAc
(
setting
.
SensitiveWords
)
hits
:=
m
.
MultiPatternSearch
([]
rune
(
checkText
),
returnImmediately
)
if
len
(
hits
)
>
0
{
words
:=
make
([]
string
,
0
)
words
:=
make
([]
string
,
0
,
len
(
hits
))
var
builder
strings
.
Builder
builder
.
Grow
(
len
(
text
))
lastPos
:=
0
for
_
,
hit
:=
range
hits
{
pos
:=
hit
.
Pos
word
:=
string
(
hit
.
Word
)
text
=
text
[
:
pos
]
+
"**###**"
+
text
[
pos
+
len
(
word
)
:
]
builder
.
WriteString
(
text
[
lastPos
:
pos
])
builder
.
WriteString
(
"**###**"
)
lastPos
=
pos
+
len
(
word
)
words
=
append
(
words
,
word
)
}
return
true
,
words
,
text
builder
.
WriteString
(
text
[
lastPos
:
])
return
true
,
words
,
builder
.
String
()
}
return
false
,
nil
,
text
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment