Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
K
Koishi Target Def
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
3rdeye
Koishi Target Def
Commits
4b4aaee8
Commit
4b4aaee8
authored
Sep 13, 2022
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update replacement
parent
cfa0ee1b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
6 deletions
+53
-6
index.ts
index.ts
+33
-4
tests/sample.spec.ts
tests/sample.spec.ts
+20
-2
No files found.
index.ts
View file @
4b4aaee8
import
{
Adapter
,
Bot
,
Context
,
Filter
}
from
'
koishi
'
;
import
{
Bot
,
Context
,
Filter
,
segment
}
from
'
koishi
'
;
import
_
from
'
lodash
'
;
import
{
RegisterSchema
,
SchemaProperty
}
from
'
schemastery-gen
'
;
...
...
@@ -95,6 +95,36 @@ export class SendTarget {
return
ctx
.
intersect
(
this
.
getFilter
());
}
private
runForEachSegemnt
(
segments
:
segment
[],
action
:
(
s
:
segment
)
=>
void
)
{
for
(
const
s
of
segments
)
{
action
(
s
);
if
(
s
.
children
?.
length
)
{
this
.
runForEachSegemnt
(
s
.
children
,
action
);
}
}
}
replaceContent
(
content
:
string
,
fromKey
:
string
,
toKey
:
string
)
{
const
segments
=
segment
.
parse
(
content
);
this
.
runForEachSegemnt
(
segments
,
(
s
)
=>
{
const
{
attrs
}
=
s
;
if
(
!
attrs
||
s
.
type
!==
'
image
'
)
{
return
;
}
if
(
attrs
.
url
?.
startsWith
(
'
base64
'
))
{
const
{
url
}
=
attrs
;
attrs
.
file
=
url
;
delete
attrs
.
url
;
}
if
(
attrs
[
fromKey
]?.
startsWith
(
'
http
'
))
{
const
{
[
fromKey
]:
url
}
=
attrs
;
attrs
[
toKey
]
=
url
;
delete
attrs
[
fromKey
];
}
});
return
segments
.
map
((
s
)
=>
s
.
toString
()).
join
(
''
);
}
async
send
(
bots
:
Bot
[],
content
:
string
)
{
const
bot
=
this
.
getBot
(
bots
);
if
(
!
bot
)
{
...
...
@@ -103,9 +133,8 @@ export class SendTarget {
let
privateContent
=
content
;
let
channelContent
=
content
;
if
(
this
.
isOneBotBot
(
bot
))
{
content
=
content
.
replace
(
/,url=base64/g
,
'
,file=base64
'
);
privateContent
=
content
.
replace
(
/,url=http/g
,
'
,file=http
'
);
// private should be file
channelContent
=
content
.
replace
(
/,file=http/g
,
'
,url=http
'
);
// channel should be url
privateContent
=
this
.
replaceContent
(
content
,
'
url
'
,
'
file
'
);
// private should be file
channelContent
=
this
.
replaceContent
(
content
,
'
file
'
,
'
url
'
);
// channel should be url
}
return
_
.
flatten
(
await
Promise
.
all
([
...
...
tests/sample.spec.ts
View file @
4b4aaee8
import
{
Session
}
from
'
koishi
'
;
import
{
SendTarget
}
from
'
..
'
;
import
{
segment
,
Session
}
from
'
koishi
'
;
import
{
SendTarget
}
from
'
..
/index
'
;
describe
(
'
Target def.
'
,
()
=>
{
const
def
=
new
SendTarget
({
...
...
@@ -81,4 +81,22 @@ describe('Target def.', () => {
}
as
Session
),
).
toBe
(
true
);
});
it
(
'
should replace content
'
,
()
=>
{
const
target
=
new
SendTarget
({});
const
imageBase64
=
segment
(
'
image
'
,
{
url
:
'
base64://foo
'
});
const
imageBase64Replaced
=
segment
.
parse
(
target
.
replaceContent
(
imageBase64
.
toString
(),
'
url
'
,
'
url
'
),
)[
0
];
const
imageUrl
=
segment
(
'
image
'
,
{
file
:
'
http://example.com/1.png
'
,
});
const
imageUrlReplaced
=
segment
.
parse
(
target
.
replaceContent
(
imageUrl
.
toString
(),
'
file
'
,
'
url
'
),
)[
0
];
expect
(
imageBase64Replaced
.
attrs
?.
url
).
toBeUndefined
();
expect
(
imageBase64Replaced
.
attrs
?.
file
).
toBe
(
'
base64://foo
'
);
expect
(
imageUrlReplaced
.
attrs
?.
url
).
toBe
(
'
http://example.com/1.png
'
);
expect
(
imageUrlReplaced
.
attrs
?.
file
).
toBeUndefined
();
});
});
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