feat(im): Task 5 - IM REST Controller + Router + Wire 集成

IMController (REST API):
- GET /api/v1/im/conversations: 会话列表
- GET /api/v1/im/messages: 历史消息(游标分页)
- PUT /api/v1/im/conversations/:id/pin: 置顶/取消
- DELETE /api/v1/im/conversations/🆔 删除会话
- DELETE /api/v1/im/conversations/:id/messages: 清空记录
- GET /api/v1/im/messages/search: 全局搜索
- GET /api/v1/im/unread: 全局未读数

Wire 集成:
- IMSet Provider Set + wire.Bind 接口注入
- App struct 新增 IM 字段
- wire_gen.go 手动更新
- OfflinePusher 通过 SetOfflinePusher 注入 WS Handler

Made-with: Cursor
This commit is contained in:
bujinyuan
2026-03-03 10:51:56 +08:00
parent 1e0c061e9d
commit 2c7be494f2
7 changed files with 344 additions and 13 deletions

View File

@@ -7,6 +7,8 @@ import (
authController "github.com/echochat/backend/app/auth/controller"
"github.com/echochat/backend/app/auth/service"
contactController "github.com/echochat/backend/app/contact/controller"
imController "github.com/echochat/backend/app/im/controller"
imHandler "github.com/echochat/backend/app/im/handler"
wsApp "github.com/echochat/backend/app/ws"
"github.com/echochat/backend/config"
"github.com/echochat/backend/pkg/db"
@@ -32,6 +34,9 @@ type App struct {
PubSub *ws.PubSub // Redis Pub/Sub 消息路由
OnlineService *wsApp.OnlineService // 在线状态管理服务
ContactController *contactController.ContactController // 联系人控制器
IMController *imController.IMController // IM 即时通讯控制器
IMEventHandler *imHandler.EventHandler // IM WS 事件处理器
OfflinePusher *imHandler.OfflinePusher // 离线消息推送器
}
// NewApp 创建应用实例
@@ -50,22 +55,31 @@ func NewApp(
pubsub *ws.PubSub,
onlineService *wsApp.OnlineService,
contactCtrl *contactController.ContactController,
imCtrl *imController.IMController,
imEventHandler *imHandler.EventHandler,
offlinePusher *imHandler.OfflinePusher,
) *App {
// 注入离线消息推送器到 WS Handler
wsHandler.SetOfflinePusher(offlinePusher)
return &App{
Config: cfg,
DB: gormDB,
Redis: redisClient,
AuthService: authService,
AuthController: authCtrl,
AdminAuthController: adminAuthCtrl,
Config: cfg,
DB: gormDB,
Redis: redisClient,
AuthService: authService,
AuthController: authCtrl,
AdminAuthController: adminAuthCtrl,
UserManageController: userManageCtrl,
OnlineController: onlineCtrl,
ContactManageController: contactManageCtrl,
WSHandler: wsHandler,
Hub: hub,
PubSub: pubsub,
OnlineService: onlineService,
ContactController: contactCtrl,
WSHandler: wsHandler,
Hub: hub,
PubSub: pubsub,
OnlineService: onlineService,
ContactController: contactCtrl,
IMController: imCtrl,
IMEventHandler: imEventHandler,
OfflinePusher: offlinePusher,
}
}

View File

@@ -7,6 +7,9 @@ import (
"github.com/echochat/backend/app/admin"
"github.com/echochat/backend/app/auth"
"github.com/echochat/backend/app/contact"
contactDAO "github.com/echochat/backend/app/contact/dao"
imApp "github.com/echochat/backend/app/im"
imService "github.com/echochat/backend/app/im/service"
wsApp "github.com/echochat/backend/app/ws"
"github.com/echochat/backend/config"
"github.com/google/wire"
@@ -20,6 +23,9 @@ func InitializeApp(cfg *config.Config) (*App, error) {
admin.AdminSet,
wsApp.WSSet,
contact.ContactSet,
imApp.IMSet,
wire.Bind(new(imService.FriendChecker), new(*contactDAO.FriendshipDAO)),
wire.Bind(new(imService.UserInfoGetter), new(*contactDAO.FriendshipDAO)),
)
return nil, nil
}

View File

@@ -16,6 +16,7 @@ import (
controller3 "github.com/echochat/backend/app/contact/controller"
dao3 "github.com/echochat/backend/app/contact/dao"
service3 "github.com/echochat/backend/app/contact/service"
imApp "github.com/echochat/backend/app/im"
"github.com/echochat/backend/app/ws"
"github.com/echochat/backend/config"
"github.com/echochat/backend/pkg/db"
@@ -57,6 +58,15 @@ func InitializeApp(cfg *config.Config) (*App, error) {
friendGroupDAO := dao3.NewFriendGroupDAO(gormDB)
contactService := service3.NewContactService(friendshipDAO, friendGroupDAO, pubSub)
contactController := controller3.NewContactController(contactService)
app := NewApp(cfg, gormDB, client, authService, authController, adminAuthController, userManageController, onlineController, contactManageController, handler, hub, pubSub, onlineService, contactController)
// IM 模块初始化
conversationDAO := imApp.ProvideConversationDAO(gormDB)
messageDAO := imApp.ProvideMessageDAO(gormDB)
imService := imApp.ProvideIMService(conversationDAO, messageDAO, pubSub, client, friendshipDAO, friendshipDAO)
imEventHandler := imApp.ProvideIMEventHandler(imService, hub)
offlinePusher := imApp.ProvideOfflinePusher(imService, conversationDAO, pubSub)
imController := imApp.ProvideIMController(imService)
app := NewApp(cfg, gormDB, client, authService, authController, adminAuthController, userManageController, onlineController, contactManageController, handler, hub, pubSub, onlineService, contactController, imController, imEventHandler, offlinePusher)
return app, nil
}