Skip to content

Commit 77e9bc4

Browse files
committed
feat(yogurt): support Milky 1.2 features
1 parent a73cf2a commit 77e9bc4

File tree

8 files changed

+96
-21
lines changed

8 files changed

+96
-21
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ kx-datetime = "0.7.1-0.6.x-compat"
1111
kx-atomicfu = "0.31.0"
1212
ktor = "3.4.0"
1313

14-
milky = "1.1.0"
14+
milky = "1.2.0-RC2"
1515
acidify-codec = "0.1.0"
1616
kompress = "1.3.1"
1717
qr-matrix = "0.1.0"

yogurt/src/commonMain/kotlin/org/ntqqrev/yogurt/api/HttpRoutes.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ fun Route.configureMilkyApiHttpRoutes() {
108108
serve(GetGroupInfo)
109109
serve(GetGroupMemberList)
110110
serve(GetGroupMemberInfo)
111+
serve(GetPeerPins)
112+
serve(SetPeerPin)
111113
serve(SetAvatar)
112114
serve(SetNickname)
113115
serve(SetBio)

yogurt/src/commonMain/kotlin/org/ntqqrev/yogurt/api/handler/Group.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.ntqqrev.milky.*
77
import org.ntqqrev.yogurt.api.MilkyApiException
88
import org.ntqqrev.yogurt.api.define
99
import org.ntqqrev.yogurt.transform.toEventType
10+
import org.ntqqrev.yogurt.transform.toIntReactionType
1011
import org.ntqqrev.yogurt.transform.toMilkyEntity
1112
import org.ntqqrev.yogurt.transform.transformEssenceMessage
1213
import org.ntqqrev.yogurt.util.resolveUri
@@ -133,8 +134,13 @@ val QuitGroup = ApiEndpoint.QuitGroup.define {
133134
val SendGroupMessageReaction = ApiEndpoint.SendGroupMessageReaction.define {
134135
bot.getGroup(it.groupId)
135136
?: throw MilkyApiException(-404, "Group not found")
136-
// TODO: will be it.reactionType in Milky 1.2
137-
bot.setGroupMessageReaction(it.groupId, it.messageSeq, it.reaction, 1, it.isAdd)
137+
bot.setGroupMessageReaction(
138+
it.groupId,
139+
it.messageSeq,
140+
it.reaction,
141+
it.reactionType.toIntReactionType(),
142+
it.isAdd
143+
)
138144
SendGroupMessageReactionOutput()
139145
}
140146

yogurt/src/commonMain/kotlin/org/ntqqrev/yogurt/api/handler/Message.kt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.ntqqrev.milky.*
88
import org.ntqqrev.yogurt.api.MilkyApiException
99
import org.ntqqrev.yogurt.api.define
1010
import org.ntqqrev.yogurt.transform.toMessageScene
11+
import org.ntqqrev.yogurt.transform.transformForwardedMessage
1112
import org.ntqqrev.yogurt.transform.transformMessage
1213
import org.ntqqrev.yogurt.transform.transformSegment
1314

@@ -154,16 +155,7 @@ val GetForwardedMessages = ApiEndpoint.GetForwardedMessages.define {
154155
val forwardedMessages = bot.getForwardedMessages(it.forwardId)
155156
val transformedMessages = forwardedMessages.map { msg ->
156157
with(application) {
157-
async {
158-
IncomingForwardedMessage(
159-
senderName = msg.senderName,
160-
avatarUrl = msg.avatarUrl,
161-
time = msg.timestamp,
162-
segments = msg.segments.map { segment ->
163-
async { transformSegment(segment) }
164-
}.awaitAll()
165-
)
166-
}
158+
async { transformForwardedMessage(msg) }
167159
}
168160
}.awaitAll()
169161
GetForwardedMessagesOutput(

yogurt/src/commonMain/kotlin/org/ntqqrev/yogurt/api/handler/System.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.ntqqrev.yogurt.api.handler
22

3+
import kotlinx.coroutines.async
4+
import kotlinx.coroutines.awaitAll
35
import org.ntqqrev.acidify.*
46
import org.ntqqrev.milky.*
57
import org.ntqqrev.yogurt.BuildKonfig
@@ -95,6 +97,27 @@ val GetGroupMemberInfo = ApiEndpoint.GetGroupMemberInfo.define {
9597
)
9698
}
9799

100+
val GetPeerPins = ApiEndpoint.GetPeerPins.define {
101+
val pins = bot.getPins()
102+
GetPeerPinsOutput(
103+
friends = pins.friendUins.map { friendUin ->
104+
application.async { bot.getFriend(friendUin)?.toMilkyEntity() }
105+
}.awaitAll().filterNotNull(),
106+
groups = pins.groupUins.map { groupUin ->
107+
application.async { bot.getGroup(groupUin)?.toMilkyEntity() }
108+
}.awaitAll().filterNotNull(),
109+
)
110+
}
111+
112+
val SetPeerPin = ApiEndpoint.SetPeerPin.define {
113+
when (it.messageScene) {
114+
"friend" -> bot.setFriendPin(it.peerId, it.isPinned)
115+
"group" -> bot.setGroupPin(it.peerId, it.isPinned)
116+
else -> throw MilkyApiException(-400, "Unknown message scene: ${it.messageScene}")
117+
}
118+
SetPeerPinOutput()
119+
}
120+
98121
val SetAvatar = ApiEndpoint.SetAvatar.define {
99122
bot.setAvatar(resolveUri(it.uri))
100123
SetAvatarOutput()

yogurt/src/commonMain/kotlin/org/ntqqrev/yogurt/transform/EntityTransform.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,16 @@ fun String.toEventType() = when (this) {
152152
"join_request" -> 1
153153
"invited_join_request" -> 22
154154
else -> throw IllegalArgumentException("Unknown notification type: $this")
155+
}
156+
157+
fun Int.toMilkyReactionType() = when (this) {
158+
1 -> "face"
159+
2 -> "emoji"
160+
else -> throw IllegalArgumentException("Unknown reaction type: $this")
161+
}
162+
163+
fun String.toIntReactionType() = when (this) {
164+
"face" -> 1
165+
"emoji" -> 2
166+
else -> throw IllegalArgumentException("Unknown reaction type: $this")
155167
}

yogurt/src/commonMain/kotlin/org/ntqqrev/yogurt/transform/EventTransform.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ suspend fun Application.transformAcidifyEvent(event: AcidifyEvent): Event? {
4545
)
4646
)
4747

48+
is PinChangedEvent -> Event.PeerPinChange(
49+
time = Clock.System.now().epochSeconds,
50+
selfId = bot.uin,
51+
data = Event.PeerPinChange.Data(
52+
messageScene = event.scene.toMilkyString(),
53+
peerId = event.peerUin,
54+
isPinned = event.isPinned
55+
)
56+
)
57+
4858
is FriendRequestEvent -> Event.FriendRequest(
4959
time = Clock.System.now().epochSeconds,
5060
selfId = bot.uin,
@@ -176,6 +186,7 @@ suspend fun Application.transformAcidifyEvent(event: AcidifyEvent): Event? {
176186
userId = event.userUin,
177187
messageSeq = event.messageSeq,
178188
faceId = event.faceId,
189+
reactionType = event.type.toMilkyReactionType(),
179190
isAdd = event.isAdd
180191
)
181192
)

yogurt/src/commonMain/kotlin/org/ntqqrev/yogurt/transform/MessageTransform.kt

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ import org.ntqqrev.acidify.getFriend
1111
import org.ntqqrev.acidify.getGroup
1212
import org.ntqqrev.acidify.message.*
1313
import org.ntqqrev.acidify.message.ImageFormat
14-
import org.ntqqrev.milky.GroupEssenceMessage
15-
import org.ntqqrev.milky.IncomingMessage
16-
import org.ntqqrev.milky.IncomingSegment
17-
import org.ntqqrev.milky.OutgoingSegment
14+
import org.ntqqrev.milky.*
1815
import org.ntqqrev.yogurt.YogurtApp
1916
import org.ntqqrev.yogurt.util.resolveUri
2017

@@ -55,6 +52,18 @@ suspend fun Application.transformMessage(msg: BotIncomingMessage): IncomingMessa
5552
}
5653
}
5754

55+
suspend fun Application.transformForwardedMessage(msg: BotForwardedMessage): IncomingForwardedMessage {
56+
return IncomingForwardedMessage(
57+
messageSeq = msg.sequence,
58+
senderName = msg.senderName,
59+
avatarUrl = msg.avatarUrl,
60+
time = msg.timestamp,
61+
segments = msg.segments.map { segment ->
62+
async { transformSegment(segment) }
63+
}.awaitAll()
64+
)
65+
}
66+
5867
suspend fun Application.transformSegment(segment: BotIncomingSegment): IncomingSegment {
5968
val bot = dependencies.resolve<AbstractBot>()
6069
return when (segment) {
@@ -67,7 +76,8 @@ suspend fun Application.transformSegment(segment: BotIncomingSegment): IncomingS
6776
is BotIncomingSegment.Mention -> if (segment.uin != null) {
6877
IncomingSegment.Mention(
6978
data = IncomingSegment.Mention.Data(
70-
userId = segment.uin!!
79+
userId = segment.uin!!,
80+
name = segment.name,
7181
)
7282
)
7383
} else {
@@ -85,7 +95,13 @@ suspend fun Application.transformSegment(segment: BotIncomingSegment): IncomingS
8595

8696
is BotIncomingSegment.Reply -> IncomingSegment.Reply(
8797
data = IncomingSegment.Reply.Data(
88-
messageSeq = segment.sequence
98+
messageSeq = segment.sequence,
99+
senderId = segment.senderUin,
100+
senderName = segment.senderName,
101+
time = segment.timestamp,
102+
segments = segment.segments.map {
103+
async { transformSegment(it) }
104+
}.awaitAll(),
89105
)
90106
)
91107

@@ -263,8 +279,8 @@ suspend fun Application.transformSegment(
263279
)
264280
}
265281

266-
is OutgoingSegment.Forward -> BotOutgoingSegment.Forward(
267-
nodes = segment.data.messages.map { msg ->
282+
is OutgoingSegment.Forward -> {
283+
val nodes = segment.data.messages.map { msg ->
268284
BotOutgoingSegment.Forward.Node(
269285
senderUin = msg.userId,
270286
senderName = msg.senderName,
@@ -273,6 +289,19 @@ suspend fun Application.transformSegment(
273289
}.awaitAll()
274290
)
275291
}
292+
BotOutgoingSegment.Forward(
293+
nodes = nodes,
294+
title = segment.data.title ?: "群聊的聊天记录",
295+
preview = segment.data.preview ?: nodes.take(4).map {
296+
it.senderName + ": " + it.segments.joinToString("")
297+
},
298+
summary = segment.data.summary ?: "查看${segment.data.messages.size}条转发消息",
299+
prompt = segment.data.prompt ?: "[聊天记录]"
300+
)
301+
}
302+
303+
is OutgoingSegment.LightApp -> BotOutgoingSegment.LightApp(
304+
jsonPayload = segment.data.jsonPayload
276305
)
277306
}
278307
}

0 commit comments

Comments
 (0)