From 0e5e2336ff9af79265dfe5b7b72acf0eb3704e74 Mon Sep 17 00:00:00 2001
From: ryoii <ryoii@foxmail.com>
Date: Sun, 23 Feb 2020 20:33:03 +0800
Subject: [PATCH] Http api return messageId after send a message

---
 mirai-api-http/README_CH.md                   | 20 ++++++++++------
 .../api/http/route/MessageRouteModule.kt      | 24 +++++++++++--------
 .../net.mamoe.mirai/message/MessageReceipt.kt |  2 +-
 3 files changed, 28 insertions(+), 18 deletions(-)

diff --git a/mirai-api-http/README_CH.md b/mirai-api-http/README_CH.md
index 274d7cd84..7019abd7d 100644
--- a/mirai-api-http/README_CH.md
+++ b/mirai-api-http/README_CH.md
@@ -175,12 +175,13 @@ fun main() {
 | target       | Long   | false | 987654321   | 发送消息目标好友的QQ号           |
 | messageChain | Array  | false | []          | 消息链,是一个消息对象构成的数组 |
 
-#### 响应: 返回统一状态码
+#### 响应: 返回统一状态码(并携带messageId)
 
 ```json5
 {
     "code": 0,
-    "msg": "success"
+    "msg": "success",
+    "messageId": 1234567890 // 一个Long类型属性,标识本条消息,用于撤回和引用回复
 }
 ```
 
@@ -213,12 +214,13 @@ fun main() {
 | target       | Long   | false | 987654321   | 发送消息目标群的群号             |
 | messageChain | Array  | false | []          | 消息链,是一个消息对象构成的数组 |
 
-#### 响应: 返回统一状态码
+#### 响应: 返回统一状态码(并携带messageId)
 
 ```json5
 {
     "code": 0,
-    "msg": "success"
+    "msg": "success",
+    "messageId": 1234567890 // 一个Long类型属性,标识本条消息,用于撤回和引用回复
 }
 ```
 
@@ -251,12 +253,13 @@ fun main() {
 | target       | Long   | false | 987654321   | 引用消息的Message Source的Uid    |
 | messageChain | Array  | false | []          | 消息链,是一个消息对象构成的数组 |
 
-#### 响应: 返回统一状态码
+#### 响应: 返回统一状态码(并携带messageId)
 
 ```json5
 {
     "code": 0,
-    "msg": "success"
+    "msg": "success",
+    "messageId": 1234567890 // 一个Long类型属性,标识本条消息,用于撤回和引用回复
 }
 ```
 
@@ -370,7 +373,10 @@ Content-Type:multipart/form-data
     }
  },{
     "type": "FriendMessage",         // 消息类型:GroupMessage或FriendMessage或各类Event
-        "messageChain": [{           // 消息链,是一个消息对象构成的数组
+    "messageChain": [{             // 消息链,是一个消息对象构成的数组
+        "type": "Source",
+        "uid": 123456
+    },{
         "type": "Plain",
         "text": "Miral牛逼"
     }],
diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt
index e5b7b0bb7..872d40497 100644
--- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt
+++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt
@@ -20,10 +20,10 @@ import io.ktor.response.respondText
 import io.ktor.routing.post
 import io.ktor.routing.routing
 import kotlinx.serialization.Serializable
-import kotlinx.serialization.Transient
 import net.mamoe.mirai.api.http.AuthedSession
 import net.mamoe.mirai.api.http.SessionManager
 import net.mamoe.mirai.api.http.data.*
+import net.mamoe.mirai.api.http.data.common.DTO
 import net.mamoe.mirai.api.http.data.common.MessageChainDTO
 import net.mamoe.mirai.api.http.data.common.VerifyDTO
 import net.mamoe.mirai.api.http.data.common.toMessageChain
@@ -44,23 +44,26 @@ fun Application.messageModule() {
 
         miraiVerify<SendDTO>("/sendFriendMessage") {
             it.session.bot.getFriend(it.target).apply {
-                sendMessage(it.messageChain.toMessageChain(this)) // this aka QQ
+                val receipt = sendMessage(it.messageChain.toMessageChain(this)) // this aka QQ
+                receipt.source.ensureSequenceIdAvailable()
+                call.respondDTO(SendRetDTO(messageId = receipt.source.id))
             }
-            call.respondStateCode(StateCode.Success)
         }
 
         miraiVerify<SendDTO>("/sendGroupMessage") {
             it.session.bot.getGroup(it.target).apply {
-                sendMessage(it.messageChain.toMessageChain(this)) // this aka Group
+                val receipt = sendMessage(it.messageChain.toMessageChain(this)) // this aka Group
+                receipt.source.ensureSequenceIdAvailable()
+                call.respondDTO(SendRetDTO(messageId = receipt.source.id))
             }
-            call.respondStateCode(StateCode.Success)
         }
 
         miraiVerify<SendDTO>("/sendQuoteMessage") {
             it.session.messageQueue.quoteCache[it.target]?.apply {
-                quoteReply(it.messageChain.toMessageChain(group))
+                val receipt = quoteReply(it.messageChain.toMessageChain(group))
+                receipt.source.ensureSequenceIdAvailable()
+                call.respondDTO(SendRetDTO(messageId = receipt.source.id))
             } ?: throw NoSuchElementException()
-            call.respondStateCode(StateCode.Success)
         }
 
         miraiVerify<SendImageDTO>("sendImageMessage") {
@@ -127,9 +130,10 @@ private data class SendImageDTO(
 
 @Serializable
 private class SendRetDTO(
-    val messageId: Long,
-    @Transient val stateCode: StateCode = Success
-) : StateCode(stateCode.code, stateCode.msg)
+    val code: Int = 0,
+    val msg: String = "success",
+    val messageId: Long
+) : DTO
 
 @Serializable
 private data class RecallDTO(
diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessageReceipt.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessageReceipt.kt
index 7d5c37657..31482e393 100644
--- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessageReceipt.kt
+++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessageReceipt.kt
@@ -31,7 +31,7 @@ import net.mamoe.mirai.utils.unsafeWeakRef
  * @see QQ.sendMessage 发送群消息, 返回回执(此对象)
  */
 open class MessageReceipt<C : Contact>(
-    private val source: MessageSource,
+    val source: MessageSource,
     target: C
 ) {
     init {