Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
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
MyCard
Mirai
Commits
878d2ab8
Commit
878d2ab8
authored
Feb 04, 2020
by
ryoii
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix serialize and rename
parent
4b9900e0
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
25 additions
and
21 deletions
+25
-21
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/dto/DTO.kt
...-http/src/main/kotlin/net/mamoe/mirai/api/http/dto/DTO.kt
+1
-1
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/dto/VerifyDTO.kt
...src/main/kotlin/net/mamoe/mirai/api/http/dto/VerifyDTO.kt
+15
-9
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/AuthRouteModule.kt
.../kotlin/net/mamoe/mirai/api/http/route/AuthRouteModule.kt
+3
-3
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/BaseRoute.kt
...c/main/kotlin/net/mamoe/mirai/api/http/route/BaseRoute.kt
+4
-4
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt
...tlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt
+2
-4
No files found.
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/dto/DTO.kt
View file @
878d2ab8
...
...
@@ -21,7 +21,7 @@ inline fun <reified T : Any> String.jsonParseOrNull(
serializer
:
DeserializationStrategy
<
T
>?
=
null
):
T
?
=
try
{
if
(
serializer
==
null
)
MiraiJson
.
json
.
parse
(
this
)
else
MiraiJson
.
json
.
parse
(
serializer
,
this
)
}
catch
(
e
:
Exception
)
{
throw
e
}
}
catch
(
e
:
Exception
)
{
null
}
...
...
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/dto/VerifyDTO.kt
View file @
878d2ab8
...
...
@@ -7,7 +7,8 @@ import net.mamoe.mirai.api.http.AuthedSession
@Serializable
abstract
class
VerifyDTO
:
DTO
{
abstract
val
sessionKey
:
String
@Transient
lateinit
var
session
:
AuthedSession
// 反序列化验证后传入
@Transient
lateinit
var
session
:
AuthedSession
// 反序列化验证后传入
}
@Serializable
...
...
@@ -16,15 +17,20 @@ data class BindDTO(override val sessionKey: String, val qq: Long) : VerifyDTO()
// 写成data class并继承DTO接口是为了返回时的形式统一
@Serializable
open
class
StateCodeDTO
(
val
code
:
Int
,
val
msg
:
String
)
:
DTO
{
companion
object
{
val
SUCCESS
=
StateCodeDTO
(
0
,
"success"
)
// 成功
// val AUTH_WRONG = CodeDTO(1) // AuthKey错误, @see AuthResDTO
val
NO_BOT
=
StateCodeDTO
(
2
,
"指定Bot不存在"
)
val
ILLEGAL_SESSION
=
StateCodeDTO
(
3
,
"Session失效或不存在"
)
val
NOT_VERIFIED_SESSION
=
StateCodeDTO
(
3
,
"Session未认证"
)
sealed
class
StateCodeDTO
(
val
code
:
Int
,
var
msg
:
String
)
:
DTO
{
object
Success
:
StateCodeDTO
(
0
,
"success"
)
// 成功
// object AUTH_WRONG : CodeDTO(1) // AuthKey错误, @see AuthResDTO
object
NoBot
:
StateCodeDTO
(
2
,
"指定Bot不存在"
)
object
IllegalSession
:
StateCodeDTO
(
3
,
"Session失效或不存在"
)
object
NotVerifySession
:
StateCodeDTO
(
3
,
"Session未认证"
)
// KS bug: 主构造器中不能有非字段参数 https://github.com/Kotlin/kotlinx.serialization/issues/575
@Serializable
class
IllegalAccess
()
:
StateCodeDTO
(
400
,
""
)
{
// 非法访问
constructor
(
msg
:
String
)
:
this
()
{
this
.
msg
=
msg
}
}
class
ILLEGAL_ACCESS
(
msg
:
String
)
:
StateCodeDTO
(
400
,
msg
)
// 非法访问
}
@Serializable
...
...
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/AuthRouteModule.kt
View file @
878d2ab8
...
...
@@ -27,15 +27,15 @@ fun Application.authModule() {
closeSession
(
it
.
sessionKey
)
allSession
[
it
.
sessionKey
]
=
AuthedSession
(
bot
,
EmptyCoroutineContext
)
}
call
.
respondDTO
(
StateCodeDTO
.
S
UCCESS
)
call
.
respondDTO
(
StateCodeDTO
.
S
uccess
)
}
catch
(
e
:
NoSuchElementException
)
{
call
.
respondDTO
(
StateCodeDTO
.
N
O_BOT
)
call
.
respondDTO
(
StateCodeDTO
.
N
oBot
)
}
}
miraiVerify
<
BindDTO
>(
"/release"
)
{
SessionManager
.
closeSession
(
it
.
sessionKey
)
call
.
respondDTO
(
StateCodeDTO
.
S
UCCESS
)
call
.
respondDTO
(
StateCodeDTO
.
S
uccess
)
}
}
...
...
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/BaseRoute.kt
View file @
878d2ab8
...
...
@@ -105,11 +105,11 @@ internal inline fun Route.intercept(crossinline blk: suspend PipelineContext<Uni
try
{
blk
(
this
)
}
catch
(
e
:
IllegalSessionException
)
{
call
.
respondDTO
(
StateCodeDTO
.
I
LLEGAL_SESSION
)
call
.
respondDTO
(
StateCodeDTO
.
I
llegalSession
)
}
catch
(
e
:
NotVerifiedSessionException
)
{
call
.
respondDTO
(
StateCodeDTO
.
N
OT_VERIFIED_SESSION
)
call
.
respondDTO
(
StateCodeDTO
.
N
otVerifySession
)
}
catch
(
e
:
IllegalAccessException
)
{
call
.
respondDTO
(
StateCodeDTO
.
I
LLEGAL_ACCESS
(
e
.
message
),
HttpStatusCode
.
BadRequest
)
call
.
respondDTO
(
StateCodeDTO
.
I
llegalAccess
(
e
.
message
),
HttpStatusCode
.
BadRequest
)
}
}
...
...
@@ -122,7 +122,7 @@ internal suspend inline fun <reified T : DTO> ApplicationCall.respondDTO(dto: T,
internal
suspend
fun
ApplicationCall
.
respondJson
(
json
:
String
,
status
:
HttpStatusCode
=
HttpStatusCode
.
OK
)
=
respondText
(
json
,
defaultTextContentType
(
ContentType
(
"application"
,
"json"
)),
status
)
internal
suspend
inline
fun
<
reified
T
:
DTO
>
ApplicationCall
.
receiveDTO
():
T
?
=
receive
<
String
>().
apply
(
::
println
).
jsonParseOrNull
()
internal
suspend
inline
fun
<
reified
T
:
DTO
>
ApplicationCall
.
receiveDTO
():
T
?
=
receive
<
String
>().
jsonParseOrNull
()
...
...
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt
View file @
878d2ab8
...
...
@@ -3,8 +3,6 @@ package net.mamoe.mirai.api.http.route
import
io.ktor.application.Application
import
io.ktor.application.call
import
io.ktor.routing.routing
import
net.mamoe.mirai.api.http.AuthedSession
import
net.mamoe.mirai.api.http.SessionManager
import
net.mamoe.mirai.api.http.dto.*
fun
Application
.
messageModule
()
{
...
...
@@ -20,12 +18,12 @@ fun Application.messageModule() {
miraiVerify
<
SendDTO
>(
"/sendFriendMessage"
)
{
it
.
session
.
bot
.
getQQ
(
it
.
target
).
sendMessage
(
it
.
messageChain
.
toMessageChain
())
call
.
respondDTO
(
StateCodeDTO
.
S
UCCESS
)
call
.
respondDTO
(
StateCodeDTO
.
S
uccess
)
}
miraiVerify
<
SendDTO
>(
"/sendGroupMessage"
)
{
it
.
session
.
bot
.
getGroup
(
it
.
target
).
sendMessage
(
it
.
messageChain
.
toMessageChain
())
call
.
respondDTO
(
StateCodeDTO
.
S
UCCESS
)
call
.
respondDTO
(
StateCodeDTO
.
S
uccess
)
}
miraiVerify
<
VerifyDTO
>(
"/event/message"
)
{
...
...
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