Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
Shamrock: 修复推送器推送错误
Browse files Browse the repository at this point in the history
Signed-off-by: WhiteChi <[email protected]>
  • Loading branch information
whitechi73 committed Oct 28, 2023
1 parent debcb4f commit 4713310
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ internal class WebSocketClientService(
}

override fun initTransmitter() {
HttpService.submitFlowJob(GlobalScope.launch {
submitFlowJob(GlobalScope.launch {
GlobalEventTransmitter.onMessageEvent { (_, event) ->
pushTo(event)
}
})
HttpService.submitFlowJob(GlobalScope.launch {
submitFlowJob(GlobalScope.launch {
GlobalEventTransmitter.onNoticeEvent { event ->
pushTo(event)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ internal class WebSocketService(port: Int): WebSocketTransmitServlet(port) {
}

override fun initTransmitter() {
HttpService.submitFlowJob(GlobalScope.launch {
submitFlowJob(GlobalScope.launch {
GlobalEventTransmitter.onMessageEvent { (_, event) ->
pushTo(event)
}
})
HttpService.submitFlowJob(GlobalScope.launch {
submitFlowJob(GlobalScope.launch {
GlobalEventTransmitter.onNoticeEvent { event ->
pushTo(event)
}
Expand Down Expand Up @@ -71,7 +71,7 @@ internal class WebSocketService(port: Int): WebSocketTransmitServlet(port) {
pushMetaLifecycle()
eventReceivers.add(conn)
}
LogCenter.log({ "WSServer连接(${conn.remoteSocketAddress.address.hostAddress}:${conn.remoteSocketAddress.port}$path)" }, Level.DEBUG)
LogCenter.log({ "WSServer连接(${conn.remoteSocketAddress.address.hostAddress}:${conn.remoteSocketAddress.port}$path)" }, Level.WARN)
}

private fun pushMetaLifecycle() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import moe.fuqiuluo.qqinterface.servlet.BaseSvc
import moe.fuqiuluo.qqinterface.servlet.GroupSvc
import moe.fuqiuluo.qqinterface.servlet.msg.convert.toSegments
import moe.fuqiuluo.shamrock.remote.service.HttpService
import moe.fuqiuluo.shamrock.remote.service.config.ShamrockConfig
import moe.fuqiuluo.shamrock.remote.service.data.push.GroupFileMsg
import moe.fuqiuluo.shamrock.remote.service.data.push.MemberRole
Expand All @@ -32,9 +31,9 @@ internal object GlobalEventTransmitter: BaseSvc() {
MutableSharedFlow<NoticeEvent>()
}

private fun pushNotice(noticeEvent: NoticeEvent) = noticeEventFlow.tryEmit(noticeEvent)
private suspend fun pushNotice(noticeEvent: NoticeEvent) = noticeEventFlow.emit(noticeEvent)

private fun transMessageEvent(record: MsgRecord, message: MessageEvent) = messageEventFlow.tryEmit(record to message)
private suspend fun transMessageEvent(record: MsgRecord, message: MessageEvent) = messageEventFlow.emit(record to message)

/**
* 消息 手淫器
Expand All @@ -51,7 +50,7 @@ internal object GlobalEventTransmitter: BaseSvc() {
postType: PostType = PostType.Msg
): Boolean {
val uin = app.longAccountUin
return transMessageEvent(record,
transMessageEvent(record,
MessageEvent(
time = record.msgTime,
selfId = uin,
Expand Down Expand Up @@ -82,6 +81,7 @@ internal object GlobalEventTransmitter: BaseSvc() {
)
)
)
return true
}

/**
Expand All @@ -95,7 +95,7 @@ internal object GlobalEventTransmitter: BaseSvc() {
postType: PostType = PostType.Msg
): Boolean {
val uin = app.longAccountUin
return transMessageEvent(record,
transMessageEvent(record,
MessageEvent(
time = record.msgTime,
selfId = uin,
Expand All @@ -116,16 +116,13 @@ internal object GlobalEventTransmitter: BaseSvc() {
userId = record.senderUin,
nickname = record.sendNickName,
card = record.sendMemberName,
role = when (record.senderUin) {
GroupSvc.getOwner(record.peerUin.toString()) -> MemberRole.Owner
in GroupSvc.getAdminList(record.peerUin.toString()) -> MemberRole.Admin
else -> MemberRole.Member
},
role = MemberRole.Member,
title = "",
level = "",
)
)
)
return true
}
}

Expand All @@ -136,7 +133,7 @@ internal object GlobalEventTransmitter: BaseSvc() {
/**
* 推送私聊文件事件
*/
fun transPrivateFileEvent(
suspend fun transPrivateFileEvent(
msgTime: Long,
userId: Long,
fileId: String,
Expand All @@ -146,7 +143,7 @@ internal object GlobalEventTransmitter: BaseSvc() {
expireTime: Long,
url: String
): Boolean {
return pushNotice(NoticeEvent(
pushNotice(NoticeEvent(
time = msgTime,
selfId = app.longAccountUin,
postType = PostType.Notice,
Expand All @@ -163,12 +160,13 @@ internal object GlobalEventTransmitter: BaseSvc() {
expire = expireTime
)
))
return true
}

/**
* 推送私聊文件事件
*/
fun transGroupFileEvent(
suspend fun transGroupFileEvent(
msgTime: Long,
userId: Long,
groupId: Long,
Expand All @@ -178,7 +176,7 @@ internal object GlobalEventTransmitter: BaseSvc() {
bizId: Int,
url: String
): Boolean {
return pushNotice(NoticeEvent(
pushNotice(NoticeEvent(
time = msgTime,
selfId = app.longAccountUin,
postType = PostType.Notice,
Expand All @@ -194,15 +192,16 @@ internal object GlobalEventTransmitter: BaseSvc() {
url = url
)
))
return true
}
}

/**
* 群聊通知 通知器
*/
object GroupNoticeTransmitter {
fun transGroupPoke(time: Long, operation: Long, target: Long, groupCode: Long): Boolean {
return pushNotice(NoticeEvent(
suspend fun transGroupPoke(time: Long, operation: Long, target: Long, groupCode: Long): Boolean {
pushNotice(NoticeEvent(
time = time,
selfId = app.longAccountUin,
postType = PostType.Notice,
Expand All @@ -213,17 +212,18 @@ internal object GlobalEventTransmitter: BaseSvc() {
groupId = groupCode,
target = target
))
return true
}

fun transGroupMemberNumChanged(
suspend fun transGroupMemberNumChanged(
time: Long,
target: Long,
groupCode: Long,
operation: Long,
noticeType: NoticeType,
noticeSubType: NoticeSubType
): Boolean {
return pushNotice(NoticeEvent(
pushNotice(NoticeEvent(
time = time,
selfId = app.longAccountUin,
postType = PostType.Notice,
Expand All @@ -235,15 +235,16 @@ internal object GlobalEventTransmitter: BaseSvc() {
target = target,
groupId = groupCode
))
return true
}

fun transGroupAdminChanged(
suspend fun transGroupAdminChanged(
msgTime: Long,
target: Long,
groupCode: Long,
setAdmin: Boolean
): Boolean {
return pushNotice(NoticeEvent(
pushNotice(NoticeEvent(
time = msgTime,
selfId = app.longAccountUin,
postType = PostType.Notice,
Expand All @@ -253,16 +254,17 @@ internal object GlobalEventTransmitter: BaseSvc() {
target = target,
groupId = groupCode
))
return true
}

fun transGroupBan(
suspend fun transGroupBan(
msgTime: Long,
operation: Long,
target: Long,
groupCode: Long,
duration: Int
): Boolean {
return pushNotice(NoticeEvent(
pushNotice(NoticeEvent(
time = msgTime,
selfId = app.longAccountUin,
postType = PostType.Notice,
Expand All @@ -275,17 +277,18 @@ internal object GlobalEventTransmitter: BaseSvc() {
groupId = groupCode,
duration = duration
))
return true
}

fun transGroupMsgRecall(
suspend fun transGroupMsgRecall(
time: Long,
operator: Long,
target: Long,
groupCode: Long,
msgHash: Int,
tipText: String
): Boolean {
return pushNotice(NoticeEvent(
pushNotice(NoticeEvent(
time = time,
selfId = app.longAccountUin,
postType = PostType.Notice,
Expand All @@ -296,15 +299,16 @@ internal object GlobalEventTransmitter: BaseSvc() {
tip = tipText,
groupId = groupCode
))
return true
}
}

/**
* 私聊通知 通知器
*/
object PrivateNoticeTransmitter {
fun transPrivatePoke(msgTime: Long, operation: Long, target: Long): Boolean {
return pushNotice(NoticeEvent(
suspend fun transPrivatePoke(msgTime: Long, operation: Long, target: Long): Boolean {
pushNotice(NoticeEvent(
time = msgTime,
selfId = app.longAccountUin,
postType = PostType.Notice,
Expand All @@ -315,10 +319,11 @@ internal object GlobalEventTransmitter: BaseSvc() {
senderId = operation,
target = target
))
return true
}

fun transPrivateRecall(time: Long, operation: Long, msgHashId: Int, tipText: String): Boolean {
return pushNotice(NoticeEvent(
suspend fun transPrivateRecall(time: Long, operation: Long, msgHashId: Int, tipText: String): Boolean {
pushNotice(NoticeEvent(
time = time,
selfId = app.longAccountUin,
postType = PostType.Notice,
Expand All @@ -329,16 +334,17 @@ internal object GlobalEventTransmitter: BaseSvc() {
msgId = msgHashId,
tip = tipText
))
return true
}
}

@ShamrockDsl
suspend fun onMessageEvent(collector: FlowCollector<Pair<MsgRecord, MessageEvent>>) {
suspend inline fun onMessageEvent(collector: FlowCollector<Pair<MsgRecord, MessageEvent>>) {
messageEventFlow.collect(collector)
}

@ShamrockDsl
suspend fun onNoticeEvent(collector: FlowCollector<NoticeEvent>) {
suspend inline fun onNoticeEvent(collector: FlowCollector<NoticeEvent>) {
noticeEventFlow.collect(collector)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@file:OptIn(DelicateCoroutinesApi::class)

package moe.fuqiuluo.shamrock.remote.service.api

import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.serialization.encodeToString
Expand Down Expand Up @@ -71,7 +74,7 @@ internal abstract class WebSocketTransmitServlet(
if (path != "/api") {
eventReceivers.remove(conn)
}
LogCenter.log({ "WSServer断开(${conn.remoteSocketAddress.address.hostAddress}:${conn.remoteSocketAddress.port}$path): $code,$reason,$remote" }, Level.DEBUG)
LogCenter.log({ "WSServer断开(${conn.remoteSocketAddress.address.hostAddress}:${conn.remoteSocketAddress.port}$path): $code,$reason,$remote" }, Level.WARN)
}

override fun onMessage(conn: WebSocket, message: String) {
Expand Down Expand Up @@ -105,8 +108,8 @@ internal abstract class WebSocketTransmitServlet(
}

override fun onStart() {
initTransmitter()
LogCenter.log("WSServer start running on ws://0.0.0.0:$port!")
initTransmitter()
}

protected inline fun <reified T> pushTo(body: T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ internal object AioListener: IKernelMsgListener {
if(!GlobalEventTransmitter.MessageTransmitter.transGroupMessage(
record, record.elements, rawMsg, msgHash
)) {
LogCenter.log("群消息推送失败 -> MessageTransmitter", Level.WARN)
LogCenter.log("群消息推送失败 -> 推送目标可能不存在", Level.WARN)
}
}
MsgConstant.KCHATTYPEC2C -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal object PrimitiveListener {
}
}

private fun onC2cPoke(msgTime: Long, pb: ProtoMap) {
private suspend fun onC2cPoke(msgTime: Long, pb: ProtoMap) {
val detail = pb[1, 3, 2]
if (detail !is ProtoMap) {
error("不支持该私聊戳一戳解析: ${(detail as ProtoByteString).toByteArray().toHexString()}")
Expand All @@ -95,7 +95,7 @@ internal object PrimitiveListener {
}
}

private fun onGroupPoke(time: Long, pb: ProtoMap) {
private suspend fun onGroupPoke(time: Long, pb: ProtoMap) {
val groupCode1 = pb[1, 1, 1].asULong

var groupCode: Long = groupCode1
Expand Down

0 comments on commit 4713310

Please sign in to comment.