Files
mayfly-go/server/internal/msg/init/init.go

52 lines
1.5 KiB
Go
Raw Normal View History

package init
import (
2025-04-15 21:42:31 +08:00
"context"
"mayfly-go/initialize"
"mayfly-go/internal/msg/api"
"mayfly-go/internal/msg/application"
2025-04-15 21:42:31 +08:00
"mayfly-go/internal/msg/application/dto"
"mayfly-go/internal/msg/infra/persistence"
"mayfly-go/internal/msg/msgx"
"mayfly-go/internal/pkg/event"
2025-04-15 21:42:31 +08:00
"mayfly-go/pkg/eventbus"
"mayfly-go/pkg/global"
"mayfly-go/pkg/ioc"
"mayfly-go/pkg/logx"
)
func init() {
2024-03-02 19:08:19 +08:00
initialize.AddInitIocFunc(func() {
persistence.InitIoc()
application.InitIoc()
api.InitIoc()
2024-03-02 19:08:19 +08:00
})
2025-04-15 21:42:31 +08:00
initialize.AddInitFunc(Init)
}
func Init() {
// 注册站内消息发送器
msgx.RegisterMsgSender(msgx.ChannelTypeSiteMsg, application.GetMsgApp())
2025-04-15 21:42:31 +08:00
msgTmplBizApp := ioc.Get[application.MsgTmplBiz]("MsgTmplBizApp")
global.EventBus.SubscribeAsync(event.EventTopicBizMsgTmplSend, "BizMsgTmplSend", func(ctx context.Context, event *eventbus.Event[any]) error {
return msgTmplBizApp.Send(ctx, event.Val.(*dto.BizMsgTmplSend))
2025-04-15 21:42:31 +08:00
}, false)
msgTmplApp := ioc.Get[application.MsgTmpl]("MsgTmplApp")
global.EventBus.SubscribeAsync(event.EventTopicMsgTmplSend, "MsgTmplSend", func(ctx context.Context, event *eventbus.Event[any]) error {
eventVal, ok := event.Val.(*dto.MsgTmplSendEvent)
if !ok {
logx.Error("the event value is not of type *dto.MsgTmplSendEvent")
return nil
}
return msgTmplApp.SendMsg(ctx, &dto.MsgTmplSend{
Tmpl: eventVal.TmplChannel.Tmpl,
Channels: eventVal.TmplChannel.Channels,
Params: eventVal.Params,
ReceiverIds: eventVal.ReceiverIds,
})
}, false)
}