diff --git a/domain/model/group_m/groupInfo.go b/domain/model/group_m/groupInfo.go index c01a323b3aa10e9fd73d27b14ff17b999359a21a..6bcee9e7cea2c62de14749cdc636f165faae489c 100644 --- a/domain/model/group_m/groupInfo.go +++ b/domain/model/group_m/groupInfo.go @@ -47,6 +47,7 @@ type GroupInfo struct { CreatedTime time.Time `gorm:"->"` UpdatedTime time.Time `gorm:"->"` VisitCount int64 `gorm:"-"` // 热度 + IsGameRoom uint8 // 是否1v1游戏房 } func GenerateGroupCode(n uint16) string { diff --git a/domain/service/group_s/group_op.go b/domain/service/group_s/group_op.go index 447925e83e379626f4d08d6f37e995b9ce84a78a..32210d99b5153364bb3aaba0c568150dd5573392 100644 --- a/domain/service/group_s/group_op.go +++ b/domain/service/group_s/group_op.go @@ -17,7 +17,9 @@ import ( "git.hilo.cn/hilo-common/utils" "gorm.io/gorm" "hilo-group/_const/enum/group_e" + "hilo-group/_const/enum/msg_e" "hilo-group/_const/redis_key" + "hilo-group/_const/redis_key/group_k" "hilo-group/common" "hilo-group/cv/property_cv" "hilo-group/domain/cache/group_c" @@ -32,6 +34,7 @@ import ( "hilo-group/myerr" "hilo-group/myerr/bizerr" "strconv" + "strings" "time" ) @@ -509,3 +512,118 @@ func dealActDataAfterEnterRoom(myContext *mycontext.MyContext, userId, rideId ui go rpc.AddActPoint(domain.CreateModelContext(myContext), userId, 5, roomId) } } + +// 创建房间,慎用 +func (s *GroupService) CreateGroupMulByUid(userId uint64, num int) error { + model := domain.CreateModel(s.svc.CtxAndDb) + + if lock := redisCli.Lock(group_k.GetGroupLockKey(userId), time.Second*5); !lock { + return bizerr.ReqTooFrequent + } + + user, err := user_m.GetUser(model, userId) + if err != nil { + model.Log.Errorf("CreateGroupMulByUid err:%v", err) + return err + } + + // 强行修正为英语,以保证建群成功 + user.Language = utils.DEFAULT_LANG + + // 群组的头像默认为用户头像,群组的名称默认为用户昵称,群简介默认为“s%的群组”,群公告默认为“欢迎来到Hilo” + name := "" + text := res_m.ResMultiText{MsgId: msg_e.MSG_ID_GROUP_NAME, Language: user.Language} + if text.Get(model.Db) == nil { + name = strings.ReplaceAll(text.Content, "{nick}", user.Nick) + } + + introduction := "" + text2 := res_m.ResMultiText{MsgId: msg_e.MSG_ID_GROUP_INTRODUCTION, Language: user.Language} + if text2.Get(model.Db) == nil { + introduction = strings.ReplaceAll(text2.Content, "{nick}", user.Nick) + } + + notification := "" + text3 := res_m.ResMultiText{MsgId: msg_e.MSG_ID_GROUP_NOTIFICATION, Language: user.Language} + if text3.Get(model.Db) == nil { + notification = strings.ReplaceAll(text3.Content, "{nick}", user.Nick) + } + + faceUrl := user.Avatar + + for idx := 0; idx < num; idx++ { + code := group_m.GenerateGroupCode(group_e.GROUP_DEFAULT_CODE_LENGTH) + i := 0 + for ; i < group_e.CREATE_GROUP_MAX_ATTEMPT; i++ { + success, err := res_m.CheckCode(model, code) + if err != nil || !success { + break + } + r, err := group_m.FindGroupByCode(model, code) + if err == nil && r == nil { + break + } + code = group_m.GenerateGroupCode(group_e.GROUP_DEFAULT_CODE_LENGTH) + } + + if i >= group_e.CREATE_GROUP_MAX_ATTEMPT { + return myerr.NewSysError("Failed in generating groupId") + } + + groupId := group_e.OverseaGroupNamePrefix + code + groupId, err = tencentyun.CreateGroup(name, groupId) + if err != nil { + model.Log.Errorf("CreateGroupMulByUid err:%v", err) + return err + } + + channelId := utils.GetUUID() + _, _, err = agora.CreateGroupAgora(channelId, uint32(userId)) + + if err != nil { + // 回滚,删除刚刚建立的TX群组 + tencentyun.DestroyGroup(groupId, false) + model.Log.Errorf("CreateGroupMulByUid err:%v", err) + return err + } + + roomType := group_e.OverseaRoom + g := group_m.GroupInfo{ + ImGroupId: groupId, + TxGroupId: groupId, + Type: uint16(roomType), + Code: code, + OriginCode: code, + Owner: userId, + Name: name, + Introduction: introduction, + Notification: notification, + FaceUrl: faceUrl, + Country: user.Country, + ChannelId: channelId, + MicOn: true, + LoadHistory: false, + MicNumType: group_e.TenMicNumType, + TouristMic: 1, + TouristSendMsg: 1, + TouristSendPic: 1, + IsGameRoom: 1, + } + if err := s.CreateGroup(userId, &g); err != nil { + // 回滚,删除刚刚建立的TX群组 + tencentyun.DestroyGroup(groupId, false) + model.Log.Errorf("CreateGroupMulByUid err:%v", err) + return err + } + + gm := group_m.GroupMember{ + GroupId: groupId, + UserId: userId, + } + if err = gm.Create(model.Db); err != nil { + model.Log.Warnf("Failed to set GroupMember +%v", gm) + } + time.Sleep(time.Millisecond * 30) + } + return nil +} diff --git a/route/router.go b/route/router.go index 9d8511c2e385ad77fc5c39c58a1c1a775a63aff0..406bf106f6b511153b5598ba5a1cf6fdb39d3398 100644 --- a/route/router.go +++ b/route/router.go @@ -48,7 +48,7 @@ func InitRouter() *gin.Engine { imGroup.GET("/ownGroup", wrapper(group_r.GetOwnGroup)) imGroup.GET("/theirGroup/:userExternalId", wrapper(group_r.GetTheirGroups)) imGroup.PUT("/pluginReady/:groupId", wrapper(group_r.PluginReady)) - imGroup.GET("/roomInfo/:groupId", EncryptHandle, wrapper(group_r.GetRoomInfo)) + imGroup.GET("/roomInfo/:groupId", EncryptHandle, wrapper(group_r.GetRoomInfo)) // 进房 // imGroup.GET("/password/:groupId", wrapper(group_r.GetGroupPassword)) imGroup.GET("/role/:groupId", wrapper(group_r.GetGroupRole)) @@ -92,7 +92,7 @@ func InitRouter() *gin.Engine { imGroup.POST("/mic/speech/close", wrapper(group_r.GroupMicSpeechClose)) imGroup.POST("/mic/mute", wrapper(group_r.GroupMicMute)) imGroup.POST("/mic/unmute", wrapper(group_r.GroupMicUnmute)) - imGroup.PUT("/in", wrapper(group_r.GroupIn)) + imGroup.PUT("/in", wrapper(group_r.GroupIn)) // 进房 imGroup.POST("/leave", wrapper(group_r.GroupLeave)) imGroup.POST("/kick", wrapper(group_r.GroupKick)) imGroup.PUT("/user/msg/status", wrapper(group_r.GroupUserMsg))