From 9ebe86919d7cecf20057d27c10718636d0ea13c2 Mon Sep 17 00:00:00 2001 From: Leufolium Date: Thu, 17 Oct 2024 16:30:32 +0800 Subject: [PATCH] by Robin at 20241017 --- app/mix/dao/mongo.go | 19 ++++++++++++ app/mix/service/logic/notif_bcst.go | 2 +- app/mix/service/logic/notif_bcst_vers.go | 9 ++++++ app/mix/service/notification_center.go | 39 +++++++++++++++--------- app/mix/service/service.go | 3 ++ 5 files changed, 57 insertions(+), 15 deletions(-) diff --git a/app/mix/dao/mongo.go b/app/mix/dao/mongo.go index 205895d4..032c305b 100644 --- a/app/mix/dao/mongo.go +++ b/app/mix/dao/mongo.go @@ -6322,6 +6322,25 @@ func (m *Mongo) GetAndUpdateNotifBcstVers(ctx *gin.Context, objType int64) (vers return &versInstance, err } +func (m *Mongo) GetNotifBcstVers(ctx *gin.Context, objType int64) (vers *dbstruct.NotifBcstVers, err error) { + col := m.getColNotifBcstVers() + + id := "" + if objType == consts.Notification_ObjType_AllStreamer { + id = "all_streamer" + } else if objType == consts.Notification_ObjType_AllUser { + id = "all_user" + } + + vers = &dbstruct.NotifBcstVers{} + err = col.Find(ctx, qmgo.M{"_id": id}).One(vers) + if err == qmgo.ErrNoSuchDocuments { + err = nil + return nil, err + } + return vers, err +} + // 通知广播接收版本号表 func (m *Mongo) GetAndUpdateNotifBcstReceiveVers(ctx *gin.Context, mid int64, vers int64) (receiveVers *dbstruct.NotifBcstReceiveVers, err error) { col := m.getColNotifBcstReceiveVers() diff --git a/app/mix/service/logic/notif_bcst.go b/app/mix/service/logic/notif_bcst.go index fe30d722..94b9ebe4 100644 --- a/app/mix/service/logic/notif_bcst.go +++ b/app/mix/service/logic/notif_bcst.go @@ -45,7 +45,7 @@ func (p *NotifBcst) OpDeleteByIds(ctx *gin.Context, ids []int64) error { return nil } -func (p *NotifBcst) OpListByByVersRange(ctx *gin.Context, vers_lb, vers_ub, offset, limit int64) ([]*dbstruct.NotifBcst, error) { +func (p *NotifBcst) OpListByVersRange(ctx *gin.Context, vers_lb, vers_ub, offset, limit int64) ([]*dbstruct.NotifBcst, error) { list, err := p.store.GetNotifBcstListByVersRange(ctx, vers_lb, vers_ub, offset, limit) if err != nil { logger.Error("GetNotificationListByMid fail, err: %v", err) diff --git a/app/mix/service/logic/notif_bcst_vers.go b/app/mix/service/logic/notif_bcst_vers.go index 265fdb24..c8d385d3 100644 --- a/app/mix/service/logic/notif_bcst_vers.go +++ b/app/mix/service/logic/notif_bcst_vers.go @@ -19,6 +19,15 @@ func NewZoneNotifBcstVers(store *dao.Store) (a *NotifBcstVers) { return } +func (p *NotifBcstVers) GetNotifBcstVers(ctx *gin.Context, objType int64) (*dbstruct.NotifBcstVers, error) { + vers, err := p.store.GetNotifBcstVers(ctx, objType) + if err != nil { + logger.Error("GetNotifBcstVers fail, err: %v", err) + return nil, err + } + return vers, err +} + func (p *NotifBcstVers) GetAndUpdateNotifBcstVers(ctx *gin.Context, objType int64) (*dbstruct.NotifBcstVers, error) { vers, err := p.store.GetAndUpdateNotifBcstVers(ctx, objType) if err != nil { diff --git a/app/mix/service/notification_center.go b/app/mix/service/notification_center.go index 0bf9b216..5c7899b3 100644 --- a/app/mix/service/notification_center.go +++ b/app/mix/service/notification_center.go @@ -1,5 +1,12 @@ package service +import ( + "service/api/consts" + "service/library/logger" + + "github.com/gin-gonic/gin" +) + var DefaultNotifBcstCenter *NotifBcstCenter // 全局广播消息中心 @@ -9,16 +16,6 @@ func NewNotifBcstCenter() *NotifBcstCenter { return new(NotifBcstCenter) } -// 获取主播全局广播消息中心版本号 -func (s *NotifBcstCenter) GetStreamerNotifBcstCenterVers() (int64, error) { - -} - -// 获取用户全局消息中心版本号 -func (s *NotifBcstCenter) GetUserNotifBcstCenterVers() (int64, error) { - -} - // 写入一条主播全局广播消息 func (s *NotifBcstCenter) BcstANotifToAllStreamers() error { @@ -35,14 +32,28 @@ func (s *NotifBcstCenter) BcstANotifToAllStreamersAndUsers() error { } // 主播获取全局广播消息 -func (s *NotifBcstCenter) ReceiveAllBcstedNotifsAsStreamer(mid int64) error { - // 获取该主播已接收的全局广播版本号 - +func (s *NotifBcstCenter) ReceiveAllBcstedNotifsAsStreamer(ctx *gin.Context, mid int64) error { // 获取当前主播全局广播的版本号 + notifBcstVers, err := _DefaultNotifBcstVers.GetNotifBcstVers(ctx, consts.Notification_ObjType_AllStreamer) + if err != nil { + logger.Error("GetNotifBcstVers fail, err: %v", err) + return err + } + + // 获取该主播已接收的全局广播版本号 + notifBcstReceiveVers, err := _DefaultNotifBcstVers.GetAndUpdateNotifBcstReceiveVers(ctx, mid, notifBcstVers.Vers) + if err != nil { + logger.Error("GetAndUpdateNotifBcstReceiveVers fail, err: %v", err) + return err + } // 如果版本号一致,则认为无需再接收新的广播 + if notifBcstReceiveVers.Vers == notifBcstVers.Vers { + return nil + } - // 如果版本号不一致看,则接收新广播的消息 + // 如果版本号不一致,则接收新广播的消息 + _DefaultNotifBcst.OpListByVersRange(ctx, notifBcstReceiveVers.Vers, notifBcstVers.Vers-1, 0, 1000) } // 用户获取全局广播消息 diff --git a/app/mix/service/service.go b/app/mix/service/service.go index ab7ba497..f1b85365 100644 --- a/app/mix/service/service.go +++ b/app/mix/service/service.go @@ -153,6 +153,9 @@ var ( _DefaultAutoResponseCreateTimes *logic.AutoResponseCreateTimes _DefaultNotification *logic.Notification _DefaultRavenIQTest *logic.RavenIQTest + _DefaultNotifBcstVers *logic.NotifBcstVers + _DefaultNotifBcst *logic.NotifBcst + _DefaultNotifReceive *logic.NotifReceive ) type Service struct {